Dynamic GANN Square Of 9 Bands
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Arun_K_Bhaskar

//@version=5
indicator(title='Dynamic GANN Square Of 9 Bands', shorttitle='Dynamic GANN', overlay=true, format=format.price)

////////////////////////////////////////////////////////////// Quick Menu

gpQ = 'Quick Menu'
// Increment
// 0 / 360 = 0
// 45 / 360 = 0.125
// 90 / 360 = 0.25
// 135 / 360 = 0.375
// 180 / 360 = 0.5
// 225 / 360 = 0.625
// 270 / 360 = 0.75
// 315 / 360 = 0.875
// 360 / 360 = 1

ttI = 'Increment Values for Various Markets:\n\n' +

     'For Intraday:\n\n' +

     'India NSE BSE: 0.125 / 0.0625 / 0.03125\n' +
     'NIFTY: 0.375, 0.25 / 0.125 / 0.0625\n' +
     'BANKNIFTY: 0.375 / 0.25 / 0.125\n' +
     'FINNIFTY: 0.25 / 0.125\n' +
     'MIDCPNIFTY: 0.25 / 0.125\n\n' +

     'USDINR: 0.0078125 / 0.00390625\n' +
     'EURUSD: 0.001953125 / 0.0009765625\n\n' +

     'BTCUSD: 0.125 / 0.0625\n' +
     'ETHUSD: 0.03125 / 0.015625\n\n' +

     'Note: The increment values are subjected to change over time & volatility'

i_increment = input.string(defval='0.125', title='Increment ' , options=['0.0009765625',
                                                                             '0.001953125',
                                                                             '0.00390625',
                                                                             '0.0078125',
                                                                             '0.015625',
                                                                             '0.03125',
                                                                             '0.0625',
                                                                             '0.125',
                                                                             '0.25',
                                                                             '0.375',
                                                                             '0.5',
                                                                             '0.625',
                                                                             '0.75',
                                                                             '0.875',
                                                                             '1'], tooltip=ttI, inline='01', group=gpQ)

increment = i_increment == '0.0009765625' ? 0.0009765625 :
             i_increment == '0.001953125' ? 0.001953125 :
             i_increment == '0.00390625' ? 0.00390625 :
             i_increment == '0.0078125' ? 0.0078125 :
             i_increment == '0.015625' ? 0.015625 :
             i_increment == '0.03125' ? 0.03125 :
             i_increment == '0.0625' ? 0.0625 :
             i_increment == '0.125' ? 0.125 :
             i_increment == '0.25' ? 0.25 :
             i_increment == '0.375' ? 0.375 :
             i_increment == '0.5' ? 0.5 :
             i_increment == '0.625' ? 0.625 :
             i_increment == '0.75' ? 0.75 :
             i_increment == '0.875' ? 0.875 : 1

////////////////////////////////////////////////////////////// Moving Average Menu

gpMA = 'Moving Average'
i_ma_type = input.string(defval="EMA", title='',
     options=["█ EMA Based:", "EMA", "DEMA", "TEMA", "ETMA", "ZLEMA",
     "█ SMA Based:", "SMA", "TMA", "SMMA (RMA)",
     "█ Volume Based:", "VWMA", "MVWAP",
     "█ Others:", "ALMA", "HMA", "LSMA", "WMA"], inline='1', group=gpMA)
i_ma_length = input.int(defval=200, title="", inline='1', group=gpMA)
i_ma_source = input.source(defval=close, title="", inline='1', group=gpMA)
i_ma_timeframe = input.timeframe(defval='', title='', inline='1', group=gpMA)
i_ma_color = input.color(defval=#00FF00, title="", inline='2', group=gpMA)
i_offset = input.int(defval=0, minval=0, title='Offset', inline='2', group=gpMA)

////////////////////////////////////////////////////////////// Support & Resistance Menu

gpG = "Support & Resistance"
i_show_r_01 = input.bool(defval=true, title="R01", group=gpG, inline="R1")
i_show_r_02 = input.bool(defval=true, title="R02", group=gpG, inline="R1")
i_show_r_03 = input.bool(defval=true, title="R03", group=gpG, inline="R1")
i_show_r_04 = input.bool(defval=true, title="R04", group=gpG, inline="R1")
i_show_r_05 = input.bool(defval=true, title="R05", group=gpG, inline="R1")
i_r_05_color = input.color(defval=#FF0000, title="", group=gpG, inline="R1")
i_show_r_06 = input.bool(defval=false, title="R06", group=gpG, inline="R2")
i_show_r_07 = input.bool(defval=false, title="R07", group=gpG, inline="R2")
i_show_r_08 = input.bool(defval=false, title="R08", group=gpG, inline="R2")
i_show_r_09 = input.bool(defval=false, title="R09", group=gpG, inline="R2")
i_show_r_10 = input.bool(defval=false, title="R10", group=gpG, inline="R2")
i_r_10_color = input.color(defval=#FF0000, title="", group=gpG, inline="R2")
i_r_color = input.color(defval=#505050, title="", group=gpG, inline="R2")

i_show_s_01 = input.bool(defval=true, title="S01", group=gpG, inline="S1")
i_show_s_02 = input.bool(defval=true, title="S02", group=gpG, inline="S1")
i_show_s_03 = input.bool(defval=true, title="S03", group=gpG, inline="S1")
i_show_s_04 = input.bool(defval=true, title="S04", group=gpG, inline="S1")
i_show_s_05 = input.bool(defval=true, title="S05", group=gpG, inline="S1")
i_s_05_color = input.color(defval=#FF0000, title="", group=gpG, inline="S1")
i_show_s_06 = input.bool(defval=false, title="S06", group=gpG, inline="S2")
i_show_s_07 = input.bool(defval=false, title="S07", group=gpG, inline="S2")
i_show_s_08 = input.bool(defval=false, title="S08", group=gpG, inline="S2")
i_show_s_09 = input.bool(defval=false, title="S09", group=gpG, inline="S2")
i_show_s_10 = input.bool(defval=false, title="S10", group=gpG, inline="S2")
i_s_10_color = input.color(defval=#FF0000, title="", group=gpG, inline="S2")
i_s_color = input.color(defval=#505050, title="", group=gpG, inline="S2")

////////////////////////////////////////////////////////////// Get OHLCV

get_ohlcv(timeframe, source, prev_value) => 
  request.security(symbol=syminfo.tickerid, timeframe=timeframe, expression=source[prev_value], gaps=barmerge.gaps_off, lookahead=barmerge.lookahead_on)

////////////////////////////////////////////////////////////// Moving Average Calculation

moving_average(source, length, ma_type, timeframe, prev_value) =>
    float result = na
    float source_ohlcv = get_ohlcv(timeframe, source, prev_value)
    //Exponential Moving Average
    if ma_type == "EMA"
        result := ta.ema(source_ohlcv, length)
     //Double Exponential Moving Average
    if ma_type == "DEMA"
        result := 2 * (ta.ema(source_ohlcv, length)) - (ta.ema((ta.ema(source_ohlcv, length)), length))
        result
     //Triple Exponential Moving Average
    if ma_type == "TEMA"
        result := 3 * ((ta.ema(source_ohlcv, length)) - (ta.ema((ta.ema(source_ohlcv, length)), length))) + ta.ema((ta.ema((ta.ema(source_ohlcv, length)), length)), length)
        result
     //Zero Lag Exponential Moving Average
    if ma_type == "ZLEMA"
        result := ta.ema(source_ohlcv + source_ohlcv - source_ohlcv[(length - 1) / 2], length)
        result

     //Simple Moving Average
    if ma_type == "SMA" 
        result := ta.sma(source_ohlcv, length)
        result
     //Triangular Simple Moving Average
    if ma_type == "TMA"
        result := ta.sma(ta.sma(source_ohlcv, length), length)
        result
     //Smoothed Moving Average (SMMA)
    if ma_type == "SMMA (RMA)"
        result := ta.rma(source_ohlcv, length)
        result
     //Weighted Moving Average
    if ma_type == "WMA"
        result := ta.wma(source_ohlcv, length)
        result
     //Volume Weighted Moving Average
    if ma_type == "VWMA" 
        result := ta.vwma(source_ohlcv, length)
        result
     // EMA VWAP
    if ma_type == "MVWAP" 
        result := ta.ema(ta.vwap, length)
        result

     //Hull Moving Average
    if ma_type == "HMA"
        result := ta.hma(source_ohlcv, length)
        result
     //Arnaud Legoux Moving Average
    if ma_type == "ALMA"
        result := ta.alma(source_ohlcv, length, 0.85, 6)
        result
     //Triangular Exponential Moving Average
    if ma_type == "ETMA"
        result := ta.ema(ta.ema(source_ohlcv, length), length)
        result
     //Least Squares Moving Average
    if ma_type == "LSMA"
        result := ta.linreg(source_ohlcv, length, i_offset)
        result
    result

// Get Moving Average
ma = moving_average(i_ma_source, i_ma_length, i_ma_type, i_ma_timeframe, 0)

////////////////////////////////////////////////////////////// Dynamic Gann Square of 9 Calculation

gann_square_of_9(price, multiplier) =>
    upper = math.pow((math.sqrt(price) + (multiplier * increment)), 2)
    lower = math.pow((math.sqrt(price) - (multiplier * increment)), 2)
    [upper, lower]

[r_01, s_01] = gann_square_of_9(ma, 1)
[r_02, s_02] = gann_square_of_9(ma, 2)
[r_03, s_03] = gann_square_of_9(ma, 3)
[r_04, s_04] = gann_square_of_9(ma, 4)
[r_05, s_05] = gann_square_of_9(ma, 5)
[r_06, s_06] = gann_square_of_9(ma, 6)
[r_07, s_07] = gann_square_of_9(ma, 7)
[r_08, s_08] = gann_square_of_9(ma, 8)
[r_09, s_09] = gann_square_of_9(ma, 9)
[r_10, s_10] = gann_square_of_9(ma, 10)

////////////////////////////////////////////////////////////// Plot

plot(series= ma, title='Moving Average', color=i_ma_color, offset=i_offset)

plot(series=i_show_r_01 ? r_01 : na, title='R01', color=i_r_color, offset=i_offset)
plot(series=i_show_r_02 ? r_02 : na, title='R02', color=i_r_color, offset=i_offset)
plot(series=i_show_r_03 ? r_03 : na, title='R03', color=i_r_color, offset=i_offset)
plot(series=i_show_r_04 ? r_04 : na, title='R04', color=i_r_color, offset=i_offset)
plot(series=i_show_r_05 ? r_05 : na, title='R05', color=i_r_05_color, offset=i_offset)
plot(series=i_show_r_06 ? r_06 : na, title='R06', color=i_r_color, offset=i_offset)
plot(series=i_show_r_07 ? r_07 : na, title='R07', color=i_r_color, offset=i_offset)
plot(series=i_show_r_08 ? r_08 : na, title='R08', color=i_r_color, offset=i_offset)
plot(series=i_show_r_09 ? r_09 : na, title='R09', color=i_r_color, offset=i_offset)
plot(series=i_show_r_10 ? r_10 : na, title='R10', color=i_r_10_color, offset=i_offset)

plot(series=i_show_s_01 ? s_01 : na, title='S01', color=i_s_color, offset=i_offset)
plot(series=i_show_s_02 ? s_02 : na, title='S02', color=i_s_color, offset=i_offset)
plot(series=i_show_s_03 ? s_03 : na, title='S03', color=i_s_color, offset=i_offset)
plot(series=i_show_s_04 ? s_04 : na, title='S04', color=i_s_color, offset=i_offset)
plot(series=i_show_s_05 ? s_05 : na, title='S05', color=i_s_05_color, offset=i_offset)
plot(series=i_show_s_06 ? s_06 : na, title='S06', color=i_s_color, offset=i_offset)
plot(series=i_show_s_07 ? s_07 : na, title='S07', color=i_s_color, offset=i_offset)
plot(series=i_show_s_08 ? s_08 : na, title='S08', color=i_s_color, offset=i_offset)
plot(series=i_show_s_09 ? s_09 : na, title='S09', color=i_s_color, offset=i_offset)
plot(series=i_show_s_10 ? s_10 : na, title='S10', color=i_s_10_color, offset=i_offset)

//////////////////////////////////////////////////////////// CODE END