Convert Tradingview Cumulative Volume Delta Indicator

ZoSo

New member
Hi all,

Could anyone assist with converting this TradingView script to ToS? I've found this to be an extremely useful indicator and having it baked in to ToS would allow me to close TV. I've found some KILLER studies on here that are similar (you guys are amazing), but I'd love to see how they compare.
https://www.tradingview.com/script/vB1T3EMp-Cumulative-Delta-Volume/
Code:
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © LonesomeTheBlue

//@version=5
indicator('Cumulative Delta Volume', 'CDV')
linestyle = input.string(defval='Candle', title='Style', options=['Candle', 'Line'])
hacandle = input(defval=true, title='Heikin Ashi Candles?')
showma1 = input.bool(defval=false, title='SMA 1', inline='ma1')
ma1len = input.int(defval=50, title='', minval=1, inline='ma1')
ma1col = input.color(defval=color.lime, title='', inline='ma1')
showma2 = input.bool(defval=false, title='SMA 2', inline='ma2')
ma2len = input.int(defval=200, title='', minval=1, inline='ma2')
ma2col = input.color(defval=color.red, title='', inline='ma2')
showema1 = input.bool(defval=false, title='EMA 1', inline='ema1')
ema1len = input.int(defval=50, title='', minval=1, inline='ema1')
ema1col = input.color(defval=color.lime, title='', inline='ema1')
showema2 = input.bool(defval=false, title='EMA 2', inline='ema2')
ema2len = input.int(defval=200, title='', minval=1, inline='ema2')
ema2col = input.color(defval=color.red, title='', inline='ema2')
colorup = input.color(defval=color.lime, title='Body', inline='bcol')
colordown = input.color(defval=color.red, title='', inline='bcol')
bcolup = input.color(defval=#74e05e, title='Border', inline='bocol')
bcoldown = input.color(defval=#ffad7d, title='', inline='bocol')
wcolup = input.color(defval=#b5b5b8, title='Wicks', inline='wcol')
wcoldown = input.color(defval=#b5b5b8, title='', inline='wcol')

tw = high - math.max(open, close)
bw = math.min(open, close) - low
body = math.abs(close - open)

_rate(cond) =>
    ret = 0.5 * (tw + bw + (cond ? 2 * body : 0)) / (tw + bw + body)
    ret := nz(ret) == 0 ? 0.5 : ret
    ret

deltaup = volume * _rate(open <= close)
deltadown = volume * _rate(open > close)
delta = close >= open ? deltaup : -deltadown
cumdelta = ta.cum(delta)
float ctl = na
float o = na
float h = na
float l = na
float c = na
if linestyle == 'Candle'
    o := cumdelta[1]
    h := math.max(cumdelta, cumdelta[1])
    l := math.min(cumdelta, cumdelta[1])
    c := cumdelta
    ctl
else
    ctl := cumdelta
    ctl

plot(ctl, title='CDV Line', color=color.new(color.blue, 0), linewidth=2)

float haclose = na
float haopen = na
float hahigh = na
float halow = na
haclose := (o + h + l + c) / 4
haopen := na(haopen[1]) ? (o + c) / 2 : (haopen[1] + haclose[1]) / 2
hahigh := math.max(h, math.max(haopen, haclose))
halow := math.min(l, math.min(haopen, haclose))

c_ = hacandle ? haclose : c
o_ = hacandle ? haopen : o
h_ = hacandle ? hahigh : h
l_ = hacandle ? halow : l

plotcandle(o_, h_, l_, c_, title='CDV Candles', color=o_ <= c_ ? colorup : colordown, bordercolor=o_ <= c_ ? bcolup : bcoldown, wickcolor=o_ <= c_ ? bcolup : bcoldown)

plot(showma1 and linestyle == 'Candle' ? ta.sma(c_, ma1len) : na, title='SMA 1', color=ma1col)
plot(showma2 and linestyle == 'Candle' ? ta.sma(c_, ma2len) : na, title='SMA 2', color=ma2col)
plot(showema1 and linestyle == 'Candle' ? ta.ema(c_, ema1len) : na, title='EMA 1', color=ema1col)
plot(showema2 and linestyle == 'Candle' ? ta.ema(c_, ema2len) : na, title='EMA 2', color=ema2col)



Thank you!!
 
Last edited by a moderator:
Solution
I have been testing this one as well on TV and i like it, i think it would be useful to have on TOS so i hope someone can give the conversion a try.
Try this

Code:
#Heikan Ashi formula candles using CDV Trading View study:
#zoyd 05/22
#Addchart does not plot when extended hours are shown

declare lower;
def tw = high - Max(open, close);
def bw = Min(open, close) - low;
def body = AbsValue(close - open);

def v = volume;
def deltaup = if (open <= close) then v * 0.5 * (tw + bw + 2 * body) / (tw + bw + body) else v * 0.5 * (tw + bw) / (tw + bw + body);
def deltadown = if (open > close) then v * 0.5 * (tw + bw + 2 * body) / (tw + bw + body) else v * 0.5 * (tw + bw) / (tw + bw + body);
def delta = if (close >= open) then deltaup else...

zeek

Active member
2019 Donor
Hi all,

Could anyone assist with converting this TradingView script to ToS? I've found this to be an extremely useful indicator and having it baked in to ToS would allow me to close TV. I've found some KILLER studies on here that are similar (you guys are amazing), but I'd love to see how they compare.
https://www.tradingview.com/script/vB1T3EMp-Cumulative-Delta-Volume/
I have been testing this one as well on TV and i like it, i think it would be useful to have on TOS so i hope someone can give the conversion a try.
 

zoyd

New member
I have been testing this one as well on TV and i like it, i think it would be useful to have on TOS so i hope someone can give the conversion a try.
Try this

Code:
#Heikan Ashi formula candles using CDV Trading View study:
#zoyd 05/22
#Addchart does not plot when extended hours are shown

declare lower;
def tw = high - Max(open, close);
def bw = Min(open, close) - low;
def body = AbsValue(close - open);

def v = volume;
def deltaup = if (open <= close) then v * 0.5 * (tw + bw + 2 * body) / (tw + bw + body) else v * 0.5 * (tw + bw) / (tw + bw + body);
def deltadown = if (open > close) then v * 0.5 * (tw + bw + 2 * body) / (tw + bw + body) else v * 0.5 * (tw + bw) / (tw + bw + body);
def delta = if (close >= open) then deltaup else -1 * deltadown;
def cumdelta = TotalSum(delta);

def o = cumdelta[1];
def h = Max(cumdelta, cumdelta[1]);
def l = Min(cumdelta, cumdelta[1]);
def c = cumdelta;

def haclose = (o + h + l + c) / 4;
rec haopen0 = CompoundValue(1, (haopen0[1] + haclose[1]) / 2, haclose);
plot haopen = haopen0;
haopen.sethiding(1);
def hahigh = Max(h, Max(haopen, haclose));
def halo = Min(l, Min(haopen, haclose));
def hacloseup = if haclose > haopen then haclose else double.nan;
def haclosedn = if haclose < haopen then haclose else double.nan;
AddChart(high = hahigh, low = halo, open = haopen, close = hacloseup, ChartType.CANDLE, Color.UPTICK);
AddChart(high = hahigh, low = halo, open = haopen, close = haclosedn, ChartType.CANDLE, Color.DOWNTICK);
 
Last edited by a moderator:
Solution

drasp

Member
Try this

Code:
#Heikan Ashi formula candles using CDV Trading View study:
#zoyd 05/22
#Addchart does not plot when extended hours are shown

declare lower;
def tw = high - Max(open, close);
def bw = Min(open, close) - low;
def body = AbsValue(close - open);

def v = volume;
def deltaup = if (open <= close) then v * 0.5 * (tw + bw + 2 * body) / (tw + bw + body) else v * 0.5 * (tw + bw) / (tw + bw + body);
def deltadown = if (open > close) then v * 0.5 * (tw + bw + 2 * body) / (tw + bw + body) else v * 0.5 * (tw + bw) / (tw + bw + body);
def delta = if (close >= open) then deltaup else -1 * deltadown;
def cumdelta = TotalSum(delta);

def o = cumdelta[1];
def h = Max(cumdelta, cumdelta[1]);
def l = Min(cumdelta, cumdelta[1]);
def c = cumdelta;

def haclose = (o + h + l + c) / 4;
rec haopen0 = CompoundValue(1, (haopen0[1] + haclose[1]) / 2, haclose);
plot haopen = haopen0;
haopen.sethiding(1);
def hahigh = Max(h, Max(haopen, haclose));
def halo = Min(l, Min(haopen, haclose));
def hacloseup = if haclose > haopen then haclose else double.nan;
def haclosedn = if haclose < haopen then haclose else double.nan;
AddChart(high = hahigh, low = halo, open = haopen, close = hacloseup, ChartType.CANDLE, Color.UPTICK);
AddChart(high = hahigh, low = halo, open = haopen, close = haclosedn, ChartType.CANDLE, Color.DOWNTICK);
Can this be made into normal candles and not HA?
 

MLlalala

Member
Could anyone translate the tradingview cumulative volume script? This seem really good

//@version=4
study("Cumulative Delta Volume", "CDV")
linestyle = input(defval = 'Candle', title = "Style", options = ['Candle', 'Line'])
hacandle = input(defval = true, title = "Heikin Ashi Candles?")
showma1 = input(defval = false, title = "SMA 1", inline = "ma1")
ma1len = input(defval = 50, title = "", minval = 1, inline = "ma1")
ma1col = input(defval = color.lime, title = "", inline = "ma1")
showma2 = input(defval = false, title = "SMA 2", inline = "ma2")
ma2len = input(defval = 200, title = "", minval = 1, inline = "ma2")
ma2col = input(defval = color.red, title = "", inline = "ma2")
showema1 = input(defval = false, title = "EMA 1", inline = "ema1")
ema1len = input(defval = 50, title = "", minval = 1, inline = "ema1")
ema1col = input(defval = color.lime, title = "", inline = "ema1")
showema2 = input(defval = false, title = "EMA 2", inline = "ema2")
ema2len = input(defval = 200, title = "", minval = 1, inline = "ema2")
ema2col = input(defval = color.red, title = "", inline = "ema2")
colorup = input(defval = color.lime, title = "Body", inline = "bcol")
colordown = input(defval = color.red, title = "", inline = "bcol")
bcolup = input(defval = #74e05e, title = "Border", inline = "bocol")
bcoldown = input(defval = #ffad7d, title = "", inline = "bocol")
wcolup = input(defval = #b5b5b8, title = "Wicks", inline = "wcol")
wcoldown = input(defval = #b5b5b8, title = "", inline = "wcol")

tw = high - max(open, close)
bw = min(open, close) - low
body = abs(close - open)

_rate(cond) =>
ret = 0.5 * (tw + bw + (cond ? 2 * body : 0)) / (tw + bw + body)
ret := nz(ret) == 0 ? 0.5 : ret
ret

deltaup = volume * _rate(open <= close)
deltadown = volume * _rate(open > close)
delta = close >= open ? deltaup : -deltadown
cumdelta = cum(delta)
float ctl = na
float o = na
float h = na
float l = na
float c = na
if linestyle == 'Candle'
o := cumdelta[1]
h := max(cumdelta, cumdelta[1])
l := min(cumdelta, cumdelta[1])
c := cumdelta
ctl
else
ctl := cumdelta

plot(ctl, title = "CDV Line", color = color.blue, linewidth = 2)

float haclose = na
float haopen = na
float hahigh = na
float halow = na
haclose := (o + h + l + c) / 4
haopen := na(haopen[1]) ? (o + c) / 2 : (haopen[1] + haclose[1]) / 2
hahigh := max(h, max(haopen, haclose))
halow := min(l, min(haopen, haclose))

c_ = hacandle ? haclose : c
o_ = hacandle ? haopen : o
h_ = hacandle ? hahigh : h
l_ = hacandle ? halow : l

plotcandle(o_, h_, l_, c_, title='CDV Candles', color = o_ <= c_ ? colorup : colordown, bordercolor = o_ <= c_ ? bcolup : bcoldown, wickcolor = o_ <= c_ ? bcolup : bcoldown)

plot(showma1 and linestyle == "Candle" ? sma(c_, ma1len) : na, title = "SMA 1", color = ma1col)
plot(showma2 and linestyle == "Candle" ? sma(c_, ma2len) : na, title = "SMA 2", color = ma2col)
plot(showema1 and linestyle == "Candle" ? ema(c_, ema1len) : na, title = "EMA 1", color = ema1col)
plot(showema2 and linestyle == "Candle" ? ema(c_, ema2len) : na, title = "EMA 2", color = ema2col)
 

MerryDay

Administrative
Staff member
Staff
VIP
Lifetime
Could anyone translate the tradingview cumulative volume script? This seem really good

//@version=4
study("Cumulative Delta Volume", "CDV")
linestyle = input(defval = 'Candle', title = "Style", options = ['Candle', 'Line'])
hacandle = input(defval = true, title = "Heikin Ashi Candles?")
showma1 = input(defval = false, title = "SMA 1", inline = "ma1")
ma1len = input(defval = 50, title = "", minval = 1, inline = "ma1")
ma1col = input(defval = color.lime, title = "", inline = "ma1")
showma2 = input(defval = false, title = "SMA 2", inline = "ma2")
ma2len = input(defval = 200, title = "", minval = 1, inline = "ma2")
ma2col = input(defval = color.red, title = "", inline = "ma2")
showema1 = input(defval = false, title = "EMA 1", inline = "ema1")
ema1len = input(defval = 50, title = "", minval = 1, inline = "ema1")
ema1col = input(defval = color.lime, title = "", inline = "ema1")
showema2 = input(defval = false, title = "EMA 2", inline = "ema2")
ema2len = input(defval = 200, title = "", minval = 1, inline = "ema2")
ema2col = input(defval = color.red, title = "", inline = "ema2")
colorup = input(defval = color.lime, title = "Body", inline = "bcol")
colordown = input(defval = color.red, title = "", inline = "bcol")
bcolup = input(defval = #74e05e, title = "Border", inline = "bocol")
bcoldown = input(defval = #ffad7d, title = "", inline = "bocol")
wcolup = input(defval = #b5b5b8, title = "Wicks", inline = "wcol")
wcoldown = input(defval = #b5b5b8, title = "", inline = "wcol")

tw = high - max(open, close)
bw = min(open, close) - low
body = abs(close - open)

_rate(cond) =>
ret = 0.5 * (tw + bw + (cond ? 2 * body : 0)) / (tw + bw + body)
ret := nz(ret) == 0 ? 0.5 : ret
ret

deltaup = volume * _rate(open <= close)
deltadown = volume * _rate(open > close)
delta = close >= open ? deltaup : -deltadown
cumdelta = cum(delta)
float ctl = na
float o = na
float h = na
float l = na
float c = na
if linestyle == 'Candle'
o := cumdelta[1]
h := max(cumdelta, cumdelta[1])
l := min(cumdelta, cumdelta[1])
c := cumdelta
ctl
else
ctl := cumdelta

plot(ctl, title = "CDV Line", color = color.blue, linewidth = 2)

float haclose = na
float haopen = na
float hahigh = na
float halow = na
haclose := (o + h + l + c) / 4
haopen := na(haopen[1]) ? (o + c) / 2 : (haopen[1] + haclose[1]) / 2
hahigh := max(h, max(haopen, haclose))
halow := min(l, min(haopen, haclose))

c_ = hacandle ? haclose : c
o_ = hacandle ? haopen : o
h_ = hacandle ? hahigh : h
l_ = hacandle ? halow : l

plotcandle(o_, h_, l_, c_, title='CDV Candles', color = o_ <= c_ ? colorup : colordown, bordercolor = o_ <= c_ ? bcolup : bcoldown, wickcolor = o_ <= c_ ? bcolup : bcoldown)

plot(showma1 and linestyle == "Candle" ? sma(c_, ma1len) : na, title = "SMA 1", color = ma1col)
plot(showma2 and linestyle == "Candle" ? sma(c_, ma2len) : na, title = "SMA 2", color = ma2col)
plot(showema1 and linestyle == "Candle" ? ema(c_, ema1len) : na, title = "EMA 1", color = ema1col)
plot(showema2 and linestyle == "Candle" ? ema(c_, ema2len) : na, title = "EMA 2", color = ema2col)
https://usethinkscript.com/threads/...tive-volume-delta-indicator.11361/#post-98032
 

samer800

New member
Can this be made into normal candles and not HA?
below added HA on/Of with SAM/EMA - best effort :)

#Heikan Ashi formula candles using CDV Trading View study:
#zoyd 05/22
#Addchart does not plot when extended hours are shown
# Added SMA/EMA + Heikan Ashi bar (Yes/No) ++ SAM4COM

declare lower;

input HA_Candles = yes;
input ma1len = 50;
input showma1 = no;
input ma2len = 200;
input showma2 = no;
input ema1len = 50;
input showema1 = no;
input ema2len = 200;
input showema2 = no;

def tw = high - Max(open, close);
def bw = Min(open, close) - low;
def body = AbsValue(close - open);

def v = volume;
def deltaup = if (open <= close) then v * 0.5 * (tw + bw + 2 * body) / (tw + bw + body) else v * 0.5 * (tw + bw) / (tw + bw + body);
def deltadown = if (open > close) then v * 0.5 * (tw + bw + 2 * body) / (tw + bw + body) else v * 0.5 * (tw + bw) / (tw + bw + body);
def delta = if (close >= open) then deltaup else -1 * deltadown;
def cumdelta = TotalSum(delta);

def o = cumdelta[1];
def h = Max(cumdelta, cumdelta[1]);
def l = Min(cumdelta, cumdelta[1]);
def c = cumdelta;

def haclose = (o + h + l + c) / 4;
rec haopen0 = CompoundValue(1, (haopen0[1] + haclose[1]) / 2, haclose);
plot haopen = haopen0;
haopen.sethiding(1);
haopen.EnableApproximation ();
def hahigh = Max(h, Max(haopen, haclose));
def halo = Min(l, Min(haopen, haclose));

def hacloseup = if haclose > haopen then haclose else double.nan;
def haclosedn = if haclose < haopen then haclose else double.nan;
def closeup = if close > open then c else double.nan;
def closedn = if close < open then c else double.nan;

AddChart(high = if HA_Candles then hahigh else h, low = if HA_Candles then halo else l, open = if HA_Candles then haopen else o, close = if HA_Candles then hacloseup else closeup, ChartType.CANDLE, Color.UPTICK);

AddChart(high = if HA_Candles then hahigh else h, low = if HA_Candles then halo else l, open = if HA_Candles then haopen else o, close = if HA_Candles then haclosedn else closedn, ChartType.CANDLE, Color.DOWNTICK);

def c_ = if HA_Candles then haclose else c;

plot SMA_1 = if showma1 then SimpleMovingAvg(c_,ma1len) else Double.NaN;
plot SMA_2 = if showma2 then SimpleMovingAvg(c_,ma2len)else Double.NaN;
plot EMA_1 = if showema1 then ExpAverage(c_,ema1len)else Double.NaN;
plot EMA_2 = if showema2 then ExpAverage(c_,ema2len)else Double.NaN;

SMA_1.setDefaultColor(color.lime);
SMA_2.setDefaultColor(color.red);
EMA_1.setDefaultColor(color.lime);
EMA_2.setDefaultColor(color.red);
##End
 

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.
Top