roc.src.js 5.8 KB

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