roc.src.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /**
  2. * @license Highstock JS v8.2.0 (2020-08-20)
  3. *
  4. * Indicator series type for Highstock
  5. *
  6. * (c) 2010-2019 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/ROCIndicator.js', [_modules['Core/Utilities.js']], function (U) {
  32. /* *
  33. *
  34. * (c) 2010-2020 Kacper Madej
  35. *
  36. * License: www.highcharts.com/license
  37. *
  38. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  39. *
  40. * */
  41. var isArray = U.isArray,
  42. seriesType = U.seriesType;
  43. /* eslint-disable require-jsdoc */
  44. // Utils:
  45. function populateAverage(xVal, yVal, i, period, index) {
  46. /* Calculated as:
  47. (Closing Price [today] - Closing Price [n days ago]) /
  48. Closing Price [n days ago] * 100
  49. Return y as null when avoiding division by zero */
  50. var nDaysAgoY,
  51. rocY;
  52. if (index < 0) {
  53. // y data given as an array of values
  54. nDaysAgoY = yVal[i - period];
  55. rocY = nDaysAgoY ?
  56. (yVal[i] - nDaysAgoY) / nDaysAgoY * 100 :
  57. null;
  58. }
  59. else {
  60. // y data given as an array of arrays and the index should be used
  61. nDaysAgoY = yVal[i - period][index];
  62. rocY = nDaysAgoY ?
  63. (yVal[i][index] - nDaysAgoY) / nDaysAgoY * 100 :
  64. null;
  65. }
  66. return [xVal[i], rocY];
  67. }
  68. /* eslint-enable require-jsdoc */
  69. /**
  70. * The ROC series type.
  71. *
  72. * @private
  73. * @class
  74. * @name Highcharts.seriesTypes.roc
  75. *
  76. * @augments Highcharts.Series
  77. */
  78. seriesType('roc', 'sma',
  79. /**
  80. * Rate of change indicator (ROC). The indicator value for each point
  81. * is defined as:
  82. *
  83. * `(C - Cn) / Cn * 100`
  84. *
  85. * where: `C` is the close value of the point of the same x in the
  86. * linked series and `Cn` is the close value of the point `n` periods
  87. * ago. `n` is set through [period](#plotOptions.roc.params.period).
  88. *
  89. * This series requires `linkedTo` option to be set.
  90. *
  91. * @sample stock/indicators/roc
  92. * Rate of change indicator
  93. *
  94. * @extends plotOptions.sma
  95. * @since 6.0.0
  96. * @product highstock
  97. * @requires stock/indicators/indicators
  98. * @requires stock/indicators/roc
  99. * @optionparent plotOptions.roc
  100. */
  101. {
  102. params: {
  103. index: 3,
  104. period: 9
  105. }
  106. },
  107. /**
  108. * @lends Highcharts.Series#
  109. */
  110. {
  111. nameBase: 'Rate of Change',
  112. getValues: function (series, params) {
  113. var period = params.period,
  114. xVal = series.xData,
  115. yVal = series.yData,
  116. yValLen = yVal ? yVal.length : 0,
  117. ROC = [],
  118. xData = [],
  119. yData = [],
  120. i,
  121. index = -1,
  122. ROCPoint;
  123. // Period is used as a number of time periods ago, so we need more
  124. // (at least 1 more) data than the period value
  125. if (xVal.length <= period) {
  126. return;
  127. }
  128. // Switch index for OHLC / Candlestick / Arearange
  129. if (isArray(yVal[0])) {
  130. index = params.index;
  131. }
  132. // i = period <-- skip first N-points
  133. // Calculate value one-by-one for each period in visible data
  134. for (i = period; i < yValLen; i++) {
  135. ROCPoint = populateAverage(xVal, yVal, i, period, index);
  136. ROC.push(ROCPoint);
  137. xData.push(ROCPoint[0]);
  138. yData.push(ROCPoint[1]);
  139. }
  140. return {
  141. values: ROC,
  142. xData: xData,
  143. yData: yData
  144. };
  145. }
  146. });
  147. /**
  148. * A `ROC` series. If the [type](#series.wma.type) option is not
  149. * specified, it is inherited from [chart.type](#chart.type).
  150. *
  151. * Rate of change indicator (ROC). The indicator value for each point
  152. * is defined as:
  153. *
  154. * `(C - Cn) / Cn * 100`
  155. *
  156. * where: `C` is the close value of the point of the same x in the
  157. * linked series and `Cn` is the close value of the point `n` periods
  158. * ago. `n` is set through [period](#series.roc.params.period).
  159. *
  160. * This series requires `linkedTo` option to be set.
  161. *
  162. * @extends series,plotOptions.roc
  163. * @since 6.0.0
  164. * @product highstock
  165. * @excluding dataParser, dataURL
  166. * @requires stock/indicators/indicators
  167. * @requires stock/indicators/roc
  168. * @apioption series.roc
  169. */
  170. ''; // to include the above in the js output
  171. });
  172. _registerModule(_modules, 'masters/indicators/roc.src.js', [], function () {
  173. });
  174. }));