TradingView
JayRogers
28 mai 2016 03:24

Strategy Code Example - Risk Management 

Euro Fx/U.S. DollarFXCM

Description

*** THIS IS JUST AN EXAMPLE OF STRATEGY RISK MANAGEMENT CODE IMPLEMENTATION ***

For my own future reference, and for anyone else who needs it.

Pine script strategy code can be confusing and awkward, so I finally sat down and had a little think about it and put something together that actually works (i think...)

Code is commented where I felt might be necessary (pretty much everything..) and covers:
  • Take Profit
  • Stop Loss
  • Trailing Stop
  • Trailing Stop Offset

...and details how to handle the input values for these in a way that allows them to be disabled if set to 0, without breaking the strategy.exit functionality or requiring a silly amount of statement nesting.

Also shows how to use functions (or variables/series) to execute trade entries and exits.

Cheers!
Commentaires
szx_
how will you create alerts on
strategy.exit("Exit Long", from_entry = "Long", profit = useTakeProfit, loss = useStopLoss, trail_points = useTrailStop, trail_offset = useTrailOffset)
strategy.exit("Exit Short", from_entry = "Short", profit = useTakeProfit, loss = useStopLoss, trail_points = useTrailStop, trail_offset = useTrailOffset)


as those are functions of strategy.exit, right?
Inerti4
@szx_, @JayRogers To resume all the comments under this thread which resonate a lot with my own backtesting and concerns in 2022 :

- The skewed results that give you the maximum high (or max low in short) of a candle as the trailing exit point can now be suppressed/mitigated with the Premium subscription, activating the "Bar Magnifier" function. To go even further : making the TSL to only be taken into account at bar-close, while not optimal for profits, would be the most reliable option for backtesting, but I don't know how to do that.

- It would be amazing to automate this strategy by setting alerts to trigger on the Trailing SL activation, but as an amateur in Pine Script, and after days of trying to convert it into an indicator that would do exactly this, I'm losing my mind :)

- Another solution for this would be to convert those "ticks" values to the a percent-based trailing SL that many brokers offer (for Bitcoin in my situation), but this also seems like a far more difficult task than I imagined, as the % value of the ticks would change as the price evolves. If I understand it correctly.

If anybody here has any insight that could help me with those issues I would be very thankful.
CryptoTrendTrader
Hello nice one but I am wondering how to calculate takeprofit and stoploss in percents rather than pips. Just can not figure it out. Thanks for any idea
lavventura
@CryptoTrendTrader, Same here did you have solution for this?
Virtual_Machinist
Trail stop = Fake strategy
dysrupt
@Virtual_Machinist, explain? im scripting now, just curios
theheirophant
@dysrupt, trailing stop creates incorrect backtest results, no matter what method you use to determine it. if you are exiting with trailing stop strategy condition its no good (skews results positive in a huge way)
dysrupt
@theheirophant, thanks for the reply. My thought process is, if the condition is "close<stop", in theory, the candle close would be the exact point of order close. Unless the stop was based on a higher resolution, resulting in major re-painting issues.
RJUN
Thanks a Ton man, i will remember you in my whole life..this post is my turning point in my trading journey. God Bless you!
discostu
This trailing stop is massively flawed. If you have a 20 pip trailing stop, it finds the next candle whose highest (long) or lowest (short) value is greater than 20 pips, and uses that as the final value. So if you long and the next candle's high is +70 pips, then you've just profited 70 pips!
Plus