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
K = stochastic[14,3]
// Conditions to ENTER LONG positions. BUY
IF NOT OnMarket AND open < close and close > high[1] and close > high[3] and K[1] >25 THEN
BUY 1 CONTRACTS AT MARKET
ENDIF
// Conditions to EXIT LONG positions. SELL
If LongOnMarket AND close< low[1] THEN
SELL AT MARKET
ENDIF
// Stops and targets
SET STOP LOSS low
You 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:
SET STOP LOSS close - low
but 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;
K = stochastic[14,3]
// Conditions to ENTER LONG positions. BUY
IF NOT OnMarket AND open < close and close > high[1] and close > high[3] and K[1] >25 THEN
BUY 1 CONTRACTS AT MARKET
StopLoss = low
ENDIF
// Conditions to EXIT LONG positions. SELL
If LongOnMarket AND close< low[1] THEN
SELL AT MARKET
ENDIF
// Stops and targets
IF OnMarket THEN
SELL AT StopLoss STOP
ENDIF
Hi 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 work
E = exponentialaverage[89]
K = stochastic[14,3]
D = average[3](D)
//LONG BUY ENTRY
if NOT OnMarket AND close > high[1] and close > high[3] and open < close and low < E and close > E then
BUY 1 CONTRACTS AT MARKET
StopLoss = low
endif
If LongOnMarket AND D> 80 and D[1]> D THEN
StopLoss = tradeprice
ENDIF
//Stop Loss and TA
IF OnMarket THEN
SELL AT StopLoss STOP
Set TARGET PROFIT 30*pointsize
ENDIF
In 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:
E = exponentialaverage[89]
K = stochastic[14,3]
D = average[3](K)
//
//LONG BUY ENTRY
if NOT OnMarket AND close > high[1] and close > high[3] and open < close and low < E and close > E then
BUY 1 CONTRACTS AT MARKET
StopLoss = low
//
endif
If LongOnMarket AND D> 80 and D[1]> D THEN
StopLoss = tradeprice
ENDIF
//
//Stop Loss and TA
IF OnMarket THEN
SELL AT StopLoss STOP
Set TARGET PROFIT 30*pointsize
ENDIF
//
graph (D[1] > D) AND (D > 80) AND LongOnMarket
graphonprice StopLoss
Oh, yes of course, to long behind the screens. I wrote it wrong and overlooked it all the time.
Grazie
Hi 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.
//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 low
if NOT OnMarket AND close > high[1] and close > high[3] and open < close and low < E and close > E and D<80 then
BUY 1 CONTRACTS AT MARKET
StopLoss = low
endif
// Step 2: Stoploss moves to neutral
If LongOnMarket AND D> 80 and D[1]> D THEN
StopLoss = tradeprice
endif
//start of cyclelow looking for the lowest price as long as Stochastic %D<50 and after %D crosses over 50
If D[1] => 50 and D < 50 then
incyclelow = 1
lowestprice2 = lowestprice
Lowestprice = low
endif
//End of cyclelow
If D [1] < 50 and D => 50 then
incyclelow = 0
endif
//lowest price in Stochastic cycle %D<50
if incyclelow then
lowestprice = 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 cyclelow
if lowestprice<>lowestprice[1] then
lowestpriceBar=barindex
StopLoss = lowestprice
endif
//Stop Loss and TA
IF OnMarket THEN
SELL AT StopLoss STOP
ENDIF
graph (D[1] > D) AND (D > 80) AND LongOnMarket
graphonprice StopLoss
GRAPH 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.
Try this one:
//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 low
if NOT OnMarket AND close > high[1] and close > high[3] and open < close and low < E and close > E and D<80 then
BUY 1 CONTRACTS AT MARKET
StopLoss = low
MyLowest = 0
endif
// Step 2: Stoploss moves to neutral
If LongOnMarket AND D> 80 and D[1]> D THEN
StopLoss = tradeprice
endif
//start of cyclelow looking for the lowest price as long as Stochastic %D<50 and after %D crosses over 50
If D[1] => 50 and D < 50 then
incyclelow = 1
lowestprice2 = lowestprice
Lowestprice = low
endif
//End of cyclelow
If D [1] < 50 and D => 50 then
incyclelow = 0
endif
////////////////////////////////////////////
IF D CROSSES UNDER 50 THEN
MyLowest = low
ENDIF
IF D < 50 THEN
MyLowest = min(MyLowest,low)
ENDIF
IF D CROSSES OVER 50 THEN
StopLoss = min(MyLowest,low)
ENDIF
//////////////////////////////
//lowest price in Stochastic cycle %D<50
if incyclelow then
lowestprice = 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 cyclelow
if lowestprice<>lowestprice[1] then
lowestpriceBar=barindex
//StopLoss = lowestprice
endif
//Stop Loss and TA
IF OnMarket THEN
SELL AT StopLoss STOP
ENDIF
//graph (D[1] > D) AND (D > 80) AND LongOnMarket
//graphonprice StopLoss
I 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.
And is there a way in the coding that it only shows 1 decimal of the calculation when I use
DRAWTEXT (“#calculation#”, barindex, low-0.003, Dialog, standard, 12)
Or do I need to add something in that calculation?
Thanks a lot Roberto,
Let me check tomorrow. I have worked behind the screens the whole day and I want to take the time to see what happens.
Grazie mille.
Decimal 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:
// ROUNDing numbers and decimals
a = 16831.45679
// round integers
j = round(a - 0.5) //remove all decimals
x1 = round((j / 10) - 0.5) * 10 //replace the rightmost unit with 0
y1 = round((j / 100) - 0.5) * 100 //replace the rightmost tens with 00
z1 = round((j / 1000) - 0.5) * 1000 //replace the rightmost hundreds with 000
w1 = round((j / 10000) - 0.5) * 10000 //replace the rightmost ten thousands with 0000
// replace rigtmost digits with 0's
x2 = round((a * 10) - 0.5) / 10 //replace 1 rightmost decimal digit with 0
y2 = round((a * 100) - 0.5) / 100 //replace 2 rightmost decimal digits with 00
z2 = round((a * 1000) - 0.5) / 1000 //replace 3 rightmost decimal digits with 000
w2 = round((a * 10000) - 0.5) / 10000 //replace 4 rightmost decimal digits with 0000
graph a
graph j
graph x1
graph y1
graph z1
graph w1
graph x2
graph y2
graph z2
graph w2
buy at -close limit
Hi 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.
You 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:
StopLoss = low[1]
Lines 34 – 44 should be changed like this (so that it is calculated only AFTER entering a trade):
////////////////////////////////////////////
IF OnMarket THEN
IF D CROSSES UNDER 50 THEN
MyLowest = low
ENDIF
IF D < 50 THEN
MyLowest = min(MyLowest,low)
ENDIF
IF D CROSSES OVER 50 THEN
StopLoss = min(MyLowest,low)
ENDIF
ENDIF
//////////////////////////////
I understand the confusion. But I mean the one you described………….the low of the setup candle, the one just closed.