ADIndicator.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /* *
  2. *
  3. * License: www.highcharts.com/license
  4. *
  5. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  6. * */
  7. 'use strict';
  8. import U from '../../Core/Utilities.js';
  9. var error = U.error, seriesType = U.seriesType;
  10. /* eslint-disable valid-jsdoc */
  11. // Utils:
  12. /**
  13. * @private
  14. */
  15. function populateAverage(xVal, yVal, yValVolume, i) {
  16. var high = yVal[i][1], low = yVal[i][2], close = yVal[i][3], volume = yValVolume[i], adY = close === high && close === low || high === low ?
  17. 0 :
  18. ((2 * close - low - high) / (high - low)) * volume, adX = xVal[i];
  19. return [adX, adY];
  20. }
  21. /* eslint-enable valid-jsdoc */
  22. /**
  23. * The AD series type.
  24. *
  25. * @private
  26. * @class
  27. * @name Highcharts.seriesTypes.ad
  28. *
  29. * @augments Highcharts.Series
  30. */
  31. seriesType('ad', 'sma',
  32. /**
  33. * Accumulation Distribution (AD). This series requires `linkedTo` option to
  34. * be set.
  35. *
  36. * @sample stock/indicators/accumulation-distribution
  37. * Accumulation/Distribution indicator
  38. *
  39. * @extends plotOptions.sma
  40. * @since 6.0.0
  41. * @product highstock
  42. * @requires stock/indicators/indicators
  43. * @requires stock/indicators/accumulation-distribution
  44. * @optionparent plotOptions.ad
  45. */
  46. {
  47. params: {
  48. /**
  49. * The id of volume series which is mandatory.
  50. * For example using OHLC data, volumeSeriesID='volume' means
  51. * the indicator will be calculated using OHLC and volume values.
  52. *
  53. * @since 6.0.0
  54. */
  55. volumeSeriesID: 'volume'
  56. }
  57. },
  58. /**
  59. * @lends Highcharts.Series#
  60. */
  61. {
  62. nameComponents: false,
  63. nameBase: 'Accumulation/Distribution',
  64. getValues: function (series, params) {
  65. var period = params.period, xVal = series.xData, yVal = series.yData, volumeSeriesID = params.volumeSeriesID, volumeSeries = series.chart.get(volumeSeriesID), yValVolume = volumeSeries && volumeSeries.yData, yValLen = yVal ? yVal.length : 0, AD = [], xData = [], yData = [], len, i, ADPoint;
  66. if (xVal.length <= period &&
  67. yValLen &&
  68. yVal[0].length !== 4) {
  69. return;
  70. }
  71. if (!volumeSeries) {
  72. error('Series ' +
  73. volumeSeriesID +
  74. ' not found! Check `volumeSeriesID`.', true, series.chart);
  75. return;
  76. }
  77. // i = period <-- skip first N-points
  78. // Calculate value one-by-one for each period in visible data
  79. for (i = period; i < yValLen; i++) {
  80. len = AD.length;
  81. ADPoint = populateAverage(xVal, yVal, yValVolume, i, period);
  82. if (len > 0) {
  83. ADPoint[1] += AD[len - 1][1];
  84. }
  85. AD.push(ADPoint);
  86. xData.push(ADPoint[0]);
  87. yData.push(ADPoint[1]);
  88. }
  89. return {
  90. values: AD,
  91. xData: xData,
  92. yData: yData
  93. };
  94. }
  95. });
  96. /**
  97. * A `AD` series. If the [type](#series.ad.type) option is not
  98. * specified, it is inherited from [chart.type](#chart.type).
  99. *
  100. * @extends series,plotOptions.ad
  101. * @since 6.0.0
  102. * @excluding dataParser, dataURL
  103. * @product highstock
  104. * @requires stock/indicators/indicators
  105. * @requires stock/indicators/accumulation-distribution
  106. * @apioption series.ad
  107. */
  108. ''; // add doclet above to transpiled file