Cumulative Delta Volume Pressure - Mod by Sam4Cok for ThinkOrSwim

MerryDay

Administrative
Staff member
Staff
VIP
Lifetime
Mod Note:
The below indicator is another great contribution from @samer800


CAVEAT:
Members will sometimes mistake these indicators as defining Volume: Buyers and Sellers.
Buyers and Sellers information is NOT available from the ToS data feeds.
When we look at the movement of price in comparison to volume, it is called Volume Pressure.
The script discussed here are representative of the PRICE spread compared to the overall volume spread.

We create a percentage for buying and selling pressure by using the candlestick patterns versus volume.
Volume Pressure identifies the price movement which when aggregated against volume it is used to define MOMENTUM not actual buyers and sellers.

nOnSBvp.png
uwFHET6.png

This does not have a direct correlation to the actual buyers or sellers or the actual cumulative delta volume.
 

Attachments

  • nOnSBvp.png
    nOnSBvp.png
    18.9 KB · Views: 4,871
Last edited:
k0UCRX1.png


This is Cumulative Delta Volume script based on LonesomeTheBlue script. I added multiple options to identify the trend based on the standard deviation and moving average. You can use it along with your strategy to confirm the entry and exit referring to the delta volume (the difference between buying and selling volume at each price level.

CODE:
CSS:
#study("Cumulative Delta Volume", "CDV")
#https://www.tradingview.com/script/vB1T3EMp-Cumulative-Delta-Volume/
# Created based on LonesomeTheBlue code link above - Sam4Cok @ 08/2022

declare lower;
input linestyle = {default Candle, Line};
input HeikinAshi = yes; #"Heikin Ashi Candles?")
input maType    =  {default "SMMA", "SMA", "HMA", "EMA", "WMA"};
input maLength  = 50;   #"MA Length"
input BarColor = no;
input BackgroundColor = yes;
input ShowMovAvg = yes;
input ShowBand = no;

def na = Double.NaN;

###############
script nz {
    input data         = 0;
    input replacement  = 0;
    def ret_val = if IsNaN(data) then replacement else data;
    plot return = ret_val;
}
#ma(source, length, type) =>
script ma {
    input source = close;
    input length = 0;
    input type   = "SMA";
    def ma;
ma = if type == "SMA" then SimpleMovingAvg(source, length) else
     if type == "EMA" then ExpAverage(source, length) else
     if type == "WMA" then WMA(source, length) else
     if type == "HMA" then WMA(2 * WMA(source, length / 2) - WMA(source, length), Round(Sqrt(length)))
       else if isNaN(ma[1]) then SimpleMovingAvg(source, length) else
             (ma[1] * (length - 1) + source) / length;
    plot result = ma;
}
#_rate(cond) =>
script _rate {
    input cond = 1;
    def criteria = if cond == 1 then open <= close else open > close;
    def tw = high - Max(open, close) ;
    def bw = Min(open, close) - low;
    def body = AbsValue(close - open);
    def ret  = 0.5 * (tw + bw + (if criteria then 2 * body else 0)) / (tw + bw + body);
    def rate = if nz(ret) == 0 then 0.5 else ret;
    plot Result = rate;
}
def deltaup   = volume * _rate(1);
def deltadown = volume * _rate(0);
def delta     = if close >= open then deltaup else -deltadown;
def cumdelta  = TotalSum(delta);

def o;
def h;
def l;
def c;
def ctl;

if linestyle == linestyle.Candle
then {
    o = cumdelta[1];
    h = Max(cumdelta, cumdelta[1]);
    l = Min(cumdelta, cumdelta[1]);
    c = cumdelta;
    ctl = na;
} else {
    o = na;
    h = na;
    l = na;
    c = na;
    ctl = cumdelta;
}
def LineClose = ExpAverage((cumdelta[1] + Max(cumdelta, cumdelta[1]) + Min(cumdelta, cumdelta[1]) + cumdelta) / 4, 5);
def LineOpen = ExpAverage(if IsNaN(LineOpen[1]) then (cumdelta[1] + cumdelta) / 2 else (LineOpen[1] + LineClose[1]) / 2, 5);

plot Line = ctl;
Line.AssignValueColor( if LineClose >= LineOpen then CreateColor(8,153,129) else
                       if LineClose < LineOpen  then CreateColor(239, 83, 80) else Color.GRAY);
Line.SetLineWeight(2);

##############
def _Close = (o + h + l + c) / 4;
def _Open = if IsNaN(_Open[1]) then (o + c) / 2 else (_Open[1] + _Close[1]) / 2;
def _High = Max(Max(h, _Open), _Close);
def _Low = Min(Min(l, _Open), _Close);

def haClose = if HeikinAshi then _Close else c;
def haOpen  = if HeikinAshi then _Open  else o;
def haHigh  = if HeikinAshi then _High  else h;
def haLow   = if HeikinAshi then _Low   else l;

def Color = if haClose >= haOpen then 1 else -1;

# Plot UP candle

def UpO;
def UpH;
def UpL;
def UpC;

if Color > 0
then {
    UpO = haOpen ;
    UpH = haHigh ;
    UpL = haLow ;
    UpC = haClose;
} else
{
    UpO = na;
    UpH = na;
    UpL = na;
    UpC = na;
}
# Plot DOWN candle
def DnO;
def DnH;
def DnL;
def DnC;
if Color < 0
then {
    DnO = haOpen ;
    DnH = haHigh ;
    DnL = haLow ;
    DnC = haClose;
} else
{
    DnO = na;
    DnH = na;
    DnL = na;
    DnC = na;
}

# Plot the new Chart
AddChart(high = UpH , low = UpL , open = UpC,  close = UpO,
         type = ChartType.CANDLE, growcolor =  CreateColor(8,153,129));

AddChart(high = DnH , low = DnL , open = DnO,  close = DnC,
         type = ChartType.CANDLE, growcolor =  CreateColor(239, 83, 80));

## Plot MA Lines
plot AvgLine = ma(ctl, maLength, maType);
AvgLine.SetPaintingStrategy(PaintingStrategy.DASHES);
AvgLine.SetHiding(!ShowMovAvg);

plot HaAvgLine = ma(haClose, maLength, maType);
HaAvgLine.SetPaintingStrategy(PaintingStrategy.DASHES);
HaAvgLine.SetHiding(!ShowMovAvg);

### Band
def offs = (1.6185 * stdev(if linestyle == linestyle.Candle then haClose else ctl, maLength));
def upBand = (if linestyle == linestyle.Candle then HaAvgLine else AvgLine) + offs;
def dnBand = (if linestyle == linestyle.Candle then HaAvgLine else AvgLine) - offs;

def BandColor = if (if linestyle == linestyle.Candle then haClose else ctl) > upBand then 1 else
 if (if linestyle == linestyle.Candle then haClose else ctl) < dnBand then -1 else 0;

plot Oband = upBand;
Oband.AssignValueColor(if BandColor > 0 then Color.GREEN else
                       if BandColor < 0 then Color.RED else CreateColor(100, 181, 246));
Oband.SetHiding(!ShowBand);

plot Sband = dnBand;
sband.AssignValueColor(if BandColor > 0 then Color.GREEN else
                       if BandColor < 0 then Color.RED else CreateColor(100, 181, 246));
Sband.SetHiding(!ShowBand);

AvgLine.AssignValueColor(if BandColor > 0 then Color.GREEN else
                       if BandColor < 0 then Color.RED else Color.WHITE);
HaAvgLine.AssignValueColor(if BandColor > 0 then Color.GREEN else
                       if BandColor < 0 then Color.RED else Color.WHITE);

### Background
addCloud(if BackgroundColor then
if(if linestyle == linestyle.Candle then haClose else ctl) > Oband then Double.POSITIVE_INFINITY else if
(if linestyle == linestyle.Candle then haClose else ctl) < Sband then Double.NEGATIVE_INFINITY else na else na,
if(if linestyle == linestyle.Candle then haClose else ctl) > Oband then Double.NEGATIVE_INFINITY else if
(if linestyle == linestyle.Candle then haClose else ctl) < Sband then Double.POSITIVE_INFINITY else na, Color.DARK_GREEN, Color.DARK_RED);

### Bar Color
def Exup = if linestyle == linestyle.Candle then (color > 0 and BandColor > 0)
           else (LineClose >= LineOpen and BandColor > 0);
def up   = if linestyle == linestyle.Candle then (color > 0 and BandColor <= 0)
           else (LineClose >= LineOpen and BandColor <= 0) ;
def Exdn = if linestyle == linestyle.Candle then (color < 0 and BandColor < 0)
           else (LineClose < LineOpen and BandColor < 0);
def dn   = if linestyle == linestyle.Candle then (color < 0 and BandColor >= 0)
           else (LineClose < LineOpen and BandColor >= 0);

AssignPriceColor(if BarColor then
                 if Exup then Color.GREEN else
                 if up   then CreateColor(0,80,80) else
                 if dn   then CreateColor(117,0,14) else
                 if Exdn then Color.RED else Color.GRAY else Color.CURRENT);

### END
 
Is this the same as "Cumulative Volume Delta" as seen on bookmap?
No.

Bookmap uses volume action. Bookmap's data feed has access to actual buying and selling volume.

ToS's data feeds do not have access to actual buyers and sellers.
This indicator uses a generally-accepted workaround that is calculated based price action, using green and red candle spreads to define buying and selling pressure.
(see @Jman831 description: https://usethinkscript.com/threads/buy-and-sell-volume-pressure-for-thinkorswim.11739/#post-101477)
This indicator has the addition of moving averages to further define bullish /bearish trends.

Read more:
https://usethinkscript.com/threads/buying-selling-volume-in-thinkorswim.12030/#post-103837
 
Last edited:
what do the shaded areas indicate? Thank you.
when cdv cross upper stdv band so background turn green and when the cdv cross the lower sdv band turn red. You can enable or disable the band from the settings.
 
Hello, I really like your script because it is simple and clean. It's definitely what I was searching for. However, whenever I add it to my studies, the script shows that its invalid and has a red font color indicating something is wrong with the source code. Could you please help me because I would love to have this script fully running on my end.
 
Did you copy the whole code? It works for me
I did copy all the code yet there were some texts that were invalid. After messing with the code prompt, I clicked "reformat code" and I guess TOS automatically fixed the issue and I could apply it. It works now! Sorry for the trouble. Thank you.
 
k0UCRX1.png


This is Cumulative Delta Volume script based on LonesomeTheBlue script. I added multiple options to identify the trend based on the standard deviation and moving average. You can use it along with your strategy to confirm the entry and exit referring to the delta volume (the difference between buying and selling volume at each price level.

CODE:
CSS:
#study("Cumulative Delta Volume", "CDV")
#https://www.tradingview.com/script/vB1T3EMp-Cumulative-Delta-Volume/
# Created based on LonesomeTheBlue code link above - Sam4Cok @ 08/2022

declare lower;
input linestyle = {default Candle, Line};
input HeikinAshi = yes; #"Heikin Ashi Candles?")
input maType    =  {default "SMMA", "SMA", "HMA", "EMA", "WMA"};
input maLength  = 50;   #"MA Length"
input BarColor = no;
input BackgroundColor = yes;
input ShowMovAvg = yes;
input ShowBand = no;

def na = Double.NaN;

###############
script nz {
    input data         = 0;
    input replacement  = 0;
    def ret_val = if IsNaN(data) then replacement else data;
    plot return = ret_val;
}
#ma(source, length, type) =>
script ma {
    input source = close;
    input length = 0;
    input type   = "SMA";
    def ma;
ma = if type == "SMA" then SimpleMovingAvg(source, length) else
     if type == "EMA" then ExpAverage(source, length) else
     if type == "WMA" then WMA(source, length) else
     if type == "HMA" then WMA(2 * WMA(source, length / 2) - WMA(source, length), Round(Sqrt(length)))
       else if isNaN(ma[1]) then SimpleMovingAvg(source, length) else
             (ma[1] * (length - 1) + source) / length;
    plot result = ma;
}
#_rate(cond) =>
script _rate {
    input cond = 1;
    def criteria = if cond == 1 then open <= close else open > close;
    def tw = high - Max(open, close) ;
    def bw = Min(open, close) - low;
    def body = AbsValue(close - open);
    def ret  = 0.5 * (tw + bw + (if criteria then 2 * body else 0)) / (tw + bw + body);
    def rate = if nz(ret) == 0 then 0.5 else ret;
    plot Result = rate;
}
def deltaup   = volume * _rate(1);
def deltadown = volume * _rate(0);
def delta     = if close >= open then deltaup else -deltadown;
def cumdelta  = TotalSum(delta);

def o;
def h;
def l;
def c;
def ctl;

if linestyle == linestyle.Candle
then {
    o = cumdelta[1];
    h = Max(cumdelta, cumdelta[1]);
    l = Min(cumdelta, cumdelta[1]);
    c = cumdelta;
    ctl = na;
} else {
    o = na;
    h = na;
    l = na;
    c = na;
    ctl = cumdelta;
}
def LineClose = ExpAverage((cumdelta[1] + Max(cumdelta, cumdelta[1]) + Min(cumdelta, cumdelta[1]) + cumdelta) / 4, 5);
def LineOpen = ExpAverage(if IsNaN(LineOpen[1]) then (cumdelta[1] + cumdelta) / 2 else (LineOpen[1] + LineClose[1]) / 2, 5);

plot Line = ctl;
Line.AssignValueColor( if LineClose >= LineOpen then CreateColor(8,153,129) else
                       if LineClose < LineOpen  then CreateColor(239, 83, 80) else Color.GRAY);
Line.SetLineWeight(2);

##############
def _Close = (o + h + l + c) / 4;
def _Open = if IsNaN(_Open[1]) then (o + c) / 2 else (_Open[1] + _Close[1]) / 2;
def _High = Max(Max(h, _Open), _Close);
def _Low = Min(Min(l, _Open), _Close);

def haClose = if HeikinAshi then _Close else c;
def haOpen  = if HeikinAshi then _Open  else o;
def haHigh  = if HeikinAshi then _High  else h;
def haLow   = if HeikinAshi then _Low   else l;

def Color = if haClose >= haOpen then 1 else -1;

# Plot UP candle

def UpO;
def UpH;
def UpL;
def UpC;

if Color > 0
then {
    UpO = haOpen ;
    UpH = haHigh ;
    UpL = haLow ;
    UpC = haClose;
} else
{
    UpO = na;
    UpH = na;
    UpL = na;
    UpC = na;
}
# Plot DOWN candle
def DnO;
def DnH;
def DnL;
def DnC;
if Color < 0
then {
    DnO = haOpen ;
    DnH = haHigh ;
    DnL = haLow ;
    DnC = haClose;
} else
{
    DnO = na;
    DnH = na;
    DnL = na;
    DnC = na;
}

# Plot the new Chart
AddChart(high = UpH , low = UpL , open = UpC,  close = UpO,
         type = ChartType.CANDLE, growcolor =  CreateColor(8,153,129));

AddChart(high = DnH , low = DnL , open = DnO,  close = DnC,
         type = ChartType.CANDLE, growcolor =  CreateColor(239, 83, 80));

## Plot MA Lines
plot AvgLine = ma(ctl, maLength, maType);
AvgLine.SetPaintingStrategy(PaintingStrategy.DASHES);
AvgLine.SetHiding(!ShowMovAvg);

plot HaAvgLine = ma(haClose, maLength, maType);
HaAvgLine.SetPaintingStrategy(PaintingStrategy.DASHES);
HaAvgLine.SetHiding(!ShowMovAvg);

### Band
def offs = (1.6185 * stdev(if linestyle == linestyle.Candle then haClose else ctl, maLength));
def upBand = (if linestyle == linestyle.Candle then HaAvgLine else AvgLine) + offs;
def dnBand = (if linestyle == linestyle.Candle then HaAvgLine else AvgLine) - offs;

def BandColor = if (if linestyle == linestyle.Candle then haClose else ctl) > upBand then 1 else
 if (if linestyle == linestyle.Candle then haClose else ctl) < dnBand then -1 else 0;

plot Oband = upBand;
Oband.AssignValueColor(if BandColor > 0 then Color.GREEN else
                       if BandColor < 0 then Color.RED else CreateColor(100, 181, 246));
Oband.SetHiding(!ShowBand);

plot Sband = dnBand;
sband.AssignValueColor(if BandColor > 0 then Color.GREEN else
                       if BandColor < 0 then Color.RED else CreateColor(100, 181, 246));
Sband.SetHiding(!ShowBand);

AvgLine.AssignValueColor(if BandColor > 0 then Color.GREEN else
                       if BandColor < 0 then Color.RED else Color.WHITE);
HaAvgLine.AssignValueColor(if BandColor > 0 then Color.GREEN else
                       if BandColor < 0 then Color.RED else Color.WHITE);

### Background
addCloud(if BackgroundColor then
if(if linestyle == linestyle.Candle then haClose else ctl) > Oband then Double.POSITIVE_INFINITY else if
(if linestyle == linestyle.Candle then haClose else ctl) < Sband then Double.NEGATIVE_INFINITY else na else na,
if(if linestyle == linestyle.Candle then haClose else ctl) > Oband then Double.NEGATIVE_INFINITY else if
(if linestyle == linestyle.Candle then haClose else ctl) < Sband then Double.POSITIVE_INFINITY else na, Color.DARK_GREEN, Color.DARK_RED);

### Bar Color
def Exup = if linestyle == linestyle.Candle then (color > 0 and BandColor > 0)
           else (LineClose >= LineOpen and BandColor > 0);
def up   = if linestyle == linestyle.Candle then (color > 0 and BandColor <= 0)
           else (LineClose >= LineOpen and BandColor <= 0) ;
def Exdn = if linestyle == linestyle.Candle then (color < 0 and BandColor < 0)
           else (LineClose < LineOpen and BandColor < 0);
def dn   = if linestyle == linestyle.Candle then (color < 0 and BandColor >= 0)
           else (LineClose < LineOpen and BandColor >= 0);

AssignPriceColor(if BarColor then
                 if Exup then Color.GREEN else
                 if up   then CreateColor(0,80,80) else
                 if dn   then CreateColor(117,0,14) else
                 if Exdn then Color.RED else Color.GRAY else Color.CURRENT);

### END
@samer800 do you have a scan for this? Maybe something when the candle crosses the avg line?
 
@samer800 do you have a scan for this? Maybe something when the candle crosses the avg line?
try this. remove/add the hash sign for up/dn cross

CSS:
#CDV scan
input maType    =  {default "SMMA", "SMA", "HMA", "EMA", "WMA"};
input maLength  = 50;   #"MA Length"

def na = Double.NaN;

###############
script nz {
    input data         = 0;
    input replacement  = 0;
    def ret_val = if IsNaN(data) then replacement else data;
    plot return = ret_val;
}
#ma(source, length, type) =>
script ma {
    input source = close;
    input length = 0;
    input type   = "SMA";
    def ma;
ma = if type == "SMA" then SimpleMovingAvg(source, length) else
     if type == "EMA" then ExpAverage(source, length) else
     if type == "WMA" then WMA(source, length) else
     if type == "HMA" then WMA(2 * WMA(source, length / 2) - WMA(source, length), Round(Sqrt(length)))
       else if isNaN(ma[1]) then SimpleMovingAvg(source, length) else
             (ma[1] * (length - 1) + source) / length;
    plot result = ma;
}
#_rate(cond) =>
script _rate {
    input cond = 1;
    def criteria = if cond == 1 then open <= close else open > close;
    def tw = high - Max(open, close) ;
    def bw = Min(open, close) - low;
    def body = AbsValue(close - open);
    def ret  = 0.5 * (tw + bw + (if criteria then 2 * body else 0)) / (tw + bw + body);
    def rate = if nz(ret) == 0 then 0.5 else ret;
    plot Result = rate;
}
def deltaup   = volume * _rate(1);
def deltadown = volume * _rate(0);
def delta     = if close >= open then deltaup else -deltadown;
def cumdelta  = TotalSum(delta);

def ctl = cumdelta;
def LineClose = ExpAverage((cumdelta[1] + Max(cumdelta, cumdelta[1]) + Min(cumdelta, cumdelta[1]) + cumdelta) / 4, 5);
def LineOpen = ExpAverage(if IsNaN(LineOpen[1]) then (cumdelta[1] + cumdelta) / 2 else (LineOpen[1] + LineClose[1]) / 2, 5);

## Plot MA Lines
def AvgLine = ma(ctl, maLength, maType);
def Line = ctl;

def upCross = ctl crosses above AvgLine and LineClose >= LineOpen;
def dnCross = ctl crosses below AvgLine and LineClose < LineOpen;

plot Uptrend = upCross;
#plot Dntrend = dnCross;
 
def cumdelta = TotalSum(delta);

Great tool, but instead of the TotalSum of any full chart (eg 10d 5m), I need the TotalSum for each day. If I have to run a backtest, I don't want the TotalSum of the entire chart. I need the right signals for each day.

What should I add in the script so that it only accumulates each day separately even though I use a 10-day chart?
 
def cumdelta = TotalSum(delta);

Great tool, but instead of the TotalSum of any full chart (eg 10d 5m), I need the TotalSum for each day. If I have to run a backtest, I don't want the TotalSum of the entire chart. I need the right signals for each day.

What should I add in the script so that it only accumulates each day separately even though I use a 10-day chart?
check it out. I added option to reset the sum daily, weekly, monthly or total.

CSS:
#study("Cumulative Delta Volume", "CDV")
#https://www.tradingview.com/script/vB1T3EMp-Cumulative-Delta-Volume/
# Created based on LonesomeTheBlue code above - Sam4Cok @ 08/2022
# Added time reset option, Added VWAP option - Sam4Cok@Samer800 - 11/2022

declare lower;
input linestyle = {default HeikinAshi, Candle, Line};
input timeFrame = {default DAY, WEEK, MONTH, ALL};
input maType    = {SMMA,default VWAP, SMA, HMA, EMA, WMA};
input maLength  = 50;   #"MA Length"
input BarColor = no;
input BackgroundColor = yes;
input ShowMovAvg = yes;
input ShowBand = no;

def na = Double.NaN;
def cl = close; def op = open; def vo = volume;
def yyyyMmDd = GetYYYYMMDD();
def periodIndx;
switch (timeFrame) {
case DAY:
    periodIndx = yyyyMmDd;
case WEEK:
    periodIndx = Floor((DaysFromDate(First(yyyyMmDd)) + GetDayOfWeek(First(yyyyMmDd))) / 7);
case MONTH:
    periodIndx = RoundDown(yyyyMmDd / 100, 0);
case ALL:
    periodIndx = na;
}
def isPeriodRolled = CompoundValue(1, periodIndx != periodIndx[1], yes);
###############
script nz {
    input data         = volume;
    input replacement  = 0;
    def ret_val = if IsNaN(data) then replacement else data;
    plot return = ret_val;
}
# volVWAP
script VVWAP {
    input src = close;
    def timeFrame = AggregationPeriod.DAY;
    def v = volume;
    def cap = GetAggregationPeriod();
    def errorInAggregation = cap >= AggregationPeriod.WEEK;
    Assert(!errorInAggregation, "timeFrame should be not less than current chart aggregation period");
    def yyyyMmDd = GetYYYYMMDD();
    def periodIndx = yyyyMmDd;
    def isPeriodRolled = CompoundValue(1, periodIndx != periodIndx[1], yes);
    def volumeSum;
    def volumeVwapSum;
    if (isPeriodRolled) {
        volumeSum = v;
        volumeVwapSum = v * src;
    } else {
        volumeSum     = CompoundValue(1, volumeSum[1] + v, v);
        volumeVwapSum = CompoundValue(1, volumeVwapSum[1] + v * src, v * src);
    }
    def price = volumeVwapSum / volumeSum;
    plot return = price;
}
#ma(source, length, type) =>
script ma {
    input source = close;
    input length = 10;
    input type   = "SMA";

    def ma;
    ma = if type == "VWAP" then VVWAP(source) else
     if type == "SMA" then SimpleMovingAvg(source, length) else
     if type == "EMA" then ExpAverage(source, length) else
     if type == "WMA" then WMA(source, length) else
     if type == "HMA" then WMA(2 * WMA(source, length / 2) - WMA(source, length), Round(Sqrt(length)))
       else if IsNaN(ma[1]) then SimpleMovingAvg(source, length) else
             (ma[1] * (length - 1) + source) / length;
    plot result = ma;
}
#_rate(cond) =>
script _rate {
    input cond = 1;
    def o = open; def c = close; def l = low;
    def criteria = if cond == 1 then o <= c else o > c;
    def tw = high - Max(o, c) ;
    def bw = Min(o, c) - l;
    def body = AbsValue(c - o);
    def ret  = 0.5 * (tw + bw + (if criteria then 2 * body else 0)) / (tw + bw + body);
    def rate = if nz(ret) == 0 then 0.5 else ret;
    plot Result = rate;
}
def deltaup   = vo * _rate(1);
def deltadown = vo * _rate(0);
def delta     = if cl >= op then deltaup else -deltadown;
def cumdelta1;
if isNaN(isPeriodRolled) {
    cumdelta1 = TotalSum(delta);
 } else {
      if (isPeriodRolled) {
            cumdelta1 = delta;
    } else {
    cumdelta1  = CompoundValue(1, cumdelta1[1] + delta, delta);
}}

def cumdelta  = cumdelta1;

def o;
def h;
def l;
def c;
def ctl;

if linestyle != linestyle.Line
then {
    o = cumdelta[1];
    h = Max(cumdelta, cumdelta[1]);
    l = Min(cumdelta, cumdelta[1]);
    c = cumdelta;
    ctl = na;
} else {
    o = na;
    h = na;
    l = na;
    c = na;
    ctl = cumdelta;
}
def LineClose = ExpAverage((cumdelta[1] + Max(cumdelta, cumdelta[1]) + Min(cumdelta, cumdelta[1]) + cumdelta) / 4, 5);
def LineOpen = ExpAverage(if IsNaN(LineOpen[1]) then (cumdelta[1] + cumdelta) / 2 else (LineOpen[1] + LineClose[1]) / 2, 5);

plot Line = ctl;
Line.AssignValueColor( if LineClose >= LineOpen then CreateColor(8, 153, 129) else
         if LineClose < LineOpen  then CreateColor(239, 83, 80) else Color.GRAY);
Line.SetLineWeight(2);

##############
def _Close = (o + h + l + c) / 4;
def _Open = if IsNaN(_Open[1]) then (o + c) / 2 else (_Open[1] + _Close[1]) / 2;
def _High = Max(Max(h, _Open), _Close);
def _Low = Min(Min(l, _Open), _Close);

def haClose = if linestyle==linestyle.HeikinAshi then _Close else c;
def haOpen  = if linestyle==linestyle.HeikinAshi then _Open  else o;
def haHigh  = if linestyle==linestyle.HeikinAshi then _High  else h;
def haLow   = if linestyle==linestyle.HeikinAshi then _Low   else l;

def Color = if haClose >= haOpen then 1 else -1;

# Plot UP candle

def UpO;
def UpH;
def UpL;
def UpC;

if Color > 0
then {
    UpO = haOpen ;
    UpH = haHigh ;
    UpL = haLow ;
    UpC = haClose;
} else
{
    UpO = na;
    UpH = na;
    UpL = na;
    UpC = na;
}
# Plot DOWN candle
def DnO;
def DnH;
def DnL;
def DnC;
if Color < 0
then {
    DnO = haOpen ;
    DnH = haHigh ;
    DnL = haLow ;
    DnC = haClose;
} else
{
    DnO = na;
    DnH = na;
    DnL = na;
    DnC = na;
}

# Plot the new Chart
AddChart(high = UpH , low = UpL , open = UpC,  close = UpO,
         type = ChartType.CANDLE, growcolor =  CreateColor(8, 153, 129));

AddChart(high = DnH , low = DnL , open = DnO,  close = DnC,
         type = ChartType.CANDLE, growcolor =  CreateColor(239, 83, 80));

## Plot MA Lines
plot AvgLine = ma(ctl, maLength, maType);
AvgLine.SetPaintingStrategy(PaintingStrategy.LINE);
AvgLine.SetHiding(!ShowMovAvg);

plot HaAvgLine = ma(haClose, maLength, maType);
HaAvgLine.SetPaintingStrategy(PaintingStrategy.LINE);
HaAvgLine.SetHiding(!ShowMovAvg);

### Band
def offs = (1.6185 * StDev(if linestyle != linestyle.Line then haClose else ctl, maLength));
def upBand = (if linestyle != linestyle.Line then HaAvgLine else AvgLine) + offs;
def dnBand = (if linestyle != linestyle.Line then HaAvgLine else AvgLine) - offs;

def BandColor = if (if(linestyle!=linestyle.Line,haClose,ctl)) > upBand then 1 else
                if (if(linestyle!=linestyle.Line,haClose,ctl)) < dnBand then -1 else 0;

plot Oband = upBand;
Oband.AssignValueColor(if BandColor > 0 then Color.GREEN else
                       if BandColor < 0 then Color.RED else CreateColor(100, 181, 246));
Oband.SetHiding(!ShowBand);

plot Sband = dnBand;
Sband.AssignValueColor(if BandColor > 0 then Color.GREEN else
                       if BandColor < 0 then Color.RED else CreateColor(100, 181, 246));
Sband.SetHiding(!ShowBand);

AvgLine.AssignValueColor(if BandColor > 0 then Color.GREEN else
                       if BandColor < 0 then Color.RED else Color.WHITE);
HaAvgLine.AssignValueColor(if BandColor > 0 then Color.GREEN else
                       if BandColor < 0 then Color.RED else Color.WHITE);

### Background
AddCloud(if BackgroundColor then
if (if(linestyle!=linestyle.Line,haClose,ctl)) > Oband then Double.POSITIVE_INFINITY else if
(if linestyle != linestyle.Line then haClose else ctl) < Sband then Double.NEGATIVE_INFINITY else na else na,
if (if linestyle != linestyle.Line then haClose else ctl) > Oband then Double.NEGATIVE_INFINITY else if
(if linestyle != linestyle.Line then haClose else ctl) < Sband then Double.POSITIVE_INFINITY else na, Color.DARK_GREEN, Color.DARK_RED);

### Bar Color
def Exup = if linestyle != linestyle.Line then (Color > 0 and BandColor > 0)
           else (LineClose >= LineOpen and BandColor > 0);
def up   = if linestyle != linestyle.Line then (Color > 0 and BandColor <= 0)
           else (LineClose >= LineOpen and BandColor <= 0) ;
def Exdn = if linestyle != linestyle.Line then (Color < 0 and BandColor < 0)
           else (LineClose < LineOpen and BandColor < 0);
def dn   = if linestyle != linestyle.Line then (Color < 0 and BandColor >= 0)
           else (LineClose < LineOpen and BandColor >= 0);

AssignPriceColor(if BarColor then
                 if Exup then Color.GREEN else
                 if up   then Color.DARK_GREEN else
                 if dn   then Color.DARK_RED else
                 if Exdn then Color.RED else Color.GRAY else Color.CURRENT);

### END
 
k0UCRX1.png


This is Cumulative Delta Volume script based on LonesomeTheBlue script. I added multiple options to identify the trend based on the standard deviation and moving average. You can use it along with your strategy to confirm the entry and exit referring to the delta volume (the difference between buying and selling volume at each price level.

CODE:
CSS:
#study("Cumulative Delta Volume", "CDV")
#https://www.tradingview.com/script/vB1T3EMp-Cumulative-Delta-Volume/
# Created based on LonesomeTheBlue code link above - Sam4Cok @ 08/2022

declare lower;
input linestyle = {default Candle, Line};
input HeikinAshi = yes; #"Heikin Ashi Candles?")
input maType    =  {default "SMMA", "SMA", "HMA", "EMA", "WMA"};
input maLength  = 50;   #"MA Length"
input BarColor = no;
input BackgroundColor = yes;
input ShowMovAvg = yes;
input ShowBand = no;

def na = Double.NaN;

###############
script nz {
    input data         = 0;
    input replacement  = 0;
    def ret_val = if IsNaN(data) then replacement else data;
    plot return = ret_val;
}
#ma(source, length, type) =>
script ma {
    input source = close;
    input length = 0;
    input type   = "SMA";
    def ma;
ma = if type == "SMA" then SimpleMovingAvg(source, length) else
     if type == "EMA" then ExpAverage(source, length) else
     if type == "WMA" then WMA(source, length) else
     if type == "HMA" then WMA(2 * WMA(source, length / 2) - WMA(source, length), Round(Sqrt(length)))
       else if isNaN(ma[1]) then SimpleMovingAvg(source, length) else
             (ma[1] * (length - 1) + source) / length;
    plot result = ma;
}
#_rate(cond) =>
script _rate {
    input cond = 1;
    def criteria = if cond == 1 then open <= close else open > close;
    def tw = high - Max(open, close) ;
    def bw = Min(open, close) - low;
    def body = AbsValue(close - open);
    def ret  = 0.5 * (tw + bw + (if criteria then 2 * body else 0)) / (tw + bw + body);
    def rate = if nz(ret) == 0 then 0.5 else ret;
    plot Result = rate;
}
def deltaup   = volume * _rate(1);
def deltadown = volume * _rate(0);
def delta     = if close >= open then deltaup else -deltadown;
def cumdelta  = TotalSum(delta);

def o;
def h;
def l;
def c;
def ctl;

if linestyle == linestyle.Candle
then {
    o = cumdelta[1];
    h = Max(cumdelta, cumdelta[1]);
    l = Min(cumdelta, cumdelta[1]);
    c = cumdelta;
    ctl = na;
} else {
    o = na;
    h = na;
    l = na;
    c = na;
    ctl = cumdelta;
}
def LineClose = ExpAverage((cumdelta[1] + Max(cumdelta, cumdelta[1]) + Min(cumdelta, cumdelta[1]) + cumdelta) / 4, 5);
def LineOpen = ExpAverage(if IsNaN(LineOpen[1]) then (cumdelta[1] + cumdelta) / 2 else (LineOpen[1] + LineClose[1]) / 2, 5);

plot Line = ctl;
Line.AssignValueColor( if LineClose >= LineOpen then CreateColor(8,153,129) else
                       if LineClose < LineOpen  then CreateColor(239, 83, 80) else Color.GRAY);
Line.SetLineWeight(2);

##############
def _Close = (o + h + l + c) / 4;
def _Open = if IsNaN(_Open[1]) then (o + c) / 2 else (_Open[1] + _Close[1]) / 2;
def _High = Max(Max(h, _Open), _Close);
def _Low = Min(Min(l, _Open), _Close);

def haClose = if HeikinAshi then _Close else c;
def haOpen  = if HeikinAshi then _Open  else o;
def haHigh  = if HeikinAshi then _High  else h;
def haLow   = if HeikinAshi then _Low   else l;

def Color = if haClose >= haOpen then 1 else -1;

# Plot UP candle

def UpO;
def UpH;
def UpL;
def UpC;

if Color > 0
then {
    UpO = haOpen ;
    UpH = haHigh ;
    UpL = haLow ;
    UpC = haClose;
} else
{
    UpO = na;
    UpH = na;
    UpL = na;
    UpC = na;
}
# Plot DOWN candle
def DnO;
def DnH;
def DnL;
def DnC;
if Color < 0
then {
    DnO = haOpen ;
    DnH = haHigh ;
    DnL = haLow ;
    DnC = haClose;
} else
{
    DnO = na;
    DnH = na;
    DnL = na;
    DnC = na;
}

# Plot the new Chart
AddChart(high = UpH , low = UpL , open = UpC,  close = UpO,
         type = ChartType.CANDLE, growcolor =  CreateColor(8,153,129));

AddChart(high = DnH , low = DnL , open = DnO,  close = DnC,
         type = ChartType.CANDLE, growcolor =  CreateColor(239, 83, 80));

## Plot MA Lines
plot AvgLine = ma(ctl, maLength, maType);
AvgLine.SetPaintingStrategy(PaintingStrategy.DASHES);
AvgLine.SetHiding(!ShowMovAvg);

plot HaAvgLine = ma(haClose, maLength, maType);
HaAvgLine.SetPaintingStrategy(PaintingStrategy.DASHES);
HaAvgLine.SetHiding(!ShowMovAvg);

### Band
def offs = (1.6185 * stdev(if linestyle == linestyle.Candle then haClose else ctl, maLength));
def upBand = (if linestyle == linestyle.Candle then HaAvgLine else AvgLine) + offs;
def dnBand = (if linestyle == linestyle.Candle then HaAvgLine else AvgLine) - offs;

def BandColor = if (if linestyle == linestyle.Candle then haClose else ctl) > upBand then 1 else
 if (if linestyle == linestyle.Candle then haClose else ctl) < dnBand then -1 else 0;

plot Oband = upBand;
Oband.AssignValueColor(if BandColor > 0 then Color.GREEN else
                       if BandColor < 0 then Color.RED else CreateColor(100, 181, 246));
Oband.SetHiding(!ShowBand);

plot Sband = dnBand;
sband.AssignValueColor(if BandColor > 0 then Color.GREEN else
                       if BandColor < 0 then Color.RED else CreateColor(100, 181, 246));
Sband.SetHiding(!ShowBand);

AvgLine.AssignValueColor(if BandColor > 0 then Color.GREEN else
                       if BandColor < 0 then Color.RED else Color.WHITE);
HaAvgLine.AssignValueColor(if BandColor > 0 then Color.GREEN else
                       if BandColor < 0 then Color.RED else Color.WHITE);

### Background
addCloud(if BackgroundColor then
if(if linestyle == linestyle.Candle then haClose else ctl) > Oband then Double.POSITIVE_INFINITY else if
(if linestyle == linestyle.Candle then haClose else ctl) < Sband then Double.NEGATIVE_INFINITY else na else na,
if(if linestyle == linestyle.Candle then haClose else ctl) > Oband then Double.NEGATIVE_INFINITY else if
(if linestyle == linestyle.Candle then haClose else ctl) < Sband then Double.POSITIVE_INFINITY else na, Color.DARK_GREEN, Color.DARK_RED);

### Bar Color
def Exup = if linestyle == linestyle.Candle then (color > 0 and BandColor > 0)
           else (LineClose >= LineOpen and BandColor > 0);
def up   = if linestyle == linestyle.Candle then (color > 0 and BandColor <= 0)
           else (LineClose >= LineOpen and BandColor <= 0) ;
def Exdn = if linestyle == linestyle.Candle then (color < 0 and BandColor < 0)
           else (LineClose < LineOpen and BandColor < 0);
def dn   = if linestyle == linestyle.Candle then (color < 0 and BandColor >= 0)
           else (LineClose < LineOpen and BandColor >= 0);

AssignPriceColor(if BarColor then
                 if Exup then Color.GREEN else
                 if up   then CreateColor(0,80,80) else
                 if dn   then CreateColor(117,0,14) else
                 if Exdn then Color.RED else Color.GRAY else Color.CURRENT);

### END
6IFyKgM.png

Hi Sameer Wondering Why the CDV is green on the hourly chart on spy? Is there an error in the calculation?
 
6IFyKgM.png

Hi Sameer Wondering Why the CDV is green on the hourly chart on spy? Is there an error in the calculation?
have you tried below code?
CSS:
#study("Cumulative Delta Volume", "CDV")
#https://www.tradingview.com/script/vB1T3EMp-Cumulative-Delta-Volume/
# Created based on LonesomeTheBlue code above - Sam4Cok @ 08/2022
# Added time reset option, Added VWAP option - Sam4Cok@Samer800 - 11/2022

declare lower;
input linestyle = {default HeikinAshi, Candle, Line};
input timeFrame = {default DAY, WEEK, MONTH, ALL};
input maType    = {SMMA,default VWAP, SMA, HMA, EMA, WMA};
input maLength  = 50;   #"MA Length"
input BarColor = no;
input BackgroundColor = yes;
input ShowMovAvg = yes;
input ShowBand = no;

def na = Double.NaN;
def cl = close; def op = open; def vo = volume;
def yyyyMmDd = GetYYYYMMDD();
def periodIndx;
switch (timeFrame) {
case DAY:
    periodIndx = yyyyMmDd;
case WEEK:
    periodIndx = Floor((DaysFromDate(First(yyyyMmDd)) + GetDayOfWeek(First(yyyyMmDd))) / 7);
case MONTH:
    periodIndx = RoundDown(yyyyMmDd / 100, 0);
case ALL:
    periodIndx = na;
}
def isPeriodRolled = CompoundValue(1, periodIndx != periodIndx[1], yes);
###############
script nz {
    input data         = volume;
    input replacement  = 0;
    def ret_val = if IsNaN(data) then replacement else data;
    plot return = ret_val;
}
# volVWAP
script VVWAP {
    input src = close;
    def timeFrame = AggregationPeriod.DAY;
    def v = volume;
    def cap = GetAggregationPeriod();
    def errorInAggregation = cap >= AggregationPeriod.WEEK;
    Assert(!errorInAggregation, "timeFrame should be not less than current chart aggregation period");
    def yyyyMmDd = GetYYYYMMDD();
    def periodIndx = yyyyMmDd;
    def isPeriodRolled = CompoundValue(1, periodIndx != periodIndx[1], yes);
    def volumeSum;
    def volumeVwapSum;
    if (isPeriodRolled) {
        volumeSum = v;
        volumeVwapSum = v * src;
    } else {
        volumeSum     = CompoundValue(1, volumeSum[1] + v, v);
        volumeVwapSum = CompoundValue(1, volumeVwapSum[1] + v * src, v * src);
    }
    def price = volumeVwapSum / volumeSum;
    plot return = price;
}
#ma(source, length, type) =>
script ma {
    input source = close;
    input length = 10;
    input type   = "SMA";

    def ma;
    ma = if type == "VWAP" then VVWAP(source) else
     if type == "SMA" then SimpleMovingAvg(source, length) else
     if type == "EMA" then ExpAverage(source, length) else
     if type == "WMA" then WMA(source, length) else
     if type == "HMA" then WMA(2 * WMA(source, length / 2) - WMA(source, length), Round(Sqrt(length)))
       else if IsNaN(ma[1]) then SimpleMovingAvg(source, length) else
             (ma[1] * (length - 1) + source) / length;
    plot result = ma;
}
#_rate(cond) =>
script _rate {
    input cond = 1;
    def o = open; def c = close; def l = low;
    def criteria = if cond == 1 then o <= c else o > c;
    def tw = high - Max(o, c) ;
    def bw = Min(o, c) - l;
    def body = AbsValue(c - o);
    def ret  = 0.5 * (tw + bw + (if criteria then 2 * body else 0)) / (tw + bw + body);
    def rate = if nz(ret) == 0 then 0.5 else ret;
    plot Result = rate;
}
def deltaup   = vo * _rate(1);
def deltadown = vo * _rate(0);
def delta     = if cl >= op then deltaup else -deltadown;
def cumdelta1;
if isNaN(isPeriodRolled) {
    cumdelta1 = TotalSum(delta);
 } else {
      if (isPeriodRolled) {
            cumdelta1 = delta;
    } else {
    cumdelta1  = CompoundValue(1, cumdelta1[1] + delta, delta);
}}

def cumdelta  = cumdelta1;

def o;
def h;
def l;
def c;
def ctl;

if linestyle != linestyle.Line
then {
    o = cumdelta[1];
    h = Max(cumdelta, cumdelta[1]);
    l = Min(cumdelta, cumdelta[1]);
    c = cumdelta;
    ctl = na;
} else {
    o = na;
    h = na;
    l = na;
    c = na;
    ctl = cumdelta;
}
def LineClose = ExpAverage((cumdelta[1] + Max(cumdelta, cumdelta[1]) + Min(cumdelta, cumdelta[1]) + cumdelta) / 4, 5);
def LineOpen = ExpAverage(if IsNaN(LineOpen[1]) then (cumdelta[1] + cumdelta) / 2 else (LineOpen[1] + LineClose[1]) / 2, 5);

plot Line = ctl;
Line.AssignValueColor( if LineClose >= LineOpen then CreateColor(8, 153, 129) else
         if LineClose < LineOpen  then CreateColor(239, 83, 80) else Color.GRAY);
Line.SetLineWeight(2);

##############
def _Close = (o + h + l + c) / 4;
def _Open = if IsNaN(_Open[1]) then (o + c) / 2 else (_Open[1] + _Close[1]) / 2;
def _High = Max(Max(h, _Open), _Close);
def _Low = Min(Min(l, _Open), _Close);

def haClose = if linestyle==linestyle.HeikinAshi then _Close else c;
def haOpen  = if linestyle==linestyle.HeikinAshi then _Open  else o;
def haHigh  = if linestyle==linestyle.HeikinAshi then _High  else h;
def haLow   = if linestyle==linestyle.HeikinAshi then _Low   else l;

def Color = if haClose >= haOpen then 1 else -1;

# Plot UP candle

def UpO;
def UpH;
def UpL;
def UpC;

if Color > 0
then {
    UpO = haOpen ;
    UpH = haHigh ;
    UpL = haLow ;
    UpC = haClose;
} else
{
    UpO = na;
    UpH = na;
    UpL = na;
    UpC = na;
}
# Plot DOWN candle
def DnO;
def DnH;
def DnL;
def DnC;
if Color < 0
then {
    DnO = haOpen ;
    DnH = haHigh ;
    DnL = haLow ;
    DnC = haClose;
} else
{
    DnO = na;
    DnH = na;
    DnL = na;
    DnC = na;
}

# Plot the new Chart
AddChart(high = UpH , low = UpL , open = UpC,  close = UpO,
         type = ChartType.CANDLE, growcolor =  CreateColor(8, 153, 129));

AddChart(high = DnH , low = DnL , open = DnO,  close = DnC,
         type = ChartType.CANDLE, growcolor =  CreateColor(239, 83, 80));

## Plot MA Lines
plot AvgLine = ma(ctl, maLength, maType);
AvgLine.SetPaintingStrategy(PaintingStrategy.LINE);
AvgLine.SetHiding(!ShowMovAvg);

plot HaAvgLine = ma(haClose, maLength, maType);
HaAvgLine.SetPaintingStrategy(PaintingStrategy.LINE);
HaAvgLine.SetHiding(!ShowMovAvg);

### Band
def offs = (1.6185 * StDev(if linestyle != linestyle.Line then haClose else ctl, maLength));
def upBand = (if linestyle != linestyle.Line then HaAvgLine else AvgLine) + offs;
def dnBand = (if linestyle != linestyle.Line then HaAvgLine else AvgLine) - offs;

def BandColor = if (if(linestyle!=linestyle.Line,haClose,ctl)) > upBand then 1 else
                if (if(linestyle!=linestyle.Line,haClose,ctl)) < dnBand then -1 else 0;

plot Oband = upBand;
Oband.AssignValueColor(if BandColor > 0 then Color.GREEN else
                       if BandColor < 0 then Color.RED else CreateColor(100, 181, 246));
Oband.SetHiding(!ShowBand);

plot Sband = dnBand;
Sband.AssignValueColor(if BandColor > 0 then Color.GREEN else
                       if BandColor < 0 then Color.RED else CreateColor(100, 181, 246));
Sband.SetHiding(!ShowBand);

AvgLine.AssignValueColor(if BandColor > 0 then Color.GREEN else
                       if BandColor < 0 then Color.RED else Color.WHITE);
HaAvgLine.AssignValueColor(if BandColor > 0 then Color.GREEN else
                       if BandColor < 0 then Color.RED else Color.WHITE);

### Background
AddCloud(if BackgroundColor then
if (if(linestyle!=linestyle.Line,haClose,ctl)) > Oband then Double.POSITIVE_INFINITY else if
(if linestyle != linestyle.Line then haClose else ctl) < Sband then Double.NEGATIVE_INFINITY else na else na,
if (if linestyle != linestyle.Line then haClose else ctl) > Oband then Double.NEGATIVE_INFINITY else if
(if linestyle != linestyle.Line then haClose else ctl) < Sband then Double.POSITIVE_INFINITY else na, Color.DARK_GREEN, Color.DARK_RED);

### Bar Color
def Exup = if linestyle != linestyle.Line then (Color > 0 and BandColor > 0)
           else (LineClose >= LineOpen and BandColor > 0);
def up   = if linestyle != linestyle.Line then (Color > 0 and BandColor <= 0)
           else (LineClose >= LineOpen and BandColor <= 0) ;
def Exdn = if linestyle != linestyle.Line then (Color < 0 and BandColor < 0)
           else (LineClose < LineOpen and BandColor < 0);
def dn   = if linestyle != linestyle.Line then (Color < 0 and BandColor >= 0)
           else (LineClose < LineOpen and BandColor >= 0);

AssignPriceColor(if BarColor then
                 if Exup then Color.GREEN else
                 if up   then Color.DARK_GREEN else
                 if dn   then Color.DARK_RED else
                 if Exdn then Color.RED else Color.GRAY else Color.CURRENT);

### END
 
have you tried below code?
CSS:
#study("Cumulative Delta Volume", "CDV")
#https://www.tradingview.com/script/vB1T3EMp-Cumulative-Delta-Volume/
# Created based on LonesomeTheBlue code above - Sam4Cok @ 08/2022
# Added time reset option, Added VWAP option - Sam4Cok@Samer800 - 11/2022

declare lower;
input linestyle = {default HeikinAshi, Candle, Line};
input timeFrame = {default DAY, WEEK, MONTH, ALL};
input maType    = {SMMA,default VWAP, SMA, HMA, EMA, WMA};
input maLength  = 50;   #"MA Length"
input BarColor = no;
input BackgroundColor = yes;
input ShowMovAvg = yes;
input ShowBand = no;

def na = Double.NaN;
def cl = close; def op = open; def vo = volume;
def yyyyMmDd = GetYYYYMMDD();
def periodIndx;
switch (timeFrame) {
case DAY:
    periodIndx = yyyyMmDd;
case WEEK:
    periodIndx = Floor((DaysFromDate(First(yyyyMmDd)) + GetDayOfWeek(First(yyyyMmDd))) / 7);
case MONTH:
    periodIndx = RoundDown(yyyyMmDd / 100, 0);
case ALL:
    periodIndx = na;
}
def isPeriodRolled = CompoundValue(1, periodIndx != periodIndx[1], yes);
###############
script nz {
    input data         = volume;
    input replacement  = 0;
    def ret_val = if IsNaN(data) then replacement else data;
    plot return = ret_val;
}
# volVWAP
script VVWAP {
    input src = close;
    def timeFrame = AggregationPeriod.DAY;
    def v = volume;
    def cap = GetAggregationPeriod();
    def errorInAggregation = cap >= AggregationPeriod.WEEK;
    Assert(!errorInAggregation, "timeFrame should be not less than current chart aggregation period");
    def yyyyMmDd = GetYYYYMMDD();
    def periodIndx = yyyyMmDd;
    def isPeriodRolled = CompoundValue(1, periodIndx != periodIndx[1], yes);
    def volumeSum;
    def volumeVwapSum;
    if (isPeriodRolled) {
        volumeSum = v;
        volumeVwapSum = v * src;
    } else {
        volumeSum     = CompoundValue(1, volumeSum[1] + v, v);
        volumeVwapSum = CompoundValue(1, volumeVwapSum[1] + v * src, v * src);
    }
    def price = volumeVwapSum / volumeSum;
    plot return = price;
}
#ma(source, length, type) =>
script ma {
    input source = close;
    input length = 10;
    input type   = "SMA";

    def ma;
    ma = if type == "VWAP" then VVWAP(source) else
     if type == "SMA" then SimpleMovingAvg(source, length) else
     if type == "EMA" then ExpAverage(source, length) else
     if type == "WMA" then WMA(source, length) else
     if type == "HMA" then WMA(2 * WMA(source, length / 2) - WMA(source, length), Round(Sqrt(length)))
       else if IsNaN(ma[1]) then SimpleMovingAvg(source, length) else
             (ma[1] * (length - 1) + source) / length;
    plot result = ma;
}
#_rate(cond) =>
script _rate {
    input cond = 1;
    def o = open; def c = close; def l = low;
    def criteria = if cond == 1 then o <= c else o > c;
    def tw = high - Max(o, c) ;
    def bw = Min(o, c) - l;
    def body = AbsValue(c - o);
    def ret  = 0.5 * (tw + bw + (if criteria then 2 * body else 0)) / (tw + bw + body);
    def rate = if nz(ret) == 0 then 0.5 else ret;
    plot Result = rate;
}
def deltaup   = vo * _rate(1);
def deltadown = vo * _rate(0);
def delta     = if cl >= op then deltaup else -deltadown;
def cumdelta1;
if isNaN(isPeriodRolled) {
    cumdelta1 = TotalSum(delta);
 } else {
      if (isPeriodRolled) {
            cumdelta1 = delta;
    } else {
    cumdelta1  = CompoundValue(1, cumdelta1[1] + delta, delta);
}}

def cumdelta  = cumdelta1;

def o;
def h;
def l;
def c;
def ctl;

if linestyle != linestyle.Line
then {
    o = cumdelta[1];
    h = Max(cumdelta, cumdelta[1]);
    l = Min(cumdelta, cumdelta[1]);
    c = cumdelta;
    ctl = na;
} else {
    o = na;
    h = na;
    l = na;
    c = na;
    ctl = cumdelta;
}
def LineClose = ExpAverage((cumdelta[1] + Max(cumdelta, cumdelta[1]) + Min(cumdelta, cumdelta[1]) + cumdelta) / 4, 5);
def LineOpen = ExpAverage(if IsNaN(LineOpen[1]) then (cumdelta[1] + cumdelta) / 2 else (LineOpen[1] + LineClose[1]) / 2, 5);

plot Line = ctl;
Line.AssignValueColor( if LineClose >= LineOpen then CreateColor(8, 153, 129) else
         if LineClose < LineOpen  then CreateColor(239, 83, 80) else Color.GRAY);
Line.SetLineWeight(2);

##############
def _Close = (o + h + l + c) / 4;
def _Open = if IsNaN(_Open[1]) then (o + c) / 2 else (_Open[1] + _Close[1]) / 2;
def _High = Max(Max(h, _Open), _Close);
def _Low = Min(Min(l, _Open), _Close);

def haClose = if linestyle==linestyle.HeikinAshi then _Close else c;
def haOpen  = if linestyle==linestyle.HeikinAshi then _Open  else o;
def haHigh  = if linestyle==linestyle.HeikinAshi then _High  else h;
def haLow   = if linestyle==linestyle.HeikinAshi then _Low   else l;

def Color = if haClose >= haOpen then 1 else -1;

# Plot UP candle

def UpO;
def UpH;
def UpL;
def UpC;

if Color > 0
then {
    UpO = haOpen ;
    UpH = haHigh ;
    UpL = haLow ;
    UpC = haClose;
} else
{
    UpO = na;
    UpH = na;
    UpL = na;
    UpC = na;
}
# Plot DOWN candle
def DnO;
def DnH;
def DnL;
def DnC;
if Color < 0
then {
    DnO = haOpen ;
    DnH = haHigh ;
    DnL = haLow ;
    DnC = haClose;
} else
{
    DnO = na;
    DnH = na;
    DnL = na;
    DnC = na;
}

# Plot the new Chart
AddChart(high = UpH , low = UpL , open = UpC,  close = UpO,
         type = ChartType.CANDLE, growcolor =  CreateColor(8, 153, 129));

AddChart(high = DnH , low = DnL , open = DnO,  close = DnC,
         type = ChartType.CANDLE, growcolor =  CreateColor(239, 83, 80));

## Plot MA Lines
plot AvgLine = ma(ctl, maLength, maType);
AvgLine.SetPaintingStrategy(PaintingStrategy.LINE);
AvgLine.SetHiding(!ShowMovAvg);

plot HaAvgLine = ma(haClose, maLength, maType);
HaAvgLine.SetPaintingStrategy(PaintingStrategy.LINE);
HaAvgLine.SetHiding(!ShowMovAvg);

### Band
def offs = (1.6185 * StDev(if linestyle != linestyle.Line then haClose else ctl, maLength));
def upBand = (if linestyle != linestyle.Line then HaAvgLine else AvgLine) + offs;
def dnBand = (if linestyle != linestyle.Line then HaAvgLine else AvgLine) - offs;

def BandColor = if (if(linestyle!=linestyle.Line,haClose,ctl)) > upBand then 1 else
                if (if(linestyle!=linestyle.Line,haClose,ctl)) < dnBand then -1 else 0;

plot Oband = upBand;
Oband.AssignValueColor(if BandColor > 0 then Color.GREEN else
                       if BandColor < 0 then Color.RED else CreateColor(100, 181, 246));
Oband.SetHiding(!ShowBand);

plot Sband = dnBand;
Sband.AssignValueColor(if BandColor > 0 then Color.GREEN else
                       if BandColor < 0 then Color.RED else CreateColor(100, 181, 246));
Sband.SetHiding(!ShowBand);

AvgLine.AssignValueColor(if BandColor > 0 then Color.GREEN else
                       if BandColor < 0 then Color.RED else Color.WHITE);
HaAvgLine.AssignValueColor(if BandColor > 0 then Color.GREEN else
                       if BandColor < 0 then Color.RED else Color.WHITE);

### Background
AddCloud(if BackgroundColor then
if (if(linestyle!=linestyle.Line,haClose,ctl)) > Oband then Double.POSITIVE_INFINITY else if
(if linestyle != linestyle.Line then haClose else ctl) < Sband then Double.NEGATIVE_INFINITY else na else na,
if (if linestyle != linestyle.Line then haClose else ctl) > Oband then Double.NEGATIVE_INFINITY else if
(if linestyle != linestyle.Line then haClose else ctl) < Sband then Double.POSITIVE_INFINITY else na, Color.DARK_GREEN, Color.DARK_RED);

### Bar Color
def Exup = if linestyle != linestyle.Line then (Color > 0 and BandColor > 0)
           else (LineClose >= LineOpen and BandColor > 0);
def up   = if linestyle != linestyle.Line then (Color > 0 and BandColor <= 0)
           else (LineClose >= LineOpen and BandColor <= 0) ;
def Exdn = if linestyle != linestyle.Line then (Color < 0 and BandColor < 0)
           else (LineClose < LineOpen and BandColor < 0);
def dn   = if linestyle != linestyle.Line then (Color < 0 and BandColor >= 0)
           else (LineClose < LineOpen and BandColor >= 0);

AssignPriceColor(if BarColor then
                 if Exup then Color.GREEN else
                 if up   then Color.DARK_GREEN else
                 if dn   then Color.DARK_RED else
                 if Exdn then Color.RED else Color.GRAY else Color.CURRENT);

### END
Excellent work Samer800 Thank You
 

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
426 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top