cmf.src.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /**
  2. * @license Highstock JS v9.1.1 (2021-06-04)
  3. *
  4. * (c) 2010-2021 Highsoft AS
  5. * Author: Sebastian Domas
  6. *
  7. * License: www.highcharts.com/license
  8. */
  9. 'use strict';
  10. (function (factory) {
  11. if (typeof module === 'object' && module.exports) {
  12. factory['default'] = factory;
  13. module.exports = factory;
  14. } else if (typeof define === 'function' && define.amd) {
  15. define('highcharts/indicators/cmf', ['highcharts', 'highcharts/modules/stock'], function (Highcharts) {
  16. factory(Highcharts);
  17. factory.Highcharts = Highcharts;
  18. return factory;
  19. });
  20. } else {
  21. factory(typeof Highcharts !== 'undefined' ? Highcharts : undefined);
  22. }
  23. }(function (Highcharts) {
  24. var _modules = Highcharts ? Highcharts._modules : {};
  25. function _registerModule(obj, path, args, fn) {
  26. if (!obj.hasOwnProperty(path)) {
  27. obj[path] = fn.apply(null, args);
  28. }
  29. }
  30. _registerModule(_modules, 'Stock/Indicators/CMF/CMFIndicator.js', [_modules['Core/Series/SeriesRegistry.js'], _modules['Core/Utilities.js']], function (SeriesRegistry, U) {
  31. /* *
  32. *
  33. * (c) 2010-2021 Highsoft AS
  34. *
  35. * Author: Sebastian Domas
  36. *
  37. * Chaikin Money Flow indicator for Highcharts Stock
  38. *
  39. * License: www.highcharts.com/license
  40. *
  41. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  42. *
  43. * */
  44. var __extends = (this && this.__extends) || (function () {
  45. var extendStatics = function (d,
  46. b) {
  47. extendStatics = Object.setPrototypeOf ||
  48. ({ __proto__: [] } instanceof Array && function (d,
  49. b) { d.__proto__ = b; }) ||
  50. function (d,
  51. b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  52. return extendStatics(d, b);
  53. };
  54. return function (d, b) {
  55. extendStatics(d, b);
  56. function __() { this.constructor = d; }
  57. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  58. };
  59. })();
  60. var SMAIndicator = SeriesRegistry.seriesTypes.sma;
  61. var merge = U.merge;
  62. /**
  63. * The CMF series type.
  64. *
  65. * @private
  66. * @class
  67. * @name Highcharts.seriesTypes.cmf
  68. *
  69. * @augments Highcharts.Series
  70. */
  71. var CMFIndicator = /** @class */ (function (_super) {
  72. __extends(CMFIndicator, _super);
  73. function CMFIndicator() {
  74. var _this = _super !== null && _super.apply(this,
  75. arguments) || this;
  76. /* *
  77. *
  78. * Properties
  79. *
  80. * */
  81. _this.data = void 0;
  82. _this.options = void 0;
  83. _this.points = void 0;
  84. _this.volumeSeries = void 0;
  85. _this.linkedParent = void 0;
  86. _this.yData = void 0;
  87. _this.nameBase = 'Chaikin Money Flow';
  88. return _this;
  89. }
  90. /**
  91. * Checks if the series and volumeSeries are accessible, number of
  92. * points.x is longer than period, is series has OHLC data
  93. * @private
  94. * @param {Highcharts.CMFIndicator} this indicator to use.
  95. * @return {boolean} True if series is valid and can be computed,
  96. * otherwise false.
  97. */
  98. CMFIndicator.prototype.isValid = function () {
  99. var chart = this.chart,
  100. options = this.options,
  101. series = this.linkedParent,
  102. volumeSeries = (this.volumeSeries ||
  103. (this.volumeSeries =
  104. chart.get(options.params.volumeSeriesID))),
  105. isSeriesOHLC = (series &&
  106. series.yData &&
  107. series.yData[0].length === 4);
  108. /**
  109. * @private
  110. * @param {Highcharts.Series} serie to check length validity on.
  111. * @return {boolean|undefined} true if length is valid.
  112. */
  113. function isLengthValid(serie) {
  114. return serie.xData &&
  115. serie.xData.length >= options.params.period;
  116. }
  117. return !!(series &&
  118. volumeSeries &&
  119. isLengthValid(series) &&
  120. isLengthValid(volumeSeries) && isSeriesOHLC);
  121. };
  122. /**
  123. * Returns indicator's data.
  124. * @private
  125. * @param {Highcharts.CMFIndicator} this indicator to use.
  126. * @param {Highcharts.Series} series to calculate values from
  127. * @param {Highcharts.CMFIndicatorParamsOptions} params to pass
  128. * @return {boolean|Highcharts.IndicatorNullableValuesObject} Returns false if the
  129. * indicator is not valid, otherwise returns Values object.
  130. */
  131. CMFIndicator.prototype.getValues = function (series, params) {
  132. if (!this.isValid()) {
  133. return;
  134. }
  135. return this.getMoneyFlow(series.xData, series.yData, this.volumeSeries.yData, params.period);
  136. };
  137. /**
  138. * @private
  139. * @param {Array<number>} xData - x timestamp values
  140. * @param {Array<number>} seriesYData - yData of basic series
  141. * @param {Array<number>} volumeSeriesYData - yData of volume series
  142. * @param {number} period - indicator's param
  143. * @return {Highcharts.IndicatorNullableValuesObject} object containing computed money
  144. * flow data
  145. */
  146. CMFIndicator.prototype.getMoneyFlow = function (xData, seriesYData, volumeSeriesYData, period) {
  147. var len = seriesYData.length,
  148. moneyFlowVolume = [],
  149. sumVolume = 0,
  150. sumMoneyFlowVolume = 0,
  151. moneyFlowXData = [],
  152. moneyFlowYData = [],
  153. values = [],
  154. i,
  155. point,
  156. nullIndex = -1;
  157. /**
  158. * Calculates money flow volume, changes i, nullIndex vars from
  159. * upper scope!
  160. * @private
  161. * @param {Array<number>} ohlc - OHLC point
  162. * @param {number} volume - Volume point's y value
  163. * @return {number|null} - volume * moneyFlowMultiplier
  164. **/
  165. function getMoneyFlowVolume(ohlc, volume) {
  166. var high = ohlc[1],
  167. low = ohlc[2],
  168. close = ohlc[3],
  169. isValid = volume !== null &&
  170. high !== null &&
  171. low !== null &&
  172. close !== null &&
  173. high !== low;
  174. /**
  175. * @private
  176. * @param {number} h - High value
  177. * @param {number} l - Low value
  178. * @param {number} c - Close value
  179. * @return {number} calculated multiplier for the point
  180. **/
  181. function getMoneyFlowMultiplier(h, l, c) {
  182. return ((c - l) - (h - c)) / (h - l);
  183. }
  184. return isValid ?
  185. getMoneyFlowMultiplier(high, low, close) * volume :
  186. ((nullIndex = i), null);
  187. }
  188. if (period > 0 && period <= len) {
  189. for (i = 0; i < period; i++) {
  190. moneyFlowVolume[i] = getMoneyFlowVolume(seriesYData[i], volumeSeriesYData[i]);
  191. sumVolume += volumeSeriesYData[i];
  192. sumMoneyFlowVolume += moneyFlowVolume[i];
  193. }
  194. moneyFlowXData.push(xData[i - 1]);
  195. moneyFlowYData.push(i - nullIndex >= period && sumVolume !== 0 ?
  196. sumMoneyFlowVolume / sumVolume :
  197. null);
  198. values.push([moneyFlowXData[0], moneyFlowYData[0]]);
  199. for (; i < len; i++) {
  200. moneyFlowVolume[i] = getMoneyFlowVolume(seriesYData[i], volumeSeriesYData[i]);
  201. sumVolume -= volumeSeriesYData[i - period];
  202. sumVolume += volumeSeriesYData[i];
  203. sumMoneyFlowVolume -= moneyFlowVolume[i - period];
  204. sumMoneyFlowVolume += moneyFlowVolume[i];
  205. point = [
  206. xData[i],
  207. i - nullIndex >= period ?
  208. sumMoneyFlowVolume / sumVolume :
  209. null
  210. ];
  211. moneyFlowXData.push(point[0]);
  212. moneyFlowYData.push(point[1]);
  213. values.push([point[0], point[1]]);
  214. }
  215. }
  216. return {
  217. values: values,
  218. xData: moneyFlowXData,
  219. yData: moneyFlowYData
  220. };
  221. };
  222. /**
  223. * Chaikin Money Flow indicator (cmf).
  224. *
  225. * @sample stock/indicators/cmf/
  226. * Chaikin Money Flow indicator
  227. *
  228. * @extends plotOptions.sma
  229. * @since 6.0.0
  230. * @excluding animationLimit
  231. * @product highstock
  232. * @requires stock/indicators/indicators
  233. * @requires stock/indicators/cmf
  234. * @optionparent plotOptions.cmf
  235. */
  236. CMFIndicator.defaultOptions = merge(SMAIndicator.defaultOptions, {
  237. /**
  238. * @excluding index
  239. */
  240. params: {
  241. index: void 0,
  242. /**
  243. * The id of another series to use its data as volume data for the
  244. * indiator calculation.
  245. */
  246. volumeSeriesID: 'volume'
  247. }
  248. });
  249. return CMFIndicator;
  250. }(SMAIndicator));
  251. SeriesRegistry.registerSeriesType('cmf', CMFIndicator);
  252. /* *
  253. *
  254. * Default Export
  255. *
  256. * */
  257. /**
  258. * A `CMF` series. If the [type](#series.cmf.type) option is not
  259. * specified, it is inherited from [chart.type](#chart.type).
  260. *
  261. * @extends series,plotOptions.cmf
  262. * @since 6.0.0
  263. * @product highstock
  264. * @excluding dataParser, dataURL
  265. * @requires stock/indicators/indicators
  266. * @requires stock/indicators/cmf
  267. * @apioption series.cmf
  268. */
  269. ''; // adds doclet above to the transpiled file
  270. return CMFIndicator;
  271. });
  272. _registerModule(_modules, 'masters/indicators/cmf.src.js', [], function () {
  273. });
  274. }));