Secondary Chart with OverSized Candles
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Protervus
//   _____   ______  _____  _______ _______  ______ _    _ _     _ _______
//  |_____] |_____/ |     |    |    |______ |_____/  \  /  |     | |______
//  |       |    \_ |_____|    |    |______ |    \_   \/   |_____| ______|
//
//@version=5
indicator("Secondary chart with OverSized Candles", shorttitle = "Chart and OV candles", overlay = true, scale = scale.left)
// Asset inputs
asset               = input.symbol(title = "Source", defval="FTX:BTCUSD", group = "Sources", inline = "source")
posCandleColor      = input.color(defval = color.new(#9598a1, 50), title = "Candles Color", group = "Sources", inline = "ccolors")
negCandlesColor     = input.color(defval = color.new(#434651, 50), title = "", group = "Sources", inline = "ccolors")
showPriceline       = input.bool(true, title = "Show Price Line", group = "Sources")

// Over-sized candles inputs
oversizeThreshold   = input.float(5.0, minval=1.5, step=0.1, title="ATR Ratio Threshold", group = "Oversized Candles", inline = "oversize")   // Size Ratio to ATR Threshold
atrLen              = input.int(60, minval=1, title="ATR Length", group = "Oversized Candles", inline = "oversize")
posOvzColor         = input.color(defval = color.new(color.green, 10), title = "Oversized Candles Color", group = "Oversized Candles", inline = "ovcolor")
negOvzColor         = input.color(defval = color.new(color.red, 10), title = "", group = "Oversized Candles", inline = "ovcolor")

// Retrieve data for asset
CLOSE       = request.security(asset, timeframe.period, close)
OPEN        = request.security(asset, timeframe.period, open)
HIGH        = request.security(asset, timeframe.period, high)
LOW         = request.security(asset, timeframe.period, low)
ATR         = request.security(asset, timeframe.period, ta.atr(atrLen))
cRange      = request.security(asset, timeframe.period, ta.tr)

// Calculate ATR range
cRange     := cRange <= 0 ? CLOSE * 0.00001 : cRange
ratio      = cRange / ATR

// Condition for Over-Sized Candles
oversizeBull = OPEN < CLOSE and ratio >= oversizeThreshold
oversizeBear = OPEN > CLOSE and ratio >= oversizeThreshold

// Plot Labels
symbol  = "⚡"

oversizeBull_label = oversizeBull ? label.new(
 bar_index,
 y = LOW,
 text = symbol + "\n" + "Oversized\n" + str.tostring(ratio, "#.##"),
 color = color.new(color.black, 75),  
 textcolor = posOvzColor,   
 style = label.style_label_upper_left,  
 yloc = yloc.price,
 size = size.normal) : na
 
oversizeBear_label = oversizeBear ? label.new(
 bar_index,
 y = HIGH,
 text = "Oversized\n" + str.tostring(ratio, "#.##") + "\n" + symbol,
 color = color.new(color.black, 75),  
 textcolor = negOvzColor,   
 style = label.style_label_lower_left,  
 yloc = yloc.price,
 size = size.normal) : na


// Print Candles w/ colors
candleColor = CLOSE > OPEN ? posCandleColor : negCandlesColor
if oversizeBull
    candleColor := posOvzColor
if oversizeBear
    candleColor := negOvzColor

plotcandle(OPEN, HIGH, LOW, CLOSE, title = "Candles", color = candleColor, wickcolor=candleColor, bordercolor=candleColor)


// Create Price Line

var line priceLine = na
if barstate.islast and showPriceline
    line.delete(priceLine)
    priceLine := line.new(bar_index, CLOSE, bar_index + 5, CLOSE, color = candleColor, extend=extend.both, style = line.style_dotted, width=1)

priceline_label = barstate.islast and showPriceline ? label.new(
 bar_index + 10,
 y = CLOSE,
 text = str.tostring(asset) + "    " + str.tostring(CLOSE, "#.##"),
 color = color.new(color.black, 75),
 textcolor = CLOSE > OPEN ? color.green : color.red,   
 style = label.style_label_left,  
 yloc = yloc.price,
 size = size.normal) : na
label.delete(barstate.islast and showPriceline ? priceline_label[1] : na)  


// Send Alerts
alertcondition(oversizeBull, message = "Bullish Oversized Candle")
alertcondition(oversizeBear, message = "Bearish Oversized Candle")