forked from cinar/indicator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
volume_indicators.go
215 lines (180 loc) · 6.54 KB
/
volume_indicators.go
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
205
206
207
208
209
210
211
212
213
214
215
// Copyright (c) 2021 Onur Cinar. All Rights Reserved.
// The source code is provided under MIT License.
//
// https://github.com/cinar/indicator
package indicator
// Starting value for NVI.
const NVI_STARTING_VALUE = 1000
// Default period of CMF.
const CMF_DEFAULT_PERIOD = 20
// Accumulation/Distribution Indicator (A/D). Cumulative indicator
// that uses volume and price to assess whether a stock is
// being accumulated or distributed.
//
// MFM = ((Closing - Low) - (High - Closing)) / (High - Low)
// MFV = MFM * Period Volume
// AD = Previous AD + CMFV
//
// Returns ad.
func AccumulationDistribution(high, low, closing []float64, volume []int64) []float64 {
checkSameSize(high, low, closing)
ad := make([]float64, len(closing))
for i := 0; i < len(ad); i++ {
if i > 0 {
ad[i] = ad[i-1]
}
ad[i] += float64(volume[i]) * (((closing[i] - low[i]) - (high[i] - closing[i])) / (high[i] - low[i]))
}
return ad
}
// On-Balance Volume (OBV). It is a technical trading momentum indicator that
// uses volume flow to predict changes in stock price.
//
// volume, if Closing > Closing-Prev
// OBV = OBV-Prev + 0, if Closing = Closing-Prev
// -volume, if Closing < Closing-Prev
//
// Returns obv
func Obv(closing []float64, volume []int64) []int64 {
if len(closing) != len(volume) {
panic("not all same size")
}
obv := make([]int64, len(volume))
for i := 1; i < len(obv); i++ {
obv[i] = obv[i-1]
if closing[i] > closing[i-1] {
obv[i] += volume[i]
} else if closing[i] < closing[i-1] {
obv[i] -= volume[i]
}
}
return obv
}
// The Money Flow Index (MFI) analyzes both the closing price and the volume
// to measure to identify overbought and oversold states. It is similar to
// the Relative Strength Index (RSI), but it also uses the volume.
//
// Raw Money Flow = Typical Price * Volume
// Money Ratio = Positive Money Flow / Negative Money Flow
// Money Flow Index = 100 - (100 / (1 + Money Ratio))
//
// Retruns money flow index values.
func MoneyFlowIndex(period int, high, low, closing []float64, volume []int64) []float64 {
typicalPrice, _ := TypicalPrice(low, high, closing)
rawMoneyFlow := multiply(typicalPrice, asFloat64(volume))
signs := extractSign(diff(rawMoneyFlow, 1))
moneyFlow := multiply(signs, rawMoneyFlow)
positiveMoneyFlow := keepPositives(moneyFlow)
negativeMoneyFlow := keepNegatives(moneyFlow)
moneyRatio := divide(
Sum(period, positiveMoneyFlow),
Sum(period, multiplyBy(negativeMoneyFlow, -1)))
moneyFlowIndex := addBy(multiplyBy(pow(addBy(moneyRatio, 1), -1), -100), 100)
return moneyFlowIndex
}
// Default money flow index with period 14.
func DefaultMoneyFlowIndex(high, low, closing []float64, volume []int64) []float64 {
return MoneyFlowIndex(14, high, low, closing, volume)
}
// The Force Index (FI) uses the closing price and the volume to assess
// the power behind a move and identify turning points.
//
// Force Index = EMA(period, (Current - Previous) * Volume)
//
// Returns force index.
func ForceIndex(period int, closing []float64, volume []int64) []float64 {
return Ema(period, multiply(diff(closing, 1), asFloat64(volume)))
}
// The default Force Index (FI) with window size of 13.
func DefaultForceIndex(closing []float64, volume []int64) []float64 {
return ForceIndex(13, closing, volume)
}
// The Ease of Movement (EMV) is a volume based oscillator measuring
// the ease of price movement.
//
// Distance Moved = ((High + Low) / 2) - ((Priod High + Prior Low) /2)
// Box Ratio = ((Volume / 100000000) / (High - Low))
// EMV(1) = Distance Moved / Box Ratio
// EMV(14) = SMA(14, EMV(1))
//
// Returns ease of movement values.
func EaseOfMovement(period int, high, low []float64, volume []int64) []float64 {
distanceMoved := diff(divideBy(add(high, low), 2), 1)
boxRatio := divide(divideBy(asFloat64(volume), float64(100000000)), substract(high, low))
emv := Sma(period, divide(distanceMoved, boxRatio))
return emv
}
// The default Ease of Movement with the default period of 14.
func DefaultEaseOfMovement(high, low []float64, volume []int64) []float64 {
return EaseOfMovement(14, high, low, volume)
}
// The Volume Price Trend (VPT) provides a correlation between the
// volume and the price.
//
// VPT = Previous VPT + (Volume * (Current Closing - Previous Closing) / Previous Closing)
//
// Returns volume price trend values.
func VolumePriceTrend(closing []float64, volume []int64) []float64 {
previousClosing := shiftRightAndFillBy(1, closing[0], closing)
vpt := multiply(asFloat64(volume), divide(substract(closing, previousClosing), previousClosing))
return Sum(len(vpt), vpt)
}
// The Volume Weighted Average Price (VWAP) provides the average price
// the asset has traded.
//
// VWAP = Sum(Closing * Volume) / Sum(Volume)
//
// Returns vwap values.
func VolumeWeightedAveragePrice(period int, closing []float64, volume []int64) []float64 {
v := asFloat64(volume)
return divide(Sum(period, multiply(closing, v)), Sum(period, v))
}
// Default volume weighted average price with period of 14.
func DefaultVolumeWeightedAveragePrice(closing []float64, volume []int64) []float64 {
return VolumeWeightedAveragePrice(14, closing, volume)
}
// The Negative Volume Index (NVI) is a cumulative indicator using
// the change in volume to decide when the smart money is active.
//
// If Volume is greather than Previous Volume:
//
// NVI = Previous NVI
//
// Otherwise:
//
// NVI = Previous NVI + (((Closing - Previous Closing) / Previous Closing) * Previous NVI)
//
// Returns nvi values.
func NegativeVolumeIndex(closing []float64, volume []int64) []float64 {
if len(closing) != len(volume) {
panic("not all same size")
}
nvi := make([]float64, len(closing))
for i := 0; i < len(nvi); i++ {
if i == 0 {
nvi[i] = NVI_STARTING_VALUE
} else if volume[i-1] < volume[i] {
nvi[i] = nvi[i-1]
} else {
nvi[i] = nvi[i-1] + (((closing[i] - closing[i-1]) / closing[i-1]) * nvi[i-1])
}
}
return nvi
}
// The Chaikin Money Flow (CMF) measures the amount of money flow volume
// over a given period.
//
// Money Flow Multiplier = ((Closing - Low) - (High - Closing)) / (High - Low)
// Money Flow Volume = Money Flow Multiplier * Volume
// Chaikin Money Flow = Sum(20, Money Flow Volume) / Sum(20, Volume)
//
func ChaikinMoneyFlow(high, low, closing []float64, volume []int64) []float64 {
moneyFlowMultiplier := divide(
substract(substract(closing, low), substract(high, closing)),
substract(high, low))
moneyFlowVolume := multiply(moneyFlowMultiplier, asFloat64(volume))
cmf := divide(
Sum(CMF_DEFAULT_PERIOD, moneyFlowVolume),
Sum(CMF_DEFAULT_PERIOD, asFloat64(volume)))
return cmf
}