Change Stop Loss in Strategy
Forums › ProRealTime English forum › ProOrder support › Change Stop Loss in Strategy
- This topic has 15 replies, 4 voices, and was last updated 2 years ago by Amin1233.
-
-
10/18/2021 at 2:49 AM #179834
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=29if 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
endif10/18/2021 at 3:46 AM #179836set 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.
10/18/2021 at 8:35 AM #17984310/18/2021 at 10:43 AM #179860No, 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.
10/19/2021 at 4:12 AM #179896Thanks, 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
endif10/19/2021 at 7:37 AM #179902this 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
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697// %trailing stop function incl. cumulative positionsonce trailingstoptype = 1if trailingstoptype then//====================trailingpercentlong = tst // %trailingpercentshort = tss // %once acceleratorlong = a1 // typically tst*0.1once acceleratorshort= a2 // typically tss*0.1ts2sensitivity = 2 // [1] close [2] high/low [3] low/high [4] typicalprice//====================once steppercentlong = (trailingpercentlong/10)*acceleratorlongonce steppercentshort = (trailingpercentshort/10)*acceleratorshortif onmarket thentrailingstartlong = positionprice*(trailingpercentlong/100)trailingstartshort = positionprice*(trailingpercentshort/100)trailingsteplong = positionprice*(steppercentlong/100)trailingstepshort = positionprice*(steppercentshort/100)endifif not onmarket or ((longonmarket and shortonmarket[1]) or (longonmarket[1] and shortonmarket)) thennewsl = 0mypositionprice = 0endifpositioncount = abs(countofposition)if newsl > 0 thenif positioncount > positioncount[1] thenif longonmarket thennewsl = max(newsl,positionprice * newsl / mypositionprice)elsenewsl = min(newsl,positionprice * newsl / mypositionprice)endifendifendifif ts2sensitivity=1 thents2sensitivitylong=closets2sensitivityshort=closeelsif ts2sensitivity=2 thents2sensitivitylong=hights2sensitivityshort=lowelsif ts2sensitivity=3 thents2sensitivitylong=lowts2sensitivityshort=highelsif ts2sensitivity=4 thents2sensitivitylong=(typicalprice)ts2sensitivityshort=(typicalprice)endifif longonmarket thenif newsl=0 and ts2sensitivitylong-positionprice>=trailingstartlong thennewsl = positionprice+trailingsteplongendifif newsl>0 and ts2sensitivitylong-newsl>=trailingsteplong thennewsl = newsl+trailingsteplongendifendifif shortonmarket thenif newsl=0 and positionprice-ts2sensitivityshort>=trailingstartshort thennewsl = positionprice-trailingstepshortendifif newsl>0 and newsl-ts2sensitivityshort>=trailingstepshort thennewsl = newsl-trailingstepshortendifendifif barindex-tradeindex>1 thenif longonmarket thenif newsl>0 thensell at newsl stopendifif newsl>0 thenif low crosses under newsl thensell at marketendifendifendifif shortonmarket thenif newsl>0 thenexitshort at newsl stopendifif newsl>0 thenif high crosses over newsl thenexitshort at marketendifendifendifendifmypositionprice = positionpriceendifif (shortonmarket and newsl > 0) or (longonmarket and newsl>0) thenif positioncount > positioncount[1] thenif longonmarket thennewsl = max(newsl,positionprice * newsl / mypositionprice)endifif shortonmarket thennewsl = min(newsl,positionprice * newsl / mypositionprice)endifendifendif1 user thanked author for this post.
10/19/2021 at 7:49 AM #179904Sorry, I’m still half asleep – you asked about stop loss, not trailing stop … maybe it’s useful to you anyway.
10/19/2021 at 10:28 AM #17993510/19/2021 at 4:08 PM #179978Try this one:
1234567891011121314151617181920212223242526272829303132Once Flag1 = 0Once Flag2 = 0IF Not OnMarket OR (LongOnMarket AND ShortOnMarket[1]) OR (LongOnMarket[1] AND ShortOnMarket) THENFlag1 = 0Flag2 = 0ENDIF//if longonmarket and close = positionprice -15 THENFlag1 = 1endifif longonmarket and close = positionprice +12 thenFlag2 = -1endifif shortonmarket and close = positionprice +15 thenFlag1 = 1endifif shortonmarket and close = positionprice -12 thenFlag2 = -1endif//if LongOnMarket AND Flag1 THENsell countoflongshares contracts at positionprice limitendifif LongOnMarket AND Flag2 thensell countoflongshares contracts at positionprice stopendifif shortonmarket Flag1 thenbuy countofshortshares contracts at positionprice limitendifif shortonmarket Flag2 thenbuy countofshortshares contracts at positionprice stopendif10/21/2021 at 5:42 AM #18004610/21/2021 at 8:13 AM #180053I think it’s due to close = positionprice, because it’s impossible that a price is equal to another one.
Try using >= or <=.
10/21/2021 at 9:24 AM #18006110/21/2021 at 1:22 PM #180078I 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?
10/21/2021 at 1:33 PM #18008410/21/2021 at 2:15 PM #180094(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.
-
AuthorPosts
Find exclusive trading pro-tools on