atr.src.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /**
  2. * @license Highstock JS v8.2.0 (2020-08-20)
  3. *
  4. * Indicator series type for Highstock
  5. *
  6. * (c) 2010-2019 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/ATRIndicator.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. var UNDEFINED;
  42. /* eslint-disable valid-jsdoc */
  43. // Utils:
  44. /**
  45. * @private
  46. */
  47. function accumulateAverage(points, xVal, yVal, i) {
  48. var xValue = xVal[i],
  49. yValue = yVal[i];
  50. points.push([xValue, yValue]);
  51. }
  52. /**
  53. * @private
  54. */
  55. function getTR(currentPoint, prevPoint) {
  56. var pointY = currentPoint,
  57. prevY = prevPoint,
  58. HL = pointY[1] - pointY[2],
  59. HCp = prevY === UNDEFINED ? 0 : Math.abs(pointY[1] - prevY[3]),
  60. LCp = prevY === UNDEFINED ? 0 : Math.abs(pointY[2] - prevY[3]),
  61. TR = Math.max(HL,
  62. HCp,
  63. LCp);
  64. return TR;
  65. }
  66. /**
  67. * @private
  68. */
  69. function populateAverage(points, xVal, yVal, i, period, prevATR) {
  70. var x = xVal[i - 1],
  71. TR = getTR(yVal[i - 1],
  72. yVal[i - 2]),
  73. y;
  74. y = (((prevATR * (period - 1)) + TR) / period);
  75. return [x, y];
  76. }
  77. /* eslint-enable valid-jsdoc */
  78. /**
  79. * The ATR series type.
  80. *
  81. * @private
  82. * @class
  83. * @name Highcharts.seriesTypes.atr
  84. *
  85. * @augments Highcharts.Series
  86. */
  87. seriesType('atr', 'sma',
  88. /**
  89. * Average true range indicator (ATR). This series requires `linkedTo`
  90. * option to be set.
  91. *
  92. * @sample stock/indicators/atr
  93. * ATR indicator
  94. *
  95. * @extends plotOptions.sma
  96. * @since 6.0.0
  97. * @product highstock
  98. * @requires stock/indicators/indicators
  99. * @requires stock/indicators/atr
  100. * @optionparent plotOptions.atr
  101. */
  102. {
  103. params: {
  104. period: 14
  105. }
  106. },
  107. /**
  108. * @lends Highcharts.Series#
  109. */
  110. {
  111. getValues: function (series, params) {
  112. var period = params.period,
  113. xVal = series.xData,
  114. yVal = series.yData,
  115. yValLen = yVal ? yVal.length : 0,
  116. xValue = xVal[0],
  117. yValue = yVal[0],
  118. range = 1,
  119. prevATR = 0,
  120. TR = 0,
  121. ATR = [],
  122. xData = [],
  123. yData = [],
  124. point,
  125. i,
  126. points;
  127. points = [[xValue, yValue]];
  128. if ((xVal.length <= period) ||
  129. !isArray(yVal[0]) ||
  130. yVal[0].length !== 4) {
  131. return;
  132. }
  133. for (i = 1; i <= yValLen; i++) {
  134. accumulateAverage(points, xVal, yVal, i);
  135. if (period < range) {
  136. point = populateAverage(points, xVal, yVal, i, period, prevATR);
  137. prevATR = point[1];
  138. ATR.push(point);
  139. xData.push(point[0]);
  140. yData.push(point[1]);
  141. }
  142. else if (period === range) {
  143. prevATR = TR / (i - 1);
  144. ATR.push([xVal[i - 1], prevATR]);
  145. xData.push(xVal[i - 1]);
  146. yData.push(prevATR);
  147. range++;
  148. }
  149. else {
  150. TR += getTR(yVal[i - 1], yVal[i - 2]);
  151. range++;
  152. }
  153. }
  154. return {
  155. values: ATR,
  156. xData: xData,
  157. yData: yData
  158. };
  159. }
  160. });
  161. /**
  162. * A `ATR` series. If the [type](#series.atr.type) option is not specified, it
  163. * is inherited from [chart.type](#chart.type).
  164. *
  165. * @extends series,plotOptions.atr
  166. * @since 6.0.0
  167. * @product highstock
  168. * @excluding dataParser, dataURL
  169. * @requires stock/indicators/indicators
  170. * @requires stock/indicators/atr
  171. * @apioption series.atr
  172. */
  173. ''; // to include the above in the js output
  174. });
  175. _registerModule(_modules, 'masters/indicators/atr.src.js', [], function () {
  176. });
  177. }));