roc.src.js 5.9 KB

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