roc.src.js 3.8 KB

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