-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCumVolDelta.tosts
More file actions
200 lines (172 loc) · 6.47 KB
/
CumVolDelta.tosts
File metadata and controls
200 lines (172 loc) · 6.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#study("Cumulative Delta Volume", "CDV")
# https://www.tradingview.com/script/vB1T3EMp-Cumulative-Delta-Volume/
# https://usethinkscript.com/threads/cumulative-delta-volume-pressure-mod-by-sam4cok-for-thinkorswim.12408/
# 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