rsi.src.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /**
  2. * @license Highstock JS v9.0.1 (2021-02-16)
  3. *
  4. * Indicator series type for Highstock
  5. *
  6. * (c) 2010-2021 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/RSI/RSIIndicator.js', [_modules['Core/Series/SeriesRegistry.js'], _modules['Core/Utilities.js']], function (SeriesRegistry, U) {
  32. /* *
  33. *
  34. * License: www.highcharts.com/license
  35. *
  36. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  37. *
  38. * */
  39. var __extends = (this && this.__extends) || (function () {
  40. var extendStatics = function (d,
  41. b) {
  42. extendStatics = Object.setPrototypeOf ||
  43. ({ __proto__: [] } instanceof Array && function (d,
  44. b) { d.__proto__ = b; }) ||
  45. function (d,
  46. b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  47. return extendStatics(d, b);
  48. };
  49. return function (d, b) {
  50. extendStatics(d, b);
  51. function __() { this.constructor = d; }
  52. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  53. };
  54. })();
  55. var SMAIndicator = SeriesRegistry.seriesTypes.sma;
  56. var isArray = U.isArray,
  57. merge = U.merge;
  58. /* eslint-disable require-jsdoc */
  59. // Utils:
  60. function toFixed(a, n) {
  61. return parseFloat(a.toFixed(n));
  62. }
  63. /* eslint-enable require-jsdoc */
  64. /**
  65. * The RSI series type.
  66. *
  67. * @private
  68. * @class
  69. * @name Highcharts.seriesTypes.rsi
  70. *
  71. * @augments Highcharts.Series
  72. */
  73. var RSIIndicator = /** @class */ (function (_super) {
  74. __extends(RSIIndicator, _super);
  75. function RSIIndicator() {
  76. var _this = _super !== null && _super.apply(this,
  77. arguments) || this;
  78. /* *
  79. *
  80. * Properties
  81. *
  82. * */
  83. _this.data = void 0;
  84. _this.points = void 0;
  85. _this.options = void 0;
  86. return _this;
  87. }
  88. /* *
  89. *
  90. * Functions
  91. *
  92. * */
  93. RSIIndicator.prototype.getValues = function (series, params) {
  94. var period = params.period,
  95. xVal = series.xData,
  96. yVal = series.yData,
  97. yValLen = yVal ? yVal.length : 0,
  98. decimals = params.decimals,
  99. // RSI starts calculations from the second point
  100. // Cause we need to calculate change between two points
  101. range = 1,
  102. RSI = [],
  103. xData = [],
  104. yData = [],
  105. index = 3,
  106. gain = 0,
  107. loss = 0,
  108. RSIPoint,
  109. change,
  110. avgGain,
  111. avgLoss,
  112. i;
  113. // RSI requires close value
  114. if ((xVal.length < period) || !isArray(yVal[0]) ||
  115. yVal[0].length !== 4) {
  116. return;
  117. }
  118. // Calculate changes for first N points
  119. while (range < period) {
  120. change = toFixed(yVal[range][index] - yVal[range - 1][index], decimals);
  121. if (change > 0) {
  122. gain += change;
  123. }
  124. else {
  125. loss += Math.abs(change);
  126. }
  127. range++;
  128. }
  129. // Average for first n-1 points:
  130. avgGain = toFixed(gain / (period - 1), decimals);
  131. avgLoss = toFixed(loss / (period - 1), decimals);
  132. for (i = range; i < yValLen; i++) {
  133. change = toFixed(yVal[i][index] - yVal[i - 1][index], decimals);
  134. if (change > 0) {
  135. gain = change;
  136. loss = 0;
  137. }
  138. else {
  139. gain = 0;
  140. loss = Math.abs(change);
  141. }
  142. // Calculate smoothed averages, RS, RSI values:
  143. avgGain = toFixed((avgGain * (period - 1) + gain) / period, decimals);
  144. avgLoss = toFixed((avgLoss * (period - 1) + loss) / period, decimals);
  145. // If average-loss is equal zero, then by definition RSI is set
  146. // to 100:
  147. if (avgLoss === 0) {
  148. RSIPoint = 100;
  149. // If average-gain is equal zero, then by definition RSI is set
  150. // to 0:
  151. }
  152. else if (avgGain === 0) {
  153. RSIPoint = 0;
  154. }
  155. else {
  156. RSIPoint = toFixed(100 - (100 / (1 + (avgGain / avgLoss))), decimals);
  157. }
  158. RSI.push([xVal[i], RSIPoint]);
  159. xData.push(xVal[i]);
  160. yData.push(RSIPoint);
  161. }
  162. return {
  163. values: RSI,
  164. xData: xData,
  165. yData: yData
  166. };
  167. };
  168. /**
  169. * Relative strength index (RSI) technical indicator. This series
  170. * requires the `linkedTo` option to be set and should be loaded after
  171. * the `stock/indicators/indicators.js` file.
  172. *
  173. * @sample stock/indicators/rsi
  174. * RSI indicator
  175. *
  176. * @extends plotOptions.sma
  177. * @since 6.0.0
  178. * @product highstock
  179. * @requires stock/indicators/indicators
  180. * @requires stock/indicators/rsi
  181. * @optionparent plotOptions.rsi
  182. */
  183. RSIIndicator.defaultOptions = merge(SMAIndicator.defaultOptions, {
  184. params: {
  185. period: 14,
  186. decimals: 4
  187. }
  188. });
  189. return RSIIndicator;
  190. }(SMAIndicator));
  191. SeriesRegistry.registerSeriesType('rsi', RSIIndicator);
  192. /* *
  193. *
  194. * Default Export
  195. *
  196. * */
  197. /**
  198. * A `RSI` series. If the [type](#series.rsi.type) option is not
  199. * specified, it is inherited from [chart.type](#chart.type).
  200. *
  201. * @extends series,plotOptions.rsi
  202. * @since 6.0.0
  203. * @product highstock
  204. * @excluding dataParser, dataURL
  205. * @requires stock/indicators/indicators
  206. * @requires stock/indicators/rsi
  207. * @apioption series.rsi
  208. */
  209. ''; // to include the above in the js output
  210. return RSIIndicator;
  211. });
  212. _registerModule(_modules, 'masters/indicators/rsi.src.js', [], function () {
  213. });
  214. }));