Average HL Range - SAT
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © sosso_bott

//@version=5
indicator(title="Average Range - SAT", shorttitle="Average Range - SAT", overlay=true)
import sosso_bott/SAT_LIB/28        as sat
import sosso_bott/LevelsManager/12   as lm

no_rep(_symbol, _res, _src) =>
    request.security(_symbol, _res, _src[barstate.isrealtime ? 1:0], barmerge.gaps_off, barmerge.lookahead_off)

no_rep2(_symbol, _res, _src) =>
    request.security(_symbol, _res, _src[1])

darkMode                        =  input.bool(defval=false,                           title="Dark Mode",                                                    group="== DISPLAY ==")
showMid                         =  input.bool(defval=true,                           title="Show Average",                                                  group="== DISPLAY ==")
showRange                       =  input.bool(defval=true,                           title="Show range",                                                    group="== DISPLAY ==")
colorBg                         =  input.bool(defval=true,                          title="color Background",                                              group="== DISPLAY ==")
showRangeColor                  =  input.bool(defval=false,                          title="Color range Background",                                        group="== DISPLAY ==")
colorSignalBg                   =  input.bool(defval=false,                          title="Color Signal Background",                                       group="== DISPLAY ==")

                /////// ********  AVERAGE RANGE   **********\\\\\\\\\\\\\\\\\\\\\\\\
res_mid                         =  input.timeframe(defval="",              title="timeFrame",                  group="MID")
src_mid                         =  input.source(defval=close,              title="mid source",                 group="MID")
choice                          =  input.bool(defval=true,                 title="Heikin Ashi Ticker Data",    group="MID")
ha_t_mid                        =  choice ? ticker.heikinashi(syminfo.tickerid) : syminfo.tickerid

//tf
ha_close                        = no_rep(ha_t_mid, res_mid, src_mid)
ha_high                         = no_rep(ha_t_mid, res_mid, high)
ha_low                          = no_rep(ha_t_mid, res_mid, low)


[mid,   ranje,  midColor,   mid_state,  uppish,     downish,    midUp,  midDn,  realb,  reals] = sat.rangeValue(ha_close, ha_high, ha_low, darkMode)

var bool rangeState             = na
rangeState                      := mid == mid[1] ? true : false

                /////// ******** AVERAGE RANGE END **********\\\\\\\\\\\\\\\\\\\\\\\\

                // ++++++++++++++++++++++++++++  LEVELS TRACK +++++++++++++++++++++++++ \\

reward                          = input.float(title="Reward Value",                                     defval=1,        step=0.5,  minval=0.5,      group="=== RISK:REWARD ===")
rr_tp                           = input.float(title='Percentage',                                       defval=0.05,     step=0.1,                   group="=== RISK:REWARD ===")


use_breakSignals                = input.bool(title="Use Range Break Signals",                           defval=true,                                                                                group="Signals")
buyEntrySource_o                = input.string(title = "Buy entry source",                              defval="Range Mid", options=["CLOSE", "LOW",  "HL2", "Range Mid", "MID DN"],                group="Signals")
sellEntrySource_o               = input.string(title = "Sell entry source",                             defval="Range Mid", options=["CLOSE", "HIGH", "HL2", "Range Mid", "MID UP"],                group="Signals")

use_TPs                         = input.bool(title="Use Take profit levels ?",                          defval=false,                               group="=== TAKEPROFIT LEVELS ===")
useTp1                          = input.bool(title="Use TP1 ?",                                         defval=true,                                group="=== TAKEPROFIT LEVELS ===")
useTp2                          = input.bool(title="Use TP2 ?",                                         defval=true,                                group="=== TAKEPROFIT LEVELS ===")
useTp3                          = input.bool(title="Use TP3 ?",                                         defval=true,                                group="=== TAKEPROFIT LEVELS ===")
useTp4                          = input.bool(title="Use TP4 ?",                                         defval=true,                                group="=== TAKEPROFIT LEVELS ===")
useTp5                          = input.bool(title="Use TP5 ?",                                         defval=true,                                group="=== TAKEPROFIT LEVELS ===")
useTp6                          = input.bool(title="Use TP6 ?",                                         defval=true,                                group="=== TAKEPROFIT LEVELS ===")
slr                             = input.float(title="Percentage to SL",                                 defval=0.10,     step=0.05,                 group="=== TAKEPROFIT LEVELS ===")
tp1x                            = input.float(title="Percentage to TP1",                                defval=0.10,     step=0.05,                 group="=== TAKEPROFIT LEVELS ===")
tp2x                            = input.float(title="Percentage to TP2",                                defval=0.30,     step=0.05,                 group="=== TAKEPROFIT LEVELS ===")
tp3x                            = input.float(title="Percentage to TP3",                                defval=0.45,     step=0.05,                 group="=== TAKEPROFIT LEVELS ===")
tp4x                            = input.float(title="Percentage to TP4",                                defval=0.60,     step=0.05,                 group="=== TAKEPROFIT LEVELS ===")
tp5x                            = input.float(title="Percentage to TP5",                                defval=0.80,     step=0.05,                 group="=== TAKEPROFIT LEVELS ===")
tp6x                            = input.float(title="Percentage to TP6",                                defval=1.00,     step=0.05,                 group="=== TAKEPROFIT LEVELS ===")

tp1_r                           = not use_TPs ? rr_tp * reward          : tp1x
tp2_r                           = not use_TPs ? 2 * (rr_tp * reward)    : tp2x
tp3_r                           = not use_TPs ? 4 * (rr_tp * reward)    : tp3x
tp4_r                           = not use_TPs ? 6 * (rr_tp * reward)    : tp4x
tp5_r                           = not use_TPs ? 8 * (rr_tp * reward)    : tp5x
tp6_r                           = not use_TPs ? 10 * (rr_tp * reward)    : tp6x
slx                             = not use_TPs ? rr_tp                   : slr

    // ENTRY LEVEL
sellEntrySource                 = switch sellEntrySource_o
    "CLOSE"                     => close
    "HIGH"                       => high
    "HL2"                       => hl2
    "Range Mid"                 => mid
    "MID UP"                    => midUp
    =>                             mid

buyEntrySource                  = switch sellEntrySource_o
    "CLOSE"                     => close
    "LOW"                       => low
    "HL2"                       => hl2
    "Range Mid"                 => mid
    "MID DN"                    => midDn
    =>                             mid
                // +++++++++++++++++++++++++++LEVELS TRACK END+++++++++++++++++++++++++ \\


                /////// ********    SIGNALS    ********** \\\\\\\\\\\\\\\\\\\\\\\\    
[L1_S1entry_touched, L1_S1entry, L1_S1_tp1, L1_S1_tp2, L1_S1_tp3, L1_S1_tp4, L1_S1_tp5, L1_S1_tp6, L1_S1_sl, OL1, OS1, L1_buyTP1_touched, L1_buyTP2_touched, L1_buyTP3_touched, L1_buyTP4_touched, L1_buyTP5_touched, L1_buyTP6_touched, L1_buySL_touched, S1_sellTP1_touched, S1_sellTP2_touched, S1_sellTP3_touched, S1_sellTP4_touched, S1_sellTP5_touched, S1_sellTP6_touched, S1_sellSL_touched] = lm.manageTrade(use_breakSignals, realb, reals, "L_1", "S_1",  buyEntrySource, sellEntrySource, useTp1, useTp2, useTp3, useTp4, useTp5, useTp6, tp1_r, tp2_r, tp3_r, tp4_r, tp5_r, tp6_r, slx)
                
                // ===================   PLOT  ===================================
    // @RANGE MID
plot(showRange ? uppish :na, color=color.red,           linewidth=1, style=plot.style_linebr,   title="Up")
plot(showRange ? downish:na, color=color.green,         linewidth=1, style=plot.style_linebr,   title="Down")
plot(showRange ? midUp  :na, color=color.orange,        linewidth=1, style=plot.style_linebr,   title="mid Up")
plot(showRange ? midDn  :na, color=color.blue,          linewidth=1, style=plot.style_linebr,   title="mid Down")
plot(not showMid ? na :mid,  color=midColor,              linewidth=3,                            title="mid")

//--SIGNAL PLOT ---\\
plotshape(OL1,  style=shape.triangleup,    color=color.new(color.green, 50),    location = location.belowbar,   text="L", size=size.normal)
plotshape(OS1,  style=shape.triangledown,  color=color.new(color.red, 50),      location = location.abovebar,   text="S", size=size.normal)
bgcolor(not colorSignalBg ? na : OL1 ?   color.new(color.green, 70)  :   OS1 ?   color.new(color.red, 70)    :   na, title="Signals Background")
bgcolor(not colorBg ? na:  mid_state == 1 ? color.new(color.green, 95)    :   mid_state == -1 ? color.new(color.red, 95) : na, title="zones Background")
              // ================ PLOT  END ====================================