I think the error is in the lines 12 -15, where it doesn’t use the low of the setup candle, the one just closed as stoploss.
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).
Hi Roberto, yes, that is clear. But besides that….. the coding doesn’t put a Stop Loss at the LOW when I code LOW and also not at LOW[1] when I code LOW[1].
So it doesn’t register the first Stop. See the attachent image.
I made a couple of changes, and it works now (I tested it on EurUsd, h1):
//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 THEN
MyLowest = 0
ENDIF
//
//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 OnMarket THEN
IF D CROSSES UNDER 50 THEN
MyLowest = low
ENDIF
IF D < 50 AND MyLowest <> 0 THEN
MyLowest = min(MyLowest,low)
ENDIF
IF D CROSSES OVER 50 AND MyLowest <> 0 THEN
StopLoss = min(MyLowest,low)
ENDIF
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
ELSE
MyLowest = 0
ENDIF
//graph (D[1] > D) AND (D > 80) AND LongOnMarket
graphonprice StopLoss
GRAPHONPRICE will plot a black line on your chart to show where the Stop Loss is.
The 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.
For soem reaosn the image doesn’t upload. It’s smaller then 2MB.
Try removing lines 39 and 49, then lines 11-13.
That doesn’t work either.
Let me try something else and I come back to it.
Hi 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.
// When ema 7 crosses over sma 50 then ENTRY BUY vv for SHORT
Defparam Cumulateorders = False
ema = 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 ENTRY
CL = ema crosses over sma
//LONG BUY ENTRY with Step 1: Stoploss at low
if Not OnMarket and CL then
BUY 1 CONTRACT AT MARKET
SL = low
StopLoss = SL
endif
//start of cyclelow looking for the lowest price as long as Stochastic %D<50 and after %D crosses over 50
If OnMarket and D crosses under 50 then
MyLowest = low
ENDIF
IF D < 50 AND MyLowest <> 0 THEN
MyLowest = min(MyLowest,low)
ENDIF
IF D CROSSES OVER 50 AND MyLowest <> 0 THEN
StopLoss = min(MyLowest,low)
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 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
“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.
Why 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?
I 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:00
Thank you for having a look.
The image didn’t upload….I don’t know why that happens
Just to be clear. The Stoploss as coded in line 18 and 19 is wrong executed. When the trade developes further and the Stoploss is moved to the Higher Low as coded in lines 23 – 30 the execution works fine.