I’m using the following code to limit cumulative orders to a max of 8:
Once MaxPositionsAllowed = 8
// Conditions to enter long positions
IF Ctime and c1 and c1a AND C3 and c3a AND C5 and c5a and abs(CountOfPosition) < MaxPositionsAllowed THEN
BUY positionsize CONTRACT AT MARKET
SET STOP %LOSS 1.3
SET TARGET %PROFIT 2.2
ENDIF
but in practice (demo) the strategy is taking more than 8, see attached pic. Is there anything wrong with the above code?
I don’t know how your positionsize is calculated and how your countofposition usually evolves, but let’s say as an example you have abs(countposition)=7 ( is lower than 8) and positionsize is 3, the “if” statement will conclude that 7 being lower than 8 (and your other conditions being respected), it’s ok to add 3 to this, and 7+3=10…
In other words, from quick read without the rest of the code, this piece of code is not preventing you from going above 8 at all, because you made your check before adding to the position, so I’d try with : abs(countofposition)<maxpositionallowed-positionsize to see there’s enough room left below the max before increasing the position
I think line 3 could be written as:
IF Ctime and c1 and c1a AND C3 and c3a AND C5 and c5a and (abs(CountOfPosition) + PositionSize) <= MaxPositionsAllowed THEN
Thanks for that, but perhaps I am missing something. Surely countofposition and positionsize are 2 different things, no?
In this case, positionsize is .4 on the DJ and I want to have a max of 8 different entries of .4 each. Would the MaxPositionsAllowed then need to be 3.2 ?
Yes, 3.2. I also had to think twice on DJI.
But how will that work with MM then? when the positionsize increases? Maybe
MaxPositionsAllowed = (positionsize x 8)
???
COUNTOFPOSITION is the number (negative for Short positions) of total positions accumulated so far, while PositionSize is the number of positions you want to trade at that very moment. After opening additional PositionSize contracts/shares, COUNTOFPOSITION will be increased by PositionSize units.
Sorry Roberto, if my positionsize = .4 and the strategy has cumulated 7 orders then the countofposition is 7 or 2.8 ?
Ok, so countofposition is actually the total number of contracts. In this case I would want that to max at 3.2 and if positionsize didn’t change then there’s no problem. But what happens when the MM changes the positionsize? Is there no way of specifying the number of instances, rather than the number of contracts?
hi nonetheless
Try this
round(COUNTOFPOSITION /positionsize)
that is supposed to say
Once MaxPositionsAllowed = 8
// Conditions to enter long positions
IF Ctime and myconditions and round(CountOfPosition/positionsize) <= MaxPositionsAllowed THEN
Why using ROUND()?
MaxPositionsAllowed is a limit. If your current COUNTOFPOSITION is 7.4 and you wanna add 0.8 the code won’t allow that.
Rounding that number will count it as 7, so you’ll be allow to add 0.8 PositionSize contracts, but doing this actually will raise COUNTOFPOSITION to 8.2, which is not what you asked with MaxPositionsAllowed.
Sorry, I just realized I misunderstood.
You want MaxPositionsAllowed to set the number of times you open positions, no matter how large they are.
In that case your code is correct.