Cumulative Delta Volume Pressure - Mod by Sam4Cok for ThinkOrSwim

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

What doe time frame do and or change?
Changing that option didn't seem to do anything. I'll just leave it.
 
Last edited by a moderator:
Changing that option didn't seem to do anything. I'll just leave it.
change the the time frame will reset the calculation period. if you select DAY, the volume calculation will be based on daily, and if you select monthly, the calculation will be on monthly and so on. the difference can be notices when your chart on higher timeframe.
trust this clarify.
 

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
374 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