RSIIndicator.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /* *
  2. *
  3. * License: www.highcharts.com/license
  4. *
  5. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  6. *
  7. * */
  8. 'use strict';
  9. import U from '../../Core/Utilities.js';
  10. var isArray = U.isArray, seriesType = U.seriesType;
  11. /* eslint-disable require-jsdoc */
  12. // Utils:
  13. function toFixed(a, n) {
  14. return parseFloat(a.toFixed(n));
  15. }
  16. /* eslint-enable require-jsdoc */
  17. /**
  18. * The RSI series type.
  19. *
  20. * @private
  21. * @class
  22. * @name Highcharts.seriesTypes.rsi
  23. *
  24. * @augments Highcharts.Series
  25. */
  26. seriesType('rsi', 'sma',
  27. /**
  28. * Relative strength index (RSI) technical indicator. This series
  29. * requires the `linkedTo` option to be set and should be loaded after
  30. * the `stock/indicators/indicators.js` file.
  31. *
  32. * @sample stock/indicators/rsi
  33. * RSI indicator
  34. *
  35. * @extends plotOptions.sma
  36. * @since 6.0.0
  37. * @product highstock
  38. * @requires stock/indicators/indicators
  39. * @requires stock/indicators/rsi
  40. * @optionparent plotOptions.rsi
  41. */
  42. {
  43. /**
  44. * @excluding index
  45. */
  46. params: {
  47. period: 14,
  48. /**
  49. * Number of maximum decimals that are used in RSI calculations.
  50. */
  51. decimals: 4
  52. }
  53. },
  54. /**
  55. * @lends Highcharts.Series#
  56. */
  57. {
  58. getValues: function (series, params) {
  59. var period = params.period, xVal = series.xData, yVal = series.yData, yValLen = yVal ? yVal.length : 0, decimals = params.decimals,
  60. // RSI starts calculations from the second point
  61. // Cause we need to calculate change between two points
  62. range = 1, RSI = [], xData = [], yData = [], index = 3, gain = 0, loss = 0, RSIPoint, change, avgGain, avgLoss, i;
  63. // RSI requires close value
  64. if ((xVal.length < period) || !isArray(yVal[0]) ||
  65. yVal[0].length !== 4) {
  66. return;
  67. }
  68. // Calculate changes for first N points
  69. while (range < period) {
  70. change = toFixed(yVal[range][index] - yVal[range - 1][index], decimals);
  71. if (change > 0) {
  72. gain += change;
  73. }
  74. else {
  75. loss += Math.abs(change);
  76. }
  77. range++;
  78. }
  79. // Average for first n-1 points:
  80. avgGain = toFixed(gain / (period - 1), decimals);
  81. avgLoss = toFixed(loss / (period - 1), decimals);
  82. for (i = range; i < yValLen; i++) {
  83. change = toFixed(yVal[i][index] - yVal[i - 1][index], decimals);
  84. if (change > 0) {
  85. gain = change;
  86. loss = 0;
  87. }
  88. else {
  89. gain = 0;
  90. loss = Math.abs(change);
  91. }
  92. // Calculate smoothed averages, RS, RSI values:
  93. avgGain = toFixed((avgGain * (period - 1) + gain) / period, decimals);
  94. avgLoss = toFixed((avgLoss * (period - 1) + loss) / period, decimals);
  95. // If average-loss is equal zero, then by definition RSI is set
  96. // to 100:
  97. if (avgLoss === 0) {
  98. RSIPoint = 100;
  99. // If average-gain is equal zero, then by definition RSI is set
  100. // to 0:
  101. }
  102. else if (avgGain === 0) {
  103. RSIPoint = 0;
  104. }
  105. else {
  106. RSIPoint = toFixed(100 - (100 / (1 + (avgGain / avgLoss))), decimals);
  107. }
  108. RSI.push([xVal[i], RSIPoint]);
  109. xData.push(xVal[i]);
  110. yData.push(RSIPoint);
  111. }
  112. return {
  113. values: RSI,
  114. xData: xData,
  115. yData: yData
  116. };
  117. }
  118. });
  119. /**
  120. * A `RSI` series. If the [type](#series.rsi.type) option is not
  121. * specified, it is inherited from [chart.type](#chart.type).
  122. *
  123. * @extends series,plotOptions.rsi
  124. * @since 6.0.0
  125. * @product highstock
  126. * @excluding dataParser, dataURL
  127. * @requires stock/indicators/indicators
  128. * @requires stock/indicators/rsi
  129. * @apioption series.rsi
  130. */
  131. ''; // to include the above in the js output