Max Drawdown in my Table is wrong, not same as in Overview (Tradingview)

Viewed 41

Can you help me fix Drawdown in this table ? Is not the same as in Overview (Tradingview)

It shows a few percent difference and cannot figure a solution.

To measure an order’s drawdown, we take the difference between the entry price and worst price. Then multiply with the order’s size and increase the outcome with commission costs.

That price difference is for long orders the distance between the entry price and lowest price while the order was open. For short orders, it’s the distance between the entry price and the highest price during the order.

var balance = strategy.initial_capital
var totalTrades = strategy.closedtrades
var totalWins = strategy.wintrades
var totalLosses = strategy.losstrades
var maxDrawdown = 0.0
var maxBalance = 0.0
var drawdown = 0.0
var winsInRow = 0
var maxWinsInRow = 0
var lossesInRow = 0
var maxLossesInRow = 0
if strategy.wintrades > strategy.wintrades[1]
    balance := strategy.initial_capital + strategy.netprofit
    // totalWins := totalWins + 1 < not necessary as we're just using strategy.wintrades now
    if balance > maxBalance
        maxBalance := balance
    winsInRow := winsInRow + 1
    if winsInRow > maxWinsInRow
        maxWinsInRow := winsInRow
    lossesInRow := 0 // reset loss counter as we've just taken a win
if strategy.losstrades > strategy.losstrades[1]
    balance := strategy.initial_capital + strategy.netprofit
    // totalLosses := totalLosses same as above
    drawdown :=  (maxBalance - balance) / maxBalance // I've changed this, but left your original here > (balance / maxBalance) - 1
    if drawdown > maxDrawdown // drawdown will now hold, as a decimal, the max % drop from maxBalance
        maxDrawdown := drawdown
    lossesInRow := lossesInRow + 1
    if lossesInRow > maxLossesInRow
        maxLossesInRow := lossesInRow
    winsInRow := 0 // reset the win counter as we've just taken a loss
var table testTable = table.new(position.bottom_right, 2, 4 , border_width=1, border_color=color.white, frame_color=color.white, frame_width=1)
fillCell(table, column, row, title, value, bgcolor, txtcolor) =>
    cellText = title + "\n" + value
    table.cell(table, column, row, cellText, bgcolor=bgcolor, text_color=txtcolor)
var bgcolor = color.new(color.blue,50)
green = color.new(color.green,50)
red = color.new(color.red,50)
if barstate.islastconfirmedhistory
    dollarReturn = strategy.netprofit
    fillCell(testTable, 0, 0, "Total Trades:", str.tostring(strategy.closedtrades), bgcolor, color.white)
    fillCell(testTable, 1, 0, "Win Rate:", str.tostring(math.round((strategy.wintrades/strategy.closedtrades)*100,2))+"%", strategy.wintrades/strategy.closedtrades >= 0.5 ? green : red,  color.white)
    fillCell(testTable, 0, 1, "Starting:", "$" + str.tostring(strategy.initial_capital), bgcolor, color.white)
    fillCell(testTable, 1, 1, "Ending:", "$" + str.tostring(math.round(balance, 2)),strategy.netprofit > 0 ? green :red, color.white)
    fillCell(testTable, 0, 3, "Wins In Row", str.tostring(maxWinsInRow), maxWinsInRow > maxLossesInRow ? green : red , color.white)
    fillCell(testTable, 1, 3, "Losses In Row", str.tostring(maxLossesInRow), maxWinsInRow > maxLossesInRow ? green : red, color.white)
    fillCell(testTable, 1, 2, "Max Drawdown", str.tostring(math.round(maxDrawdown * 100, 2))+"%", strategy.netprofit > maxDrawdown ? green : red, color.white)
    fillCell(testTable, 0, 2, "Net Profit", str.tostring(math.round(strategy.netprofit/strategy.initial_capital*100,2))+"%", strategy.netprofit/strategy.initial_capital *100 > 0 ? green : red, color.white)```
0 Answers
Related