1

I don't understand how strategy.max_drawdown and strategy.max_runup are calculated on TradingView Pine Script. My problem is the documented calculation just not correct if there are multiple exit points.

I know what is max drawdown and runup, but TradingView calculates different way than it is logical for me.

I have the following pine script:

//@version=5
strategy("StratTest", overlay=true, precision = 10, initial_capital=1000, default_qty_type=strategy.fixed, default_qty_value=100,
         commission_type=strategy.commission.cash_per_contract, 
         commission_value=0.033)

var float limit = na
var float stop = na
var float tp1 = na
var float tp2 = na

if bar_index == 860
    limit := math.round_to_mintick(close + 10 * syminfo.mintick)
    stop := math.round_to_mintick(high + 70 * syminfo.mintick)
    tp1 := math.round_to_mintick(low - 20 * syminfo.mintick)
    tp2 := math.round_to_mintick(low - 200 * syminfo.mintick)

    strategy.entry("Entry", strategy.short, limit=limit)
    strategy.exit("Exit/1", "Entry", stop=stop, limit=tp1, qty_percent = 50)
    strategy.exit("Exit/2", "Entry", stop=stop, limit=tp2, qty_percent = 50)

plotchar(strategy.max_drawdown, "max_drawdown", '', location.top, color=color.yellow)
plotchar(strategy.max_runup, "max_runup", '', location.top, color=color.yellow)

plotchar(strategy.opentrades.max_drawdown(0), "opentrades.max_drawdown(0)", '', location.top, color.gray)
plotchar(strategy.opentrades.max_runup(0), "opentrades.max_runup(0)", '', location.top, color.gray)

plotchar(strategy.closedtrades.max_drawdown(0), "closedtrades.max_drawdown(0)", '', location.top, color.white)
plotchar(strategy.closedtrades.max_drawdown(1), "closedtrades.max_drawdown(1)", '', location.top, color.white)
plotchar(strategy.closedtrades.max_runup(0), "closedtrades.max_runup(0)", '', location.top, color.white)
plotchar(strategy.closedtrades.max_runup(1), "closedtrades.max_runup(1)", '', location.top, color.white)

If I run this on CAPITALCOM:GBPJPY 15 min interval on TradingView, the strategy.max_drawdown will be 6.00. How is it possible? Without commission strategy.max_drawdown is 3.45. How it is calculated?

My calculations

  1. My start equity is 1000. I sell 100 contracts at 152.873 , high is 152.9, low is 152.823
    The order of execution is o→h→l→c
    The equity at low price: 1005, at high: 997.3. -> Runup: 5, drawdown 2.7
    This is the same on TradingView so it is OK

  2. Buy back 50 (from 100) contracts at 152.8, high is 152.861 low is 152.794
    The order of execution is o→h→l→c
    The equity at high is 1000 - 100 * (152.861 - 152.873) = 1001.2
    Then the order is executed at 152.8, profit: -50 * (152.8 - 152.873) = 3.65
    The remaining reach low, so the equity on low will be: 1000 + 3.65 - 50 * (152.794 - 152.873) = 1007.6
    So the max drawdown is still 2.7 but runup changes to 7.6. It is still OK

  3. Buy back 50 (from 50) contracts at 152.969, high is 152.982 low is 152.829
    The order of execution is o→l→h→c
    So the equity at low is 1000 + 3.65 - 50 * (152.829 - 152.873) = 1005.85
    Then the order is executed at 152.969, profit: -50 * (152.969 - 152.873) = -4.8
    The equity: 1000 + 3.65 - 4.8 = 998.85
    So the max drawdown is still 2.7 and runup is still 7.6 . It is not OK, on TradingView drawdown is 3.45 . How is it possible?


The list of trades when commission is used: Trades with commission

The list of trades without commission: Trades without commission

This is a snapshot image of the problem: TradingView Snapshot

Adam Wallner
  • 2,292
  • 23
  • 20
  • 1
    Looks like a bug with a second trade. I've directed it to the Pine team. – Starr Lucky Jan 17 '23 at 11:58
  • @StarrLucky Thanks, it was a nightmare to to make the support to even read my question. They've just sent it to "Tier 2 support team", so I think they also realized it is a bug. How you can direct bug reports to Pine team? I have another bug as well. – Adam Wallner Jan 17 '23 at 15:06
  • The Pine team received your support ticket as well. Fastest way for bugreports is still through support. – Starr Lucky Jan 18 '23 at 12:13

0 Answers0