vwap.src.js 7.1 KB

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