Trailing stop loss
Forums › ProRealTime English forum › ProOrder support › Trailing stop loss
- This topic has 8 replies, 4 voices, and was last updated 3 years ago by GraHal.
-
-
06/11/2020 at 12:38 PM #135585
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,
Anton1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465defparam cumulateorders = falseenterafter = time > 090000enterbefore = time < 220000distance = (close - open)c1 = (distance > 35)c2 = (Close > Dclose(1))Buyconditions = c1 and c2 and enterafter and enterbeforeIF BuyConditions THENBUY 0.4 CONTRACT AT MARKETset stop ploss 100ENDIFindicator1 = CALL "RSI retest under"c5 = (indicator1 => 1)SellConditions = c5IF SellConditions THENSELL AT MARKETENDIFtrailingstart = 50trailingstep = 8//reset the stoploss valueIF NOT ONMARKET THENnewSL=0ENDIFIF LONGONMARKET THEN//first move (breakeven)IF newSL=0 AND close-tradeprice(1)>=trailingstart*pipsize THENnewSL = tradeprice(1)+trailingstep*pipsizeENDIF//next movesIF newSL>0 AND close-newSL>=trailingstep*pipsize THENnewSL = newSL+trailingstep*pipsizeENDIFENDIFIF newSL>0 THENSELL AT newSL STOPENDIFGRAPH newSL as "trailing"06/11/2020 at 12:40 PM #13558606/11/2020 at 1:55 PM #135598The 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.
06/11/2020 at 6:45 PM #135623The 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.
06/11/2020 at 8:14 PM #135631Count entry price + 50 pips.
You are summing up wrong values.
06/13/2020 at 7:04 PM #135844okay, 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.
06/13/2020 at 7:55 PM #135851Yes, that’s correct.
06/17/2020 at 9:10 AM #13622406/17/2020 at 9:24 AM #136225 -
AuthorPosts
Find exclusive trading pro-tools on