roc.src.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /**
  2. * @license Highstock JS v9.1.1 (2021-06-04)
  3. *
  4. * Indicator series type for Highcharts Stock
  5. *
  6. * (c) 2010-2021 Kacper Madej
  7. *
  8. * License: www.highcharts.com/license
  9. */
  10. 'use strict';
  11. (function (factory) {
  12. if (typeof module === 'object' && module.exports) {
  13. factory['default'] = factory;
  14. module.exports = factory;
  15. } else if (typeof define === 'function' && define.amd) {
  16. define('highcharts/indicators/roc', ['highcharts', 'highcharts/modules/stock'], function (Highcharts) {
  17. factory(Highcharts);
  18. factory.Highcharts = Highcharts;
  19. return factory;
  20. });
  21. } else {
  22. factory(typeof Highcharts !== 'undefined' ? Highcharts : undefined);
  23. }
  24. }(function (Highcharts) {
  25. var _modules = Highcharts ? Highcharts._modules : {};
  26. function _registerModule(obj, path, args, fn) {
  27. if (!obj.hasOwnProperty(path)) {
  28. obj[path] = fn.apply(null, args);
  29. }
  30. }
  31. _registerModule(_modules, 'Stock/Indicators/ROC/ROCIndicator.js', [_modules['Core/Series/SeriesRegistry.js'], _modules['Core/Utilities.js']], function (SeriesRegistry, U) {
  32. /* *
  33. *
  34. * (c) 2010-2021 Kacper Madej
  35. *
  36. * License: www.highcharts.com/license
  37. *
  38. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  39. *
  40. * */
  41. var __extends = (this && this.__extends) || (function () {
  42. var extendStatics = function (d,
  43. b) {
  44. extendStatics = Object.setPrototypeOf ||
  45. ({ __proto__: [] } instanceof Array && function (d,
  46. b) { d.__proto__ = b; }) ||
  47. function (d,
  48. b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  49. return extendStatics(d, b);
  50. };
  51. return function (d, b) {
  52. extendStatics(d, b);
  53. function __() { this.constructor = d; }
  54. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  55. };
  56. })();
  57. var SMAIndicator = SeriesRegistry.seriesTypes.sma;
  58. var isArray = U.isArray,
  59. merge = U.merge,
  60. extend = U.extend;
  61. /* eslint-disable require-jsdoc */
  62. // Utils:
  63. function populateAverage(xVal, yVal, i, period, index) {
  64. /* Calculated as:
  65. (Closing Price [today] - Closing Price [n days ago]) /
  66. Closing Price [n days ago] * 100
  67. Return y as null when avoiding division by zero */
  68. var nDaysAgoY,
  69. rocY;
  70. if (index < 0) {
  71. // y data given as an array of values
  72. nDaysAgoY = yVal[i - period];
  73. rocY = nDaysAgoY ?
  74. (yVal[i] - nDaysAgoY) / nDaysAgoY * 100 :
  75. null;
  76. }
  77. else {
  78. // y data given as an array of arrays and the index should be used
  79. nDaysAgoY = yVal[i - period][index];
  80. rocY = nDaysAgoY ?
  81. (yVal[i][index] - nDaysAgoY) / nDaysAgoY * 100 :
  82. null;
  83. }
  84. return [xVal[i], rocY];
  85. }
  86. /* eslint-enable require-jsdoc */
  87. /* *
  88. *
  89. * Class
  90. *
  91. * */
  92. /**
  93. * The ROC series type.
  94. *
  95. * @private
  96. * @class
  97. * @name Highcharts.seriesTypes.roc
  98. *
  99. * @augments Highcharts.Series
  100. */
  101. var ROCIndicator = /** @class */ (function (_super) {
  102. __extends(ROCIndicator, _super);
  103. function ROCIndicator() {
  104. var _this = _super !== null && _super.apply(this,
  105. arguments) || this;
  106. /* *
  107. *
  108. * Properties
  109. *
  110. * */
  111. _this.data = void 0;
  112. _this.options = void 0;
  113. _this.points = void 0;
  114. return _this;
  115. }
  116. /* *
  117. *
  118. * Functions
  119. *
  120. * */
  121. ROCIndicator.prototype.getValues = function (series, params) {
  122. var period = params.period,
  123. xVal = series.xData,
  124. yVal = series.yData,
  125. yValLen = yVal ? yVal.length : 0,
  126. ROC = [],
  127. xData = [],
  128. yData = [],
  129. i,
  130. index = -1,
  131. ROCPoint;
  132. // Period is used as a number of time periods ago, so we need more
  133. // (at least 1 more) data than the period value
  134. if (xVal.length <= period) {
  135. return;
  136. }
  137. // Switch index for OHLC / Candlestick / Arearange
  138. if (isArray(yVal[0])) {
  139. index = params.index;
  140. }
  141. // i = period <-- skip first N-points
  142. // Calculate value one-by-one for each period in visible data
  143. for (i = period; i < yValLen; i++) {
  144. ROCPoint = populateAverage(xVal, yVal, i, period, index);
  145. ROC.push(ROCPoint);
  146. xData.push(ROCPoint[0]);
  147. yData.push(ROCPoint[1]);
  148. }
  149. return {
  150. values: ROC,
  151. xData: xData,
  152. yData: yData
  153. };
  154. };
  155. /**
  156. * Rate of change indicator (ROC). The indicator value for each point
  157. * is defined as:
  158. *
  159. * `(C - Cn) / Cn * 100`
  160. *
  161. * where: `C` is the close value of the point of the same x in the
  162. * linked series and `Cn` is the close value of the point `n` periods
  163. * ago. `n` is set through [period](#plotOptions.roc.params.period).
  164. *
  165. * This series requires `linkedTo` option to be set.
  166. *
  167. * @sample stock/indicators/roc
  168. * Rate of change indicator
  169. *
  170. * @extends plotOptions.sma
  171. * @since 6.0.0
  172. * @product highstock
  173. * @requires stock/indicators/indicators
  174. * @requires stock/indicators/roc
  175. * @optionparent plotOptions.roc
  176. */
  177. ROCIndicator.defaultOptions = merge(SMAIndicator.defaultOptions, {
  178. params: {
  179. index: 3,
  180. period: 9
  181. }
  182. });
  183. return ROCIndicator;
  184. }(SMAIndicator));
  185. extend(ROCIndicator.prototype, {
  186. nameBase: 'Rate of Change'
  187. });
  188. SeriesRegistry.registerSeriesType('roc', ROCIndicator);
  189. /* *
  190. *
  191. * Default Export
  192. *
  193. * */
  194. /**
  195. * A `ROC` series. If the [type](#series.wma.type) option is not
  196. * specified, it is inherited from [chart.type](#chart.type).
  197. *
  198. * Rate of change indicator (ROC). The indicator value for each point
  199. * is defined as:
  200. *
  201. * `(C - Cn) / Cn * 100`
  202. *
  203. * where: `C` is the close value of the point of the same x in the
  204. * linked series and `Cn` is the close value of the point `n` periods
  205. * ago. `n` is set through [period](#series.roc.params.period).
  206. *
  207. * This series requires `linkedTo` option to be set.
  208. *
  209. * @extends series,plotOptions.roc
  210. * @since 6.0.0
  211. * @product highstock
  212. * @excluding dataParser, dataURL
  213. * @requires stock/indicators/indicators
  214. * @requires stock/indicators/roc
  215. * @apioption series.roc
  216. */
  217. ''; // to include the above in the js output
  218. return ROCIndicator;
  219. });
  220. _registerModule(_modules, 'masters/indicators/roc.src.js', [], function () {
  221. });
  222. }));