vwap.src.js 7.5 KB

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