Hello,
I read an article on here regarding a trailing stop loss (https://www.prorealcode.com/blog/trading/complete-trailing-stop-code-function/). I copied the exact code into one of my strategies, but I seem to be doing something wrong or understanding it incorrectly.
As i understood it, the stop ploss would first activate when it has moved (in my code) 50 points, and place the stop loss (again, in my code) 8 points over entry. After this it would move the stop loss 8 points for every 8 points the position moves in my direction. But the backtests doesn’t show that. In my backtests the stop less gets placed very weirdly and doesn’t trail the price. Am i coding something wrong or is it how it is supposed to work?
I have inserted a printscreen of the backtest as well as my code.
Regards,
Anton
defparam cumulateorders = false
enterafter = time > 090000
enterbefore = time < 220000
distance = (close - open)
c1 = (distance > 35)
c2 = (Close > Dclose(1))
Buyconditions = c1 and c2 and enterafter and enterbefore
IF BuyConditions THEN
BUY 0.4 CONTRACT AT MARKET
set stop ploss 100
ENDIF
indicator1 = CALL "RSI retest under"
c5 = (indicator1 => 1)
SellConditions = c5
IF SellConditions THEN
SELL AT MARKET
ENDIF
trailingstart = 50
trailingstep = 8
//reset the stoploss value
IF NOT ONMARKET THEN
newSL=0
ENDIF
IF LONGONMARKET THEN
//first move (breakeven)
IF newSL=0 AND close-tradeprice(1)>=trailingstart*pipsize THEN
newSL = tradeprice(1)+trailingstep*pipsize
ENDIF
//next moves
IF newSL>0 AND close-newSL>=trailingstep*pipsize THEN
newSL = newSL+trailingstep*pipsize
ENDIF
ENDIF
IF newSL>0 THEN
SELL AT newSL STOP
ENDIF
GRAPH newSL as "trailing"
Seems like the screenshot didn’t post(:
The price in your pic, where your mouse is sitting, is 27363.2 and you entry price was 27377.3, at that point you were suffering a loss, while your trailing stop would only activate when the closing price is at least 27377 + 50 pips.
That’s why it is still 0.
The mouse placement was random, didn’t mean to point anything out with it.
If you look at the plotted graph of the trailing sl and at the price, you can see that price moved a lot more than 8 points after the newsl got triggered, but it remained at the same pricelevel.
Count entry price + 50 pips.
You are summing up wrong values.
okay, I dont think i fully understand unfortunately but how would I code for it to turn out the way i described before? I.e. the trailing stop gets activated when the price has moved 50 points/pips in my favour, and after that the trailing stop moves 8 points/pips up from the previous stop everytime the position moves 8 points/pips in my favour.
I jump in with a follow upp question regarding the Trailing Stop Loss code.
Is it possible to graph the “trailing” on a code running Live?
No, but you could run the same strategy in backtest at same time as running in ProOrder and then you will see the TS using GRAPH in backtest.