vwap.src.js 7.1 KB

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