Advertisement
Maurizio-Ciullo

Indicatore Drawdown Long Time Days

Oct 15th, 2024
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
  2. // © Maurizio-Ciullo
  3.  
  4. //@version=5
  5. strategy("Indicatore Drawdown Long Time Days", overlay=true, margin_long=100, margin_short=100)
  6.  
  7. longCondition = ta.crossover(ta.sma(close, 14), ta.sma(close, 28))
  8. if (longCondition)
  9.     strategy.entry("My Long Entry Id", strategy.long)
  10.  
  11. shortCondition = ta.crossunder(ta.sma(close, 14), ta.sma(close, 28))
  12. if (shortCondition)
  13.     strategy.entry("My Short Entry Id", strategy.short)
  14.  
  15.  
  16. ////////////////////////////////////////////////////////////
  17. // START DRAWDOWN LONG TIME IN DAYS //
  18. ////////////////////////////////////////////////////////////
  19.  
  20. // Drawdown time code
  21. var float equityPeak = strategy.initial_capital
  22.  
  23. var int currentDrawdownBars = 0
  24. var array<int> drawdownBarsArray = array.new<int>(0)
  25.  
  26. if (barstate.isconfirmed)
  27.     // If bar is closed, check the following:
  28.     if (strategy.closedtrades != strategy.closedtrades[1]) // Check if a trade just closed
  29.         if (strategy.equity > equityPeak) // If so, update REALIZED equity peak
  30.             equityPeak := strategy.equity
  31.     if (strategy.equity < equityPeak) // If current equity is lower than the peak, we're in drawdown
  32.         currentDrawdownBars := currentDrawdownBars + 1
  33.     else // If current equity is not lower than the peak, we've cleared the drawdown
  34.         if (currentDrawdownBars > 0) // Save this last drawdown so we can average all DD's later
  35.             drawdownBarsArray.push(currentDrawdownBars)
  36.         // Reset counter
  37.         currentDrawdownBars := 0
  38.  
  39. // Calculate average drawdown
  40. int averageBarsInDD = drawdownBarsArray.avg()
  41.  
  42. // Draw table information
  43. var table displayTable = na
  44.  
  45. if (barstate.isconfirmed)
  46.     displayTable := table.new(position.top_right, columns = 1, rows = 2, bgcolor = color.black, border_width = 1)
  47.     // Get timeframe period
  48.     string timeInDrawdown = "Timeframe Unsupported"
  49.     string avgDrawdown = "Timeframe Unsupported"
  50.     if (timeframe.isdwm)
  51.         if (timeframe.isdaily)
  52.             timeInDrawdown := str.tostring(drawdownBarsArray.max() * timeframe.multiplier) + " days"
  53.             avgDrawdown := str.tostring(averageBarsInDD * timeframe.multiplier, "#.##") + " days"
  54.         else
  55.             displayTable.cell(0, 0, "Timeframe Unsupported")
  56.     else
  57.         int minutesInDrawdown = drawdownBarsArray.max() * int(str.tonumber(timeframe.period))
  58.         if (minutesInDrawdown <= 60) // Less than 1 hour
  59.             timeInDrawdown := str.tostring(minutesInDrawdown) + " minutes"
  60.             avgDrawdown := str.tostring(averageBarsInDD * str.tonumber(timeframe.period), "#.##") + " minutes"
  61.         else if (minutesInDrawdown <= 1440) // Less than 1 day
  62.             timeInDrawdown := str.tostring(minutesInDrawdown / 60, "#.##") + " hours"
  63.             avgDrawdown := str.tostring((averageBarsInDD * str.tonumber(timeframe.period)) / 60, "#.##") + " hours"
  64.         else // More than 1 day
  65.             timeInDrawdown := str.tostring(minutesInDrawdown / 1440, "#.##") + " days"
  66.             avgDrawdown := str.tostring((averageBarsInDD * str.tonumber(timeframe.period)) / 1440, "#.##") + " days"
  67.  
  68.     displayTable.cell(0, 0, "Worst Drawdown: " + timeInDrawdown, text_color=color.white)
  69.     displayTable.cell(0, 1, "Avg Drawdown: " + avgDrawdown, tooltip="Total Drawdown Count: " + str.tostring(drawdownBarsArray.size()), text_color=color.white)
  70.  
  71. ////////////////////////////////////////////////////////////
  72. // END DRAWDOWN LONG TIME IN DAYS //
  73. ////////////////////////////////////////////////////////////
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement