rsi.src.js 6.3 KB

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