ATRIndicator.js 3.3 KB

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