rsi.src.js 3.9 KB

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