Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
- // © Maurizio-Ciullo
- //@version=5
- strategy("Indicatore Drawdown Long Time Days", overlay=true, margin_long=100, margin_short=100)
- longCondition = ta.crossover(ta.sma(close, 14), ta.sma(close, 28))
- if (longCondition)
- strategy.entry("My Long Entry Id", strategy.long)
- shortCondition = ta.crossunder(ta.sma(close, 14), ta.sma(close, 28))
- if (shortCondition)
- strategy.entry("My Short Entry Id", strategy.short)
- ////////////////////////////////////////////////////////////
- // START DRAWDOWN LONG TIME IN DAYS //
- ////////////////////////////////////////////////////////////
- // Drawdown time code
- var float equityPeak = strategy.initial_capital
- var int currentDrawdownBars = 0
- var array<int> drawdownBarsArray = array.new<int>(0)
- if (barstate.isconfirmed)
- // If bar is closed, check the following:
- if (strategy.closedtrades != strategy.closedtrades[1]) // Check if a trade just closed
- if (strategy.equity > equityPeak) // If so, update REALIZED equity peak
- equityPeak := strategy.equity
- if (strategy.equity < equityPeak) // If current equity is lower than the peak, we're in drawdown
- currentDrawdownBars := currentDrawdownBars + 1
- else // If current equity is not lower than the peak, we've cleared the drawdown
- if (currentDrawdownBars > 0) // Save this last drawdown so we can average all DD's later
- drawdownBarsArray.push(currentDrawdownBars)
- // Reset counter
- currentDrawdownBars := 0
- // Calculate average drawdown
- int averageBarsInDD = drawdownBarsArray.avg()
- // Draw table information
- var table displayTable = na
- if (barstate.isconfirmed)
- displayTable := table.new(position.top_right, columns = 1, rows = 2, bgcolor = color.black, border_width = 1)
- // Get timeframe period
- string timeInDrawdown = "Timeframe Unsupported"
- string avgDrawdown = "Timeframe Unsupported"
- if (timeframe.isdwm)
- if (timeframe.isdaily)
- timeInDrawdown := str.tostring(drawdownBarsArray.max() * timeframe.multiplier) + " days"
- avgDrawdown := str.tostring(averageBarsInDD * timeframe.multiplier, "#.##") + " days"
- else
- displayTable.cell(0, 0, "Timeframe Unsupported")
- else
- int minutesInDrawdown = drawdownBarsArray.max() * int(str.tonumber(timeframe.period))
- if (minutesInDrawdown <= 60) // Less than 1 hour
- timeInDrawdown := str.tostring(minutesInDrawdown) + " minutes"
- avgDrawdown := str.tostring(averageBarsInDD * str.tonumber(timeframe.period), "#.##") + " minutes"
- else if (minutesInDrawdown <= 1440) // Less than 1 day
- timeInDrawdown := str.tostring(minutesInDrawdown / 60, "#.##") + " hours"
- avgDrawdown := str.tostring((averageBarsInDD * str.tonumber(timeframe.period)) / 60, "#.##") + " hours"
- else // More than 1 day
- timeInDrawdown := str.tostring(minutesInDrawdown / 1440, "#.##") + " days"
- avgDrawdown := str.tostring((averageBarsInDD * str.tonumber(timeframe.period)) / 1440, "#.##") + " days"
- displayTable.cell(0, 0, "Worst Drawdown: " + timeInDrawdown, text_color=color.white)
- displayTable.cell(0, 1, "Avg Drawdown: " + avgDrawdown, tooltip="Total Drawdown Count: " + str.tostring(drawdownBarsArray.size()), text_color=color.white)
- ////////////////////////////////////////////////////////////
- // END DRAWDOWN LONG TIME IN DAYS //
- ////////////////////////////////////////////////////////////
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement