Stoploss doesn’t work on Backtest
Forums › ProRealTime English forum › ProBuilder support › Stoploss doesn’t work on Backtest
- This topic has 30 replies, 2 voices, and was last updated 3 years ago by robertogozzi.
-
-
04/17/2021 at 3:03 PM #16733604/17/2021 at 3:11 PM #167337
The setup candle IS the one just closed, when the strategy places an order.
The order will be entered as soon as the new candle opens.
LOW is the low of the closed candle (the candle prior to the one where PeroBackTest plots an arrow).
LOW[1] is the candle prior to the closed candle (two candles prior to the plotted arrow).
04/17/2021 at 3:26 PM #16733904/17/2021 at 3:43 PM #167342I made a couple of changes, and it works now (I tested it on EurUsd, h1):
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172//Backtest: Entry crossing EMA89, with moving stoploss.// LONG// Step 1: stoploss first at low,// Step 2: then move stoploss to neutral when D>80 and D[1]>D,// Step 3: then move stoploss to the lowest low within the stochastic cyclelow (D<50) after the cycle has finished, so after D crosses over 50 again.E = exponentialaverage[89]K = stochastic[14,3]D = average[3](K)//IF Not OnMarket THENMyLowest = 0ENDIF////LONG BUY ENTRY with Step 1: Stoploss at lowif NOT OnMarket AND close > high[1] and close > high[3] and open < close and low < E and close > E and D<80 thenBUY 1 CONTRACTS AT MARKETStopLoss = lowMyLowest = 0endif// Step 2: Stoploss moves to neutralIf LongOnMarket AND D> 80 and D[1]> D THENStopLoss = tradepriceendif//start of cyclelow looking for the lowest price as long as Stochastic %D<50 and after %D crosses over 50If D[1] => 50 and D < 50 thenincyclelow = 1lowestprice2 = lowestpriceLowestprice = lowendif//End of cyclelowIf D [1] < 50 and D => 50 thenincyclelow = 0endif////////////////////////////////////////////IF OnMarket THENIF D CROSSES UNDER 50 THENMyLowest = lowENDIFIF D < 50 AND MyLowest <> 0 THENMyLowest = min(MyLowest,low)ENDIFIF D CROSSES OVER 50 AND MyLowest <> 0 THENStopLoss = min(MyLowest,low)ENDIFENDIF////////////////////////////////lowest price in Stochastic cycle %D<50if incyclelow thenlowestprice = min(lowestprice, low)Dlowest = min(Dlowest, D)endif// What is the lowest price bar of this cycle after the cyclelow has closed and %D crosses over 50?// Step 3: Move stoploss to lowest price in cyclelowif lowestprice<>lowestprice[1] thenlowestpriceBar=barindex//StopLoss = lowestpriceendif//Stop Loss and TAIF OnMarket THENSELL AT StopLoss STOPELSEMyLowest = 0ENDIF//graph (D[1] > D) AND (D > 80) AND LongOnMarketgraphonprice StopLossGRAPHONPRICE will plot a black line on your chart to show where the Stop Loss is.
04/17/2021 at 4:40 PM #167346The Stoploss line is really good. Thank you for that. That is really helpfull. But there are still problems which weren’t there befor.
The Stoploss at the Buy Entry at the Low works fine now. Also the moving from the Stoploss to neutral work. But now Stoploss based on the Higher Lows do’n’t work anymore. they did befor in the coding 04/16/2021 at 3:24 PM as you posted. But in that coding the Stoploss at the Buy Entry at the Low didn’t work. I attached an image with 2 trades with wrong exits.
– The one on the left has the Buy on 8th October 2020. The Stoploss based on the Higher low is ignored and the Exit becomes neutral.
– The one on the right has the Buy on te 19th of October where the Stoploss moved to the third Higher Low. But he the trade didn’t exit when the wick crossed the Stoploss and it exits the second time it crosses the Stoploss.I can’t put a finger on what is going on here. You can take out the lines which are of no use. I have a copy of it all.
04/17/2021 at 4:43 PM #16734704/17/2021 at 7:52 PM #167368Try removing lines 39 and 49, then lines 11-13.
04/18/2021 at 10:16 AM #16740604/19/2021 at 4:30 PM #167526Hi Roberto,
I have just tested a simple Buy Entry and Stoploss scenario to test what works and what doesn’t. The first BUY scenario is to Buy when ema7 crosses over sma50. It is only a Buy scenario to keep it simple.
I only use 2 Steps to define my Stoploss2 Stoploss.
Step 1: Stoploss at low after BUY
Step 2: When OnMarket and D crosses under 50 then move stoploss to the lowest low within the stochastic cyclelow (D<50) after the cycle has finished, so after D crosses over 50 again.The Problem is in the first Stoploss. It executes it after the close of the bar and on the close of the last bar instead of the Low defined in Step 1. (see image left trade)
When the trade is able to develope into Step 2 and the Stoploss is moved at the Lowes Low of the Stoch cycle it does execute immediatelywhen price crosses below the new Stoploss, So that works. ( see image trade on the right)I don’t know where I make the mistake.
123456789101112131415161718192021222324252627282930313233343536373839// When ema 7 crosses over sma 50 then ENTRY BUY vv for SHORTDefparam Cumulateorders = Falseema = exponentialaverage[7] (close)sma = average[50] (close)K = stochastic[14,3]D = average[3](K)// Step 1: at BUY stoploss at low,// Step 3: move stoploss to the lowest low within the stochastic cyclelow (D<50) after the cycle has finished, so after D crosses over 50 again.// CONDITIONS for ENTRYCL = ema crosses over sma//LONG BUY ENTRY with Step 1: Stoploss at lowif Not OnMarket and CL thenBUY 1 CONTRACT AT MARKETSL = lowStopLoss = SLendif//start of cyclelow looking for the lowest price as long as Stochastic %D<50 and after %D crosses over 50If OnMarket and D crosses under 50 thenMyLowest = lowENDIFIF D < 50 AND MyLowest <> 0 THENMyLowest = min(MyLowest,low)ENDIFIF D CROSSES OVER 50 AND MyLowest <> 0 THENStopLoss = min(MyLowest,low)ENDIF//Stop Loss and TAIF OnMarket THENSell at Stoploss Stopendif//graph (D[1] > D) AND (D > 80) AND LongOnMarket//graphonprice StopLoss04/19/2021 at 11:28 PM #167557I see, the previous low can be higher so it is immediately hit.
You have two alternative solutions:
- you don’t take a trade when the previous low is higher than the current price (CLOSE)
- you set a SL in a different way
04/20/2021 at 7:12 AM #167561“you don’t take a trade when the previous low is higher than the current price (CLOSE)” ——- In both trades this is not happening. So that cannot be the problem. You see that ENTRY is above the previous LOW.
” you set a SL in a different way” —————— That will cost me money. The strategy isn’t the problem. The coding doens’t work the way it is coded.
04/20/2021 at 8:15 AM #167565Why is the first one (left) wrong?
It enters a long trade and the StopLoss, at the low of the previous trade, is hit immediately,
What instrument, TF, date and time is your pic, so I can replicate those trades?
04/20/2021 at 9:38 AM #167583I have included a new image with Time and even better commends. The white text balloons belong to the left trade and the peach text balloons belong to the trade on the right.
– EURUSED 1 hour chart
The left trade has BUY ENTRY 31st of March 2012, 20:00
The right BUY ENTRY 1st of April 2021, 10:00Thank you for having a look.
04/20/2021 at 9:39 AM #16758404/20/2021 at 9:48 AM #167589 -
AuthorPosts
Find exclusive trading pro-tools on