Main Market Opener Breakout [RH]
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © HasanRifat

//@version=5
indicator("Main Market Opener Breakout [RH]", overlay=true, max_lines_count = 500, max_labels_count = 500)

// New York main market settings
newyorkShow = input.bool(defval = true, title = "", inline = "newyork")
newyorkName = input.string(defval = "New York", title = "", inline = "newyork")
newyorkTimeIn = input.session(defval = "0930-1600:23456", title = "", inline = "newyork", tooltip = "Timezone: America/New_York") 
newyorkColor = input.color(defval = color.rgb(4, 159, 9), title = "", inline = "newyork")

// London main market settings
londonShow = input.bool(defval = false, title = "", inline = "london")
londonName = input.string(defval = "London", title = "", inline = "london")
londonTimeIn = input.session(defval = "0800-1630:23456", title = "", inline = "london", tooltip = "Timezone: Europe/London")
londonColor = input.color(defval = color.rgb(9, 101, 177), title = "", inline = "london")

// Tokyo main market settings
tokyoShow = input.bool(defval = false, title = "", inline = "tokyo")
tokyoName = input.string(defval = "Tokyo", title = "", inline = "tokyo")
tokyoTimeIn = input.session(defval = "0900-1500:23456", title = "", inline = "tokyo", tooltip = "Timezone: Japan")
tokyoColor = input.color(defval = color.rgb(247, 154, 14), title = "", inline = "tokyo")

// ATR settings
length = input.int(title="ATR Length", defval=14, minval=1)
smoothing = input.string(title="ATR Smoothing", defval="RMA", options=["RMA", "SMA", "EMA", "WMA"])

// ATR value calculation
ma_function(source, length) =>
	switch smoothing
		"RMA" => ta.rma(source, length)
		"SMA" => ta.sma(source, length)
		"EMA" => ta.ema(source, length)
		=> ta.wma(source, length)
atrValue = ma_function(ta.tr(true), length)

newyorkTime = time(timeframe.period, newyorkTimeIn, "America/New_York")
londonTime = time(timeframe.period, londonTimeIn, "Europe/London")
tokyoTime = time(timeframe.period, tokyoTimeIn, "Japan")

stocks = syminfo.type == "stock"
notStocks = syminfo.type != "stock"

// Get first candle's high, low, ATR-high, ATR-low of the session
getValue(time1, sessionColor) =>
    var int lineX = na
    var float line1Y = na 
    var float line2Y = na
    var float atr1Y = na 
    var float atr2Y = na  

    if notStocks
        if time1 and not time1[1]
            lineX := bar_index
            line1Y := high
            line2Y := low
            atr1Y := high+atrValue
            atr2Y := low-atrValue
    if stocks
        if timeframe.change("D")
            lineX := bar_index
            line1Y := high
            line2Y := low
            atr1Y := high+atrValue
            atr2Y := low-atrValue
    [lineX, line1Y, line2Y, atr1Y, atr2Y]

// Draw lines based on the user settings and candle value
drawLine(lineDex, lineH, lineL, atrH, atrL, sessionColor) =>
    var int indexPlus = na 
    if notStocks
        indexPlus := 1440/timeframe.multiplier
    if stocks
        indexPlus := 360/timeframe.multiplier
    dexDif = (lineDex+(indexPlus-1)) - last_bar_index
    currDex = last_bar_index + 498
    line.new(lineDex, lineH, dexDif > 499 ? currDex : lineDex+(indexPlus-1), lineH, color = sessionColor)
    line.new(lineDex, lineL, dexDif > 499 ? currDex : lineDex+(indexPlus-1), lineL, color = sessionColor)
    line.new(lineDex, atrH, dexDif > 499 ? currDex : lineDex+(indexPlus-1), atrH, style = line.style_dashed, color = sessionColor)
    line.new(lineDex, atrL, dexDif > 499 ? currDex : lineDex+(indexPlus-1), atrL, style = line.style_dashed, color = sessionColor)

// Candle high, low, ATR-high, ATR-low value breakout finding  
breakAlert(valueH, valueL, cls, time1) =>
    var bullBreakStatus = 0
    var bearBreakStatus = 0
    if bullBreakStatus == 0
        if cls[1] > valueH
            bullBreakStatus := 1
    if bearBreakStatus == 0
        if cls[1] < valueL
            bearBreakStatus := -1
    if notStocks
        if time1 and not time1[1]
            bullBreakStatus := 0
            bearBreakStatus := 0 
    if stocks
        if timeframe.change("D")
            bullBreakStatus := 0
            bearBreakStatus := 0
    bullBreak = bullBreakStatus == 1 and bullBreakStatus[1] == 0
    bearBreak = bearBreakStatus == -1 and bearBreakStatus[1] == 0
    [bullBreak, bearBreak]

[yorkX ,yorkH, yorkL, yorkatrH, yorkatrL] = getValue(newyorkTime, newyorkColor)
[donX, donH, donL, donatrH, donatrL] = getValue(londonTime, londonColor)
[toyX, toyH, toyL, toyatrH, toyatrL] = getValue(tokyoTime, tokyoColor)

// Line plotting for tickers other than stocks
if newyorkShow and notStocks and newyorkTime and not newyorkTime[1]
    drawLine(yorkX ,yorkH, yorkL, yorkatrH, yorkatrL, newyorkColor)

if londonShow  and notStocks and londonTime and not londonTime[1]
    drawLine(donX, donH, donL, donatrH, donatrL, londonColor)

if tokyoShow  and notStocks and tokyoTime and not tokyoTime[1]
    drawLine(toyX, toyH, toyL, toyatrH, toyatrL, tokyoColor)

// Line plotting for stock tickers 
if (newyorkShow or londonShow or tokyoShow) and stocks and timeframe.change("D")
    drawLine(yorkX ,yorkH, yorkL, yorkatrH, yorkatrL, newyorkColor)

[yorkBullBreak, yorkBearBreak] = breakAlert(yorkH, yorkL, close, newyorkTime)
[donBullBreak, donBearBreak] = breakAlert(donH, donL, close, londonTime)
[toyBullBreak, toyBearBreak] = breakAlert(toyH, toyL, close, tokyoTime)

[yorkatrBullBreak, yorkatrBearBreak] = breakAlert(yorkatrH, yorkatrL, close, newyorkTime)
[donatrBullBreak, donatrBearBreak] = breakAlert(donatrH, donatrL, close, londonTime)
[toyatrBullBreak, toyatrBearBreak] = breakAlert(toyatrH, toyatrL, close, tokyoTime)

// Shapes plotting for break-above and break-down of the candle high, low, ATR-high and ATR-low
plotshape(newyorkShow ? yorkBullBreak : na, title = "New York Candle High Break", style = shape.triangleup, location = location.belowbar, color = color.green, offset = -1, size = size.tiny)
plotshape(newyorkShow ? yorkBearBreak : na, title = "New York Candle Low Break", style = shape.triangledown, location = location.abovebar, color = color.red, offset = -1, size = size.tiny)
plotshape(newyorkShow ? yorkatrBullBreak : na, title = "New York ATR High Break", style = shape.diamond, location = location.belowbar, color = color.green, offset = -1, size = size.tiny)
plotshape(newyorkShow ? yorkatrBearBreak : na, title = "New York ATR Low Break", style = shape.diamond, location = location.abovebar, color = color.red, offset = -1, size = size.tiny)

plotshape(londonShow ? donBullBreak : na, title = "London Candle High Break", style = shape.triangleup, location = location.belowbar, color = color.green, offset = -1, size = size.tiny)
plotshape(londonShow ? donBearBreak : na, title = "London Candle Low Break", style = shape.triangledown, location = location.abovebar, color = color.red, offset = -1, size = size.tiny)
plotshape(londonShow ? donatrBullBreak : na, title = "London ATR High Break", style = shape.diamond, location = location.belowbar, color = color.green, offset = -1, size = size.tiny)
plotshape(londonShow ? donatrBearBreak : na, title = "London ATR Low Break", style = shape.diamond, location = location.abovebar, color = color.red, offset = -1, size = size.tiny)

plotshape(tokyoShow ? toyBullBreak : na, title = "Tokyo Candle High Break", style = shape.triangleup, location = location.belowbar, color = color.green, offset = -1, size = size.tiny)
plotshape(tokyoShow ? toyBearBreak : na, title = "Tokyo Candle Low Break", style = shape.triangledown, location = location.abovebar, color = color.red, offset = -1, size = size.tiny)
plotshape(tokyoShow ? toyatrBullBreak : na, title = "Tokyo ATR High Break", style = shape.diamond, location = location.belowbar, color = color.green, offset = -1, size = size.tiny)
plotshape(tokyoShow ? toyatrBearBreak : na, title = "Tokyo ATR Low Break", style = shape.diamond, location = location.abovebar, color = color.red, offset = -1, size = size.tiny)

// Background coloring for different market sessions 
bgcolor(newyorkShow ? (newyorkTime ? color.new(color.green, 90) : na) : na, title = "New York")
bgcolor(londonShow ? (londonTime ? color.new(color.blue, 90) : na) : na, title = "London")
bgcolor(tokyoShow ? (tokyoTime ? color.new(color.orange, 90) : na) : na, title = "Tokyo")

// Alerts 
alertcondition(yorkBullBreak, title = "New York Candle High Break", message = "New York Candle High Break")
alertcondition(yorkBearBreak, title = "New York Candle Low Break", message = "New York Candle Low Break")
alertcondition(yorkatrBullBreak, title = "New York ATR High Break", message = "New York ATR High Break")
alertcondition(yorkatrBearBreak, title = "New York ATR Low Break", message = "New York ATR Low Break")
alertcondition(donBullBreak, title = "London Candle High Break", message = "London Candle High Break")
alertcondition(donBearBreak, title = "London Candle Low Break", message = "London Candle Low Break")
alertcondition(donatrBullBreak, title = "London ATR High Break", message = "London ATR High Break")
alertcondition(donatrBearBreak, title = "London ATR Low Break", message = "London ATR Low Break")
alertcondition(toyBullBreak, title = "Tokyo Candle High Break", message = "Tokyo Candle High Break")
alertcondition(toyBearBreak, title = "Tokyo Candle Low Break", message = "Tokyo Candle Low Break")
alertcondition(toyatrBullBreak, title = "Tokyo ATR High Break", message = "Tokyo ATR High Break")
alertcondition(toyatrBearBreak, title = "Tokyo ATR Low Break", message = "Tokyo ATR Low Break")


