```
# https://www.tradingview.com/script/8mytIJeV-volume-spread-analysis/
#//@RahulKrishna
#strategy("volume spread analysis ", overlay=true)
# Converted by Sam4Cok@Samer800 - 05/2024
input minperiodsrwist = 2; #(title="short term min periods", type=integer, defval=2, minval=1)
input maxperiodsrwist = 8;#(title="short term max periods", type=integer, defval=8, minval=1)
input minperiodsrwilt = 10;#(title="long term min periods", type=integer, defval=10, minval=1)
input maxperiodsrwilt = 40;#(title="long term max periods", type=integer, defval=40, minval=1)
def na = Double.NaN;
def volavg = Average(volume, 40);
def c = close;
def l = low;
def h = high;
def v = volume;
def volmean = StDev(volavg, 30);
def volupband3 = volavg + 3 * volmean;
def volupband2 = volavg + 2 * volmean;
def volupband1 = volavg + 1 * volmean;
def voldnband1 = volavg - 1 * volmean;
def voldnband2 = volavg - 2 * volmean;
def midprice = (high + low) / 2;
def spread = (high - low);
def avgspread = Average(spread, 80);
def avgspreadbar = spread > avgspread;
def widerangebar = spread > (1.5 * avgspread);
def narrowrangebar = spread < (0.7 * avgspread);
def lowvolume = volume < volume[1] and volume < volume[2];
def upbar = close > close[1];
def downbar = close < close[1];
def highvolume = volume > volume[1] and volume[1] > volume[2];
def closefactor = close - low;
def clsposition = spread / closefactor;
def closeposition = If(closefactor == 0, avgspread, clsposition);
def vb = volume > volavg or volume > volume[1];
def upclose = close >= ((spread * 0.7) + low); #// close is above 70% of the bar
def downclose = close <= ((spread * 0.3) + low); #// close is below the 30% of the bar
def aboveclose = close > ((spread * 0.5) + low); #// close is between 50% and 70% of the bar
def belowclose = close < ((spread * 0.5) + low); #// close is between 50% and 30% of the bar
def midclose = close > ((spread * 0.3) + low) and c < ((spread * 0.7) + l); #// close is between 30% and 70% of the bar
def verylowclose = closeposition > 4; #//close is below 25% of the bar
def veryhighclose = closeposition < 1.35; #// close is above 80% of the bar
def closepos = If(close <= ((spread * 0.2) + low), 1, If(close <= ((spread * 0.4) + low), 2, If(close <= ((spread * 0.6) + low), 3, If(close <= ((spread * 0.8) + low), 4, 5))));
def volpos = If(volume > volavg * 2, 1, If(volume > volavg * 1.3, 2, If(volume > volavg, 3, If(volume < volavg and volume > volavg * 0.7, 4, 5))));
def freshgndhi = close > Highest(h, 5);
def freshgndlo = close < Lowest(l, 5);
def rwhmins = (high - low[minperiodsrwist]) / (ATR(minperiodsrwist) * Sqrt(minperiodsrwist));
def rwhmaxs = (high - low[maxperiodsrwist]) / (ATR(maxperiodsrwist) * Sqrt(maxperiodsrwist));
def rwhs = Max(rwhmins, rwhmaxs);
def rwlmins = (high[minperiodsrwist] - low) / (ATR(minperiodsrwist) * Sqrt(minperiodsrwist));
def rwlmaxs = (high[maxperiodsrwist] - low) / (ATR(maxperiodsrwist) * Sqrt(maxperiodsrwist));
def rwls = Max( rwlmins, rwlmaxs );
def rwhminl = (high - low[minperiodsrwilt]) / (ATR(minperiodsrwilt) * Sqrt(minperiodsrwilt));
def rwhmaxl = (high - low[maxperiodsrwilt]) / (ATR(maxperiodsrwilt) * Sqrt(maxperiodsrwilt));
def rwhl = Max( rwhminl, rwhmaxl );
def rwlminl = (high[minperiodsrwilt] - low) / (ATR(minperiodsrwilt) * Sqrt(minperiodsrwilt));
def rwlmaxl = (high[maxperiodsrwilt] - low) / (ATR(maxperiodsrwilt) * Sqrt(maxperiodsrwilt));
def rwll = Max( rwlminl, rwlmaxl );
def ground = rwhs;
def sky = rwls;
def j = rwhs-rwls;
def k = rwhl-rwll;
def j2 = rwhl;
def k2 = rwll;
def ja = crosses(j,1);
def jb = crosses(1,j);
def jc = crosses(-1,j);
def jd = crosses(j,-1);
def j2a = crosses(j2,1);
def j2b = crosses(1,j2);
def k2a = crosses(k2,1);
def k2b = crosses(1,k2);
def upmajoron = j > 1 and ja[1];
def upmajoroff = j < 1 and jb[1];
def upminoron = j2 > 1 and j2a[1];
def upminoroff = j2 < 1 and j2b[1];
def dnmajoron = j < -1 and jc[1];
def dnmajoroff = j > -1 and jd[1];
def dnminoron = k2 > 1 and k2a[1];
def dnminoroff = k2 < 1 and k2b[1];
def upimd = if(ground > 1, 1,0);
def dnimd = if(sky > 1, 1, 0);
def upmajor = if(j>1,1,if(j<(-1),-1,0));
def upminor = if(j2>1,1,-1);
def dnminor = if(k2>1,1,-1);
#//====Band
def Buy_stop = lowest(low[1],5) - atr(Length = 20)[1];
def Sell_stop = highest(high[1],5) + atr(Length = 20)[1];
plot buy_stoploss = Buy_stop; #, color=red, title="buy_stoploss")
plot sell_stoploss = Sell_stop; # color=green, title="sell_stoploss")
buy_stoploss.SetDefaultColor(Color.RED);
sell_stoploss.SetDefaultColor(Color.GREEN);
#-- Strategy
def nut = widerangebar and downclose and freshgndhi and highvolume;
def bc = widerangebar and aboveclose and volume == highest(volume,60) and upmajor==1;
def upthrustbar = widerangebar and (closepos==1 or closepos==2) and upminor>0 and high>high[1] and
(upimd>0 or upmajor>0) and volpos <4;
def upthrustbartrue = widerangebar and closepos==1 and upmajor>0 and high>high[1] and volpos <4;
def upthrustcond1 = upthrustbar[1] and downbar and !narrowrangebar;
def upthrustcond2 = upthrustbar[1] and downbar and volpos == 2;
def upthrustcond3 = upthrustbar and volpos ==1;
def toprevbar = volume[1]>volavg and upbar[1] and widerangebar[1] and downbar and
downclose and widerangebar and upmajor>0 and high==highest(high,10);
def pseudoupthrust = upbar[1] and high>high[1] and volume[1]>1.5*volavg and downbar and downclose and !upthrustbar;
def pseudoutcond = pseudoupthrust[1] and downbar and downclose and !upthrustbar;
def trendchange = upbar[1] and high==highest(high,5) and downbar and (downclose or midclose) and volume>volavg and upmajor>0 and upimd>0 and !widerangebar and !pseudoupthrust;
def nodemandbarut = upbar and narrowrangebar and lowvolume and closepos> 3 and ((upminor>0 and upimd>0)or (upminor<0 and upminor>0));
def nodemandbardt = upbar and narrowrangebar and lowvolume and closepos> 3 and (upminor<=0or upimd<=0);
def nosupplybar = downbar and narrowrangebar and lowvolume and closepos<3 and ((upminor<1 and upimd<1)or (upminor>0 and upimd<1));
def lowvoltest = low==lowest(low,5) and upclose and lowvolume;
def lowvoltest1 = low==lowest(low,5) and volume<volavg and low<low[1] and upclose and upminor>0 and upmajor>0;
def lowvoltest2 = lowvoltest[1] and upbar and upclose;
def sellcond1 = (upthrustcond1 or upthrustcond2 or upthrustcond3);
def sellcond2 = sellcond1[1]==0;
def sellcond = sellcond1 and sellcond2;
def strengthdown0 = upmajor<0 and volpos<4 and downbar[1] and upbar and closepos>3 and upminor<0 and upimd<=0;
def strengthdown = volpos<4 and downbar[1] and upbar and closepos>3 and upimd<=00 and upminor<0;
def strengthdown1 = upmajor<0 and volume>(volavg*1.5) and downbar[1] and upbar and closepos>3 and upminor<0 and upimd<=0;
def strengthdown2 = upimd<=0 and volume[1]<volavg and upbar and veryhighclose and volpos<4;
def buycond1 = strengthdown or strengthdown1;
def buycond = upbar and buycond1[1];
def stopvolume = low==lowest(low,5) and (upclose or midclose) and v>1.5*volavg and upmajor<0;
def revupthrust = upmajor<0 and upbar and upclose and volume>volume[1] and volume>volavg
and widerangebar and downbar[1] and downclose[1] and upminor<0;
def effortup = high>high[1] and low>low[1] and close>close[1] and close>=((high-low)*0.7+low) and spread>avgspread and volpos<4;
def effortupfail = effortup[1] and (upthrustbar or upthrustcond1 or upthrustcond2 or upthrustcond3 or (downbar and avgspreadbar));
def effortdown = high<high[1] and low<low[1] and close<close[1] and close<=((high-low)*0.25+low)
and widerangebar and volume>volume[1];
def effortdownfail = effortdown[1] and ((upbar and avgspreadbar)or revupthrust or buycond1);
def upflag = (sellcond or buycond or effortup or effortupfail or stopvolume or effortdown or effortdownfail or revupthrust
or nodemandbardt or nodemandbarut or nosupplybar or lowvoltest or lowvoltest1 or lowvoltest2 or bc);
def bullbar = (volume>volavg or volume>volume[1]) and closeposition <2 and upbar and !upflag;
def bearbar = vb and downclose and downbar and spread>avgspread and !upflag;
def buy = (upbar and revupthrust[1])or lowvoltest2;
def burely = strengthdown1 and stopvolume[1]or (upbar and revupthrust[1])or lowvoltest2;
def sell = effortup[1] and effortupfail and upthrustcond3 and upthrustbartrue and toprevbar;
AddChartBubble((upbar and revupthrust[1]) or lowvoltest2, low, "simpleBuy", Color.GREEN, no);
AddChartBubble(upthrustbartrue, high, "Close Buy", Color.RED);
#/|==Signals==================|
plot upThrust = if (upthrustbar or upthrustbartrue) then high else na; # title="upthrustbaro" ,style=shape.arrowdown ,color=red
plot TrendChg = if trendchange then hl2 else na; # "trendchange" ,style=shape.xcross ,size=size.small,color=red )
plot revUp = if revupthrust then low else na; # "revupthrust" ,style=shape.arrowup ,size=size.huge,color=green )
plot lowVol = if lowvoltest then low else na; # "lowvoltest" ,style=shape.triangleup ,size=size.small,color=blue )
plot effUp = if effortup then low else na; # "effortup" ,style=shape.diamond ,size=size.small,color=lime )
plot effupFail = if effortupfail then hl2 else na; # "effortupfail" ,style=shape.xcross ,size=size.small,color=blue )
plot effDn = if effortdown then high else na; # "effortdown" ,style=shape.triangledown ,size=size.small,color=red )
plot effdnFail = if effortdownfail then hl2 else na; # "effortdownfail" ,style=shape.xcross ,size=size.small,color=green )
upThrust.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
TrendChg.SetPaintingStrategy(PaintingStrategy.SQUARES);
revUp.SetPaintingStrategy(paintingStrategy.BOOLEAN_ARROW_UP);
lowVol.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_DOWN);
effUp.SetPaintingStrategy(PaintingStrategy.SQUARES);
effupFail.SetPaintingStrategy(PaintingStrategy.TRIANGLES);
effDn.SetPaintingStrategy(PaintingStrategy.SQUARES);
effdnFail.SetPaintingStrategy(PaintingStrategy.TRIANGLES);
TrendChg.SetLineWeight(3);
upThrust.SetDefaultColor(Color.RED);
TrendChg.SetDefaultColor(Color.WHITE);
revUp.SetDefaultColor(Color.CYAN);
lowVol.SetDefaultColor(Color.CYAN);
effUp.SetDefaultColor(Color.CYAN);
effupFail.SetDefaultColor(Color.CYAN);
effDn.SetDefaultColor(Color.MAGENTA);
effdnFail.SetDefaultColor(Color.MAGENTA);
#-- END of CODE
```