Welcome to the BEST forum for traders

Did you know we're sharing tons of exclusive content here?
Our community is growing quickly. Take a look around and say "Hello".

  • Forums
  • Pinescript
  • How to calculate the average of underwater equity (drawdown) in Pine Script?

How to calculate the average of underwater equity (drawdown) in Pine Script?

Aug 19, 2021 - 11:14 AM

Viewed 919 times

https://best-trading-indicator.com/community/bti/forums/4180/topics/25616 COPY
  • I wrote a program to calculate the underwater equity - relative draw down related to the last 52 week high and it looks good.

    Below is the code for this and a screenshot

    Now I would like to expand the program and calculate the average of all drawdown values ​​in the displayed time period and draw it as a horizontal line in the chart. For this I wanted to write all drawdown values ​​in an array and calculate the average value from these values ​​and display it in the chart. However, I don't get a straight horizontal line, but a curve. Is anyone able to help me in this issue?
    thanks in advance
    Holger

    underwater equity 2.jpg

    underwater equity.jpg

    0
  • Hi Holger

    1) By curve did you mean the red stuff in the panel below?

    2) You don't get a straight horizontal line because your formula is based on the highest() and it's being recalculated at every candle - hence the plot/curve

    3/ to calculate the average drawdown, why not using a strategy script as this information is already available?

    You could use grossloss / nb of losing trades

    grossloss: https://www.tradingview.com/pine-script-reference/v4/#var_strategy{dot}grossloss

    number of losing trades: https://www.tradingview.com/pine-script-reference/v4/#var_strategy{dot}losstrades

    0
  • Hi Dave,

    thank you for your quick answer.

    Yes, the red stuff is the underwater equity curve, that shows me the draw down on each day related to the last 52 week high back from this day..

    I am looking for a code solution, where I additionally have in the window with red curve the average of all current visible drawdown values. That means, that by changing the visible time frame (for example from one year to three years) also the average value would change.

    The average value (for example - 12,3 %) should be plotted as a horizontal line in the red curve chart.

    Thanks again!

    Holger

    0
  • Hi

    Then do something like this

    plotuwe = plot(series=uwe, color=color.red, linewidth=2, show_last=1, trackprice=true)
    
    0
CONTENTS