Gents,
Could you check this code why the Cumulating positions are not working??
// Definition of code parameters
DEFPARAM CumulateOrders = True // Cumulating positions activated
timeframe(DEFAULT)
MaxPositionsAllowed = 5
// Conditions to enter long positions
indicator1 = MACDline[5,35,5](close)
indicator2 = MACDSignal[5,35,5](close)
c1 = (indicator1 CROSSES OVER indicator2)
PP = (DHigh(1) + DLow(1) + DClose(1))/3
C2 = (close < indicator1)
S3 = DLow(1)-2*(DHigh(1)-(DHigh(1) + DLow(1) + DClose(1))/3)
C3 = (close > indicator2)
IF c1 AND c2 AND c3 THEN
BUY 50 CONTRACT AT MARKET
ENDIF
// Stops and targets
SET TARGET pPROFIT 2.0
Hello,
What is the issue that you have and make you say it’s not working?
For sure, the variable MaxPositionsAllowed is defined but not used in the code, which is not good, the system will rebuy more than 5 times
Assuming that the conditions are met, I would try something like this, and verifying the number of positions in the graph once the results are generated.
ounce n=0
....
IF NOT LongOnMarket THEN
n=0
ENDIF
IF c1 AND c2 AND c3 AND n<MaxPositionsAllowed THEN
BUY 50 CONTRACT AT MARKET
n=n+1
ENDIF
Conditions in lines 13 and 15 seem to be impossible to be both true at the same time.
The issue is that the code buy only once and never buy again when the conditions are met??!!
Condition C2 & C3, you are checking the price with an MACD line?! They do not share the same scale.
When do you see an order opening? At beginning of the data history or later?
// Definition of code parameters
DEFPARAM CumulateOrders = True // Cumulating positions activated
timeframe(DEFAULT)
MaxPositionsAllowed = 5
// Conditions to enter long positions
indicator1 = MACDline[5,35,5](close)
indicator2 = MACDSignal[5,35,5](close)
c1 = (indicator1 CROSSES OVER indicator2)
PP = (DHigh(1) + DLow(1) + DClose(1))/3
C2 = (close < PP)
S3 = DLow(1)-2*(DHigh(1)-(DHigh(1) + DLow(1) + DClose(1))/3)
C3 = (close > S3)
IF c1 AND c2 AND c3 THEN
BUY 50 CONTRACT AT MARKET
ENDIF
// Stops and targets
SET TARGET pPROFIT 2.0
this is the entry code for a cumulating algo I have up and running and I know works. In this case the limit is 3 entries instead of 5, but you can adapt it.
The logic of MaxPositionsAllowed is to control the total number of contracts, not the number of entries. The code below will make 3 entries of 50 contracts each.
Another stipulation I use, that you might want to consider, is adding “and positionperf <0” to your additional entries, so that your total position is always averaging down.
positionsize = 50
MaxPositionsAllowed = 3*positionsize
if not onmarket then
flag = 1
flag1 = 1
ENDIF
// Conditions to enter long positions
IF not longonmarket and (conditions) THEN
BUY positionsize CONTRACT AT MARKET
elsif longonmarket and (conditions) and flag and COUNTOFLONGSHARES < MaxPositionsAllowed then
BUY positionsize CONTRACT AT MARKET
flag = 0
elsif longonmarket and (conditions) and flag1 and COUNTOFLONGSHARES < MaxPositionsAllowed then
BUY positionsize CONTRACT AT MARKET
flag1 = 0
ENDIF
If LongOnMarket THEN
SET STOP %LOSS sl
SET TARGET %PROFIT tp
ENDIF
I really like the addition of the “and positionperf <0” , neat snippet – thank you for sharing.