atr.src.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /* *
  2. *
  3. * License: www.highcharts.com/license
  4. *
  5. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  6. *
  7. * */
  8. 'use strict';
  9. import H from '../parts/Globals.js';
  10. import U from '../parts/Utilities.js';
  11. var isArray = U.isArray;
  12. var seriesType = H.seriesType, UNDEFINED;
  13. /* eslint-disable valid-jsdoc */
  14. // Utils:
  15. /**
  16. * @private
  17. */
  18. function accumulateAverage(points, xVal, yVal, i) {
  19. var xValue = xVal[i], yValue = yVal[i];
  20. points.push([xValue, yValue]);
  21. }
  22. /**
  23. * @private
  24. */
  25. function getTR(currentPoint, prevPoint) {
  26. 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);
  27. return TR;
  28. }
  29. /**
  30. * @private
  31. */
  32. function populateAverage(points, xVal, yVal, i, period, prevATR) {
  33. var x = xVal[i - 1], TR = getTR(yVal[i - 1], yVal[i - 2]), y;
  34. y = (((prevATR * (period - 1)) + TR) / period);
  35. return [x, y];
  36. }
  37. /* eslint-enable valid-jsdoc */
  38. /**
  39. * The ATR series type.
  40. *
  41. * @private
  42. * @class
  43. * @name Highcharts.seriesTypes.atr
  44. *
  45. * @augments Highcharts.Series
  46. */
  47. seriesType('atr', 'sma',
  48. /**
  49. * Average true range indicator (ATR). This series requires `linkedTo`
  50. * option to be set.
  51. *
  52. * @sample stock/indicators/atr
  53. * ATR indicator
  54. *
  55. * @extends plotOptions.sma
  56. * @since 6.0.0
  57. * @product highstock
  58. * @requires stock/indicators/indicators
  59. * @requires stock/indicators/atr
  60. * @optionparent plotOptions.atr
  61. */
  62. {
  63. params: {
  64. period: 14
  65. }
  66. },
  67. /**
  68. * @lends Highcharts.Series#
  69. */
  70. {
  71. getValues: function (series, params) {
  72. 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;
  73. points = [[xValue, yValue]];
  74. if ((xVal.length <= period) ||
  75. !isArray(yVal[0]) ||
  76. yVal[0].length !== 4) {
  77. return;
  78. }
  79. for (i = 1; i <= yValLen; i++) {
  80. accumulateAverage(points, xVal, yVal, i);
  81. if (period < range) {
  82. point = populateAverage(points, xVal, yVal, i, period, prevATR);
  83. prevATR = point[1];
  84. ATR.push(point);
  85. xData.push(point[0]);
  86. yData.push(point[1]);
  87. }
  88. else if (period === range) {
  89. prevATR = TR / (i - 1);
  90. ATR.push([xVal[i - 1], prevATR]);
  91. xData.push(xVal[i - 1]);
  92. yData.push(prevATR);
  93. range++;
  94. }
  95. else {
  96. TR += getTR(yVal[i - 1], yVal[i - 2]);
  97. range++;
  98. }
  99. }
  100. return {
  101. values: ATR,
  102. xData: xData,
  103. yData: yData
  104. };
  105. }
  106. });
  107. /**
  108. * A `ATR` series. If the [type](#series.atr.type) option is not specified, it
  109. * is inherited from [chart.type](#chart.type).
  110. *
  111. * @extends series,plotOptions.atr
  112. * @since 6.0.0
  113. * @product highstock
  114. * @excluding dataParser, dataURL
  115. * @requires stock/indicators/indicators
  116. * @requires stock/indicators/atr
  117. * @apioption series.atr
  118. */
  119. ''; // to include the above in the js output