Hi I am using a strategy to open up to 2 positions and would like to change the stop loss after the price moved 10 point from the entry. In backtesting it seems that it is working however, it doesnt work in real execution. I ran this strategy today and it didnt change the stop loss. Luckily I was in profit and hit the target.
Does it have anything to do with IG limitation? Is there any work around?
sl=21
tp=29
if countoflongshares<80 and countofshortshares<80 then
if dhigh(0) – lowest[1]>30 and close[0]>open[1] and dopen(0) – lowest[1]>1 then
buy 50 contracts at market
SET STOP LOSS sl
set target profit tp
endif
if highest[1] – dlow(0)>30 and close[0]<open[1] and highest[1] – dopen(0) >1 then
sellshort 50 contracts at market
SET STOP LOSS sl
set target profit tp
endif
endif
if longonmarket and close = tradeprice + 10 then
set stop loss 0
elsif longonmarket and close = tradeprice -20 then
set target profit 0
endif
if shortonmarket and close= tradeprice -10 then
set stop loss 0
elsif shortonmarket and close= tradeprice +20 then
set target profit 0
endif
set stop loss 0 disables the Stop Loss and set target profit 0 disables the Take Profit.
Why are you using them?
Moreover, when accumulating positions TradePrice should be replaced by PositionPrice.
thanks for your reply, I am trying to change the stop loss to 0 (that means the entry price ) when I am 10 points away from my entry .
No, to set the stop loss to breakeven you can’t do that, as this will leave your strategy WITHOUT a stop loss.
You will have to exit using a pending STOP order.
Thanks, I changed it to the below but it seems that it is still not working:
f longonmarket and close = positionprice -15 then
sell countoflongshares contracts at positionprice limit
endif
if longonmarket and close = positionprice +12 then
sell countoflongshares contracts at positionprice stop
endif
if shortonmarket and close= positionprice +15 then
buy countofshortshares contracts at positionprice limit
endif
if shortonmarket and close= positionprice -12 then
buy countofshortshares contracts at positionprice stop
endif
this trail works with cumulating positions, but it’s triggered at a % level rather than points – typically 0.2 – 0.3 %.
a1 and a2 are how fast you want it to trail.
Sensitivity is a choice of what triggers it – close, high, low or typicalprice (close+high+low)/3
// %trailing stop function incl. cumulative positions
once trailingstoptype = 1
if trailingstoptype then
//====================
trailingpercentlong = tst // %
trailingpercentshort = tss // %
once acceleratorlong = a1 // typically tst*0.1
once acceleratorshort= a2 // typically tss*0.1
ts2sensitivity = 2 // [1] close [2] high/low [3] low/high [4] typicalprice
//====================
once steppercentlong = (trailingpercentlong/10)*acceleratorlong
once steppercentshort = (trailingpercentshort/10)*acceleratorshort
if onmarket then
trailingstartlong = positionprice*(trailingpercentlong/100)
trailingstartshort = positionprice*(trailingpercentshort/100)
trailingsteplong = positionprice*(steppercentlong/100)
trailingstepshort = positionprice*(steppercentshort/100)
endif
if not onmarket or ((longonmarket and shortonmarket[1]) or (longonmarket[1] and shortonmarket)) then
newsl = 0
mypositionprice = 0
endif
positioncount = abs(countofposition)
if newsl > 0 then
if positioncount > positioncount[1] then
if longonmarket then
newsl = max(newsl,positionprice * newsl / mypositionprice)
else
newsl = min(newsl,positionprice * newsl / mypositionprice)
endif
endif
endif
if ts2sensitivity=1 then
ts2sensitivitylong=close
ts2sensitivityshort=close
elsif ts2sensitivity=2 then
ts2sensitivitylong=high
ts2sensitivityshort=low
elsif ts2sensitivity=3 then
ts2sensitivitylong=low
ts2sensitivityshort=high
elsif ts2sensitivity=4 then
ts2sensitivitylong=(typicalprice)
ts2sensitivityshort=(typicalprice)
endif
if longonmarket then
if newsl=0 and ts2sensitivitylong-positionprice>=trailingstartlong then
newsl = positionprice+trailingsteplong
endif
if newsl>0 and ts2sensitivitylong-newsl>=trailingsteplong then
newsl = newsl+trailingsteplong
endif
endif
if shortonmarket then
if newsl=0 and positionprice-ts2sensitivityshort>=trailingstartshort then
newsl = positionprice-trailingstepshort
endif
if newsl>0 and newsl-ts2sensitivityshort>=trailingstepshort then
newsl = newsl-trailingstepshort
endif
endif
if barindex-tradeindex>1 then
if longonmarket then
if newsl>0 then
sell at newsl stop
endif
if newsl>0 then
if low crosses under newsl then
sell at market
endif
endif
endif
if shortonmarket then
if newsl>0 then
exitshort at newsl stop
endif
if newsl>0 then
if high crosses over newsl then
exitshort at market
endif
endif
endif
endif
mypositionprice = positionprice
endif
if (shortonmarket and newsl > 0) or (longonmarket and newsl>0) then
if positioncount > positioncount[1] then
if longonmarket then
newsl = max(newsl,positionprice * newsl / mypositionprice)
endif
if shortonmarket then
newsl = min(newsl,positionprice * newsl / mypositionprice)
endif
endif
endif
thanked this post
Sorry, I’m still half asleep – you asked about stop loss, not trailing stop … maybe it’s useful to you anyway.
Link to above TS code added as Log 308 here …
Snippet Link Library
Try this one:
Once Flag1 = 0
Once Flag2 = 0
IF Not OnMarket OR (LongOnMarket AND ShortOnMarket[1]) OR (LongOnMarket[1] AND ShortOnMarket) THEN
Flag1 = 0
Flag2 = 0
ENDIF
//
if longonmarket and close = positionprice -15 THEN
Flag1 = 1
endif
if longonmarket and close = positionprice +12 then
Flag2 = -1
endif
if shortonmarket and close = positionprice +15 then
Flag1 = 1
endif
if shortonmarket and close = positionprice -12 then
Flag2 = -1
endif
//
if LongOnMarket AND Flag1 THEN
sell countoflongshares contracts at positionprice limit
endif
if LongOnMarket AND Flag2 then
sell countoflongshares contracts at positionprice stop
endif
if shortonmarket Flag1 then
buy countofshortshares contracts at positionprice limit
endif
if shortonmarket Flag2 then
buy countofshortshares contracts at positionprice stop
endif
thanks it is working now, however, if I hit the stop or target the order stays open . Is there any command to check at each bar :
if not onmarket cancel all open orders?
I think it’s due to close = positionprice, because it’s impossible that a price is equal to another one.
Try using >= or <=.
yeah make sense, how about canceling the order after the position is closed? I dont want for the order to stay after I am not in the market.
I just read in the other discussion that the Pending orders are cancelled at the end of each bar. Let me explain again what I am looking for:
If long on the market and price moves in my favor by 15 points I want to change the stop loss to the entry to breakeven if the price moved back. If I use the pending order when the first bar closes above the entry + 15 it will create a pending order however , this pending order will be cancelled as soon as price closes between entry and 15 points. So it cannot be used as a stop loss
Is there any other workaround?
Thanks for your help .I figured it out.
Just couples of question , why are you using this: (LongOnMarket AND ShortOnMarket[1]) OR (LongOnMarket[1] AND ShortOnMarket)
and is there any reason that you are using “-1” for flag2?
(LongOnMarket AND ShortOnMarket[1]) OR (LongOnMarket[1] AND ShortOnMarket) is used to reset some variables when there’s a Stop & Reverse, because Not OnMarket is false in that case,
“-1” and “1” are used to tell flag2 from flag1.