wma.src.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /**
  2. * @license Highstock JS v8.2.0 (2020-08-20)
  3. *
  4. * Indicator series type for Highstock
  5. *
  6. * (c) 2010-2019 Kacper Madej
  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/wma', ['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/WMAIndicator.js', [_modules['Core/Utilities.js']], function (U) {
  32. /* *
  33. *
  34. * (c) 2010-2020 Kacper Madej
  35. *
  36. * License: www.highcharts.com/license
  37. *
  38. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  39. *
  40. * */
  41. var isArray = U.isArray,
  42. seriesType = U.seriesType;
  43. /* eslint-disable valid-jsdoc */
  44. // Utils:
  45. /**
  46. * @private
  47. */
  48. function accumulateAverage(points, xVal, yVal, i, index) {
  49. var xValue = xVal[i],
  50. yValue = index < 0 ? yVal[i] : yVal[i][index];
  51. points.push([xValue, yValue]);
  52. }
  53. /**
  54. * @private
  55. */
  56. function weightedSumArray(array, pLen) {
  57. // The denominator is the sum of the number of days as a triangular number.
  58. // If there are 5 days, the triangular numbers are 5, 4, 3, 2, and 1.
  59. // The sum is 5 + 4 + 3 + 2 + 1 = 15.
  60. var denominator = (pLen + 1) / 2 * pLen;
  61. // reduce VS loop => reduce
  62. return array.reduce(function (prev, cur, i) {
  63. return [null, prev[1] + cur[1] * (i + 1)];
  64. })[1] / denominator;
  65. }
  66. /**
  67. * @private
  68. */
  69. function populateAverage(points, xVal, yVal, i) {
  70. var pLen = points.length,
  71. wmaY = weightedSumArray(points,
  72. pLen),
  73. wmaX = xVal[i - 1];
  74. points.shift(); // remove point until range < period
  75. return [wmaX, wmaY];
  76. }
  77. /* eslint-enable valid-jsdoc */
  78. /**
  79. * The SMA series type.
  80. *
  81. * @private
  82. * @class
  83. * @name Highcharts.seriesTypes.wma
  84. *
  85. * @augments Highcharts.Series
  86. */
  87. seriesType('wma', 'sma',
  88. /**
  89. * Weighted moving average indicator (WMA). This series requires `linkedTo`
  90. * option to be set.
  91. *
  92. * @sample stock/indicators/wma
  93. * Weighted moving average indicator
  94. *
  95. * @extends plotOptions.sma
  96. * @since 6.0.0
  97. * @product highstock
  98. * @requires stock/indicators/indicators
  99. * @requires stock/indicators/wma
  100. * @optionparent plotOptions.wma
  101. */
  102. {
  103. params: {
  104. index: 3,
  105. period: 9
  106. }
  107. },
  108. /**
  109. * @lends Highcharts.Series#
  110. */
  111. {
  112. getValues: function (series, params) {
  113. var period = params.period,
  114. xVal = series.xData,
  115. yVal = series.yData,
  116. yValLen = yVal ? yVal.length : 0,
  117. range = 1,
  118. xValue = xVal[0],
  119. yValue = yVal[0],
  120. WMA = [],
  121. xData = [],
  122. yData = [],
  123. index = -1,
  124. i,
  125. points,
  126. WMAPoint;
  127. if (xVal.length < period) {
  128. return;
  129. }
  130. // Switch index for OHLC / Candlestick
  131. if (isArray(yVal[0])) {
  132. index = params.index;
  133. yValue = yVal[0][index];
  134. }
  135. // Starting point
  136. points = [[xValue, yValue]];
  137. // Accumulate first N-points
  138. while (range !== period) {
  139. accumulateAverage(points, xVal, yVal, range, index);
  140. range++;
  141. }
  142. // Calculate value one-by-one for each period in visible data
  143. for (i = range; i < yValLen; i++) {
  144. WMAPoint = populateAverage(points, xVal, yVal, i);
  145. WMA.push(WMAPoint);
  146. xData.push(WMAPoint[0]);
  147. yData.push(WMAPoint[1]);
  148. accumulateAverage(points, xVal, yVal, i, index);
  149. }
  150. WMAPoint = populateAverage(points, xVal, yVal, i);
  151. WMA.push(WMAPoint);
  152. xData.push(WMAPoint[0]);
  153. yData.push(WMAPoint[1]);
  154. return {
  155. values: WMA,
  156. xData: xData,
  157. yData: yData
  158. };
  159. }
  160. });
  161. /**
  162. * A `WMA` series. If the [type](#series.wma.type) option is not specified, it
  163. * is inherited from [chart.type](#chart.type).
  164. *
  165. * @extends series,plotOptions.wma
  166. * @since 6.0.0
  167. * @product highstock
  168. * @excluding dataParser, dataURL
  169. * @requires stock/indicators/indicators
  170. * @requires stock/indicators/wma
  171. * @apioption series.wma
  172. */
  173. ''; // adds doclet above to the transpiled file
  174. });
  175. _registerModule(_modules, 'masters/indicators/wma.src.js', [], function () {
  176. });
  177. }));