PHP Code:
//@version=6
indicator('..', overlay = false, max_lines_count = 500, max_boxes_count = 500)
enum RenkoStyle
atr = "ATR"
percent = "Percent"
static = "Static Value"
////////////
enum Direction
up = "Up"
down = "Down"
nan = "No Trend"
///////
type Renko
float current
float hold
float high_level
float low_level
Direction direction
color colour
// //////////
atr_rma(float source, float length)=>
float alpha = 1 / math.min(length, bar_index + 1)
float i_alpha = 1 - alpha
float rma = source
rma := source * alpha + nz(rma[1], rma) * i_alpha
rma
//////////////
tr()=>
math.max(high - low
, math.abs(high - nz(close[1], math.max(open, close)))
, math.abs(low - nz(close[1], math.min(open, close)))
)
/////////////
atr(float period)=>
atr_rma(tr(), period)
// ////////////
init_renko()=>
var Renko renko = Renko.new(open, open, open, open, Direction.nan, na)
renko
/////////////////
method get_color(Renko self, color bullish, color bearish)=>
var color colour = chart.fg_color
float value = self.current
float delta = value - nz(value[1], value)
colour := switch self.direction
Direction.up => color.from_gradient(close, self.low_level, self.current, chart.fg_color, bullish)
Direction.down => color.from_gradient(close, self.current, self.high_level, bearish, chart.fg_color)
=> color.from_gradient(close, self.low_level, self.high_level, bearish, bullish)
colour
///////////
method atr(Renko self, float close_source, float period, float multiplier, bool hold_until_new)=>
float atr = atr(period) * multiplier
var float renko_atr = atr
if not hold_until_new
renko_atr := atr
int new_bars = math.floor(math.abs(self.hold - close_source) / renko_atr)
int direction = close_source > self.hold ? 1 : -1
if new_bars > 0
self.direction := direction == 1 ? Direction.up : Direction.down
while new_bars > 0
float new_open = self.hold + renko_atr * direction
self.hold := new_open
new_bars := math.floor(math.abs(new_open - close_source) / renko_atr)
if hold_until_new
renko_atr := atr
self.current := self.hold
self.high_level := self.hold + renko_atr
self.low_level := self.hold - renko_atr
self
////////////
method percent(Renko self, float close_source, float percent_change, bool hold_until_new)=>
float percent = percent_change * 0.01
float up_level = self.hold * (1 + percent)
float down_level = self.hold * (1 - percent)
self.high_level := up_level
self.low_level := down_level
float up_range = math.abs(self.hold - up_level)
float down_range = math.abs(self.hold - down_level)
bool new_up = close_source >= up_level
bool new_down = close_source <= down_level
if new_up
self.direction := Direction.up
int new_bars = math.floor(math.abs(close_source - self.hold) / up_range)
while new_bars > 0
up_range := hold_until_new ? up_range : math.abs(self.hold - self.hold * (1 + percent))
float new = self.hold + up_range
self.hold := new
new_bars := math.floor(math.abs(close_source - self.hold) / up_range)
else if new_down
self.direction := Direction.down
int new_bars = math.floor(math.abs(close_source - self.hold) / down_range)
while new_bars > 0
down_range := hold_until_new ? down_range : math.abs(self.hold - self.hold * (1 - percent))
float new = self.hold - down_range
self.hold := new
new_bars := math.floor(math.abs(close_source - self.hold) / down_range)
self.high_level := self.hold * (1 + percent)
self.low_level := self.hold * (1 - percent)
self.current := self.hold
self
//////////////
method static(Renko self, float close_source, float static_change)=>
float static_distance = math.round_to_mintick(static_change)
int new_bars = math.floor(math.abs(self.current - close_source) / static_distance)
int direction = close_source > self.current ? 1 : -1
while new_bars > 0
self.direction := direction == 1 ? Direction.up : Direction.down
float new = self.hold + static_distance * direction
self.hold := new
new_bars := math.floor(math.abs(self.hold - close_source) / static_distance)
self.current := self.hold
self.high_level := self.hold + static_distance
self.low_level := self.hold - static_distance
self
/////////////////
renko(float close_source, float atr_period, float atr_multiplier, float percent_change, float static_change, bool hold_atr_until_new, bool hold_percent_until_new, float smoothing, color bullish_color, color bearish_color, simple RenkoStyle style)=>
var Renko renko = init_renko()
switch style
RenkoStyle.atr => renko.atr(close_source, atr_period, atr_multiplier, hold_atr_until_new)
RenkoStyle.percent => renko.percent(close_source, percent_change, hold_percent_until_new)
RenkoStyle.static => renko.static(close_source, static_change)
=> renko
renko.current := atr_rma(renko.current, smoothing)
renko.high_level := atr_rma(renko.high_level, smoothing)
renko.low_level := atr_rma(renko.low_level, smoothing)
renko.colour := renko.get_color(bullish_color, bearish_color)
renko
// renko }
var const string global_group = "Global Settings"
var const string trend_color_inline = "trend_color_inline"
var const string close_source_tt = "The closing source for the renko calculations."
var const string renko_style_tt = "Renko brick size calculation method.\n\n"
+ "• ATR - Dynamic brick size based on Average True Range.\n"
+ "• Percent - Fixed percentage of current price for brick size.\n"
+ "• Static Value - Fixed absolute price distance for bricks."
var const string smoothing_tt = "RMA smoothing period for Renko levels.\n"
+ "Larger values create smoother, more stable bands.\n"
+ "Minimum value of 1 disables smoothing."
var const string static_color_tt = "Fixed color for Renko line when trend coloring is disabled.\n"
+ "Used when 'Use Trend Color' is turned off."
var const string trend_color_tt = "Gradient colors for trend-based coloring.\n"
+ "Renko line and candles interpolate between these colors based on price position.\n\n"
+ "• Left color - Bullish/uptrend color.\n"
+ "• Right color - Bearish/downtrend color."
var const string show_renko_line_tt = "Toggle display of the main Renko centerline.\n"
+ "The line tracks the current smoothed Renko brick price."
var const string use_polar_color_tt = "Enable dynamic gradient coloring based on trend.\n\n"
+ "• Enabled - Uses gradient between bullish and bearish colors.\n"
+ "• Disabled - Uses static color for Renko line."
var const string enable_candle_color_tt = "Apply trend-based gradient coloring to chart candles.\n"
+ "Colors candles based on position relative to Renko levels."
float close_source = input.source(close, "Close Source", tooltip = close_source_tt, group = global_group)
var RenkoStyle renko_style = input.enum(RenkoStyle.atr, "Style", tooltip = renko_style_tt, group = global_group)
float smoothing = input.float(1.25, "Smoothing", minval = 1, step = 0.125, tooltip = smoothing_tt, group = global_group)
color static_color = input.color(color.blue, "Static Color", tooltip = static_color_tt, group = global_group)
color bullish_color = input.color(#00FF00, "Trend Color", inline = trend_color_inline, group = global_group)
color bearish_color = input.color(#FF0000, "", inline = trend_color_inline, tooltip = trend_color_tt, group = global_group)
bool show_renko_line = input.bool(true, "Show Renko Line", tooltip = show_renko_line_tt, group = global_group)
bool use_polar_color = input.bool(true, "Use Trend Color", tooltip = use_polar_color_tt, group = global_group)
bool enable_candle_color = input.bool(false, "Candle Color", tooltip = enable_candle_color_tt, group = global_group)
var const string atr_group = "ATR Settings"
bool atr_active = renko_style == RenkoStyle.atr
var const string atr_period_tt = "Period for ATR calculation.\n"
+ "Longer periods create smoother, less sensitive brick sizing.\n\n"
+ "Only active when Style is set to ATR."
var const string atr_multiplier_tt = "Multiplier applied to ATR value for brick size.\n"
+ "Larger multipliers create wider bricks and less frequent updates.\n\n"
+ "Only active when Style is set to ATR."
var const string hold_atr_until_new_tt = "Control when ATR brick size recalculates.\n\n"
+ "• Enabled - Lock ATR value until a new brick forms (more stable).\n"
+ "• Disabled - Recalculate ATR every bar (more responsive).\n\n"
+ "Only active when Style is set to ATR."
float atr_period = input.float(14, "ATR Period", minval = 1, tooltip = atr_period_tt, active = atr_active, group = atr_group)
float atr_multiplier = input.float(2, "ATR Multiplier", minval = 0, step = 0.125, tooltip = atr_multiplier_tt, active = atr_active, group = atr_group)
bool hold_atr_until_new = input.bool(true, "Hold ATR Until New Renko", tooltip = hold_atr_until_new_tt, active = atr_active, group = atr_group)
var const string percent_group = "Percent Settings"
bool percent_active = renko_style == RenkoStyle.percent
var const string percent_change_tt = "Percentage change required for new brick formation.\n"
+ "For example, 0.5 means a 0.5% move from the last brick creates a new brick.\n\n"
+ "Only active when Style is set to Percent."
var const string hold_percent_until_new_tt = "Control when percentage base recalculates.\n\n"
+ "• Enabled - Lock percentage calculation to original brick price (compounding).\n"
+ "• Disabled - Recalculate percentage from each new brick (non-compounding).\n\n"
+ "Only active when Style is set to Percent."
float percent_change = input.float(0.5, "Percent Change", minval = 0.001, step = 0.001, tooltip = percent_change_tt, active = percent_active, group = percent_group)
bool hold_percent_until_new = input.bool(false, "Hold Percent Until New Renko", tooltip = hold_percent_until_new_tt, active = percent_active, group = percent_group)
var const string static_group = "Static Settings"
bool static_active = renko_style == RenkoStyle.static
var const string static_change_tt = "Fixed price distance for brick formation.\n"
+ "New bricks form when price moves this absolute amount from the last brick.\n"
+ "Value is automatically rounded to the instrument's minimum tick size.\n\n"
+ "Only active when Style is set to Static Value."
float static_change = input.float(25, "Static Change", minval = 1, step = 0.01, tooltip = static_change_tt, active = static_active, group = static_group)
// inputs }
// calc {
Renko renko = renko(close_source, atr_period, atr_multiplier, percent_change, static_change, hold_atr_until_new, hold_percent_until_new, smoothing, bullish_color, bearish_color, renko_style)
float renko_line = show_renko_line ? renko.current : na
color renko_color = use_polar_color ? renko.colour : static_color
color candle_color = enable_candle_color ? renko.colour : na
// calc }
// plot {
plot(renko_line, "Renko", renko_color, 2)
//plot(renko.high_level, "Renko Max", bullish_color)
//plot(renko.low_level, "Renko Min", bearish_color)
//barcolor(candle_color)
// plot }
type = input.string(defval = 'High-Low', title = 'Candlestick style', options = ['High-Low', 'Open-Close', 'Just Color'])
rcol = input(true, 'Color bars for pivots?')
range_1 = input.int(5, minval = 1, title = 'High/Low Range')
ccol = input(true, 'Color bars for consolidation?')
cons = input.int(2, minval = 1, title = 'Consolidation Range')
opener = (close[1] + open[1]) / 2
closer = (open + high + low + close) / 4
haO1 = (opener[1] + closer[1]) / 2
haC1 = (opener + high + low + closer) / 4
haO = (haO1[1] + haC1[1]) / 2
haC = (haO + high + low + haC1) / 4
haH = high
haL = low
btm = ta.lowest(low, range_1)
top = ta.highest(high, range_1)
tbtm = btm < btm[1]
ttop = top > top[1]
sideways = hl2 < ta.highest(cons)[1] and hl2 > ta.lowest(cons)[1]
col = tbtm and rcol ? color.red : ttop and rcol ? color.white : sideways and ccol ? color.yellow : haO < haC ? color.blue : color.fuchsia
//plot(ta.sma(close, 1), color = col, linewidth = 1, editable = false, trackprice = true, show_last = 1)
hv = if type == 'High-Low'
high
else
if type == 'Open-Close'
open
else
na
lv = if type == 'High-Low'
low
else
if type == 'Open-Close'
close
else
na
bcol = type == 'Just Color' ? col : na
//barcolor(bcol)
plotcandle(hv, hv, lv, lv, color = col, wickcolor = na)
//////////////////
// Global Settings
chartTimeframe = input.int(title = 'Chart Timeframe (minutes)', defval = 1, minval = 1, maxval = 60, group = 'Global Settings')
labelPosition = input.float(title = 'Label Position', defval = 0.4, 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 = 15, minval = 1, maxval = 1440, group = 'Candle 1 Settings')
offsetBars1 = input.int(title = 'Offset (bars) for Candle 1', defval = 2, 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 = 1, 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 = 'dotted', 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 = 30, minval = 1, maxval = 1440, group = 'Candle 2 Settings')
offsetBars2 = input.int(title = 'Offset (bars) for Candle 2', defval = 2, 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 = 1, minval = 1, maxval = 10, group = 'Candle 2 Settings')
lookbackLineColor2 = input.color(title = 'Lookback Line Color (Candle 2)', defval = color.rgb(0, 188, 212), 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(0, 188, 212), group = 'Candle 2 Settings')
highLowStyle2 = input.string(title = 'Lookback H/L Lines Style (Candle 2)', defval = 'dotted', 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 = 45, minval = 1, maxval = 1440, group = 'Candle 3 Settings')
offsetBars3 = input.int(title = 'Offset (bars) for Candle 3', defval = 2, 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(240, 239, 244), 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(240, 239, 244), group = 'Candle 3 Settings')
highLowStyle3 = input.string(title = 'Lookback H/L Lines Style (Candle 3)', defval = 'dotted', 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(132, 100, 247), 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 = 60, minval = 1, maxval = 1440, group = 'Candle 4 Settings')
offsetBars4 = input.int(title = 'Offset (bars) for Candle 4', defval = 2, 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 = 1, 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 = 'dotted', 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 = 90, minval = 1, maxval = 1440, group = 'Candle 5 Settings')
offsetBars5 = input.int(title = 'Offset (bars) for Candle 5', defval = 2, 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 = 1, minval = 1, maxval = 10, group = 'Candle 5 Settings')
lookbackLineColor5 = input.color(title = 'Lookback Line Color (Candle 5)', defval = color.rgb(255, 152, 0), 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, 152, 0), group = 'Candle 5 Settings')
highLowStyle5 = input.string(title = 'Lookback H/L Lines Style (Candle 5)', defval = 'dotted', 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
// Calculate minute changes outside of conditional statements
var bool minuteChanged = false
minuteChanged := ta.change(time('1')) != 0
// Calculate color changes for each candle
var bool colorChanged1 = false
var bool colorChanged2 = false
var bool colorChanged3 = false
var bool colorChanged4 = false
var bool colorChanged5 = false
colorChanged1 := colorCandle1 != colorCandle1[1]
colorChanged2 := colorCandle2 != colorCandle2[1]
colorChanged3 := colorCandle3 != colorCandle3[1]
colorChanged4 := colorCandle4 != colorCandle4[1]
colorChanged5 := colorCandle5 != colorCandle5[1]
// Calculate if all candles are the same color
allSameColor = colorCandle1 == colorCandle2 and colorCandle2 == colorCandle3 and colorCandle3 == colorCandle4 and colorCandle4 == colorCandle5
// Alert conditions
alertcondition(colorChanged1, title = 'Candle 1 Color Change', message = 'Candle 1 (30min) color changed')
alertcondition(colorChanged2, title = 'Candle 2 Color Change', message = 'Candle 2 (1hr) color changed')
alertcondition(colorChanged3, title = 'Candle 3 Color Change', message = 'Candle 3 (2hr) color changed')
alertcondition(colorChanged4, title = 'Candle 4 Color Change', message = 'Candle 4 (4hr) color changed')
alertcondition(colorChanged5, title = 'Candle 5 Color Change', message = 'Candle 5 (12hr) color changed')
alertcondition(allSameColor, title = 'All Candles Same Color', message = 'All candles are the same color')
// Alert handling
if colorChanged1
currentColor = colorCandle1 == color.green ? 'GREEN' : 'RED'
previousColor = colorCandle1[1] == color.green ? 'GREEN' : 'RED'
alert('Candle 1 (30min) changed from ' + previousColor + ' to ' + currentColor, alert.freq_once_per_bar_close)
if colorChanged2
currentColor = colorCandle2 == color.green ? 'GREEN' : 'RED'
previousColor = colorCandle2[1] == color.green ? 'GREEN' : 'RED'
alert('Candle 2 (1hr) changed from ' + previousColor + ' to ' + currentColor, alert.freq_once_per_bar_close)
if colorChanged3
currentColor = colorCandle3 == color.green ? 'GREEN' : 'RED'
previousColor = colorCandle3[1] == color.green ? 'GREEN' : 'RED'
alert('Candle 3 (2hr) changed from ' + previousColor + ' to ' + currentColor, alert.freq_once_per_bar_close)
if colorChanged4
currentColor = colorCandle4 == color.green ? 'GREEN' : 'RED'
previousColor = colorCandle4[1] == color.green ? 'GREEN' : 'RED'
alert('Candle 4 (4hr) changed from ' + previousColor + ' to ' + currentColor, alert.freq_once_per_bar_close)
if colorChanged5
currentColor = colorCandle5 == color.green ? 'GREEN' : 'RED'
previousColor = colorCandle5[1] == color.green ? 'GREEN' : 'RED'
alert('Candle 5 (12hr) changed from ' + previousColor + ' to ' + currentColor, alert.freq_once_per_bar_close)
if allSameColor
colorMsg = colorCandle1 == color.green ? 'All candles are GREEN - Strong Bullish Signal' : 'All candles are RED - Strong Bearish Signal'
alert(colorMsg, alert.freq_once_per_bar_close)
// 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)
lookbackLine5
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)
lowLine5
//if showLabel5
//lookbackLabel5 := label.new(x5, labelHeight, text = formatTimePeriod(lookbackMinutes5), color = labelColor5, textcolor = color.black, style = label.style_label_up, size = size.normal)
//lookbackLabel5
// 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)
lookbackLine4
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)
lowLine4
//if showLabel4
//lookbackLabel4 := label.new(x4, labelHeight, text = formatTimePeriod(lookbackMinutes4), color = labelColor4, textcolor = color.black, style = label.style_label_up, size = size.normal)
//lookbackLabel4
// 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)
lookbackLine3
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)
lowLine3
//if showLabel3
//lookbackLabel3 := label.new(x3, labelHeight, text = formatTimePeriod(lookbackMinutes3), color = labelColor3, textcolor = color.black, style = label.style_label_up, size = size.normal)
//lookbackLabel3
// 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)
lookbackLine2
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)
lowLine2
//if showLabel2
//lookbackLabel2 := label.new(x2, labelHeight, text = formatTimePeriod(lookbackMinutes2), color = labelColor2, textcolor = color.black, style = label.style_label_up, size = size.normal)
//lookbackLabel2
// 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)
lookbackLine1
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)
lowLine1
//if showLabel1
//lookbackLabel1 := label.new(x1, labelHeight, text = formatTimePeriod(lookbackMinutes1), color = labelColor1, textcolor = color.black, style = label.style_label_up, size = size.normal)
//lookbackLabel1
Yer İmleri