cmf.src.js
10.1 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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/**
* @license Highstock JS v7.2.0 (2019-09-03)
*
* (c) 2010-2019 Highsoft AS
* Author: Sebastian Domas
*
* License: www.highcharts.com/license
*/
'use strict';
(function (factory) {
if (typeof module === 'object' && module.exports) {
factory['default'] = factory;
module.exports = factory;
} else if (typeof define === 'function' && define.amd) {
define('highcharts/indicators/cmf', ['highcharts', 'highcharts/modules/stock'], function (Highcharts) {
factory(Highcharts);
factory.Highcharts = Highcharts;
return factory;
});
} else {
factory(typeof Highcharts !== 'undefined' ? Highcharts : undefined);
}
}(function (Highcharts) {
var _modules = Highcharts ? Highcharts._modules : {};
function _registerModule(obj, path, args, fn) {
if (!obj.hasOwnProperty(path)) {
obj[path] = fn.apply(null, args);
}
}
_registerModule(_modules, 'indicators/cmf.src.js', [_modules['parts/Globals.js']], function (H) {
/* *
*
* (c) 2010-2019 Highsoft AS
*
* Author: Sebastian Domas
*
* Chaikin Money Flow indicator for Highstock
*
* License: www.highcharts.com/license
*
* */
/**
* @private
* @interface Highcharts.CmfValuesObject
*//**
* Combined xData and yData values into a tuple.
* @name Highcharts.CmfValuesObject#values
* @type {Array<Array<number,number>>}
*//**
* Values represent x timestamp values
* @name Highcharts.CmfValuesObject#xData
* @type {Array<number>}
*//**
* Values represent y values
* @name Highcharts.CmfValuesObject#yData
* @type {Array<number>}
*/
/**
* The CMF series type.
*
* @private
* @class
* @name Highcharts.seriesTypes.cmf
*
* @augments Highcharts.Series
*/
H.seriesType('cmf', 'sma',
/**
* Chaikin Money Flow indicator (cmf).
*
* @sample stock/indicators/cmf/
* Chaikin Money Flow indicator
*
* @extends plotOptions.sma
* @since 6.0.0
* @excluding animationLimit
* @product highstock
* @optionparent plotOptions.cmf
*/
{
params: {
period: 14,
/**
* The id of another series to use its data as volume data for the
* indiator calculation.
*/
volumeSeriesID: 'volume'
}
},
/**
* @lends Highcharts.Series#
*/
{
nameBase: 'Chaikin Money Flow',
/**
* Checks if the series and volumeSeries are accessible, number of
* points.x is longer than period, is series has OHLC data
* @private
* @return {boolean} True if series is valid and can be computed,
* otherwise false.
*/
isValid: function () {
var chart = this.chart,
options = this.options,
series = this.linkedParent,
volumeSeries = (
this.volumeSeries ||
(
this.volumeSeries =
chart.get(options.params.volumeSeriesID)
)
),
isSeriesOHLC = (
series &&
series.yData &&
series.yData[0].length === 4
);
function isLengthValid(serie) {
return serie.xData &&
serie.xData.length >= options.params.period;
}
return !!(
series &&
volumeSeries &&
isLengthValid(series) &&
isLengthValid(volumeSeries) && isSeriesOHLC
);
},
/**
* Returns indicator's data.
* @private
* @return {boolean|Highcharts.CmfValuesObject} Returns false if the
* indicator is not valid, otherwise returns Values object.
*/
getValues: function (series, params) {
if (!this.isValid()) {
return false;
}
return this.getMoneyFlow(
series.xData,
series.yData,
this.volumeSeries.yData,
params.period
);
},
/**
* @private
* @param {Array<number>} xData - x timestamp values
* @param {Array<number>} seriesYData - yData of basic series
* @param {Array<number>} volumeSeriesYData - yData of volume series
* @param {number} period - indicator's param
* @return {Highcharts.CmfValuesObject} object containing computed money
* flow data
*/
getMoneyFlow: function (xData, seriesYData, volumeSeriesYData, period) {
var len = seriesYData.length,
moneyFlowVolume = [],
sumVolume = 0,
sumMoneyFlowVolume = 0,
moneyFlowXData = [],
moneyFlowYData = [],
values = [],
i,
point,
nullIndex = -1;
/**
* Calculates money flow volume, changes i, nullIndex vars from
* upper scope!
* @private
* @param {Array<number>} ohlc - OHLC point
* @param {number} volume - Volume point's y value
* @return {number} - volume * moneyFlowMultiplier
**/
function getMoneyFlowVolume(ohlc, volume) {
var high = ohlc[1],
low = ohlc[2],
close = ohlc[3],
isValid =
volume !== null &&
high !== null &&
low !== null &&
close !== null &&
high !== low;
/**
* @private
* @param {number} h - High value
* @param {number} l - Low value
* @param {number} c - Close value
* @return {number} calculated multiplier for the point
**/
function getMoneyFlowMultiplier(h, l, c) {
return ((c - l) - (h - c)) / (h - l);
}
return isValid ?
getMoneyFlowMultiplier(high, low, close) * volume :
((nullIndex = i), null);
}
if (period > 0 && period <= len) {
for (i = 0; i < period; i++) {
moneyFlowVolume[i] = getMoneyFlowVolume(
seriesYData[i],
volumeSeriesYData[i]
);
sumVolume += volumeSeriesYData[i];
sumMoneyFlowVolume += moneyFlowVolume[i];
}
moneyFlowXData.push(xData[i - 1]);
moneyFlowYData.push(
i - nullIndex >= period && sumVolume !== 0 ?
sumMoneyFlowVolume / sumVolume :
null
);
values.push([moneyFlowXData[0], moneyFlowYData[0]]);
for (; i < len; i++) {
moneyFlowVolume[i] = getMoneyFlowVolume(
seriesYData[i],
volumeSeriesYData[i]
);
sumVolume -= volumeSeriesYData[i - period];
sumVolume += volumeSeriesYData[i];
sumMoneyFlowVolume -= moneyFlowVolume[i - period];
sumMoneyFlowVolume += moneyFlowVolume[i];
point = [
xData[i],
i - nullIndex >= period ?
sumMoneyFlowVolume / sumVolume :
null
];
moneyFlowXData.push(point[0]);
moneyFlowYData.push(point[1]);
values.push([point[0], point[1]]);
}
}
return {
values: values,
xData: moneyFlowXData,
yData: moneyFlowYData
};
}
});
/**
* A `CMF` series. If the [type](#series.cmf.type) option is not
* specified, it is inherited from [chart.type](#chart.type).
*
* @extends series,plotOptions.cmf
* @since 6.0.0
* @product highstock
* @excluding dataParser, dataURL
* @apioption series.cmf
*/
});
_registerModule(_modules, 'masters/indicators/cmf.src.js', [], function () {
});
}));