set stop loss in profit region when in profit
Forums › ProRealTime English forum › ProOrder support › set stop loss in profit region when in profit
- This topic has 15 replies, 6 voices, and was last updated 4 years ago by
murre87.
-
-
02/24/2021 at 3:15 AM #162496
hi,
anyone know how to code set stop loss in profit region when in profit?
thank you
02/24/2021 at 8:28 AM #162498You need to use pending a STOP order.
Example to set SL at breakeven + 10 pips, after profits have reached 50 pips:
1234567891011121314151617If not OnMarket thenStopLoss = 0EndifIf StopLoss = 0 and OnMarket thenProfit = PositionPerf * PositionPriceIf Profit > 50 * pipsize thenIf LongOnMarket thenStopLoss = PositionPrice + 10 * pipsizeElseStopLoss = PositionPrice - 10 * pipsizeEndifEndifEndifIf StopLoss > 0 thenSell at StopLoss STOPExitshort at StopLoss STOPEndif(not tested)
1 user thanked author for this post.
02/24/2021 at 11:07 AM #162505can someone explain why I exit there?
123456789101112131415161718IF c1 and uptrend and not onmarket THENBUY 1 CONTRACTS AT MARKETENDIFIf OnMarket thentotalProfit = PositionPerf * PositionPriceif totalprofit > 10*pipsize thenstoploss = positionprice + 5*pipsizesell at stoploss stopexitshort at stoploss stopEndifEndifIf longonmarket and totalProfit > 10 * pipsize thenif habearcandle5mn thenSell at marketEndifendifI’m not suppose to have a stop loss in the loss region
thanks
1 user thanked author for this post.
02/24/2021 at 11:14 AM #162507sorry problem solved. the reason why it stopped because the backtest period ended where it stopped.
02/24/2021 at 12:04 PM #162514Be warned that pending orders expire each bar and you have to place them again and again, if needed.
Lines 9-10 will only be executed the first time.
You should place them outside that check (like my example), otherwise you SL will placed correctly when 10 pips are reached but, if not hit and the next bar profit decreses to 6 pips, it won’t be placed at all!
02/26/2021 at 2:03 AM #162645hi, i want to put stoploss at 0 when profit is 5 pips above the positionprice and move to 5 pips in profit when it is 10pips above the positionprice. can you check my codes if i have done that? thank you
1234567891011121314151617If longOnMarket thentotalProfit = PositionPerf * PositionPriceif totalprofit > 5*pipsize thenstoploss = 0endifif totalprofit > 10*pipsize thenstoploss = positionprice + 5*pipsizeEndifEndifif stoploss=0 thensell at stoploss stopendifif stoploss > 0 thensell at stoploss stopendif02/26/2021 at 3:49 AM #1626471234567891011121314151617181920212223if not onmarket thenstoploss = 0endifIf longOnMarket thentotalProfit = PositionPerf * PositionPriceif totalprofit > 10*pipsize thenstoploss = positionprice + 5*pipsizeEndifEndifif longonmarket and close <10*pipsize thensell at stoploss stopendifIf longonmarket and totalProfit > 10 * pipsize thenif habearcandle5mn thenSell at marketEndifendifset target pprofit 100graph stoploss02/26/2021 at 3:50 AM #162649does not seem to trigger the stop loss i have on 5 pips above position price.
02/26/2021 at 6:02 AM #16265202/26/2021 at 8:51 AM #16265902/27/2021 at 7:59 AM #162704change line 12 to
1if longonmarket and ((close - positionprice )< 10*pipsize) then02/27/2021 at 8:01 AM #162705but for going short it does not work. it opens and closes immediately and the stoploss variable is always zero.
12345678910111213141516171819if not onmarket thenstoploss = 0endifIf shortOnMarket thentotalProfit = PositionPerf * PositionPriceif totalprofit > 10*pipsize thenstoploss = positionprice - 5*pipsizeEndifEndifif shortonmarket and (( positionprice - close )< 10*pipsize) thenexitshort at stoploss stopendifIf shortonmarket and totalProfit > 10 * pipsize thenif habullcandle5mn thenexitshort at marketEndifendifanyone got any ideas?
1 user thanked author for this post.
02/27/2021 at 8:09 AM #162706seems like it worked when i changed line 2 to stoploss=2 because i have spread =2 when i backtested
1 user thanked author for this post.
02/27/2021 at 8:18 AM #162707Obviously when going short prices and stop losses are reversed. So the calculations in lines 7 and 11 need to be reversed.
1stoploss = positionprice + 5*pipsize1if shortonmarket and (( close - positionprice)< 10*pipsize) then2 users thanked author for this post.
10/05/2021 at 2:59 PM #179088Thanks – very useful to code this to minimise losing any initial capital.
-
AuthorPosts
Find exclusive trading pro-tools on