Hello everybody first time on this forum I hope this is the right place for this discussion.
I am having trouble coding a strategy to back test a number of different algorithms ( combination of indicators). Below is what I have so far and it is not doing what I expect or more importantly what I want. I have commented where I think it is needed to help explain what I am trying to do.
In summary I wish to have a stop loss 1.5 X the ATR at time of trade entry and when break even is reached I wish to close ½ the position and let the other half run (still with the same stop loss). When the trade reaches 2 X the ATR figure at trade entry in favour of my trade I wish to apply a trailing stop to move with the trade as profit increases, that trailing stop is the same as the original stop loss (1.5 X ATR at trade entry).
The trade is closed when either my stop loss is hit or my exit conditions (indicators) signal me to exit. You will see there is an entry condition I have named “continuation trade” this is simply to allow for re-traces (to exit the trade and then get back in if conditions are favourable).
Any help is welcome however time is getting away from me and I really need to solve this quickly so I can get testing algo’s. Thanks in advance.
DEFPARAM CumulateOrders = False // Cumulating positions deactivated
// Prevents the system from placing new orders on specified days of the week
daysForbiddenEntry = OpenDayOfWeek = 0
// --- settings
amount = 2 // amount of contract/lots/shares to open for each order
Lot2Close = 1 // amount of contract/lots/shares to close when breakeven occurs
breakeven = averagetruerange*1.5 // multiple of ATR at time of trade entry to reach no loss trade
stoploss = averagetruerange*1.5 // multiple of ATR at time of trade entry to stop further loss
//trailing stop function
trailingstart = averagetruerange*2 //trailingstop will start at mulitple of ATR points profit at time of trade entry
//trailing step is moving the "stoploss" as trade moves in your favour
// end settings
// Conditions to enter long positions
indicator1 = HullAverage[50](close)
c1 = (close > indicator1)
indicator2, indicator3, ignored, indicator4 = CALL "Aroon oscillator"[14]
c2 = (indicator2 CROSSES OVER indicator3)
indicator5 = Average[5](close)-Average[25](close)
c3 = (indicator5 > 0)
c4 = (indicator2 CROSSES OVER indicator4)
// original entry signal Aroon zero cross
IF (c1 AND c2 AND c3) AND not daysForbiddenEntry THEN
BUY amount CONTRACT AT MARKET
// "continuation trade" signal (not opening 2nd position) Aroon line crosses upper line c4 and correct side of moving average c1 2nd confirmation indicator long c3 (long again signal)
ELSIF NOT LONGONMARKET AND c4 AND c1 AND c3 AND not daysForbiddenEntry THEN
BUY amount CONTRACT AT MARKET
stoploss = averagetruerange[14]*1.5
breakeven = averagetruerange[14]*1.5
trailingstart = averagetruerange[14]*2
ENDIF
// Conditions to exit long positions
indicator6 = HullAverage[50](close)
c5 = (close < indicator6)
indicator7, indicator8, ignored, indicator9 = CALL "Aroon oscillator"[14]
c6 = (indicator7 CROSSES UNDER indicator8)
indicator10 = Average[5](close)-Average[25](close)
c7 = (indicator10 < 0)
c8 = (indicator7 CROSSES UNDER indicator9)
// Manage trade "scale out" at breakeven (half off the table)
IF NOT ONMARKET THEN
breakeven =0
stoploss =0
trailingstart =0
newSL =0
ENDIF
IF LONGONMARKET AND countoflongshares= amount AND close-tradeprice[1]=> breakeven THEN
SELL Lot2Close CONTRACT AT MARKET
ENDIF
// Move to "trailing stop" (trailing start) then apply trailing step (difference in close of trade price in favour of trade) to new stoploss (trailing stop)
IF LONGONMARKET AND newSL=0 AND close-tradeprice(1)=> trailingstart THEN
newSL = close-tradeprice(1) - stoploss
ENDIF
//next moves
IF LONGONMARKET AND newSL>0 AND close-tradeprice(1)> close-tradeprice (-1) THEN
newSL = newSL+(close-tradeprice(1)-close-tradeprice(-1))
ENDIF
// New Stoploss
IF LONGONMARKET AND close-tradeprice(1)=< newSL THEN
SELL AT MARKET
// Normal exit indicator signals
ELSIF c5 OR c6 OR c7 OR c8 THEN
SELL AT MARKET
stoploss = averagetruerange[14]*1.5
breakeven = averagetruerange[14]*1.5
ENDIF
// Conditions to enter short positions
indicator11 = HullAverage[50](close)
c9 = (close < indicator11)
indicator12, indicator13, indicator14, ignored = CALL "Aroon oscillator"[14]
c10 = (indicator12 CROSSES UNDER indicator13)
indicator15 = Average[5](close)-Average[25](close)
c11 = (indicator15 < 0)
c12 = (indicator12 CROSSES UNDER indicator14)
// original entry signal Aroon zero cross
IF (c9 AND c10 AND c11) AND not daysForbiddenEntry THEN
SELLSHORT amount CONTRACT AT MARKET
// "continuation trade" signal(not opening 2nd position) Aroon line crosses lower line c12 and correct side of moving average c9 2nd confirmation indicator short c11 (short again signal)
ELSIF NOT SHORTONMARKET AND c12 AND c9 AND c11 AND not daysForbiddenEntry THEN
SELLSHORT amount CONTRACT AT MARKET
stoploss = averagetruerange[14]*1.5
breakeven = averagetruerange[14]*1.5
trailingstart = averagetruerange[14]*2
ENDIF
// Conditions to exit short positions
indicator16 = HullAverage[50](close)
c13 = (close > indicator16)
indicator17, indicator18, indicator19, ignored = CALL "Aroon oscillator"[14]
c14 = (indicator17 CROSSES OVER indicator18)
indicator20 = Average[5](close)-Average[25](close)
c15 = (indicator20 CROSSES OVER 0)
c16 = (indicator17 CROSSES OVER indicator19)
// Manage trade "scale out" at breakeven (half off the table)
IF NOT ONMARKET THEN
breakeven =0
stoploss =0
trailingstart =0
newSL =0
ENDIF
IF SHORTONMARKET AND countofshortshares= amount AND close-tradeprice[1]=> breakeven THEN
EXITSHORT Lot2Close CONTRACT AT MARKET
ENDIF
// Move to "trailing stop" (trailing start) then apply trailing step (difference in close of trade price in favour of trade) to new stoploss (trailing stop)
IF SHORTONMARKET AND newSL=0 AND close-tradeprice(1)=> trailingstart THEN
newSL= close-tradeprice(1)+ stoploss
ENDIF
//next moves
IF SHORTONMARKET AND newSL>0 AND close-tradeprice(1)< close-tradeprice(-1) THEN
newSL = newSL-(close-tradeprice(-1)-close-tradeprice(1))
ENDIF
// New Stoploss
IF SHORTONMARKET AND close-tradeprice(1)=> newSL THEN
EXITSHORT AT MARKET
// Normal exit indicator signals
ELSIF c13 OR c14 OR c15 OR c16 THEN
EXITSHORT AT MARKET
stoploss = averagetruerange[14]*1.5
breakeven = averagetruerange[14]*1.5
ENDIF
// Stops and targets
SET STOP pLOSS stoploss
P.S. from time to time I get an error message when I click the back
test my system button. The message reads.
Line 141 (the line number varies)
One of the following characters would be more suitable than “End of code” : null
However there is no code at this line even the line number does not appear, the code finishes two lines earlier and then the next lines number appears 1 line below where the code ends with again no code written.
Once again thanks in advance.
Regards 19robhfx60