ChaikinIndicator.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 '../../Core/Globals.js';
  10. import './ADIndicator.js';
  11. import U from '../../Core/Utilities.js';
  12. var correctFloat = U.correctFloat, error = U.error, seriesType = U.seriesType;
  13. import requiredIndicator from '../../Mixins/IndicatorRequired.js';
  14. var EMA = H.seriesTypes.ema, AD = H.seriesTypes.ad;
  15. /**
  16. * The Chaikin series type.
  17. *
  18. * @private
  19. * @class
  20. * @name Highcharts.seriesTypes.chaikin
  21. *
  22. * @augments Highcharts.Series
  23. */
  24. seriesType('chaikin', 'ema',
  25. /**
  26. * Chaikin Oscillator. This series requires the `linkedTo` option to
  27. * be set and should be loaded after the `stock/indicators/indicators.js`
  28. * and `stock/indicators/ema.js`.
  29. *
  30. * @sample {highstock} stock/indicators/chaikin
  31. * Chaikin Oscillator
  32. *
  33. * @extends plotOptions.ema
  34. * @since 7.0.0
  35. * @product highstock
  36. * @excluding allAreas, colorAxis, joinBy, keys, navigatorOptions,
  37. * pointInterval, pointIntervalUnit, pointPlacement,
  38. * pointRange, pointStart, showInNavigator, stacking
  39. * @requires stock/indicators/indicators
  40. * @requires stock/indicators/ema
  41. * @requires stock/indicators/chaikin
  42. * @optionparent plotOptions.chaikin
  43. */
  44. {
  45. /**
  46. * Paramters used in calculation of Chaikin Oscillator
  47. * series points.
  48. *
  49. * @excluding index, period
  50. */
  51. params: {
  52. /**
  53. * The id of volume series which is mandatory.
  54. * For example using OHLC data, volumeSeriesID='volume' means
  55. * the indicator will be calculated using OHLC and volume values.
  56. */
  57. volumeSeriesID: 'volume',
  58. /**
  59. * Periods for Chaikin Oscillator calculations.
  60. *
  61. * @type {Array<number>}
  62. * @default [3, 10]
  63. */
  64. periods: [3, 10]
  65. }
  66. },
  67. /**
  68. * @lends Highcharts.Series#
  69. */
  70. {
  71. nameBase: 'Chaikin Osc',
  72. nameComponents: ['periods'],
  73. init: function () {
  74. var args = arguments, ctx = this;
  75. requiredIndicator.isParentLoaded(EMA, 'ema', ctx.type, function (indicator) {
  76. indicator.prototype.init.apply(ctx, args);
  77. return;
  78. });
  79. },
  80. getValues: function (series, params) {
  81. var periods = params.periods, period = params.period,
  82. // Accumulation Distribution Line data
  83. ADL,
  84. // 0- date, 1- Chaikin Oscillator
  85. CHA = [], xData = [], yData = [], periodsOffset,
  86. // Shorter Period EMA
  87. SPE,
  88. // Longer Period EMA
  89. LPE, oscillator, i;
  90. // Check if periods are correct
  91. if (periods.length !== 2 || periods[1] <= periods[0]) {
  92. error('Error: "Chaikin requires two periods. Notice, first ' +
  93. 'period should be lower than the second one."');
  94. return;
  95. }
  96. ADL = AD.prototype.getValues.call(this, series, {
  97. volumeSeriesID: params.volumeSeriesID,
  98. period: period
  99. });
  100. // Check if adl is calculated properly, if not skip
  101. if (!ADL) {
  102. return;
  103. }
  104. SPE = EMA.prototype.getValues.call(this, ADL, {
  105. period: periods[0]
  106. });
  107. LPE = EMA.prototype.getValues.call(this, ADL, {
  108. period: periods[1]
  109. });
  110. // Check if ema is calculated properly, if not skip
  111. if (!SPE || !LPE) {
  112. return;
  113. }
  114. periodsOffset = periods[1] - periods[0];
  115. for (i = 0; i < LPE.yData.length; i++) {
  116. oscillator = correctFloat(SPE.yData[i + periodsOffset] -
  117. LPE.yData[i]);
  118. CHA.push([LPE.xData[i], oscillator]);
  119. xData.push(LPE.xData[i]);
  120. yData.push(oscillator);
  121. }
  122. return {
  123. values: CHA,
  124. xData: xData,
  125. yData: yData
  126. };
  127. }
  128. });
  129. /**
  130. * A `Chaikin Oscillator` series. If the [type](#series.chaikin.type)
  131. * option is not specified, it is inherited from [chart.type](#chart.type).
  132. *
  133. * @extends series,plotOptions.chaikin
  134. * @since 7.0.0
  135. * @product highstock
  136. * @excluding allAreas, colorAxis, dataParser, dataURL, joinBy, keys,
  137. * navigatorOptions, pointInterval, pointIntervalUnit,
  138. * pointPlacement, pointRange, pointStart, stacking, showInNavigator
  139. * @requires stock/indicators/indicators
  140. * @requires stock/indicators/ema
  141. * @requires stock/indicators/chaikin
  142. * @apioption series.chaikin
  143. */
  144. ''; // to include the above in the js output