atr.src.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /**
  2. * @license Highstock JS v9.1.1 (2021-06-04)
  3. *
  4. * Indicator series type for Highcharts Stock
  5. *
  6. * (c) 2010-2021 Sebastian Bochan
  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/atr', ['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/ATR/ATRIndicator.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 valid-jsdoc */
  59. // Utils:
  60. /**
  61. * @private
  62. */
  63. function accumulateAverage(points, xVal, yVal, i) {
  64. var xValue = xVal[i],
  65. yValue = yVal[i];
  66. points.push([xValue, yValue]);
  67. }
  68. /**
  69. * @private
  70. */
  71. function getTR(currentPoint, prevPoint) {
  72. var pointY = currentPoint, prevY = prevPoint, HL = pointY[1] - pointY[2], HCp = typeof prevY === 'undefined' ? 0 : Math.abs(pointY[1] - prevY[3]), LCp = typeof prevY === 'undefined' ? 0 : Math.abs(pointY[2] - prevY[3]), TR = Math.max(HL, HCp, LCp);
  73. return TR;
  74. }
  75. /**
  76. * @private
  77. */
  78. function populateAverage(points, xVal, yVal, i, period, prevATR) {
  79. var x = xVal[i - 1],
  80. TR = getTR(yVal[i - 1],
  81. yVal[i - 2]),
  82. y;
  83. y = (((prevATR * (period - 1)) + TR) / period);
  84. return [x, y];
  85. }
  86. /* eslint-enable valid-jsdoc */
  87. /* *
  88. *
  89. * Class
  90. *
  91. * */
  92. /**
  93. * The ATR series type.
  94. *
  95. * @private
  96. * @class
  97. * @name Highcharts.seriesTypes.atr
  98. *
  99. * @augments Highcharts.Series
  100. */
  101. var ATRIndicator = /** @class */ (function (_super) {
  102. __extends(ATRIndicator, _super);
  103. function ATRIndicator() {
  104. var _this = _super !== null && _super.apply(this,
  105. arguments) || this;
  106. /* *
  107. *
  108. * Properties
  109. *
  110. * */
  111. _this.data = void 0;
  112. _this.points = void 0;
  113. _this.options = void 0;
  114. return _this;
  115. }
  116. /* *
  117. *
  118. * Functions
  119. *
  120. * */
  121. ATRIndicator.prototype.getValues = function (series, params) {
  122. var period = params.period,
  123. xVal = series.xData,
  124. yVal = series.yData,
  125. yValLen = yVal ? yVal.length : 0,
  126. xValue = xVal[0],
  127. yValue = yVal[0],
  128. range = 1,
  129. prevATR = 0,
  130. TR = 0,
  131. ATR = [],
  132. xData = [],
  133. yData = [],
  134. point,
  135. i,
  136. points;
  137. points = [[xValue, yValue]];
  138. if ((xVal.length <= period) ||
  139. !isArray(yVal[0]) ||
  140. yVal[0].length !== 4) {
  141. return;
  142. }
  143. for (i = 1; i <= yValLen; i++) {
  144. accumulateAverage(points, xVal, yVal, i);
  145. if (period < range) {
  146. point = populateAverage(points, xVal, yVal, i, period, prevATR);
  147. prevATR = point[1];
  148. ATR.push(point);
  149. xData.push(point[0]);
  150. yData.push(point[1]);
  151. }
  152. else if (period === range) {
  153. prevATR = TR / (i - 1);
  154. ATR.push([xVal[i - 1], prevATR]);
  155. xData.push(xVal[i - 1]);
  156. yData.push(prevATR);
  157. range++;
  158. }
  159. else {
  160. TR += getTR(yVal[i - 1], yVal[i - 2]);
  161. range++;
  162. }
  163. }
  164. return {
  165. values: ATR,
  166. xData: xData,
  167. yData: yData
  168. };
  169. };
  170. /**
  171. * Average true range indicator (ATR). This series requires `linkedTo`
  172. * option to be set.
  173. *
  174. * @sample stock/indicators/atr
  175. * ATR indicator
  176. *
  177. * @extends plotOptions.sma
  178. * @since 6.0.0
  179. * @product highstock
  180. * @requires stock/indicators/indicators
  181. * @requires stock/indicators/atr
  182. * @optionparent plotOptions.atr
  183. */
  184. ATRIndicator.defaultOptions = merge(SMAIndicator.defaultOptions, {
  185. /**
  186. * @excluding index
  187. */
  188. params: {
  189. index: void 0 // unused index, do not inherit (#15362)
  190. }
  191. });
  192. return ATRIndicator;
  193. }(SMAIndicator));
  194. SeriesRegistry.registerSeriesType('atr', ATRIndicator);
  195. /* *
  196. *
  197. * Default Export
  198. *
  199. * */
  200. /**
  201. * A `ATR` series. If the [type](#series.atr.type) option is not specified, it
  202. * is inherited from [chart.type](#chart.type).
  203. *
  204. * @extends series,plotOptions.atr
  205. * @since 6.0.0
  206. * @product highstock
  207. * @excluding dataParser, dataURL
  208. * @requires stock/indicators/indicators
  209. * @requires stock/indicators/atr
  210. * @apioption series.atr
  211. */
  212. ''; // to include the above in the js output
  213. return ATRIndicator;
  214. });
  215. _registerModule(_modules, 'masters/indicators/atr.src.js', [], function () {
  216. });
  217. }));