Trading Zones based on RS / Volume / Pullback
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © InvestIn10

//@version=5
indicator("Trading Zones based on RS / Volume / Pullback", shorttitle = "Trading Zones from: RS / Volume / Pullback", overlay = true)

// Default color constants using tranparency of 25.
GREY_COLOR   = #7d7e7f2f
GREEN_COLOR   = #30663177
ORANGE_COLOR = color.rgb(255, 128, 0, 80)
RED_COLOR   = #e30f0f52
NO_COLOR     = color(na)

var SessionColor = NO_COLOR


// Relative Symbol
Rel_sym = input.symbol("NSE:NIFTY", "Relative Symbol")

// Flags
var LongActive = false
var EntryZone = false
var WarningZone = false
var ProbableTopZone = false


Relative = request.security(Rel_sym, timeframe.period, close)
Sym = request.security(syminfo.tickerid, timeframe.period, close)

HYr_period = 125
Qtr_period = 63
Month_period = 21
Week_period = 5

// Volume Avg
var float Volume_Avg_Qtr = 0
var float Volume_Avg_Month = 0
var float Volume_Avg_Week = 0

Volume_Avg_Week := 0
for i = 0 to Week_period by 1
    Volume_Avg_Week := Volume_Avg_Week + volume[i]
Volume_Avg_Week := Volume_Avg_Week / Week_period

Volume_Avg_Month := 0
for i = 0 to Month_period by 1
    Volume_Avg_Month := Volume_Avg_Month + volume[i]
Volume_Avg_Month := Volume_Avg_Month / Month_period

Volume_Avg_Qtr := 0
for i = 0 to Qtr_period by 1
    Volume_Avg_Qtr := Volume_Avg_Qtr + volume[i]
Volume_Avg_Qtr := Volume_Avg_Qtr / Qtr_period

// -- Retracements

HYr_high = ta.highest(Sym,HYr_period)
HYr_low = ta.lowest(Sym,HYr_period)
HYr_Swing = (HYr_high - HYr_low)/HYr_low
HYr_Retrace = (HYr_high - Sym)/(HYr_high - HYr_low)

Qtr_high = ta.highest(Sym,Qtr_period)
Qtr_low = ta.lowest(Sym,Qtr_period)
Qtr_Swing = (Qtr_high - Qtr_low)/Qtr_low
Qtr_Retrace = (Qtr_high - Sym)/(Qtr_high - Qtr_low)

Month_high = ta.highest(Sym,Month_period)
Month_low = ta.lowest(Sym,Month_period)
Month_Swing = (Month_high - Month_low)/Month_low
Month_Retrace = (Month_high - Sym)/(Month_high - Month_low)

Combi_Retrace = HYr_Retrace+Qtr_Retrace+Month_Retrace

//-- RS

RS_HYr = (Sym / Sym[HYr_period]) / (Relative / Relative[HYr_period]) - 1
RS_Qtr = (Sym / Sym[Qtr_period]) / (Relative / Relative[Qtr_period]) - 1
RS_Month = (Sym / Sym[Month_period]) / (Relative / Relative[Month_period]) - 1

RS_ma = 5
RS_HYr_ma = ta.ema(RS_HYr,RS_ma)
RS_Qtr_ma = ta.ema(RS_Qtr,RS_ma)
RS_Month_ma = ta.ema(RS_Month,RS_ma)

//---------- Generate Signals: State Machine: for Different Zones


if LongActive == false and Combi_Retrace < 0.1 and RS_Qtr_ma > 0 and RS_HYr_ma[0] > RS_HYr_ma[5]
    LongActive := true

if LongActive == true 

    // entry zone
    if Combi_Retrace > 0.75 and RS_Qtr_ma > 0 and RS_Qtr_ma[0] > RS_Qtr_ma[5]
        if Volume_Avg_Week < 0.9*Volume_Avg_Month or Volume_Avg_Month < 0.85*Volume_Avg_Qtr
            EntryZone := true
        else
            EntryZone := false
    else
        EntryZone := false

    // warning zone
    if Combi_Retrace > 2.2 and RS_Qtr_ma < 0
        WarningZone := true
        EntryZone := false

    if WarningZone == true and Combi_Retrace < 0.1 and RS_Qtr_ma > 0
        WarningZone := false
    
    // search probable tops
    if WarningZone == true and Combi_Retrace < 1.0
        ProbableTopZone := true

    if ProbableTopZone == true
        if Combi_Retrace < 1.2 and Combi_Retrace >= 0.1
            ProbableTopZone := true
        else if Combi_Retrace < 0.1 and RS_Qtr_ma > 0
            ProbableTopZone := false
            LongActive := true
        else if Combi_Retrace > 1 or RS_Qtr_ma < 0
            ProbableTopZone := false
            LongActive := false

// ------------- END State Machine


if LongActive == true    
    SessionColor := GREY_COLOR
    if EntryZone == true
        SessionColor := GREEN_COLOR
    if WarningZone == true
        SessionColor := ORANGE_COLOR
    if ProbableTopZone == true
        SessionColor := RED_COLOR
else
    SessionColor := NO_COLOR




// DEBUG Plots -- Dont delete -- will be needed for future debug
// hline(0,linewidth = 2)
// hline(1,linewidth = 1)
// hline(0.23, color = color.green)
// hline(0.50, color = color.red)
// plot(HYr_Retrace,color = color.lime)
// plot(Qtr_Retrace, color = color.rgb(230, 128, 11))
// plot(Month_Retrace, color = color.red)
// plot(Combi_Retrace, color =color.gray, linewidth = 2)
// hline(2)
// plot(2+RS_Qtr_ma, color = color.rgb(230, 128, 11),linewidth = 2)
// plot(2+RS_Month_ma, color = color.rgb(230, 33, 11, 3),linewidth = 2)


bgcolor(SessionColor)

//Table
var table Index_color = table.new(position.top_right,1,4,bgcolor = GREY_COLOR)
if barstate.islast
    table.cell(Index_color, 0, 0, "LONG Zone",bgcolor = GREY_COLOR, text_color = color.white)
    table.cell(Index_color, 0, 1, "ENTRY Zone",bgcolor = GREEN_COLOR, text_color = color.white)
    table.cell(Index_color, 0, 2, "WARNING Zone",bgcolor = ORANGE_COLOR, text_color = color.white)
    table.cell(Index_color, 0, 3, "EXIT Zone",bgcolor = RED_COLOR, text_color = color.white)
