Hi there’s two issues I’m having, once does the code I’m using below have a built even break even stop loss in it somewhere? For some reason it keeps closing trades they hit 0 and I’m really not sure why.
An secondly whenever I’m trading 1 point + on anything the trailing stop, trailing step and take profit levels populate on IG as per my instructions but for some reason when I say buy 0.1 instead of buy 1.0 it never populates any trailing stop, trailing step and take profit levels. So basically it’s just an open trade which can obviously get really expensive on things like the lumber market.
I set it to 0.1 and nothing came up on IG at the time it was moving hundreds of points in minutes where the lumber market is bigger. An I had no trailing stop no nothing but a live trade. An it’s happened on smaller markets like the French market yesterday, it didn’t give me any trailing stop, take profit etc. So I had to exit the trade at a loss as my stop and take profit is set by the ATR and I didn’t want to guess the ATR and work out values while live trading something moving fast. This also happened with the Australian market once even when I had a buy 1.0 order but it happens pretty much every time with 0.5 star orders and anything under 1.0.
DEFPARAM CumulateOrders = True // Cumulating positions deactivated
ATR = AverageTrueRange[168]
//take profit
SET TARGET pPROFIT TKPROFIT
IF NOT ONMARKET AND (PositionPerf(1) + PositionPerf(2) + PositionPerf(3) + PositionPerf(4)) < -100 OR STRATEGYPROFIT < -100 THEN
QUIT
ENDIF
//trailing stop function (make equal to absolute value when trailing stop and absolute value)
IF CountOfPosition < 2 AND ONMARKET AND PositionPerf(1) > 0 THEN
//trailing will start @trailinstart points profit
TRSTART = (ATR*0.50)*40
//trailing step to move the "stoploss"
TRSTEP = (ATR*0.50)*4
// Target profit = S*T
TKPROFIT = (ATR*0.50)*16
ENDIF
IF CountOfPosition < 2 AND ONMARKET AND PositionPerf(1) <= 0 THEN
//trailing will start @trailinstart points profit
TRSTART = (ATR*0.45)*40
//trailing step to move the "stoploss"
TRSTEP = (ATR*0.45)*4
// Target profit = S*T
TKPROFIT = (ATR*0.45)*16
ENDIF
//reset the stoploss value
IF NOT ONMARKET THEN
SPLOSS = 0
ENDIF
//manage long positions
IF LONGONMARKET THEN
//first move (breakeven)
IF SPLOSS = 0 AND close-TradePrice(1)>=TRSTART THEN
SPLOSS = TradePrice(1)+TRSTEP
ENDIF
//next moves
IF SPLOSS > 0 AND close-SPLOSS>TRSTEP THEN
SPLOSS = SPLOSS+TRSTEP
ENDIF
ENDIF
//manage short positions
IF SHORTONMARKET THEN
//first move (breakeven)
IF SPLOSS = 0 AND TradePrice(1)-close>=TRSTART THEN
SPLOSS = TradePrice(1)-TRSTEP
ENDIF
//next moves
IF SPLOSS> 0 AND SPLOSS-close>TRSTEP THEN
SPLOSS = SPLOSS-TRSTEP
ENDIF
ENDIF
//stop order to exit the positions
IF SPLOSS > 0 THEN
SELL AT SPLOSS STOP
EXITSHORT AT SPLOSS STOP
ENDIF
//put the first stoploss
IF ONMARKET AND SPLOSS = 0 THEN
SET STOP pTRAILING TRSTART
ENDIF
At line 6 you are setting TP at 0, because TKPROFIT is defined below that line and only when some conditions are met.
I suggest adding this line 4:
ONCE TKPROFIT = (ATR*0.50)*16
or something similar.
Thanks for your help, it’s definitely really appreciated. I’m going to add that to the code now. Do you have any idea why if I try to buy 0.1 of lumber for example the trailing stop, trailing step and take profit won’t appear on IG? but if I order 1.0 of anything then 99% of the time it’s fine?
I have no clue about that, sorry!