#//      Thanks to ballsforcalls, and LazyBear for ideas.
#//      If you find it useful, tip BTC to >> 3HCQ41cRpxcq2MJdRUSYoq9N1RnEy98xD2
#study("Stochastic RSI 3in1 & Volatility peaks + ADX by AGU", shorttitle="STOCH 3in1 RVI ADX")
# Converted by Sam4Cok@Samer800    - 11/2023 - request from useThinkScript.com member
declare lower;
input colorBars = yes;
input lookback_period1 = 22;
input lookback_period2 = 28;
input lookback_period3 = 350;
input lookback_period4 = 110;
input st1_m1 = 1;
input st1_m2 = 2;
input st2_m1 = 6;
input st2_m2 = 6;
input st3_m1 = 35;
input st3_m2 = 40;
input st4_m1 = 27;
input st4_m2 = 27;
input stdevLength = 300; # "Standard deviation length"
input NoOfdeviations = 1; # "Number of deviations"
input allowNegativePlots = yes;
input matchVolumeColor = yes;
input diLength = 22;    # "DI Length"
input adxSmoothing = 14;    # "ADX Smoothing"
input ExhaustionLevelForAdx = 40;    # "Exhaustion Level for ADX"
input rsiLength = 14;#, minval=1, title="Length")
def na = Double.NaN;
def x = stdevLength;
def y = NoOfdeviations;
def len = diLength;
def lensig = adxSmoothing;
def osob = ExhaustionLevelForAdx;
#-- Color
DefineGlobalColor("orange", CreateColor(255,127,0));
DefineGlobalColor("dgreen", CreateColor(0,128,0));
DefineGlobalColor("lgreen", CreateColor(152,255,152));
DefineGlobalColor("lred", CreateColor(231,116,113));
DefineGlobalColor("lime", CreateColor(0,255,0));
DefineGlobalColor("red", CreateColor(255,0,0));
DefineGlobalColor("maroon", CreateColor(128,0,0));
# stoch(source, high, low, length) =>
script stoch {
    input src = close;
    input h = high;
    input l = low;
    input len = 14;
    def hh = Highest(h, len);
    def ll = Lowest(l, len);
    def stoch = 100 * (src - ll) / (hh - ll);
    plot return = stoch;
}
def k1 = Average(stoch(close, high, low, lookback_period1), st1_m2);
def k2 = Average(stoch(close, high, low, lookback_period2), st2_m2);
def k3 = Average(stoch(close, high, low, lookback_period3), st3_m2);
def k4 = Average(stoch(close, high, low, lookback_period4), st4_m2);
def d1 = Average(k1, st1_m1);
def d2 = Average(k2, st2_m1);
def d3 = Average(k3, st3_m1);
def d4 = Average(k4, st4_m1);
AddCloud(k3, k4, Color.DARK_GRAY, Color.DARK_GRAY);
plot stoc3 = k3;#,title='Storch 3', color=#FF0000, linewidth=2, transp=50)
plot stoc2 = k2;#,title='Storch 2', color=#008000, linewidth=3)
plot stoc1 = k1;#,title='Storch 1', color=#FF7F00, linewidth=1, transp=35)
stoc2.SetLineWeight(3);
stoc3.SetLineWeight(2);
stoc1.SetDefaultColor(GlobalColor("orange"));
stoc2.SetDefaultColor(GlobalColor("dgreen"));
stoc3.AssignValueColor(if k3 > k3[4] then Color.GREEN else Color.RED);
#//cross over circle
def kCrosses = crosses(k3, k4);
plot kcross = if kCrosses then k4 else na;
kcross.SetLineWeight(3);
kcross.SetPaintingStrategy(PaintingStrategy.POINTS);
kcross.AssignValueColor(if (k4 - k3) > 0 then Color.CYAN else Color.MAGENTA);
#//Volatility
def av = Average(volume, x);
def sd = stdev(volume, x);
def relVol= if sd!=0 then (volume-av)/sd else 0;
def relV = if allowNegativePlots then max(relVol, 0) else relVol;
def b_color = if relV > y then
              if matchVolumeColor then (if close>open then 1 else -1) else 0 else -2;# #00BCD4
plot volatility = relV;#, title='volatility'
volatility.SetLineWeight(2);
volatility.AssignValueColor(if b_color > 0 then Color.GREEN else
                            if b_color == -1 then Color.RED else
                            if b_color==0 then Color.CYAN else Color.GRAY);
#//ADX
def up = high - high[1];
def down = - (low - low[1]);
def trur = atr(Length = len);
def plus = (100 * WildersAverage(if up > down and up > 0 then up else 0, len) / trur);
def minus = (100 * WildersAverage(if down > up and down > 0 then down else 0, len) / trur);
def sum = plus + minus;
def adx = 100 * WildersAverage(AbsValue(plus - minus) / (if sum == 0 then 1 else sum), lensig);
def col = if adx <= 10 then 0 else
          if adx > 10 and adx <= 20 and plus > minus then 1 else
          if adx > 10 and adx <= 20 and plus < minus then -1 else
          if adx > 20 and adx <= osob and plus > minus then 2 else
          if adx > 20 and adx <= osob and plus < minus then -2 else
          if adx > osob and plus > minus then 3 else
          if adx > osob and plus < minus then -3 else 5;
plot adxHist = adx;    # "ADX"
adxHist.SetPaintingStrategy(PaintingStrategy.SQUARED_HISTOGRAM);
adxHist.AssignValueColor(if col== 0 then Color.GRAY else
                 if col== 1 then GlobalColor("lgreen") else
                 if col==-1 then GlobalColor("lred") else
                 if col== 2 then GlobalColor("lime") else
                 if col==-2 then GlobalColor("red") else
                 if col== 3 then GlobalColor("dgreen") else
                 if col==-3 then GlobalColor("maroon") else Color.WHITE);
#//rsi
def src8 = ohlc4;
def nRSI = rsi(Price = src8,Length =  rsiLength);
def RSI50 = nRSI < 55 and nRSI > 45;
AssignPriceColor(if !colorBars then Color.CURRENT else
                 if RSI50 then Color.GRAY else
                 if col== 0 then Color.GRAY else
                 if col== 1 then GlobalColor("lgreen") else
                 if col==-1 then GlobalColor("lred") else
                 if col== 2 then GlobalColor("lime") else
                 if col==-2 then GlobalColor("red") else
                 if col== 3 then GlobalColor("dgreen") else
                 if col==-3 then GlobalColor("maroon") else Color.WHITE);
def readydown = nRSI > 78;
def readyup = nRSI < 22;
def buy8 = Crosses(nRSI, 19, CrossingDirection.BELOW);
def buy88 = Crosses(nRSI, 19, CrossingDirection.ABOVE);
def sell8 = Crosses(nRSI, 80, CrossingDirection.ABOVE);
def sell88 = Crosses(nRSI,80, CrossingDirection.BELOW);
def extremedown = Crosses(nRSI, 90, CrossingDirection.ABOVE);
def extremeup   = Crosses(nRSI, 10, CrossingDirection.BELOW);
def colTop = if sell88 then -3 else
             if sell8 then -2 else
             if readydown then -1 else 0;
def colBot = if buy88 then 3 else
             if buy8 then 2 else
             if readyup then 1 else 0;
plot ReadySell = if colTop < 0 then 105 else na;#,"get ready sell"
plot ReadyBuy = if colBot > 0 then -5 else na;#,"get ready buy"
AddChartBubble(extremedown, 0, "RSI90", Color.RED);
AddChartBubble(extremeup, 100, "RSI10", Color.GREEN);
ReadySell.SetPaintingStrategy(PaintingStrategy.SQUARES);
ReadySell.AssignValueColor(if colTop==-3 then Color.RED else
                           if colTop==-2 then Color.YELLOW else Color.DARK_ORANGE);
ReadyBuy.SetPaintingStrategy(PaintingStrategy.SQUARES);
ReadyBuy.AssignValueColor(if colBot==3 then Color.GREEN else
                          if colBot==2 then Color.YELLOW else Color.DARK_ORANGE);
#-- END OF CODE