WilliamsRIndicator.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /* *
  2. *
  3. * License: www.highcharts.com/license
  4. *
  5. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  6. *
  7. * */
  8. 'use strict';
  9. import U from '../../Core/Utilities.js';
  10. var isArray = U.isArray, seriesType = U.seriesType;
  11. import reduceArrayMixin from '../../Mixins/ReduceArray.js';
  12. var getArrayExtremes = reduceArrayMixin.getArrayExtremes;
  13. /**
  14. * The Williams %R series type.
  15. *
  16. * @private
  17. * @class
  18. * @name Highcharts.seriesTypes.williamsr
  19. *
  20. * @augments Highcharts.Series
  21. */
  22. seriesType('williamsr', 'sma',
  23. /**
  24. * Williams %R. This series requires the `linkedTo` option to be
  25. * set and should be loaded after the `stock/indicators/indicators.js`.
  26. *
  27. * @sample {highstock} stock/indicators/williams-r
  28. * Williams %R
  29. *
  30. * @extends plotOptions.sma
  31. * @since 7.0.0
  32. * @product highstock
  33. * @excluding allAreas, colorAxis, joinBy, keys, navigatorOptions,
  34. * pointInterval, pointIntervalUnit, pointPlacement,
  35. * pointRange, pointStart, showInNavigator, stacking
  36. * @requires stock/indicators/indicators
  37. * @requires stock/indicators/williams-r
  38. * @optionparent plotOptions.williamsr
  39. */
  40. {
  41. /**
  42. * Paramters used in calculation of Williams %R series points.
  43. * @excluding index
  44. */
  45. params: {
  46. /**
  47. * Period for Williams %R oscillator
  48. */
  49. period: 14
  50. }
  51. },
  52. /**
  53. * @lends Highcharts.Series#
  54. */
  55. {
  56. nameBase: 'Williams %R',
  57. getValues: function (series, params) {
  58. var period = params.period, xVal = series.xData, yVal = series.yData, yValLen = yVal ? yVal.length : 0, WR = [], // 0- date, 1- Williams %R
  59. xData = [], yData = [], slicedY, close = 3, low = 2, high = 1, extremes, R, HH, // Highest high value in period
  60. LL, // Lowest low value in period
  61. CC, // Current close value
  62. i;
  63. // Williams %R requires close value
  64. if (xVal.length < period ||
  65. !isArray(yVal[0]) ||
  66. yVal[0].length !== 4) {
  67. return;
  68. }
  69. // For a N-period, we start from N-1 point, to calculate Nth point
  70. // That is why we later need to comprehend slice() elements list
  71. // with (+1)
  72. for (i = period - 1; i < yValLen; i++) {
  73. slicedY = yVal.slice(i - period + 1, i + 1);
  74. extremes = getArrayExtremes(slicedY, low, high);
  75. LL = extremes[0];
  76. HH = extremes[1];
  77. CC = yVal[i][close];
  78. R = ((HH - CC) / (HH - LL)) * -100;
  79. if (xVal[i]) {
  80. WR.push([xVal[i], R]);
  81. xData.push(xVal[i]);
  82. yData.push(R);
  83. }
  84. }
  85. return {
  86. values: WR,
  87. xData: xData,
  88. yData: yData
  89. };
  90. }
  91. });
  92. /**
  93. * A `Williams %R Oscillator` series. If the [type](#series.williamsr.type)
  94. * option is not specified, it is inherited from [chart.type](#chart.type).
  95. *
  96. * @extends series,plotOptions.williamsr
  97. * @since 7.0.0
  98. * @product highstock
  99. * @excluding allAreas, colorAxis, dataParser, dataURL, joinBy, keys,
  100. * navigatorOptions, pointInterval, pointIntervalUnit,
  101. * pointPlacement, pointRange, pointStart, showInNavigator, stacking
  102. * @requires stock/indicators/indicators
  103. * @requires stock/indicators/williams-r
  104. * @apioption series.williamsr
  105. */
  106. ''; // adds doclets above to the transpiled file