vwap.src.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /**
  2. * @license Highstock JS v9.0.1 (2021-02-16)
  3. *
  4. * Indicator series type for Highstock
  5. *
  6. * (c) 2010-2021 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/VWAP/VWAPIndicator.js', [_modules['Core/Series/SeriesRegistry.js'], _modules['Core/Utilities.js']], function (SeriesRegistry, U) {
  32. /* *
  33. *
  34. * (c) 2010-2021 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 __extends = (this && this.__extends) || (function () {
  44. var extendStatics = function (d,
  45. b) {
  46. extendStatics = Object.setPrototypeOf ||
  47. ({ __proto__: [] } instanceof Array && function (d,
  48. b) { d.__proto__ = b; }) ||
  49. function (d,
  50. b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  51. return extendStatics(d, b);
  52. };
  53. return function (d, b) {
  54. extendStatics(d, b);
  55. function __() { this.constructor = d; }
  56. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  57. };
  58. })();
  59. var SMAIndicator = SeriesRegistry.seriesTypes.sma;
  60. var error = U.error,
  61. isArray = U.isArray,
  62. merge = U.merge;
  63. /* *
  64. *
  65. * Class
  66. *
  67. * */
  68. /**
  69. * The Volume Weighted Average Price (VWAP) series type.
  70. *
  71. * @private
  72. * @class
  73. * @name Highcharts.seriesTypes.vwap
  74. *
  75. * @augments Highcharts.Series
  76. */
  77. var VWAPIndicator = /** @class */ (function (_super) {
  78. __extends(VWAPIndicator, _super);
  79. function VWAPIndicator() {
  80. var _this = _super !== null && _super.apply(this,
  81. arguments) || this;
  82. /* *
  83. *
  84. * Properties
  85. *
  86. * */
  87. _this.data = void 0;
  88. _this.points = void 0;
  89. _this.options = void 0;
  90. return _this;
  91. }
  92. /* *
  93. *
  94. * Functions
  95. *
  96. * */
  97. VWAPIndicator.prototype.getValues = function (series, params) {
  98. var indicator = this,
  99. chart = series.chart,
  100. xValues = series.xData,
  101. yValues = series.yData,
  102. period = params.period,
  103. isOHLC = true,
  104. volumeSeries;
  105. // Checks if volume series exists
  106. if (!(volumeSeries = (chart.get(params.volumeSeriesID)))) {
  107. error('Series ' +
  108. params.volumeSeriesID +
  109. ' not found! Check `volumeSeriesID`.', true, chart);
  110. return;
  111. }
  112. // Checks if series data fits the OHLC format
  113. if (!(isArray(yValues[0]))) {
  114. isOHLC = false;
  115. }
  116. return indicator.calculateVWAPValues(isOHLC, xValues, yValues, volumeSeries, period);
  117. };
  118. /**
  119. * Main algorithm used to calculate Volume Weighted Average Price (VWAP)
  120. * values
  121. * @private
  122. * @param {boolean} isOHLC - says if data has OHLC format
  123. * @param {Array<number>} xValues - array of timestamps
  124. * @param {Array<number|Array<number,number,number,number>>} yValues -
  125. * array of yValues, can be an array of a four arrays (OHLC) or array of
  126. * values (line)
  127. * @param {Array<*>} volumeSeries - volume series
  128. * @param {number} period - number of points to be calculated
  129. * @return {object} - Object contains computed VWAP
  130. **/
  131. VWAPIndicator.prototype.calculateVWAPValues = function (isOHLC, xValues, yValues, volumeSeries, period) {
  132. var volumeValues = volumeSeries.yData,
  133. volumeLength = volumeSeries.xData.length,
  134. pointsLength = xValues.length,
  135. cumulativePrice = [],
  136. cumulativeVolume = [],
  137. xData = [],
  138. yData = [],
  139. VWAP = [],
  140. commonLength,
  141. typicalPrice,
  142. cPrice,
  143. cVolume,
  144. i,
  145. j;
  146. if (pointsLength <= volumeLength) {
  147. commonLength = pointsLength;
  148. }
  149. else {
  150. commonLength = volumeLength;
  151. }
  152. for (i = 0, j = 0; i < commonLength; i++) {
  153. // Depending on whether series is OHLC or line type, price is
  154. // average of the high, low and close or a simple value
  155. typicalPrice = isOHLC ?
  156. ((yValues[i][1] + yValues[i][2] +
  157. yValues[i][3]) / 3) :
  158. yValues[i];
  159. typicalPrice *= volumeValues[i];
  160. cPrice = j ?
  161. (cumulativePrice[i - 1] + typicalPrice) :
  162. typicalPrice;
  163. cVolume = j ?
  164. (cumulativeVolume[i - 1] + volumeValues[i]) :
  165. volumeValues[i];
  166. cumulativePrice.push(cPrice);
  167. cumulativeVolume.push(cVolume);
  168. VWAP.push([xValues[i], (cPrice / cVolume)]);
  169. xData.push(VWAP[i][0]);
  170. yData.push(VWAP[i][1]);
  171. j++;
  172. if (j === period) {
  173. j = 0;
  174. }
  175. }
  176. return {
  177. values: VWAP,
  178. xData: xData,
  179. yData: yData
  180. };
  181. };
  182. /**
  183. * Volume Weighted Average Price indicator.
  184. *
  185. * This series requires `linkedTo` option to be set.
  186. *
  187. * @sample stock/indicators/vwap
  188. * Volume Weighted Average Price indicator
  189. *
  190. * @extends plotOptions.sma
  191. * @since 6.0.0
  192. * @product highstock
  193. * @requires stock/indicators/indicators
  194. * @requires stock/indicators/vwap
  195. * @optionparent plotOptions.vwap
  196. */
  197. VWAPIndicator.defaultOptions = merge(SMAIndicator.defaultOptions, {
  198. /**
  199. * @excluding index
  200. */
  201. params: {
  202. period: 30,
  203. /**
  204. * The id of volume series which is mandatory. For example using
  205. * OHLC data, volumeSeriesID='volume' means the indicator will be
  206. * calculated using OHLC and volume values.
  207. */
  208. volumeSeriesID: 'volume'
  209. }
  210. });
  211. return VWAPIndicator;
  212. }(SMAIndicator));
  213. SeriesRegistry.registerSeriesType('vwap', VWAPIndicator);
  214. /* *
  215. *
  216. * Default Export
  217. *
  218. * */
  219. /**
  220. * A `Volume Weighted Average Price (VWAP)` series. If the
  221. * [type](#series.vwap.type) option is not specified, it is inherited from
  222. * [chart.type](#chart.type).
  223. *
  224. * @extends series,plotOptions.vwap
  225. * @since 6.0.0
  226. * @product highstock
  227. * @excluding dataParser, dataURL
  228. * @requires stock/indicators/indicators
  229. * @requires stock/indicators/vwap
  230. * @apioption series.vwap
  231. */
  232. ''; // to include the above in the js output
  233. return VWAPIndicator;
  234. });
  235. _registerModule(_modules, 'masters/indicators/vwap.src.js', [], function () {
  236. });
  237. }));