/
volume.jl
204 lines (149 loc) · 4.57 KB
/
volume.jl
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
200
201
202
203
204
@doc raw"""
obv(ohlcv; price = :Close, v = :Volume)
On Balance Volume
# Formula
```math
\begin{align*}
\text{OBV}_t = \text{OBV_{t - 1} +
\begin{cases}
\text{volume} & \text{if} P^\text{Close}_t > P^\text{Close}_{t-1} \\
0 & \text{if} P^\text{Close}_t = P^\text{Close}_{t-1} \\
-\text{volume} & \text{if} P^\text{Close}_t < P^\text{Close}_{t-1}
\end{cases}
\end{align*}
```
"""
function obv(ohlcv::TimeArray; price = :Close, v = :Volume)
ret = percentchange(ohlcv[price])
vol = zeros(length(ohlcv))
_vol_values = values(ohlcv[v])
_ret_values = values(ret)
vol[1] = _vol_values[1]
for i=2:length(ohlcv)
if _ret_values[i-1] >= 0
vol[i] += _vol_values[i]
else _ret_values[i-1] < 0
vol[i] -= _vol_values[i]
end
end
TimeArray(timestamp(ohlcv), reshape(nancumsum(vol), (length(timestamp(ohlcv)), 1)), [:obv], meta(ohlcv))
end
"""
obv_mean(ohlcv; price = :Close, v = :Volume)
On Balance Volume Mean
"""
obv_mean(ohlcv::TimeArray, n::Integer = 10; price = :Close, v = :Volume) =
moving(nanmean, obv(ohlcv, price = price, v = v), n)
@doc raw"""
vwap(ohlcv, n = 10; price = :Close, v = :Volume)
Volume Weight-Adjusted Price
# Formula
```math
P = \frac{\sum_j P_j V_j}{\sum_j V_j}
```
"""
function vwap(ohlcv::TimeArray, n::Integer = 10; price = :Close, v = :Volume)
p = ohlcv[price]
q = ohlcv[v]
∑PQ = moving(nansum, p .* q, n)
∑Q = moving(nansum, q, n)
val = ∑PQ ./ ∑Q
TimeArray(timestamp(val), values(val), [:vwap], meta(ohlcv))
end
function advance_decline(x)
#code here
end
function mcclellan_summation(x)
#code here
end
function williams_ad(x)
#code here
end
@doc raw"""
adl(ohlcv; h = :High, l = :Low, c = :Close, v = :Volume)
Accumulation/Distribution Line
Developed by Marc Chaikin.
# Formula
```math
ADL_t = ADL_{t-1} +
'frac{(Close_t - Low_t) - (High_t - Close_t)}{High_t - Low_t}'
'times Volume_t'
```
# References
- [StockCharts]
(http://stockcharts.com/school/doku.php?id=chart_school:technical_indicators:accumulation_distribution_line)
"""
function adl(ohlcv::TimeArray; h = :High, l = :Low, c = :Close, v = :Volume)
_h = ohlcv[h]
_l = ohlcv[l]
_c = ohlcv[c]
_v = ohlcv[v]
flow_facor = ((_c .- _l) .- (_h .- _c)) ./ (_h .- _l)
flow_vol = flow_facor .* _v
_flowvol_values = values(flow_vol)
vals = similar(_flowvol_values)
vals[1] = isnan(_flowvol_values[1]) ? 0.0 : _flowvol_values[1]
for i ∈ 2:length(_flowvol_values)
vals[i] = vals[i-1] + (isnan(_flowvol_values[i]) ? 0.0 : _flowvol_values[i])
end
TimeArray(timestamp(ohlcv), vals, [:adl], meta(ohlcv))
end
"""
Chaikin Money Flow
"""
function chaikinmoneyflow(ohlcv::TimeArray, n::Integer = 10;
h = :High, l = :Low, c = :Close, v = :Volume)
_h = ohlcv[h]
_l = ohlcv[l]
_c = ohlcv[c]
_v = ohlcv[v]
flow_facor = ((_c .- _l) .- (_h .- _c)) ./ (_h .- _l)
flow_vol = flow_facor .* _v
cmf = rename(moving(nansum, flow_vol, n) ./ moving(nansum, _v, n), :cmf)
end
"""
Force Index
It illustrates how strong the actual buying or selling pressure is. High
positive values mean there is a strong rising trend, and low values signify
a strong downward trend.
# References
- [StockCharts]
(http://stockcharts.com/school/doku.php?id=chart_school:technical_indicators:force_index)
"""
function forceindex(ohlcv::TimeArray, n::Integer = 10; c = :Close, v = :Volume)
_c = ohlcv[c]
_v = ohlcv[v]
fi = rename(diff(_c, differences = n) .* diff(_v, differences = n), :fi)
end
"""
Ease of Movement
It relate an asset's price change to its volume and is particularly useful
for assessing the strength of a trend.
# References
- [Wikipedia]
(https://en.wikipedia.org/wiki/Ease_of_movement)
"""
function easeofmovement(ohlcv::TimeArray, n::Integer = 10; v = :Volume, h = :High, l = :Low)
_h = ohlcv[h]
_l = ohlcv[l]
_v = ohlcv[v]
_emv = (diff(_h) .+ diff(_l)) .* (_h .- _l) ./ (2 .* (_v./100000))
emv = rename(moving(nanmean, _emv, n), :emv)
end
"""
Volume Price Trend
Is based on a running cumulative volume that adds or substracts a multiple
of the percentage change in share price trend and current volume, depending
upon the investment's upward or downward movements.
# References
- [Wikipedia]
(https://en.wikipedia.org/wiki/Volume%E2%80%93price_trend)
"""
function volumepricetrend(ohlcv::TimeArray, n::Integer = 10; c = :Close, v = :Volume)
_c = ohlcv[c]
_v = ohlcv[v]
_lagc = lagfill(_c, 1, nanmean(values(_c)))
_vpt = (_v./100000) .* (_c .- _lagc) ./ _lagc
_lagvpt = lagfill(_vpt, 1, nanmean(values(_vpt)))
vpt = rename(_vpt .+ _lagvpt, :vpt)
end