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/14/2021 at 3:11 PM #167064
Hi
I am using PRT version V11.1-1.8.0_202
I am backtesting a LONG strategy where I enter and BUY at a certain candle pattern. I SELL at another candle pattern and I put a Stoploss at the low. When I look at the backtest order list I see that it only exits when it shows the SELL candle pattern and it doesn’t exit at the Stop Loss.
I have set it on the low and I also tried low1], but it doesn’t work.I have included the coding.
Thanks, Marc
12345678910111213141516K = stochastic[14,3]// Conditions to ENTER LONG positions. BUYIF NOT OnMarket AND open < close and close > high[1] and close > high[3] and K[1] >25 THENBUY 1 CONTRACTS AT MARKETENDIF// Conditions to EXIT LONG positions. SELLIf LongOnMarket AND close< low[1] THENSELL AT MARKETENDIF// Stops and targetsSET STOP LOSS low04/14/2021 at 3:25 PM #167066You cannot use a price to set a STOP LOSS, but a difference in price (even expressed in pips with pLOSS). You can deal with it with:
1SET STOP LOSS close - lowbut you should write that at line 6, because at line 16 it would change every candle.
If you prefer using a price, you need to use a pending STOP order (this one at line 16 because it needs to be placed each candle), but must have been previously set at line 6;
1234567891011121314151617K = stochastic[14,3]// Conditions to ENTER LONG positions. BUYIF NOT OnMarket AND open < close and close > high[1] and close > high[3] and K[1] >25 THENBUY 1 CONTRACTS AT MARKETStopLoss = lowENDIF// Conditions to EXIT LONG positions. SELLIf LongOnMarket AND close< low[1] THENSELL AT MARKETENDIF// Stops and targetsIF OnMarket THENSELL AT StopLoss STOPENDIF04/14/2021 at 4:28 PM #16707104/15/2021 at 9:09 PM #167208Hi Roberto,
One more thing. I speak here only for a LONG entry.
I have set my Stoploss like in the coding, and I want to move my Stop to the entry price (so neutral), when the Stochastic D>80 and when D[1]>D.
But the way I coded this doesn’t work12345678910111213141516171819E = exponentialaverage[89]K = stochastic[14,3]D = average[3](D)//LONG BUY ENTRYif NOT OnMarket AND close > high[1] and close > high[3] and open < close and low < E and close > E thenBUY 1 CONTRACTS AT MARKETStopLoss = lowendifIf LongOnMarket AND D> 80 and D[1]> D THENStopLoss = tradepriceENDIF//Stop Loss and TAIF OnMarket THENSELL AT StopLoss STOPSet TARGET PROFIT 30*pointsizeENDIF04/15/2021 at 10:48 PM #167213In line 3 replace (D) with (K), it cannot be an average of itself.
I also suggest to use GRAPH and GRAPHONPRICE make it easier to debug your code:
12345678910111213141516171819202122E = exponentialaverage[89]K = stochastic[14,3]D = average[3](K)////LONG BUY ENTRYif NOT OnMarket AND close > high[1] and close > high[3] and open < close and low < E and close > E thenBUY 1 CONTRACTS AT MARKETStopLoss = low//endifIf LongOnMarket AND D> 80 and D[1]> D THENStopLoss = tradepriceENDIF////Stop Loss and TAIF OnMarket THENSELL AT StopLoss STOPSet TARGET PROFIT 30*pointsizeENDIF//graph (D[1] > D) AND (D > 80) AND LongOnMarketgraphonprice StopLoss04/16/2021 at 7:18 AM #16722304/16/2021 at 8:31 AM #167229Hi Roberto,
Thanks for your help in the first place. I really appreciate that.
I added another move of my stoploss. It is still only for Long entry. I will add the short version later.
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 cyle has finished, so after D crosses over 50 again.
Step 1n and Step 2 work fine, But Step 3 doesn’t work. I programmed Step 3 originally as an indicator to show the lowest low within th cyclelow, But it shows me that lowest low even when the cycle is still going. Now for the backtest it has to use the low after the cycle has finished. And I tried different things, but that didn’t work out.I don’t know what GRAPH and GRAPHONPRICE actually do. I googled it and it is to mathematical for me as explained there.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354//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)////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 = lowendif// 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//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=barindexStopLoss = lowestpriceendif//Stop Loss and TAIF OnMarket THENSELL AT StopLoss STOPENDIFgraph (D[1] > D) AND (D > 80) AND LongOnMarketgraphonprice StopLoss04/16/2021 at 9:00 AM #167239GRAPH and GRAPHONRICE are istructions to debug your code. You will find a lot of information searching this forum and online documentation.
As to your code, I’ll check it as soon as possible.
04/16/2021 at 3:24 PM #167276Try this one:
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465//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)////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 D CROSSES UNDER 50 THENMyLowest = lowENDIFIF D < 50 THENMyLowest = min(MyLowest,low)ENDIFIF D CROSSES OVER 50 THENStopLoss = min(MyLowest,low)ENDIF////////////////////////////////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 STOPENDIF//graph (D[1] > D) AND (D > 80) AND LongOnMarket//graphonprice StopLossI commented out your line 55, then added lines 34-44 and line 15.
I did not remove any of your lines, in case you need them.
04/16/2021 at 3:43 PM #16727704/16/2021 at 4:03 PM #16727804/16/2021 at 5:27 PM #167287Decimal places cannot be limited. You can round them, but you will still see the rightmost 0’s.
Run this in ProBackTest and you will see the output in the variable window:
12345678910111213141516171819202122232425262728293031// ROUNDing numbers and decimalsa = 16831.45679// round integersj = round(a - 0.5) //remove all decimalsx1 = round((j / 10) - 0.5) * 10 //replace the rightmost unit with 0y1 = round((j / 100) - 0.5) * 100 //replace the rightmost tens with 00z1 = round((j / 1000) - 0.5) * 1000 //replace the rightmost hundreds with 000w1 = round((j / 10000) - 0.5) * 10000 //replace the rightmost ten thousands with 0000// replace rigtmost digits with 0'sx2 = round((a * 10) - 0.5) / 10 //replace 1 rightmost decimal digit with 0y2 = round((a * 100) - 0.5) / 100 //replace 2 rightmost decimal digits with 00z2 = round((a * 1000) - 0.5) / 1000 //replace 3 rightmost decimal digits with 000w2 = round((a * 10000) - 0.5) / 10000 //replace 4 rightmost decimal digits with 0000graph agraph jgraph x1graph y1graph z1graph w1graph x2graph y2graph z2graph w2buy at -close limit04/17/2021 at 2:12 PM #167324Hi Roberto, I checked you coding. It almost works. The stoploss of the first step, so the one at the BUY doesn’t work. For BUY It should put the stop on the low of the candle befor the entry candle (because there is not yet a low at the entry candle).I can’t put a finger on where it puts the stop.
The step 2 and step 3 work. Great the moving of the SL with the Lower highs work.I checked it on the EURUSD 1 hour chart and at the 5th April 2021 at 2:00 you see what I mean> It is a great Profit, but it should have been stopped out by the SL of the low of the candle of 1:00.
04/17/2021 at 2:53 PM #167333You said “For BUY It should put the stop on the low of the candle befor the entry candle (because there is not yet a low at the entry candle)“. YES, there is a low, it’s the low of the setup candle, the one just closed. But if you want to use the previous low, then replace line 14 with:
1StopLoss = low[1]Lines 34 – 44 should be changed like this (so that it is calculated only AFTER entering a trade):
12345678910111213////////////////////////////////////////////IF OnMarket THENIF D CROSSES UNDER 50 THENMyLowest = lowENDIFIF D < 50 THENMyLowest = min(MyLowest,low)ENDIFIF D CROSSES OVER 50 THENStopLoss = min(MyLowest,low)ENDIFENDIF//////////////////////////////04/17/2021 at 3:00 PM #167335 -
AuthorPosts
Find exclusive trading pro-tools on