Moving Stop Loss once based on condition
Forums › ProRealTime English forum › ProOrder support › Moving Stop Loss once based on condition
- This topic has 4 replies, 3 voices, and was last updated 2 months ago by Crazzycameel.
-
-
10/08/2024 at 1:36 PM #238717
Hello, first post on this forum so hopefully I have not misinterpreted any rules. I have for many hours tried to create a moving stop loss that should only move once and based on a condition. But with all my tries it mostly just exit price at the opening of the candle rather than moving the stop loss. What I want is: if a fractal low is created after the opening of my trade, I want to trail my stoploss to that low, thus tightening the Stoploss. But I only want it to be done once.
1234567891011//BARINDEX-TRADEINDEX(1)>1 = to make sure price doesn't look back a signal ahead of the entry//cp1 and CB1H1 = variables for recognizing a fractal//close > MoveSL = gives the signal that a fractal low has been createdstoploss = abs(close - (lowest[5](low))-5*pipsize)IF longonmarket thenif BARINDEX-TRADEINDEX(1)>1 and cp1 and CB1H1 and close > MoveSL THENset stop loss stoplossendifendifwhen running this it just closes at the open of the next candle when the signal is given instead of moving the stop loss to the lowest low (-5 pips) of the last five candles. the code is essentially just as this snippet:
12345IF longonmarket thenif BARINDEX-TRADEINDEX(1)>1 and cp1 and CB1H1 and close > MoveSL THENsell at marketendifendifAnyone who has any advice would be greatly appreciated.
Regards
Jonathan10/08/2024 at 4:15 PM #238732Try this one:
12345678910IF Not OnMarket THENSetStop = 1ENDIFstoploss = abs(close - (lowest[5](low))-5*pipsize)IF longonmarket and SetStop thenif BARINDEX-TRADEINDEX(1)>1 and cp1 and CB1H1 and close > MoveSL THENset stop loss stoplossSetStop = 0endifendif2 users thanked author for this post.
10/08/2024 at 4:59 PM #238735thanks roberto, its unfortunately reacting in the same manner and closes upon the opening of the next candle
i have added a printscreen below from my PRT just visually describing the issue if its any help
if you have any other suggestions i would appreciate it very much
10/09/2024 at 8:36 AM #238750Roberto’s code seems correct. Maybe you should check CP1, CB1H1 or moveSL.
i suggest work withgraphonprice
andgraph
in order to check exactly what is going on.
Here you have an example of code with StopLoss changing to breakeven.123456789101112131415161718192021222324252627282930ema1=average[10,1](close)ema2=average[50,1](close)lmin=lowest[20](low)rr=2bk=1if not onmarket and ema1 crosses over ema2 thencloseprice=closestopLong=lminriskLong=close-lmintpLong=close+rr*riskLongbreakEvenLevel=close+bk*riskLongbuy 1 contract at marketset stop price stopLongset target price tpLongendifif onmarket and high crosses over breakEvenLevel thenstopLong=tradepriceset stop breakevenendifgraphonprice ema1 coloured("blue")graphonprice ema2 coloured("purple")graphonprice tpLong coloured("green")graphonprice stopLong coloured("red")graphonprice breakEvenLevel coloured("fuchsia")1 user thanked author for this post.
10/10/2024 at 1:49 PM #238826Hey Ivan
Thanks for the advice of graphing the indicators, I have found a solution, I am not entirely sure of the logic of the solution though haha
123456789101112131415161718192021//MOVING SL ONCE IF A FRACTAL IS CREATEDIF Not OnMarket THENSetStop = 1ENDIFstoploss = (lowest[5](low))stoploss1 = (lowest[5](low))IF longonmarket and SetStop = 1 thenif BARINDEX-TRADEINDEX(1)>1 and CB1H and close > entry1 THENstoploss=tradepriceNEWSL = stoploss-stoploss1set stop loss NEWSLSetStop = 0endifendifgraphonprice CB1H coloured("green")graphonprice stoploss coloured("red")graphonprice stoploss1 coloured("blue")The CB1H indicator only returns a value in one candle(green spikes in the picture added), which seems to affect the stoploss function to return a value (just speculating). If you have any insight for me to learn, that would be greatly appreciated
//Also Robertos code does also work I when the signal occurs I noticed, but only when I write it a flat value in the stoploss, like this:
IF Not OnMarket THEN
SetStop = 1
ENDIFIF longonmarket and SetStop then
if BARINDEX-TRADEINDEX(1)>1 and CB1H and close > entry1 THEN
set stop loss 30
SetStop = 0
endif
endif//cannot manage to write this part as code here for some reason, sorry haha
-
AuthorPosts
Find exclusive trading pro-tools on