vwap.src.js 4.9 KB

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