VWAPIndicator.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /* *
  2. *
  3. * (c) 2010-2020 Paweł Dalek
  4. *
  5. * Volume Weighted Average Price (VWAP) indicator for Highstock
  6. *
  7. * License: www.highcharts.com/license
  8. *
  9. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  10. *
  11. * */
  12. 'use strict';
  13. import U from '../../Core/Utilities.js';
  14. var error = U.error, isArray = U.isArray, seriesType = U.seriesType;
  15. /**
  16. * The Volume Weighted Average Price (VWAP) series type.
  17. *
  18. * @private
  19. * @class
  20. * @name Highcharts.seriesTypes.vwap
  21. *
  22. * @augments Highcharts.Series
  23. */
  24. seriesType('vwap', 'sma',
  25. /**
  26. * Volume Weighted Average Price indicator.
  27. *
  28. * This series requires `linkedTo` option to be set.
  29. *
  30. * @sample stock/indicators/vwap
  31. * Volume Weighted Average Price indicator
  32. *
  33. * @extends plotOptions.sma
  34. * @since 6.0.0
  35. * @product highstock
  36. * @requires stock/indicators/indicators
  37. * @requires stock/indicators/vwap
  38. * @optionparent plotOptions.vwap
  39. */
  40. {
  41. /**
  42. * @excluding index
  43. */
  44. params: {
  45. period: 30,
  46. /**
  47. * The id of volume series which is mandatory. For example using
  48. * OHLC data, volumeSeriesID='volume' means the indicator will be
  49. * calculated using OHLC and volume values.
  50. */
  51. volumeSeriesID: 'volume'
  52. }
  53. },
  54. /**
  55. * @lends Highcharts.Series#
  56. */
  57. {
  58. /**
  59. * Returns the final values of the indicator ready to be presented on a
  60. * chart
  61. * @private
  62. * @param {Highcharts.VWAPIndicator} this indicator
  63. * @param {Highcharts.Series} series - series for indicator
  64. * @param {object} params - params
  65. * @return {object} - computed VWAP
  66. **/
  67. getValues: function (series, params) {
  68. var indicator = this, chart = series.chart, xValues = series.xData, yValues = series.yData, period = params.period, isOHLC = true, volumeSeries;
  69. // Checks if volume series exists
  70. if (!(volumeSeries = (chart.get(params.volumeSeriesID)))) {
  71. error('Series ' +
  72. params.volumeSeriesID +
  73. ' not found! Check `volumeSeriesID`.', true, chart);
  74. return;
  75. }
  76. // Checks if series data fits the OHLC format
  77. if (!(isArray(yValues[0]))) {
  78. isOHLC = false;
  79. }
  80. return indicator.calculateVWAPValues(isOHLC, xValues, yValues, volumeSeries, period);
  81. },
  82. /**
  83. * Main algorithm used to calculate Volume Weighted Average Price (VWAP)
  84. * values
  85. * @private
  86. * @param {boolean} isOHLC - says if data has OHLC format
  87. * @param {Array<number>} xValues - array of timestamps
  88. * @param {Array<number|Array<number,number,number,number>>} yValues -
  89. * array of yValues, can be an array of a four arrays (OHLC) or array of
  90. * values (line)
  91. * @param {Array<*>} volumeSeries - volume series
  92. * @param {number} period - number of points to be calculated
  93. * @return {object} - Object contains computed VWAP
  94. **/
  95. calculateVWAPValues: function (isOHLC, xValues, yValues, volumeSeries, period) {
  96. var volumeValues = volumeSeries.yData, volumeLength = volumeSeries.xData.length, pointsLength = xValues.length, cumulativePrice = [], cumulativeVolume = [], xData = [], yData = [], VWAP = [], commonLength, typicalPrice, cPrice, cVolume, i, j;
  97. if (pointsLength <= volumeLength) {
  98. commonLength = pointsLength;
  99. }
  100. else {
  101. commonLength = volumeLength;
  102. }
  103. for (i = 0, j = 0; i < commonLength; i++) {
  104. // Depending on whether series is OHLC or line type, price is
  105. // average of the high, low and close or a simple value
  106. typicalPrice = isOHLC ?
  107. ((yValues[i][1] + yValues[i][2] +
  108. yValues[i][3]) / 3) :
  109. yValues[i];
  110. typicalPrice *= volumeValues[i];
  111. cPrice = j ?
  112. (cumulativePrice[i - 1] + typicalPrice) :
  113. typicalPrice;
  114. cVolume = j ?
  115. (cumulativeVolume[i - 1] + volumeValues[i]) :
  116. volumeValues[i];
  117. cumulativePrice.push(cPrice);
  118. cumulativeVolume.push(cVolume);
  119. VWAP.push([xValues[i], (cPrice / cVolume)]);
  120. xData.push(VWAP[i][0]);
  121. yData.push(VWAP[i][1]);
  122. j++;
  123. if (j === period) {
  124. j = 0;
  125. }
  126. }
  127. return {
  128. values: VWAP,
  129. xData: xData,
  130. yData: yData
  131. };
  132. }
  133. });
  134. /**
  135. * A `Volume Weighted Average Price (VWAP)` series. If the
  136. * [type](#series.vwap.type) option is not specified, it is inherited from
  137. * [chart.type](#chart.type).
  138. *
  139. * @extends series,plotOptions.vwap
  140. * @since 6.0.0
  141. * @product highstock
  142. * @excluding dataParser, dataURL
  143. * @requires stock/indicators/indicators
  144. * @requires stock/indicators/vwap
  145. * @apioption series.vwap
  146. */
  147. ''; // to include the above in the js output