atr.src.js 5.2 KB

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