Chimpanzee V2.5 part A by joylay83
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
//@version=5

// Chimpanzee V2.9 © joylay83 Part A: overlay scale RIGHT, Part B: overlay scale LEFT or overlay=FALSE
// The aim of this code is to generate signals and hook it to 3commas

// Credits : Thank you for doing an awesome job.  I learnt a lot from your codes and tutorials. 
// Credits not listed in any order. If your code is used here and did not receive due credit, kindly drop me a note. tq.
// Blessing 3 by JTA Today, ZenAndTheArtOfTrading, Galactus-B, TheTradingParrot, Zendog123, ydeniz2000
// TradingView built-in scripts

// ChangeLog
// 03/01/21 Added Timezone input.int 
// 20/01/21 V2.6 BB now Upper and Lower band Std Deviation are separately configurable. BB can be enabled/disabled. added smoothing. Fixed bug indicators lag 1 bar behind.
// 20/01/21 V2.7 TEMA and IFTRSI now can be enabled/disabled. Code clean up. High Timeframe (HTF) indys are now labeled with '2' instead of 'HTF'
// 06/02/21 V2.8 plotting mode. plotting mode can be main (overlay) or companion (overlay=false or scale.left).
// 06/02/21 V2.9 HTF or '2' Indis now can be enabled/disabled as a whole, or individually. BB now can have a different upper and lower standard deviation (adapted from Nick Radge's strategy) 
// 06/02/21 V2.9 Stil having issues with smoothing. suggest to leave it disabled for now.



indicator('Chimpanzee V2.9 by joylay83', shorttitle='Chimp2.9', precision=4, overlay=true)

var gGlobal = "Global settings"
source      = input(close, title="Source", group=gGlobal)

var gMain   = "Main (Overlay)"
var gComp   = "Companion (Non-Overlay)"
PlotMode    = input.string(title='Plot Mode', options=[gMain, gComp], defval=gMain, group=gGlobal)

EnableHTF   = input.bool(true, title="Enable HTF", group=gGlobal)
Smoothing   = input.bool(false, title="Smoothing", group=gGlobal)

f_secureSecurity(sym, tf, src) =>
    request.security(sym, tf, src[barstate.isconfirmed ? 0 : 1], barmerge.gaps_off, barmerge.lookahead_off)

f_smoothSecurity(sym, tf, src) =>
    request.security(sym, tf, src[barstate.isconfirmed ? 0 : 1], barmerge.gaps_on, barmerge.lookahead_off)
    
//%B
//%B User Input
var gBB      = "BB / %B"
EnableBB    = input.bool(true, title="Enable BB / %B", group=gBB)
BBtf        = input.timeframe(title="%B Timeframe", defval="240", group=gBB)
BBlength    = input.int(20, minval=1, title="BB Length", group=gBB)
BBMA        = input.string(defval='SMA', options=['SMA', 'EMA'], title="%B MA Type", group=gBB)
Uppermult   = input.float(2.0, minval=0.001, maxval=50, title="BB Upper StdDev", group=gBB)
Lowermult   = input.float(2.0, minval=0.001, maxval=50, title="BB Lower StdDev", group=gBB)

basis       = BBMA=='EMA' ? ta.ema(source, BBlength) : BBMA=='SMA' ? ta.sma(source, BBlength) : na
Upperdev    = Uppermult * ta.stdev(source, BBlength)
Lowerdev    = Lowermult * ta.stdev(source, BBlength)

BBbasis     = EnableBB ? (Smoothing ? f_smoothSecurity(syminfo.tickerid, BBtf, basis) : f_secureSecurity(syminfo.tickerid, BBtf, basis)) : na
BBUpperdev  = f_secureSecurity(syminfo.tickerid, BBtf, Upperdev)
BBLowerdev  = f_secureSecurity(syminfo.tickerid, BBtf, Lowerdev)

UpperBand   = BBbasis + BBUpperdev
LowerBand   = BBbasis - BBLowerdev
bbr         = (source - LowerBand)/(UpperBand - LowerBand)
BBob        = input.float(1, "BB Overbought", group=gBB)
BBos        = input.float(0, "BB Oversold", group=gBB)
BBOverbought= bbr >= BBob
BBOversold  = bbr <= BBos


//**************************************************************************************************************************************************************

//%B 2
//%B 2 User Input
var gBB2      = "BB / %B 2"
EnableBB2    = input.bool(true, title="Enable BB / %B 2", group=gBB2)
BBtf2        = input.timeframe(title="%B 2 Timeframe", defval="D", group=gBB2)
BBlength2    = input.int(20, minval=1, title="BB 2 Length", group=gBB2)
BBMA2        = input.string(defval='SMA', options=['SMA', 'EMA'], title="%B 2 MA Type", group=gBB2)
Uppermult2   = input.float(2.0, minval=0.001, maxval=50, title="BB 2 Upper StdDev", group=gBB2)
Lowermult2   = input.float(2.0, minval=0.001, maxval=50, title="BB 2 Lower StdDev", group=gBB2)

basis2       = BBMA2=='EMA' ? ta.ema(source, BBlength2) : BBMA2=='SMA' ? ta.sma(source, BBlength2) : na
Upperdev2    = Uppermult2 * ta.stdev(source, BBlength2)
Lowerdev2    = Lowermult2 * ta.stdev(source, BBlength2)

BBbasis2     = EnableHTF ==true and EnableBB2 ==true ? (Smoothing ? f_smoothSecurity(syminfo.tickerid, BBtf2, basis2) : f_secureSecurity(syminfo.tickerid, BBtf2, basis2)) : na
BBUpperdev2  = f_secureSecurity(syminfo.tickerid, BBtf2, Upperdev2)
BBLowerdev2  = f_secureSecurity(syminfo.tickerid, BBtf2, Lowerdev2)

UpperBand2   = BBbasis2 + BBUpperdev2
LowerBand2   = BBbasis2 - BBLowerdev2
bbr2         = (source - LowerBand2)/(UpperBand2 - LowerBand2)
BBob2        = input.float(1, "BB 2 Overbought", group=gBB2)
BBos2        = input.float(0, "BB 2 Oversold", group=gBB2)
BBOverbought2= bbr2 >= BBob2
BBOversold2  = bbr2 <= BBos2


//**************************************************************************************************************************************************************


//TEMA ORI
var gTEMA      = "TEMA"
EnableTEMA      = input.bool(false, title="Enable TEMA", group=gTEMA)
TEMAtf          = input.timeframe(title="TEMA Timeframe", defval="", group=gTEMA)
TEMAlength      = input.int(21, minval=1, title="TEMA Length", group=gTEMA)
//ema1 = ta.ema(source, TEMAlength)
//ema2 = ta.ema(ema1, TEMAlength)
//ema3 = ta.ema(ema2, TEMAlength)
//out = 3 * (ema1 - ema2) + ema3

//TEMA MTF 
TEMAema1     = EnableTEMA ? (Smoothing ? f_smoothSecurity(syminfo.tickerid, TEMAtf, ta.ema(source, TEMAlength)) : f_secureSecurity(syminfo.tickerid, TEMAtf, ta.ema(source, TEMAlength))) : na
TEMAema2     = f_secureSecurity(syminfo.tickerid, TEMAtf, ta.ema(TEMAema1,TEMAlength))
TEMAema3     = f_secureSecurity(syminfo.tickerid, TEMAtf, ta.ema(TEMAema2, TEMAlength))
TEMAout      = 3 * (TEMAema1 - TEMAema2) + TEMAema3

TEMAup       = ta.crossover(TEMAout,UpperBand)
TEMAup_End   = ta.crossunder(TEMAout,UpperBand)
TEMAdown     = ta.crossunder(TEMAout,LowerBand)
TEMAdown_End = ta.crossover(TEMAout,LowerBand)


//**************************************************************************************************************************************************************


//TEMA MTF 2
var gTEMA2      = "TEMA 2"
EnableTEMA2     = input.bool(true, title="Enable TEMA 2", group=gTEMA2)
TEMAtf2         = input.timeframe(title="TEMA 2 Timeframe", defval="D", group=gTEMA2)
TEMAlength2     = input.int(21, minval=1, title="TEMA 2 Length", group=gTEMA2)

TEMAema1_2      = EnableHTF ==true and EnableTEMA2 ==true ? (Smoothing ? f_smoothSecurity(syminfo.tickerid, TEMAtf2, ta.ema(source, TEMAlength2)) : f_secureSecurity(syminfo.tickerid, TEMAtf2, ta.ema(source, TEMAlength2))) : na
TEMAema2_2      = f_secureSecurity(syminfo.tickerid, TEMAtf2, ta.ema(TEMAema1_2,TEMAlength2))
TEMAema3_2      = f_secureSecurity(syminfo.tickerid, TEMAtf2, ta.ema(TEMAema2_2, TEMAlength2))
TEMAout_2       = 3 * (TEMAema1_2 - TEMAema2_2) + TEMAema3_2

TEMAup_2        = ta.crossover(TEMAout_2,UpperBand2)
TEMAup_End_2    = ta.crossunder(TEMAout_2,UpperBand2)
TEMAdown_2      = ta.crossunder(TEMAout_2,LowerBand2)
TEMAdown_End_2  = ta.crossover(TEMAout_2,LowerBand2)


//**************************************************************************************************************************************************************


//IFTRSI
var gIFTRSI = "IFTRSI"
EnableIFish = input.bool(true, group=gIFTRSI, title="Enable IFTRSI")
length      = input(5, 'RSI length', group=gIFTRSI)
lengthwma   = input(9, title='Smoothing length', group=gIFTRSI)
TH_Buy      = input(-0.9, title='IFTRSI Buy Threshold', group=gIFTRSI)
TH_Sell     = input(0.996, title='IFTRSI Sell Threshold', group=gIFTRSI)

calc_ifish(series, lengthwma) =>
    v1      = 0.1 * (series - 50)
    v2      = ta.wma(v1, lengthwma)
    ifish   = (math.exp(2 * v2) - 1) / (math.exp(2 * v2) + 1)
    ifish

//IFTRSI Determine colour
IFTRSI      = EnableIFish ? calc_ifish(ta.rsi(source, length), lengthwma): na
lineColour  = IFTRSI <= TH_Buy ? color.green : IFTRSI >= TH_Sell ? color.blue : color.orange

//IFTRSI Signal
IFTRSI_Signal_Mode  = input.string(title='Signal Mode', options=['Threshold', 'Cross'], defval='Cross', group=gIFTRSI)

IFTRSI_Crossover    = ta.crossover(IFTRSI, TH_Buy)
IFTRSI_Crossunder   = ta.crossunder(IFTRSI, TH_Sell)

IFTRSI_signal       = if IFTRSI_Signal_Mode == 'Threshold'
    IFTRSI < TH_Buy ? -1 : IFTRSI > TH_Sell ? 1 : 0
else if IFTRSI_Signal_Mode == 'Cross'
    IFTRSI_Crossover ? -1 : IFTRSI_Crossunder ? 1 : 0
else
    na


//**************************************************************************************************************************************************************


//IFTRSI 2
var gIFTRSI2 = "IFTRSI 2"
EnableIFish2 = input.bool(true, group=gIFTRSI2, title="Enable IFTRSI 2")
IH_tf        = input.timeframe(title="IFTRSI 2 Timeframe", defval="240", group=gIFTRSI2)
IH_length    = input(5, 'IFTRSI 2 length', group=gIFTRSI2)
IH_lengthwma = input(9, title='IFTRSI 2 Smoothing length', group=gIFTRSI2)
IH_TH_Buy    = input(-0.9, title='IFTRSI 2 Buy Threshold', group=gIFTRSI2)
IH_TH_Sell   = input(0.996, title='IFTRSI 2 Sell Threshold', group=gIFTRSI2)

IH_calc_ifish(series, IH_lengthwma) =>
    IH_v1    = 0.1 * (series - 50)
    IH_v2    = ta.wma(IH_v1, IH_lengthwma)
    IH_ifish = (math.exp(2 * IH_v2) - 1) / (math.exp(2 * IH_v2) + 1)
    IH_ifish

// IFTRSI HTF Determine colour
IFTRSI2         = EnableHTF ==true and EnableIFish2 ==true ? IH_calc_ifish(ta.rsi(source, length), IH_lengthwma) : na
IH_IFTRSI       = f_secureSecurity(syminfo.tickerid, IH_tf, IFTRSI2)

IH_lineColour   = IH_IFTRSI <= IH_TH_Buy ? color.new(color.green,0) : IH_IFTRSI >= IH_TH_Sell ? color.new(color.blue,0) : color.new(color.orange,0)

//IFTRSI HTF Signal
IH_IFTRSI_Signal_Mode   = input.string(title='IFTRSI 2 Signal Mode', options=['Threshold', 'Cross'], defval='Cross', group=gIFTRSI2)

IH_IFTRSI_Crossover     = ta.crossover(IH_IFTRSI, IH_TH_Buy)
IH_IFTRSI_Crossunder    = ta.crossunder(IH_IFTRSI, IH_TH_Sell)

IH_IFTRSI_signal        = if IH_IFTRSI_Signal_Mode == 'Threshold'
    IH_IFTRSI < IH_TH_Buy ? -1 : IH_IFTRSI > IH_TH_Sell ? 1 : 0
else if IH_IFTRSI_Signal_Mode == 'Cross'
    IH_IFTRSI_Crossover ? -1 : IH_IFTRSI_Crossunder ? 1 : 0
else
    na


//**************************************************************************************************************************************************************


//StochRSI
var gStochRSI = "StochRSI"
EnableStochRSI  = input.bool(true, title="Enable StochRSI", group=gStochRSI)
SRtf            = input.timeframe(title="StochRSI Timeframe", defval="D", group=gStochRSI)
smoothK         = input.int(3, "K", minval=1, group=gStochRSI)
smoothD         = input.int(3, "D", minval=1, group=gStochRSI)
lengthRSI       = input.int(14, "RSI Length", minval=1, group=gStochRSI)
lengthStoch     = input.int(14, "Stochastic Length", minval=1, group=gStochRSI)
rsi1            = ta.rsi(source, lengthRSI)
Srupper         = input.int(80, "StochRSI OverBought", minval=1, group=gStochRSI)
Srlower         = input.int(20, "StochRSI OverSold", minval=1, group=gStochRSI)
k               = ta.sma(ta.stoch(rsi1, rsi1, rsi1, lengthStoch), smoothK)
d               = ta.sma(k, smoothD)
SRk             = EnableStochRSI ? (Smoothing ? f_smoothSecurity(syminfo.tickerid, SRtf, k) : f_secureSecurity(syminfo.tickerid, SRtf, k)) : na
SRd             = EnableStochRSI ? (Smoothing ? f_smoothSecurity(syminfo.tickerid, SRtf, d) : f_secureSecurity(syminfo.tickerid, SRtf, d)) : na

Fast_K_OS       = ta.crossunder(SRk, Srlower)
Fast_K_OB       = ta.crossover(SRk, Srupper)
Slow_D_OS       = ta.crossunder(SRd, Srlower)
Slow_D_OB       = ta.crossover(SRd, Srupper)
KD_OS           = ta.crossover(SRk, SRd) and SRd <= Srlower
KD_OB           = ta.crossunder(SRk, SRd) and SRd >= Srupper



//**************************************************************************************************************************************************************


//BG COLOR - DONE
var gBGcolor    = "BG color"
EnableBGcolor   = input.bool(true, title="Enable BG color", group=gBGcolor)
BGcolorOption   = input.string(title='BG Color Option', options=['BB OB/OS', 'StochRSI OB/OS'], defval='StochRSI OB/OS', group=gBGcolor)

BBObOs          = BBOverbought ? color.new(color.red,50) : BBOversold ? color.new(color.green,50) : na
StochRSIObOs    = SRk >= Srupper ? color.new(color.red,50) : SRk <= Srlower ? color.new(color.green,50) : na

bgcolor(EnableBGcolor == true and BGcolorOption == "BB OB/OS" ? BBObOs : EnableBGcolor == true and BGcolorOption == "StochRSI OB/OS" ? StochRSIObOs : na)


//**************************************************************************************************************************************************************


//Buy/Sell Signal
BuySignal = BBOversold and IFTRSI_signal == -1
SellSignal = BBOverbought and IFTRSI_signal == 1

//HTF Buy/Sell Signal
BuySignalHTF = BBOversold2 and IH_IFTRSI_signal == -1
SellSignalHTF = BBOverbought2 and IH_IFTRSI_signal == 1


//**************************************************************************************************************************************************************


//PLOTTING OVERLAY

//PLOT BB - OVERLAY - DONE
BBbasis_gMain = PlotMode == gMain? BBbasis : na
UpperBand_gMain = PlotMode == gMain? UpperBand : na
LowerBand_gMain = PlotMode == gMain? LowerBand : na

plot(BBbasis_gMain, "BB SMA", color=color.new(color.blue, 0))
plot(UpperBand_gMain, "BB upper", color=color.new(color.blue, 0))
plot(LowerBand_gMain, "BB lower", color=color.new(color.blue, 0))


//PLOT BB 2 - OVERLAY - DONE
BBbasis2_gMain = PlotMode == gMain? BBbasis2 : na
UpperBand2_gMain = PlotMode == gMain? UpperBand2 : na
LowerBand2_gMain = PlotMode == gMain? LowerBand2 : na

plot(BBbasis2_gMain, "BB 2 MA", color.new(color.purple,60), linewidth=2)
plot(UpperBand2_gMain, "BB 2 Upper Band", color.new(color.purple,60), linewidth=2)
plot(LowerBand2_gMain, "BB 2 Lower Band", color.new(color.purple,60), linewidth=2)


//PLOT TEMA - OVERLAY
TEMAout_gMain = PlotMode == gMain ? TEMAout : na

TEMAup_gMain = PlotMode == gMain ? TEMAup : na
TEMAdown_gMain = PlotMode == gMain ? TEMAdown : na
TEMAup_End_gMain = PlotMode == gMain ? TEMAup_End : na
TEMAdown_End_gMain = PlotMode == gMain ? TEMAdown_End : na

plot(TEMAout_gMain, color=color.navy, title="TEMA")

plotshape(TEMAup_gMain, 'TEMA Up', style=shape.flag, location=location.abovebar, color=color.green, size=size.tiny)
plotshape(TEMAdown_gMain, 'TEMA Down', style=shape.flag, location=location.belowbar, color=color.red, size=size.tiny)
plotshape(TEMAup_End_gMain, 'TEMA Up End', style=shape.xcross, location=location.abovebar, color=color.green, size=size.tiny)
plotshape(TEMAdown_End_gMain, 'TEMA Down End', style=shape.xcross, location=location.belowbar, color=color.red, size=size.tiny)


//PLOT TEMA 2 - OVERLAY
bullflagcolor=color.new(color.green, 70)
bearflagcolor=color.new(color.red, 70)
TEMAout_2_gMain = PlotMode == gMain ? TEMAout_2 : na

TEMAup_2_gMain = PlotMode == gMain ? TEMAup_2 : na
TEMAdown_2_gMain = PlotMode == gMain ? TEMAdown_2 : na
TEMAup_End_2_gMain = PlotMode == gMain ? TEMAup_End_2 : na
TEMAdown_End_2_gMain = PlotMode == gMain ? TEMAdown_End_2 : na

plot(TEMAout_2_gMain, 'TEMA 2', color.new(color.fuchsia, 0))

plotshape(TEMAup_2_gMain, 'TEMA 2 UP', style=shape.flag, location=location.abovebar, size=size.small, color=bullflagcolor)
plotshape(TEMAdown_2_gMain, 'TEMA 2 Down', style=shape.flag, location=location.belowbar, size=size.small, color=bearflagcolor)
plotshape(TEMAup_End_2_gMain, 'TEMA 2 Up End', style=shape.xcross, location=location.abovebar, size=size.small, color=bullflagcolor)
plotshape(TEMAdown_End_2_gMain, 'TEMA 2 Down End', style=shape.xcross, location=location.belowbar, size=size.small, color=bearflagcolor)


//PLOT StochRSI OB/OS - OVERLAY
Fast_K_OS_gMain = PlotMode == gMain ? Fast_K_OS : na
Fast_K_OB_gMain = PlotMode == gMain ? Fast_K_OB : na
KD_OS_gMain = PlotMode == gMain ? KD_OS : na
KD_OB_gMain = PlotMode == gMain ? KD_OB : na

plotshape(Fast_K_OS_gMain, "Fast K OVERSOLD", style=shape.labelup , text="K OS", location=location.belowbar, color=color.navy, textcolor=color.white, size=size.small)
plotshape(Fast_K_OB_gMain, "Fast K OVERBOUGHT", style=shape.labeldown , text="K OB", location=location.abovebar, color=color.navy, textcolor=color.white, size=size.small)
plotshape(KD_OS_gMain, "KD Cross OVERSOLD", style=shape.labelup , text="KD Cross OS", location=location.belowbar, color=color.navy, textcolor=color.white, size=size.small)
plotshape(KD_OB_gMain, "KD Cross OVERBOUGHT", style=shape.labeldown , text="KD Cross OB", location=location.abovebar, color=color.navy, textcolor=color.white, size=size.small)


//PLOT BUY SELL SIGNAL - OVERLAY
BuySignal_gMain = PlotMode == gMain ? BuySignal : na
SellSignal_gMain = PlotMode == gMain ? SellSignal : na

plotshape(BuySignal_gMain, "Buy", style=shape.arrowup, text="Buy", location=location.belowbar, color=color.green, size=size.small)
plotshape(SellSignal_gMain, "Sell", style=shape.arrowdown , text="Sell", location=location.abovebar, color=color.red, size=size.small)


//PLOT BUY SELL HTF SIGNAL - OVERLAY
BuySignalHTF_gMain = PlotMode == gMain ? BuySignalHTF : na
SellSignalHTF_gMain = PlotMode == gMain ? SellSignalHTF : na

plotshape(BuySignalHTF_gMain, "HTF Buy", style=shape.arrowup, text="HTF Buy", location=location.belowbar, color=color.green, textcolor=color.white, size=size.small)
plotshape(SellSignalHTF_gMain, "HTF Sell", style=shape.arrowdown , text="HTF Sell", location=location.abovebar, color=color.red, textcolor=color.white, size=size.small)



//**************************************************************************************************************************************************************


//PLOTTING NON-OVERLAY

//SEPARATORS
SepColor = color.new(color.navy,30)

BuySell_gComp = PlotMode == gComp? 1.5 : na
plot (BuySell_gComp, color=SepColor, linewidth =3)

Set1_gComp = PlotMode == gComp? -1.5 : na
plot (Set1_gComp, color=SepColor, linewidth =3)

Set2_gComp = PlotMode == gComp? -4.5 : na
plot (Set2_gComp, color=SepColor, linewidth =3)

Set3_gComp = PlotMode == gComp? -7.5 : na
plot (Set3_gComp, color=SepColor, linewidth =3)

//PLOT BUY SELL SIGNAL NON-OVERLAY ( -1 to 1)
ChimpSignal = BuySignal ? -0.5 : SellSignal ? 0.5 : 0
plot(PlotMode == gComp? ChimpSignal : na, 'ChimpSignal', linewidth=2, color=color.blue)


//PLOT BUY SELL HTF SIGNAL NON-OVERLAY (-1 to 1)
ChimpHTFSignal = BuySignalHTF ? -1 : SellSignalHTF ? 1 : 0
plot(PlotMode == gComp? ChimpHTFSignal: na, 'ChimpHTFSignal', linewidth=2, color=color.red)


//PLOT %B NON-OVERLAY - DONE ()
BBob_gComp = PlotMode == gComp? 2*BBob -4: na
BBos_gComp = PlotMode == gComp? 2*BBos -4: na
bbr_gComp = PlotMode == gComp? 2*bbr -4: na

plot(BBob_gComp, "BB Overbought", color=color.new(color.blue, 30))
plot(BBos_gComp, "BB Oversold", color=color.new(color.blue, 30))
plot(bbr_gComp, "%B", color=color.new(color.blue, 0))



//PLOT IFTRSI NON-OVERLAY - DONE
IFTRSI_gComp = PlotMode == gComp ? IFTRSI -3: na
TH_Buy_gComp = PlotMode == gComp ? TH_Buy -3: na
TH_Sell_gComp = PlotMode == gComp ? TH_Sell -3: na
IFTRSI_signal_gComp = PlotMode == gComp ? IFTRSI_signal -3: na
ChimpSignal_gComp =  PlotMode == gComp ? 2*ChimpSignal -3: na

plot(IFTRSI_gComp, color=lineColour)
plot(TH_Buy_gComp, title='IFTRSI Buy Threshold', color=color.new(color.silver, 0))
plot(TH_Sell_gComp, title='IFTRSI Sell Threshold', color=color.new(color.silver, 0))
plot(IFTRSI_signal_gComp, title=' IFTRSI Signal', color=color.new(color.gray, 0))
plot(ChimpSignal_gComp, linewidth=3, color=color.blue)


//PLOT %B 2 NON-OVERLAY - DONE
BBob2_gComp = PlotMode == gComp? 2*BBob2 -7: na
BBos2_gComp = PlotMode == gComp? 2*BBos2 -7: na
bbr2_gComp = PlotMode == gComp? 2*bbr2 -7: na

plot(BBob2_gComp, "BB 2 Overbought", color=color.new(color.purple, 30))
plot(BBos2_gComp, "BB 2 Oversold", color=color.new(color.purple, 30))
plot(bbr2_gComp, "%B 2", color=color.new(color.purple, 0), linewidth=2)


//PLOT IFTRSI 2 NON-OVERLAY - DONE
IH_IFTRSI_gComp = PlotMode == gComp ? IH_IFTRSI -6: na
IH_TH_Buy_gComp = PlotMode == gComp ? IH_TH_Buy -6: na
IH_TH_Sell_gComp = PlotMode == gComp ? IH_TH_Sell -6: na
IH_IFTRSI_signal_gComp = PlotMode == gComp ? IH_IFTRSI_signal -6: na
ChimpHTFSignal_gComp =  PlotMode == gComp ? ChimpHTFSignal -6: na

plot(IH_IFTRSI_gComp, "IFTRSI 2", color=IH_lineColour, linewidth=2)
plot(IH_TH_Buy_gComp, title='IFTRSI 2 Buy Threshold', color=color.new(color.silver, 0))
plot(IH_TH_Sell_gComp, title='IFTRSI 2 Sell Threshold', color=color.new(color.silver, 0))
plot(IH_IFTRSI_signal_gComp, title=' IFTRSI 2 Signal', color=color.new(color.gray, 0))
plot(ChimpHTFSignal_gComp, linewidth=3, color=color.red)

//PLOT STOCHRSI NON-OVERLAY - DONE
SRk_gComp = PlotMode == gComp ? SRk/50 -9.5: na
SRd_gComp = PlotMode == gComp ? SRd/50 -9.5: na
Srupper_gComp = PlotMode == gComp ? Srupper/50 -9.5: na
Srlower_gComp = PlotMode == gComp ? Srlower/50 -9.5: na

plot(SRk_gComp, "K", color=#2962FF, linewidth=2)
plot(SRd_gComp, "D", color=#FF6D00, linewidth=2)
plot(Srupper_gComp, "Upper Band", color=#787B86)
plot(Srlower_gComp, "Lower Band", color=#787B86)


//**************************************************************************************************************************************************************


//Alerts
TimeZone = input.int(8, title="Time Zone", group="Alerts")
timehr = hour(time_close) + TimeZone
Ticker = 'TF: ' + str.tostring(timeframe.period) + '. BBtf: ' + str.tostring(BBtf) + '. ' + syminfo.tickerid + ' is ' + str.tostring(close) + '. ' + str.tostring(timehr >=24 ? timehr -24 : timehr) + ':' + str.tostring(minute(time_close))
Ticker2 =  'TF: ' + str.tostring(timeframe.period) + '. BBtf2: ' + str.tostring(BBtf2) + '. ' + syminfo.tickerid + ' is ' + str.tostring(close) + '. ' + str.tostring(timehr >=24 ? timehr -24 : timehr) + ':' + str.tostring(minute(time_close))
TickerStoch = 'TF: ' + str.tostring(timeframe.period) + '. SRtf: ' + str.tostring(SRtf) + '. ' + syminfo.tickerid + ' is ' + str.tostring(close) + '. ' + str.tostring(timehr >=24 ? timehr -24 : timehr) + ':' + str.tostring(minute(time_close))


//Enable/Disable Alerts - signals are still generated eventhough alerts are disabled
//Buy Sell Commands
Buy2 = input('Buy Signal', title='BuyCommand', group="Alerts")
Sell2 = input('Sell Signal', title='SellCommand', group="Alerts")
Buy_HTF = input('HTF Buy Signal', title='HTF BuyCommand', group="Alerts")
Sell_HTF = input('HTF Sell Signal', title='HTF SellCommand', group="Alerts")

//Alerts Buy Sell 
Enable_BuySell_Alerts  = input.bool(true, title='Enable Buy Sell Alerts', group="Alerts")
if BuySignal and Enable_BuySell_Alerts == true
    alert('BUY SIG. '+ Ticker, alert.freq_once_per_bar_close)
    alert(Buy2, alert.freq_once_per_bar_close)
else if SellSignal and Enable_BuySell_Alerts == true
    alert('SELL SIG. ' + Ticker, alert.freq_once_per_bar_close)
    alert(Sell2, alert.freq_once_per_bar_close)

Enable_BuySell_HTF_Alerts = input.bool(true, title='Enable HTF Buy Sell Alerts', group="Alerts")
if BuySignalHTF and Enable_BuySell_HTF_Alerts == true
    alert('HTF BUY SIG. '+ Ticker2, alert.freq_once_per_bar_close)
    alert(Buy_HTF, alert.freq_once_per_bar_close)
else if SellSignalHTF and Enable_BuySell_HTF_Alerts == true
    alert('HTF SELL SIG. ' + Ticker2, alert.freq_once_per_bar_close)
    alert(Sell_HTF, alert.freq_once_per_bar_close)
    

//Alerts BB
Enable_BB_Alerts = input.bool(true, title='Enable BB Alerts', group="Alerts")
if BBOverbought and Enable_BB_Alerts == true
    alert('BB Up. '+ Ticker, alert.freq_once_per_bar_close)
else if BBOversold and Enable_BB_Alerts == true 
    alert('BB Down. '+ Ticker, alert.freq_once_per_bar_close)

Enable_BB_2_Alerts = input.bool(true, title='Enable BB 2 Alerts', group="Alerts")
if BBOverbought2 and Enable_BB_2_Alerts == true 
    alert('BB 2 Up. '+ Ticker2, alert.freq_once_per_bar_close)
else if BBOversold2 and Enable_BB_2_Alerts == true  
    alert('BB 2 Down. '+ Ticker2, alert.freq_once_per_bar_close)
    

//Alert for TEMA
Enable_TEMA_Alerts = input.bool(true, title='Enable TEMA Alerts', group="Alerts")
if TEMAup and Enable_TEMA_Alerts == true 
    alert('TEMA Up. '+ Ticker, alert.freq_once_per_bar_close)
if TEMAdown and Enable_TEMA_Alerts == true 
    alert('TEMA Down. '+ Ticker, alert.freq_once_per_bar_close)
if TEMAup_End and Enable_TEMA_Alerts == true 
    alert('TEMA Up End. '+ Ticker, alert.freq_once_per_bar_close)
if TEMAdown_End and Enable_TEMA_Alerts == true 
    alert('TEMA Down End. '+ Ticker, alert.freq_once_per_bar_close)


Enable_TEMA_2_Alerts = input.bool(true, title='Enable TEMA 2 Alerts', group="Alerts")
if TEMAup_2 and Enable_TEMA_2_Alerts == true 
    alert('TEMA 2 Up. '+ Ticker2, alert.freq_once_per_bar_close)
if TEMAdown_2 and Enable_TEMA_2_Alerts == true 
    alert('TEMA 2 Down. '+ Ticker2, alert.freq_once_per_bar_close)
if TEMAup_End_2 and Enable_TEMA_2_Alerts == true 
    alert('TEMA 2 Up End. '+ Ticker2, alert.freq_once_per_bar_close)
if TEMAdown_End_2 and Enable_TEMA_2_Alerts == true 
    alert('TEMA 2 Down End. '+ Ticker2, alert.freq_once_per_bar_close)


//Alert for StochRSI
Enable_StochRSI_Alerts = input.bool(true, title='Enable StochRSI Alerts', group="Alerts")
if Fast_K_OS and Enable_StochRSI_Alerts == true 
    alert('Fast K OVERSOLD. '+ TickerStoch, alert.freq_once_per_bar_close)
if Fast_K_OB and Enable_StochRSI_Alerts == true 
    alert('Fast K OVERBOUGHT. '+ TickerStoch, alert.freq_once_per_bar_close)
if KD_OS and Enable_StochRSI_Alerts == true 
    alert('KD Cross OVERSOLD. '+ TickerStoch, alert.freq_once_per_bar_close)
if KD_OB and Enable_StochRSI_Alerts == true 
    alert('KD Cross OVERBOUGHT. '+ TickerStoch, alert.freq_once_per_bar_close)
