Hell0,
I am working on my backtest code for EUR/USD. To makes things easy I only talk about LONG positions!
My buy is when the average[20] crosses over average[50]. I work with a fixed target of 10 pips and original stoploss at the lowest of the last 5 bars. BUT after a high has reached the 4 pips, I want to move the stoploss to neutral, which means the buy price. That last thing doesn’t work.
How do need to describe this?
Thanks a lot, Marc
defparam cumulateorders = false
//conditions for long
mashort = average[20] (close)
malong = average[30] (close)
c1= (mashort crosses over malong)
if c1 then
BUY 1 contract at market
stoploss = lowest[5] (low)
endif
// Conditions to put the stoploss to neutral after the high has been above the buy plus 4 pips
if LongOnMarket and high => positionprice + 4 then
stoploss = positionprice
endif
// Stops and Targets
SET TARGET pPROFIT 10
SET STOP ploss stoploss
nb = barindex - tradeindex
highesthigh = highest[nb+1](high)
If longonmarket and (not c1) and highesthigh >= positionprice + 4 then
sell at positionprice stop
endif
You are mixing prices with pips.
StopLoss is a price, while SET STOP PLOSS requires pips, with Dax your SL would become 13700 pips, with Eur/Usd just a little more than 1 pip.
Try replacing line 15 with:
stoploss = abs(positionprice - high) / PipSize
But I think the new stoploss would be set from PositionPrice, not high, so it wouldn’t be a breakeven.
Using a pending Stop order might be a better solution.
Let me know if the above doesn’t work.
Thanks Xorandnot,
It looks logical, though, when I backtest, it doesn’t do what I expect it will do. I changed the original SL to a fixed numberof 4 pips, because I see that it didn’t take the stoploss at the lowest low from the last 5 candles as descibed befor. But even then, it doesn’t move the SL to neutral after the price has reached 4 pips.
See the image.
I know it has to do with the order in my programming.
// defenition of code parameters
defparam cumulateorders = false
//conditions for long
mashort = average[20] (close)
malong = average[30] (close)
c1= (mashort crosses over malong)
if c1 then
BUY 1 contract at market
endif
// Conditions to put the stoploss to neutral after the high has been above the positionprice of 4 pips
nb = barindex - tradeindex
highesthigh = highest[nb+1](high)
If longonmarket and (not c1) and highesthigh >= positionprice + 4 then
sell at positionprice stop
endif
// Stops and Targets
SET TARGET pPROFIT 10
SET STOP ploss 4
Thanks Roberto,
Yes of course, I mix pips and price. Not good 🙂 I use EUR/USD, 600 ticks
But still… 2 things don’t work in my coding.
1. When BUY then I want the stoploss below the lowest low form the previous 5 candles. The code like down here doesn’t work like that.
if c1 then
BUY 1 contract at market
stoploss = lowest[5] (low)
endif
2. When after BUY the price has reached 4 pips above the BUY, then I want the SL moved to neutral.
I have changed my line 15 the way you suggested, but that doesn’t seem to work.
defparam cumulateorders = false
//conditions for long
mashort = average[20] (close)
malong = average[30] (close)
c1= (mashort crosses over malong)
if c1 then
BUY 1 contract at market
stoploss = lowest[5] (low)
endif
// Conditions to put the stoploss to neutral after the high has been above the buy plus 4 pips
if LongOnMarket and high => positionprice + 0.0004 then
stoploss = abs(positionprice - high) / PipSize
endif
// Stops and Targets
SET TARGET pPROFIT 10
SET STOP ploss stoploss
4 pips might be too few, check with IG what’s the minimum required.
At line 9 you should add AND Not OnMarket to your conditions, so that your calculation is not made after entering a trade since this makes your SL to move each candle.
A pending stop order, similar to the one suggested by XorAndNot might be useful.