Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //@version=5
- indicator("Dynamic Time Period Candles", overlay=true)
- // Global Settings
- chartTimeframe = input.int(title="Chart Timeframe (minutes)", defval=3, minval=1, maxval=60, group="Global Settings")
- labelPosition = input.float(title="Label Position", defval=1.0, minval=-2.0, maxval=2.0, group="Global Settings")
- // Candle 1 Settings (30min)
- enableCandle1 = input.bool(title="Enable Candle 1", defval=true, group="Candle 1 Settings")
- lookbackMinutes1 = input.int(title="Candle 1 Time Period (minutes)", defval=30, minval=1, maxval=1440, group="Candle 1 Settings")
- offsetBars1 = input.int(title="Offset (bars) for Candle 1", defval=25, minval=1, maxval=500, group="Candle 1 Settings")
- showLookbackLine1 = input.bool(title="Show Lookback Line (Candle 1)", defval=true, group="Candle 1 Settings")
- lookbackLineWidth1 = input.int(title="Lookback Line Width (Candle 1)", defval=2, minval=1, maxval=10, group="Candle 1 Settings")
- lookbackLineColor1 = input.color(title="Lookback Line Color (Candle 1)", defval=color.rgb(0, 188, 212), group="Candle 1 Settings")
- lookbackLineStyle1 = input.string(title="Lookback Line Style (Candle 1)", defval="dotted", options=["solid", "dotted", "dashed"], group="Candle 1 Settings")
- showHighLow1 = input.bool(title="Show Lookback High & Low (Candle 1)", defval=true, group="Candle 1 Settings")
- highLowColor1 = input.color(title="Lookback H/L Lines Color (Candle 1)", defval=color.rgb(0, 188, 212), group="Candle 1 Settings")
- highLowStyle1 = input.string(title="Lookback H/L Lines Style (Candle 1)", defval="dashed", options=["solid", "dotted", "dashed"], group="Candle 1 Settings")
- showLabel1 = input.bool(title="Show Label (Candle 1)", defval=true, group="Candle 1 Settings")
- labelColor1 = input.color(title="Label Color (Candle 1)", defval=color.rgb(0, 188, 212), group="Candle 1 Settings")
- // Candle 2 Settings (1hr)
- enableCandle2 = input.bool(title="Enable Candle 2", defval=true, group="Candle 2 Settings")
- lookbackMinutes2 = input.int(title="Candle 2 Time Period (minutes)", defval=60, minval=1, maxval=1440, group="Candle 2 Settings")
- offsetBars2 = input.int(title="Offset (bars) for Candle 2", defval=50, minval=1, maxval=500, group="Candle 2 Settings")
- showLookbackLine2 = input.bool(title="Show Lookback Line (Candle 2)", defval=true, group="Candle 2 Settings")
- lookbackLineWidth2 = input.int(title="Lookback Line Width (Candle 2)", defval=2, minval=1, maxval=10, group="Candle 2 Settings")
- lookbackLineColor2 = input.color(title="Lookback Line Color (Candle 2)", defval=color.rgb(233, 30, 99), group="Candle 2 Settings")
- lookbackLineStyle2 = input.string(title="Lookback Line Style (Candle 2)", defval="dotted", options=["solid", "dotted", "dashed"], group="Candle 2 Settings")
- showHighLow2 = input.bool(title="Show Lookback High & Low (Candle 2)", defval=true, group="Candle 2 Settings")
- highLowColor2 = input.color(title="Lookback H/L Lines Color (Candle 2)", defval=color.rgb(233, 30, 99), group="Candle 2 Settings")
- highLowStyle2 = input.string(title="Lookback H/L Lines Style (Candle 2)", defval="dashed", options=["solid", "dotted", "dashed"], group="Candle 2 Settings")
- showLabel2 = input.bool(title="Show Label (Candle 2)", defval=true, group="Candle 2 Settings")
- labelColor2 = input.color(title="Label Color (Candle 2)", defval=color.rgb(233, 30, 99), group="Candle 2 Settings")
- // Candle 3 Settings (2hr)
- enableCandle3 = input.bool(title="Enable Candle 3", defval=true, group="Candle 3 Settings")
- lookbackMinutes3 = input.int(title="Candle 3 Time Period (minutes)", defval=120, minval=1, maxval=1440, group="Candle 3 Settings")
- offsetBars3 = input.int(title="Offset (bars) for Candle 3", defval=75, minval=1, maxval=500, group="Candle 3 Settings")
- showLookbackLine3 = input.bool(title="Show Lookback Line (Candle 3)", defval=true, group="Candle 3 Settings")
- lookbackLineWidth3 = input.int(title="Lookback Line Width (Candle 3)", defval=2, minval=1, maxval=10, group="Candle 3 Settings")
- lookbackLineColor3 = input.color(title="Lookback Line Color (Candle 3)", defval=color.rgb(255, 99, 71), group="Candle 3 Settings")
- lookbackLineStyle3 = input.string(title="Lookback Line Style (Candle 3)", defval="dotted", options=["solid", "dotted", "dashed"], group="Candle 3 Settings")
- showHighLow3 = input.bool(title="Show Lookback High & Low (Candle 3)", defval=true, group="Candle 3 Settings")
- highLowColor3 = input.color(title="Lookback H/L Lines Color (Candle 3)", defval=color.rgb(255, 99, 71), group="Candle 3 Settings")
- highLowStyle3 = input.string(title="Lookback H/L Lines Style (Candle 3)", defval="dashed", options=["solid", "dotted", "dashed"], group="Candle 3 Settings")
- showLabel3 = input.bool(title="Show Label (Candle 3)", defval=true, group="Candle 3 Settings")
- labelColor3 = input.color(title="Label Color (Candle 3)", defval=color.rgb(255, 99, 71), group="Candle 3 Settings")
- // Candle 4 Settings (4hr)
- enableCandle4 = input.bool(title="Enable Candle 4", defval=true, group="Candle 4 Settings")
- lookbackMinutes4 = input.int(title="Candle 4 Time Period (minutes)", defval=240, minval=1, maxval=1440, group="Candle 4 Settings")
- offsetBars4 = input.int(title="Offset (bars) for Candle 4", defval=100, minval=1, maxval=500, group="Candle 4 Settings")
- showLookbackLine4 = input.bool(title="Show Lookback Line (Candle 4)", defval=true, group="Candle 4 Settings")
- lookbackLineWidth4 = input.int(title="Lookback Line Width (Candle 4)", defval=2, minval=1, maxval=10, group="Candle 4 Settings")
- lookbackLineColor4 = input.color(title="Lookback Line Color (Candle 4)", defval=color.rgb(255, 152, 0), group="Candle 4 Settings")
- lookbackLineStyle4 = input.string(title="Lookback Line Style (Candle 4)", defval="dotted", options=["solid", "dotted", "dashed"], group="Candle 4 Settings")
- showHighLow4 = input.bool(title="Show Lookback High & Low (Candle 4)", defval=true, group="Candle 4 Settings")
- highLowColor4 = input.color(title="Lookback H/L Lines Color (Candle 4)", defval=color.rgb(255, 152, 0), group="Candle 4 Settings")
- highLowStyle4 = input.string(title="Lookback H/L Lines Style (Candle 4)", defval="dashed", options=["solid", "dotted", "dashed"], group="Candle 4 Settings")
- showLabel4 = input.bool(title="Show Label (Candle 4)", defval=true, group="Candle 4 Settings")
- labelColor4 = input.color(title="Label Color (Candle 4)", defval=color.rgb(255, 152, 0), group="Candle 4 Settings")
- // Candle 5 Settings (12hr)
- enableCandle5 = input.bool(title="Enable Candle 5", defval=true, group="Candle 5 Settings")
- lookbackMinutes5 = input.int(title="Candle 5 Time Period (minutes)", defval=720, minval=1, maxval=1440, group="Candle 5 Settings")
- offsetBars5 = input.int(title="Offset (bars) for Candle 5", defval=125, minval=1, maxval=500, group="Candle 5 Settings")
- showLookbackLine5 = input.bool(title="Show Lookback Line (Candle 5)", defval=true, group="Candle 5 Settings")
- lookbackLineWidth5 = input.int(title="Lookback Line Width (Candle 5)", defval=2, minval=1, maxval=10, group="Candle 5 Settings")
- lookbackLineColor5 = input.color(title="Lookback Line Color (Candle 5)", defval=color.rgb(255, 87, 34), group="Candle 5 Settings")
- lookbackLineStyle5 = input.string(title="Lookback Line Style (Candle 5)", defval="dotted", options=["solid", "dotted", "dashed"], group="Candle 5 Settings")
- showHighLow5 = input.bool(title="Show Lookback High & Low (Candle 5)", defval=true, group="Candle 5 Settings")
- highLowColor5 = input.color(title="Lookback H/L Lines Color (Candle 5)", defval=color.rgb(255, 87, 34), group="Candle 5 Settings")
- highLowStyle5 = input.string(title="Lookback H/L Lines Style (Candle 5)", defval="dashed", options=["solid", "dotted", "dashed"], group="Candle 5 Settings")
- showLabel5 = input.bool(title="Show Label (Candle 5)", defval=true, group="Candle 5 Settings")
- labelColor5 = input.color(title="Label Color (Candle 5)", defval=color.rgb(255, 87, 34), group="Candle 5 Settings")
- // Function to format time period for label
- formatTimePeriod(minutes) =>
- if minutes >= 60
- hours = math.floor(minutes / 60)
- str.tostring(hours) + " hrs"
- else
- str.tostring(minutes) + " mins"
- // Function to calculate the candle's elements
- f_getCandleData(_lookbackMinutes) =>
- periodsForLookback = _lookbackMinutes / chartTimeframe
- periodHigh = ta.highest(high, int(periodsForLookback))
- periodLow = ta.lowest(low, int(periodsForLookback))
- firstPrice = close[int(periodsForLookback)]
- lastPrice = close
- colorCandle = (lastPrice > firstPrice) ? color.green : color.red
- [periodHigh, periodLow, firstPrice, lastPrice, colorCandle]
- // Get data for each candle
- [periodHigh1, periodLow1, firstPrice1, lastPrice1, colorCandle1] = f_getCandleData(lookbackMinutes1)
- [periodHigh2, periodLow2, firstPrice2, lastPrice2, colorCandle2] = f_getCandleData(lookbackMinutes2)
- [periodHigh3, periodLow3, firstPrice3, lastPrice3, colorCandle3] = f_getCandleData(lookbackMinutes3)
- [periodHigh4, periodLow4, firstPrice4, lastPrice4, colorCandle4] = f_getCandleData(lookbackMinutes4)
- [periodHigh5, periodLow5, firstPrice5, lastPrice5, colorCandle5] = f_getCandleData(lookbackMinutes5)
- // Calculate the highest point among all lookback periods
- maxHigh = math.max(periodHigh1, math.max(periodHigh2, math.max(periodHigh3, math.max(periodHigh4, periodHigh5))))
- minLow = math.min(periodLow1, math.min(periodLow2, math.min(periodLow3, math.min(periodLow4, periodLow5))))
- labelHeight = maxHigh + (maxHigh - minLow) * labelPosition
- // Define variables with types and initialize with `na`
- var line bodyLine1 = na
- var line highWick1 = na
- var line lookbackLine1 = na
- var line highLine1 = na
- var line lowLine1 = na
- var label lookbackLabel1 = na
- var line bodyLine2 = na
- var line highWick2 = na
- var line lookbackLine2 = na
- var line highLine2 = na
- var line lowLine2 = na
- var label lookbackLabel2 = na
- var line bodyLine3 = na
- var line highWick3 = na
- var line lookbackLine3 = na
- var line highLine3 = na
- var line lowLine3 = na
- var label lookbackLabel3 = na
- var line bodyLine4 = na
- var line highWick4 = na
- var line lookbackLine4 = na
- var line highLine4 = na
- var line lowLine4 = na
- var label lookbackLabel4 = na
- var line bodyLine5 = na
- var line highWick5 = na
- var line lookbackLine5 = na
- var line highLine5 = na
- var line lowLine5 = na
- var label lookbackLabel5 = na
- // Delete previous lines and labels if they exist
- if not na(bodyLine1)
- line.delete(bodyLine1)
- if not na(highWick1)
- line.delete(highWick1)
- if not na(lookbackLine1)
- line.delete(lookbackLine1)
- if not na(highLine1)
- line.delete(highLine1)
- if not na(lowLine1)
- line.delete(lowLine1)
- if not na(lookbackLabel1)
- label.delete(lookbackLabel1)
- if not na(bodyLine2)
- line.delete(bodyLine2)
- if not na(highWick2)
- line.delete(highWick2)
- if not na(lookbackLine2)
- line.delete(lookbackLine2)
- if not na(highLine2)
- line.delete(highLine2)
- if not na(lowLine2)
- line.delete(lowLine2)
- if not na(lookbackLabel2)
- label.delete(lookbackLabel2)
- if not na(bodyLine3)
- line.delete(bodyLine3)
- if not na(highWick3)
- line.delete(highWick3)
- if not na(lookbackLine3)
- line.delete(lookbackLine3)
- if not na(highLine3)
- line.delete(highLine3)
- if not na(lowLine3)
- line.delete(lowLine3)
- if not na(lookbackLabel3)
- label.delete(lookbackLabel3)
- if not na(bodyLine4)
- line.delete(bodyLine4)
- if not na(highWick4)
- line.delete(highWick4)
- if not na(lookbackLine4)
- line.delete(lookbackLine4)
- if not na(highLine4)
- line.delete(highLine4)
- if not na(lowLine4)
- line.delete(lowLine4)
- if not na(lookbackLabel4)
- label.delete(lookbackLabel4)
- if not na(bodyLine5)
- line.delete(bodyLine5)
- if not na(highWick5)
- line.delete(highWick5)
- if not na(lookbackLine5)
- line.delete(lookbackLine5)
- if not na(highLine5)
- line.delete(highLine5)
- if not na(lowLine5)
- line.delete(lowLine5)
- if not na(lookbackLabel5)
- label.delete(lookbackLabel5)
- // Calculate the starting bar index for the lookback period for each candle
- lookbackBars1 = int(lookbackMinutes1 / chartTimeframe)
- lookbackBars2 = int(lookbackMinutes2 / chartTimeframe)
- lookbackBars3 = int(lookbackMinutes3 / chartTimeframe)
- lookbackBars4 = int(lookbackMinutes4 / chartTimeframe)
- lookbackBars5 = int(lookbackMinutes5 / chartTimeframe)
- // Draw the candle for each period
- x1 = bar_index + offsetBars1
- x2 = bar_index + offsetBars2
- x3 = bar_index + offsetBars3
- x4 = bar_index + offsetBars4
- x5 = bar_index + offsetBars5
- // Original code remains the same until the line drawing sections
- // Only showing the modified sections for clarity
- // [Previous code remains exactly the same until the drawing sections]
- // Only showing the modified sections where we reverse the drawing order
- // For Candle 5 (Draw first - will be at bottom)
- if enableCandle5
- bodyLine5 := line.new(x5, firstPrice5, x5, lastPrice5, color=colorCandle5, width=24)
- highWick5 := line.new(x5, periodHigh5, x5, periodLow5, color=colorCandle5, width=2)
- if showLookbackLine5
- lookbackLine5 := line.new(bar_index - lookbackBars5, periodLow5, bar_index - lookbackBars5, periodHigh5, color=lookbackLineColor5, width=lookbackLineWidth5, style=lookbackLineStyle5 == "solid" ? line.style_solid : lookbackLineStyle5 == "dotted" ? line.style_dotted : line.style_dashed)
- if showHighLow5
- highLine5 := line.new(bar_index - lookbackBars5, periodHigh5, bar_index, periodHigh5, color=highLowColor5, width=lookbackLineWidth5, style=highLowStyle5 == "solid" ? line.style_solid : highLowStyle5 == "dotted" ? line.style_dotted : line.style_dashed)
- lowLine5 := line.new(bar_index - lookbackBars5, periodLow5, bar_index, periodLow5, color=highLowColor5, width=lookbackLineWidth5, style=highLowStyle5 == "solid" ? line.style_solid : highLowStyle5 == "dotted" ? line.style_dotted : line.style_dashed)
- if showLabel5
- lookbackLabel5 := label.new(x5, labelHeight, text=formatTimePeriod(lookbackMinutes5), color=labelColor5, textcolor=color.black, style=label.style_label_up, size=size.normal)
- // For Candle 4
- if enableCandle4
- bodyLine4 := line.new(x4, firstPrice4, x4, lastPrice4, color=colorCandle4, width=24)
- highWick4 := line.new(x4, periodHigh4, x4, periodLow4, color=colorCandle4, width=2)
- if showLookbackLine4
- lookbackLine4 := line.new(bar_index - lookbackBars4, periodLow4, bar_index - lookbackBars4, periodHigh4, color=lookbackLineColor4, width=lookbackLineWidth4, style=lookbackLineStyle4 == "solid" ? line.style_solid : lookbackLineStyle4 == "dotted" ? line.style_dotted : line.style_dashed)
- if showHighLow4
- highLine4 := line.new(bar_index - lookbackBars4, periodHigh4, bar_index, periodHigh4, color=highLowColor4, width=lookbackLineWidth4, style=highLowStyle4 == "solid" ? line.style_solid : highLowStyle4 == "dotted" ? line.style_dotted : line.style_dashed)
- lowLine4 := line.new(bar_index - lookbackBars4, periodLow4, bar_index, periodLow4, color=highLowColor4, width=lookbackLineWidth4, style=highLowStyle4 == "solid" ? line.style_solid : highLowStyle4 == "dotted" ? line.style_dotted : line.style_dashed)
- if showLabel4
- lookbackLabel4 := label.new(x4, labelHeight, text=formatTimePeriod(lookbackMinutes4), color=labelColor4, textcolor=color.black, style=label.style_label_up, size=size.normal)
- // For Candle 3
- if enableCandle3
- bodyLine3 := line.new(x3, firstPrice3, x3, lastPrice3, color=colorCandle3, width=24)
- highWick3 := line.new(x3, periodHigh3, x3, periodLow3, color=colorCandle3, width=2)
- if showLookbackLine3
- lookbackLine3 := line.new(bar_index - lookbackBars3, periodLow3, bar_index - lookbackBars3, periodHigh3, color=lookbackLineColor3, width=lookbackLineWidth3, style=lookbackLineStyle3 == "solid" ? line.style_solid : lookbackLineStyle3 == "dotted" ? line.style_dotted : line.style_dashed)
- if showHighLow3
- highLine3 := line.new(bar_index - lookbackBars3, periodHigh3, bar_index, periodHigh3, color=highLowColor3, width=lookbackLineWidth3, style=highLowStyle3 == "solid" ? line.style_solid : highLowStyle3 == "dotted" ? line.style_dotted : line.style_dashed)
- lowLine3 := line.new(bar_index - lookbackBars3, periodLow3, bar_index, periodLow3, color=highLowColor3, width=lookbackLineWidth3, style=highLowStyle3 == "solid" ? line.style_solid : highLowStyle3 == "dotted" ? line.style_dotted : line.style_dashed)
- if showLabel3
- lookbackLabel3 := label.new(x3, labelHeight, text=formatTimePeriod(lookbackMinutes3), color=labelColor3, textcolor=color.black, style=label.style_label_up, size=size.normal)
- // For Candle 2
- if enableCandle2
- bodyLine2 := line.new(x2, firstPrice2, x2, lastPrice2, color=colorCandle2, width=24)
- highWick2 := line.new(x2, periodHigh2, x2, periodLow2, color=colorCandle2, width=2)
- if showLookbackLine2
- lookbackLine2 := line.new(bar_index - lookbackBars2, periodLow2, bar_index - lookbackBars2, periodHigh2, color=lookbackLineColor2, width=lookbackLineWidth2, style=lookbackLineStyle2 == "solid" ? line.style_solid : lookbackLineStyle2 == "dotted" ? line.style_dotted : line.style_dashed)
- if showHighLow2
- highLine2 := line.new(bar_index - lookbackBars2, periodHigh2, bar_index, periodHigh2, color=highLowColor2, width=lookbackLineWidth2, style=highLowStyle2 == "solid" ? line.style_solid : highLowStyle2 == "dotted" ? line.style_dotted : line.style_dashed)
- lowLine2 := line.new(bar_index - lookbackBars2, periodLow2, bar_index, periodLow2, color=highLowColor2, width=lookbackLineWidth2, style=highLowStyle2 == "solid" ? line.style_solid : highLowStyle2 == "dotted" ? line.style_dotted : line.style_dashed)
- if showLabel2
- lookbackLabel2 := label.new(x2, labelHeight, text=formatTimePeriod(lookbackMinutes2), color=labelColor2, textcolor=color.black, style=label.style_label_up, size=size.normal)
- // For Candle 1 (Draw last - will be on top)
- if enableCandle1
- bodyLine1 := line.new(x1, firstPrice1, x1, lastPrice1, color=colorCandle1, width=24)
- highWick1 := line.new(x1, periodHigh1, x1, periodLow1, color=colorCandle1, width=2)
- if showLookbackLine1
- lookbackLine1 := line.new(bar_index - lookbackBars1, periodLow1, bar_index - lookbackBars1, periodHigh1, color=lookbackLineColor1, width=lookbackLineWidth1, style=lookbackLineStyle1 == "solid" ? line.style_solid : lookbackLineStyle1 == "dotted" ? line.style_dotted : line.style_dashed)
- if showHighLow1
- highLine1 := line.new(bar_index - lookbackBars1, periodHigh1, bar_index, periodHigh1, color=highLowColor1, width=lookbackLineWidth1, style=highLowStyle1 == "solid" ? line.style_solid : highLowStyle1 == "dotted" ? line.style_dotted : line.style_dashed)
- lowLine1 := line.new(bar_index - lookbackBars1, periodLow1, bar_index, periodLow1, color=highLowColor1, width=lookbackLineWidth1, style=highLowStyle1 == "solid" ? line.style_solid : highLowStyle1 == "dotted" ? line.style_dotted : line.style_dashed)
- if showLabel1
- lookbackLabel1 := label.new(x1, labelHeight, text=formatTimePeriod(lookbackMinutes1), color=labelColor1, textcolor=color.black, style=label.style_label_up, size=size.normal)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement