AroonIndicator.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 merge = U.merge, pick = U.pick, seriesType = U.seriesType;
  11. import multipleLinesMixin from '../../Mixins/MultipleLines.js';
  12. /* eslint-disable valid-jsdoc */
  13. // Utils
  14. // Index of element with extreme value from array (min or max)
  15. /**
  16. * @private
  17. */
  18. function getExtremeIndexInArray(arr, extreme) {
  19. var extremeValue = arr[0], valueIndex = 0, i;
  20. for (i = 1; i < arr.length; i++) {
  21. if (extreme === 'max' && arr[i] >= extremeValue ||
  22. extreme === 'min' && arr[i] <= extremeValue) {
  23. extremeValue = arr[i];
  24. valueIndex = i;
  25. }
  26. }
  27. return valueIndex;
  28. }
  29. /* eslint-enable valid-jsdoc */
  30. /**
  31. * The Aroon series type.
  32. *
  33. * @private
  34. * @class
  35. * @name Highcharts.seriesTypes.aroon
  36. *
  37. * @augments Highcharts.Series
  38. */
  39. seriesType('aroon', 'sma',
  40. /**
  41. * Aroon. This series requires the `linkedTo` option to be
  42. * set and should be loaded after the `stock/indicators/indicators.js`.
  43. *
  44. * @sample {highstock} stock/indicators/aroon
  45. * Aroon
  46. *
  47. * @extends plotOptions.sma
  48. * @since 7.0.0
  49. * @product highstock
  50. * @excluding allAreas, colorAxis, compare, compareBase, joinBy, keys,
  51. * navigatorOptions, pointInterval, pointIntervalUnit,
  52. * pointPlacement, pointRange, pointStart, showInNavigator,
  53. * stacking
  54. * @requires stock/indicators/indicators
  55. * @requires stock/indicators/aroon
  56. * @optionparent plotOptions.aroon
  57. */
  58. {
  59. /**
  60. * Paramters used in calculation of aroon series points.
  61. *
  62. * @excluding periods, index
  63. */
  64. params: {
  65. /**
  66. * Period for Aroon indicator
  67. */
  68. period: 25
  69. },
  70. marker: {
  71. enabled: false
  72. },
  73. tooltip: {
  74. pointFormat: '<span style="color:{point.color}">\u25CF</span><b> {series.name}</b><br/>Aroon Up: {point.y}<br/>Aroon Down: {point.aroonDown}<br/>'
  75. },
  76. /**
  77. * aroonDown line options.
  78. */
  79. aroonDown: {
  80. /**
  81. * Styles for an aroonDown line.
  82. */
  83. styles: {
  84. /**
  85. * Pixel width of the line.
  86. */
  87. lineWidth: 1,
  88. /**
  89. * Color of the line. If not set, it's inherited from
  90. * [plotOptions.aroon.color](#plotOptions.aroon.color).
  91. *
  92. * @type {Highcharts.ColorString}
  93. */
  94. lineColor: void 0
  95. }
  96. },
  97. dataGrouping: {
  98. approximation: 'averages'
  99. }
  100. },
  101. /**
  102. * @lends Highcharts.Series#
  103. */
  104. merge(multipleLinesMixin, {
  105. nameBase: 'Aroon',
  106. pointArrayMap: ['y', 'aroonDown'],
  107. pointValKey: 'y',
  108. linesApiNames: ['aroonDown'],
  109. getValues: function (series, params) {
  110. var period = params.period, xVal = series.xData, yVal = series.yData, yValLen = yVal ? yVal.length : 0,
  111. // 0- date, 1- Aroon Up, 2- Aroon Down
  112. AR = [], xData = [], yData = [], slicedY, low = 2, high = 1, aroonUp, aroonDown, xLow, xHigh, i;
  113. // For a N-period, we start from N-1 point, to calculate Nth point
  114. // That is why we later need to comprehend slice() elements list
  115. // with (+1)
  116. for (i = period - 1; i < yValLen; i++) {
  117. slicedY = yVal.slice(i - period + 1, i + 2);
  118. xLow = getExtremeIndexInArray(slicedY.map(function (elem) {
  119. return pick(elem[low], elem);
  120. }), 'min');
  121. xHigh = getExtremeIndexInArray(slicedY.map(function (elem) {
  122. return pick(elem[high], elem);
  123. }), 'max');
  124. aroonUp = (xHigh / period) * 100;
  125. aroonDown = (xLow / period) * 100;
  126. if (xVal[i + 1]) {
  127. AR.push([xVal[i + 1], aroonUp, aroonDown]);
  128. xData.push(xVal[i + 1]);
  129. yData.push([aroonUp, aroonDown]);
  130. }
  131. }
  132. return {
  133. values: AR,
  134. xData: xData,
  135. yData: yData
  136. };
  137. }
  138. }));
  139. /**
  140. * A Aroon indicator. If the [type](#series.aroon.type) option is not
  141. * specified, it is inherited from [chart.type](#chart.type).
  142. *
  143. * @extends series,plotOptions.aroon
  144. * @since 7.0.0
  145. * @product highstock
  146. * @excluding allAreas, colorAxis, compare, compareBase, dataParser, dataURL,
  147. * joinBy, keys, navigatorOptions, pointInterval, pointIntervalUnit,
  148. * pointPlacement, pointRange, pointStart, showInNavigator, stacking
  149. * @requires stock/indicators/indicators
  150. * @requires stock/indicators/aroon
  151. * @apioption series.aroon
  152. */
  153. ''; // to avoid removal of the above jsdoc