ROCIndicator.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /* *
  2. *
  3. * (c) 2010-2020 Kacper Madej
  4. *
  5. * License: www.highcharts.com/license
  6. *
  7. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  8. *
  9. * */
  10. 'use strict';
  11. import U from '../../Core/Utilities.js';
  12. var isArray = U.isArray, seriesType = U.seriesType;
  13. /* eslint-disable require-jsdoc */
  14. // Utils:
  15. function populateAverage(xVal, yVal, i, period, index) {
  16. /* Calculated as:
  17. (Closing Price [today] - Closing Price [n days ago]) /
  18. Closing Price [n days ago] * 100
  19. Return y as null when avoiding division by zero */
  20. var nDaysAgoY, rocY;
  21. if (index < 0) {
  22. // y data given as an array of values
  23. nDaysAgoY = yVal[i - period];
  24. rocY = nDaysAgoY ?
  25. (yVal[i] - nDaysAgoY) / nDaysAgoY * 100 :
  26. null;
  27. }
  28. else {
  29. // y data given as an array of arrays and the index should be used
  30. nDaysAgoY = yVal[i - period][index];
  31. rocY = nDaysAgoY ?
  32. (yVal[i][index] - nDaysAgoY) / nDaysAgoY * 100 :
  33. null;
  34. }
  35. return [xVal[i], rocY];
  36. }
  37. /* eslint-enable require-jsdoc */
  38. /**
  39. * The ROC series type.
  40. *
  41. * @private
  42. * @class
  43. * @name Highcharts.seriesTypes.roc
  44. *
  45. * @augments Highcharts.Series
  46. */
  47. seriesType('roc', 'sma',
  48. /**
  49. * Rate of change indicator (ROC). The indicator value for each point
  50. * is defined as:
  51. *
  52. * `(C - Cn) / Cn * 100`
  53. *
  54. * where: `C` is the close value of the point of the same x in the
  55. * linked series and `Cn` is the close value of the point `n` periods
  56. * ago. `n` is set through [period](#plotOptions.roc.params.period).
  57. *
  58. * This series requires `linkedTo` option to be set.
  59. *
  60. * @sample stock/indicators/roc
  61. * Rate of change indicator
  62. *
  63. * @extends plotOptions.sma
  64. * @since 6.0.0
  65. * @product highstock
  66. * @requires stock/indicators/indicators
  67. * @requires stock/indicators/roc
  68. * @optionparent plotOptions.roc
  69. */
  70. {
  71. params: {
  72. index: 3,
  73. period: 9
  74. }
  75. },
  76. /**
  77. * @lends Highcharts.Series#
  78. */
  79. {
  80. nameBase: 'Rate of Change',
  81. getValues: function (series, params) {
  82. var period = params.period, xVal = series.xData, yVal = series.yData, yValLen = yVal ? yVal.length : 0, ROC = [], xData = [], yData = [], i, index = -1, ROCPoint;
  83. // Period is used as a number of time periods ago, so we need more
  84. // (at least 1 more) data than the period value
  85. if (xVal.length <= period) {
  86. return;
  87. }
  88. // Switch index for OHLC / Candlestick / Arearange
  89. if (isArray(yVal[0])) {
  90. index = params.index;
  91. }
  92. // i = period <-- skip first N-points
  93. // Calculate value one-by-one for each period in visible data
  94. for (i = period; i < yValLen; i++) {
  95. ROCPoint = populateAverage(xVal, yVal, i, period, index);
  96. ROC.push(ROCPoint);
  97. xData.push(ROCPoint[0]);
  98. yData.push(ROCPoint[1]);
  99. }
  100. return {
  101. values: ROC,
  102. xData: xData,
  103. yData: yData
  104. };
  105. }
  106. });
  107. /**
  108. * A `ROC` series. If the [type](#series.wma.type) option is not
  109. * specified, it is inherited from [chart.type](#chart.type).
  110. *
  111. * Rate of change indicator (ROC). The indicator value for each point
  112. * is defined as:
  113. *
  114. * `(C - Cn) / Cn * 100`
  115. *
  116. * where: `C` is the close value of the point of the same x in the
  117. * linked series and `Cn` is the close value of the point `n` periods
  118. * ago. `n` is set through [period](#series.roc.params.period).
  119. *
  120. * This series requires `linkedTo` option to be set.
  121. *
  122. * @extends series,plotOptions.roc
  123. * @since 6.0.0
  124. * @product highstock
  125. * @excluding dataParser, dataURL
  126. * @requires stock/indicators/indicators
  127. * @requires stock/indicators/roc
  128. * @apioption series.roc
  129. */
  130. ''; // to include the above in the js output