rsi.src.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /**
  2. * @license Highstock JS v8.0.0 (2019-12-10)
  3. *
  4. * Indicator series type for Highstock
  5. *
  6. * (c) 2010-2019 Paweł Fus
  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/rsi', ['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/rsi.src.js', [_modules['parts/Globals.js'], _modules['parts/Utilities.js']], function (H, U) {
  32. /* *
  33. *
  34. * License: www.highcharts.com/license
  35. *
  36. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  37. *
  38. * */
  39. var isArray = U.isArray;
  40. /* eslint-disable require-jsdoc */
  41. // Utils:
  42. function toFixed(a, n) {
  43. return parseFloat(a.toFixed(n));
  44. }
  45. /* eslint-enable require-jsdoc */
  46. /**
  47. * The RSI series type.
  48. *
  49. * @private
  50. * @class
  51. * @name Highcharts.seriesTypes.rsi
  52. *
  53. * @augments Highcharts.Series
  54. */
  55. H.seriesType('rsi', 'sma',
  56. /**
  57. * Relative strength index (RSI) technical indicator. This series
  58. * requires the `linkedTo` option to be set and should be loaded after
  59. * the `stock/indicators/indicators.js` file.
  60. *
  61. * @sample stock/indicators/rsi
  62. * RSI indicator
  63. *
  64. * @extends plotOptions.sma
  65. * @since 6.0.0
  66. * @product highstock
  67. * @requires stock/indicators/indicators
  68. * @requires stock/indicators/rsi
  69. * @optionparent plotOptions.rsi
  70. */
  71. {
  72. /**
  73. * @excluding index
  74. */
  75. params: {
  76. period: 14,
  77. /**
  78. * Number of maximum decimals that are used in RSI calculations.
  79. */
  80. decimals: 4
  81. }
  82. },
  83. /**
  84. * @lends Highcharts.Series#
  85. */
  86. {
  87. getValues: function (series, params) {
  88. var period = params.period, xVal = series.xData, yVal = series.yData, yValLen = yVal ? yVal.length : 0, decimals = params.decimals,
  89. // RSI starts calculations from the second point
  90. // Cause we need to calculate change between two points
  91. range = 1, RSI = [], xData = [], yData = [], index = 3, gain = 0, loss = 0, RSIPoint, change, avgGain, avgLoss, i;
  92. // RSI requires close value
  93. if ((xVal.length < period) || !isArray(yVal[0]) ||
  94. yVal[0].length !== 4) {
  95. return;
  96. }
  97. // Calculate changes for first N points
  98. while (range < period) {
  99. change = toFixed(yVal[range][index] - yVal[range - 1][index], decimals);
  100. if (change > 0) {
  101. gain += change;
  102. }
  103. else {
  104. loss += Math.abs(change);
  105. }
  106. range++;
  107. }
  108. // Average for first n-1 points:
  109. avgGain = toFixed(gain / (period - 1), decimals);
  110. avgLoss = toFixed(loss / (period - 1), decimals);
  111. for (i = range; i < yValLen; i++) {
  112. change = toFixed(yVal[i][index] - yVal[i - 1][index], decimals);
  113. if (change > 0) {
  114. gain = change;
  115. loss = 0;
  116. }
  117. else {
  118. gain = 0;
  119. loss = Math.abs(change);
  120. }
  121. // Calculate smoothed averages, RS, RSI values:
  122. avgGain = toFixed((avgGain * (period - 1) + gain) / period, decimals);
  123. avgLoss = toFixed((avgLoss * (period - 1) + loss) / period, decimals);
  124. // If average-loss is equal zero, then by definition RSI is set
  125. // to 100:
  126. if (avgLoss === 0) {
  127. RSIPoint = 100;
  128. // If average-gain is equal zero, then by definition RSI is set
  129. // to 0:
  130. }
  131. else if (avgGain === 0) {
  132. RSIPoint = 0;
  133. }
  134. else {
  135. RSIPoint = toFixed(100 - (100 / (1 + (avgGain / avgLoss))), decimals);
  136. }
  137. RSI.push([xVal[i], RSIPoint]);
  138. xData.push(xVal[i]);
  139. yData.push(RSIPoint);
  140. }
  141. return {
  142. values: RSI,
  143. xData: xData,
  144. yData: yData
  145. };
  146. }
  147. });
  148. /**
  149. * A `RSI` series. If the [type](#series.rsi.type) option is not
  150. * specified, it is inherited from [chart.type](#chart.type).
  151. *
  152. * @extends series,plotOptions.rsi
  153. * @since 6.0.0
  154. * @product highstock
  155. * @excluding dataParser, dataURL
  156. * @requires stock/indicators/indicators
  157. * @requires stock/indicators/rsi
  158. * @apioption series.rsi
  159. */
  160. ''; // to include the above in the js output
  161. });
  162. _registerModule(_modules, 'masters/indicators/rsi.src.js', [], function () {
  163. });
  164. }));