rsi.src.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /**
  2. * @license Highstock JS v9.1.1 (2021-06-04)
  3. *
  4. * Indicator series type for Highcharts Stock
  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 isNumber = U.isNumber,
  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 = params.index,
  106. gain = 0,
  107. loss = 0,
  108. RSIPoint,
  109. change,
  110. avgGain,
  111. avgLoss,
  112. i,
  113. values;
  114. if ((xVal.length < period)) {
  115. return;
  116. }
  117. if (isNumber(yVal[0])) {
  118. values = yVal;
  119. }
  120. else {
  121. // in case of the situation, where the series type has data length
  122. // longer then 4 (HLC, range), this ensures that we are not trying
  123. // to reach the index out of bounds
  124. index = Math.min(index, yVal[0].length - 1);
  125. values = yVal.map(function (value) { return value[index]; });
  126. }
  127. // Calculate changes for first N points
  128. while (range < period) {
  129. change = toFixed(values[range] - values[range - 1], decimals);
  130. if (change > 0) {
  131. gain += change;
  132. }
  133. else {
  134. loss += Math.abs(change);
  135. }
  136. range++;
  137. }
  138. // Average for first n-1 points:
  139. avgGain = toFixed(gain / (period - 1), decimals);
  140. avgLoss = toFixed(loss / (period - 1), decimals);
  141. for (i = range; i < yValLen; i++) {
  142. change = toFixed(values[i] - values[i - 1], decimals);
  143. if (change > 0) {
  144. gain = change;
  145. loss = 0;
  146. }
  147. else {
  148. gain = 0;
  149. loss = Math.abs(change);
  150. }
  151. // Calculate smoothed averages, RS, RSI values:
  152. avgGain = toFixed((avgGain * (period - 1) + gain) / period, decimals);
  153. avgLoss = toFixed((avgLoss * (period - 1) + loss) / period, decimals);
  154. // If average-loss is equal zero, then by definition RSI is set
  155. // to 100:
  156. if (avgLoss === 0) {
  157. RSIPoint = 100;
  158. // If average-gain is equal zero, then by definition RSI is set
  159. // to 0:
  160. }
  161. else if (avgGain === 0) {
  162. RSIPoint = 0;
  163. }
  164. else {
  165. RSIPoint = toFixed(100 - (100 / (1 + (avgGain / avgLoss))), decimals);
  166. }
  167. RSI.push([xVal[i], RSIPoint]);
  168. xData.push(xVal[i]);
  169. yData.push(RSIPoint);
  170. }
  171. return {
  172. values: RSI,
  173. xData: xData,
  174. yData: yData
  175. };
  176. };
  177. /**
  178. * Relative strength index (RSI) technical indicator. This series
  179. * requires the `linkedTo` option to be set and should be loaded after
  180. * the `stock/indicators/indicators.js` file.
  181. *
  182. * @sample stock/indicators/rsi
  183. * RSI indicator
  184. *
  185. * @extends plotOptions.sma
  186. * @since 6.0.0
  187. * @product highstock
  188. * @requires stock/indicators/indicators
  189. * @requires stock/indicators/rsi
  190. * @optionparent plotOptions.rsi
  191. */
  192. RSIIndicator.defaultOptions = merge(SMAIndicator.defaultOptions, {
  193. params: {
  194. decimals: 4,
  195. index: 3
  196. }
  197. });
  198. return RSIIndicator;
  199. }(SMAIndicator));
  200. SeriesRegistry.registerSeriesType('rsi', RSIIndicator);
  201. /* *
  202. *
  203. * Default Export
  204. *
  205. * */
  206. /**
  207. * A `RSI` series. If the [type](#series.rsi.type) option is not
  208. * specified, it is inherited from [chart.type](#chart.type).
  209. *
  210. * @extends series,plotOptions.rsi
  211. * @since 6.0.0
  212. * @product highstock
  213. * @excluding dataParser, dataURL
  214. * @requires stock/indicators/indicators
  215. * @requires stock/indicators/rsi
  216. * @apioption series.rsi
  217. */
  218. ''; // to include the above in the js output
  219. return RSIIndicator;
  220. });
  221. _registerModule(_modules, 'masters/indicators/rsi.src.js', [], function () {
  222. });
  223. }));