AOIndicator.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 H from '../../Core/Globals.js';
  10. import U from '../../Core/Utilities.js';
  11. var correctFloat = U.correctFloat, isArray = U.isArray, seriesType = U.seriesType;
  12. var noop = H.noop;
  13. /**
  14. * The AO series type
  15. *
  16. * @private
  17. * @class
  18. * @name Highcharts.seriesTypes.ao
  19. *
  20. * @augments Highcharts.Series
  21. */
  22. seriesType('ao', 'sma',
  23. /**
  24. * Awesome Oscillator. This series requires the `linkedTo` option to
  25. * be set and should be loaded after the `stock/indicators/indicators.js`
  26. *
  27. * @sample {highstock} stock/indicators/ao
  28. * Awesome
  29. *
  30. * @extends plotOptions.sma
  31. * @since 7.0.0
  32. * @product highstock
  33. * @excluding allAreas, colorAxis, joinBy, keys, navigatorOptions,
  34. * params, pointInterval, pointIntervalUnit, pointPlacement,
  35. * pointRange, pointStart, showInNavigator, stacking
  36. * @requires stock/indicators/indicators
  37. * @requires stock/indicators/ao
  38. * @optionparent plotOptions.ao
  39. */
  40. {
  41. /**
  42. * Color of the Awesome oscillator series bar that is greater than the
  43. * previous one. Note that if a `color` is defined, the `color`
  44. * takes precedence and the `greaterBarColor` is ignored.
  45. *
  46. * @sample {highstock} stock/indicators/ao/
  47. * greaterBarColor
  48. *
  49. * @type {Highcharts.ColorString|Highcharts.GradientColorObject|Highcharts.PatternObject}
  50. * @since 7.0.0
  51. */
  52. greaterBarColor: '#06B535',
  53. /**
  54. * Color of the Awesome oscillator series bar that is lower than the
  55. * previous one. Note that if a `color` is defined, the `color`
  56. * takes precedence and the `lowerBarColor` is ignored.
  57. *
  58. * @sample {highstock} stock/indicators/ao/
  59. * lowerBarColor
  60. *
  61. * @type {Highcharts.ColorString|Highcharts.GradientColorObject|Highcharts.PatternObject}
  62. * @since 7.0.0
  63. */
  64. lowerBarColor: '#F21313',
  65. threshold: 0,
  66. groupPadding: 0.2,
  67. pointPadding: 0.2,
  68. crisp: false,
  69. states: {
  70. hover: {
  71. halo: {
  72. size: 0
  73. }
  74. }
  75. }
  76. },
  77. /**
  78. * @lends Highcharts.Series#
  79. */
  80. {
  81. nameBase: 'AO',
  82. nameComponents: false,
  83. // Columns support:
  84. markerAttribs: noop,
  85. getColumnMetrics: H.seriesTypes.column.prototype.getColumnMetrics,
  86. crispCol: H.seriesTypes.column.prototype.crispCol,
  87. translate: H.seriesTypes.column.prototype.translate,
  88. drawPoints: H.seriesTypes.column.prototype.drawPoints,
  89. drawGraph: function () {
  90. var indicator = this, options = indicator.options, points = indicator.points, userColor = indicator.userOptions.color, positiveColor = options.greaterBarColor, negativeColor = options.lowerBarColor, firstPoint = points[0], i;
  91. if (!userColor && firstPoint) {
  92. firstPoint.color = positiveColor;
  93. for (i = 1; i < points.length; i++) {
  94. if (points[i].y > points[i - 1].y) {
  95. points[i].color = positiveColor;
  96. }
  97. else if (points[i].y < points[i - 1].y) {
  98. points[i].color = negativeColor;
  99. }
  100. else {
  101. points[i].color = points[i - 1].color;
  102. }
  103. }
  104. }
  105. },
  106. getValues: function (series) {
  107. var shortPeriod = 5, longPeriod = 34, xVal = series.xData || [], yVal = series.yData || [], yValLen = yVal.length, AO = [], // 0- date, 1- Awesome Oscillator
  108. xData = [], yData = [], high = 1, low = 2, shortSum = 0, longSum = 0, shortSMA, // Shorter Period SMA
  109. longSMA, // Longer Period SMA
  110. awesome, shortLastIndex, longLastIndex, price, i, j;
  111. if (xVal.length <= longPeriod ||
  112. !isArray(yVal[0]) ||
  113. yVal[0].length !== 4) {
  114. return;
  115. }
  116. for (i = 0; i < longPeriod - 1; i++) {
  117. price = (yVal[i][high] + yVal[i][low]) / 2;
  118. if (i >= longPeriod - shortPeriod) {
  119. shortSum = correctFloat(shortSum + price);
  120. }
  121. longSum = correctFloat(longSum + price);
  122. }
  123. for (j = longPeriod - 1; j < yValLen; j++) {
  124. price = (yVal[j][high] + yVal[j][low]) / 2;
  125. shortSum = correctFloat(shortSum + price);
  126. longSum = correctFloat(longSum + price);
  127. shortSMA = shortSum / shortPeriod;
  128. longSMA = longSum / longPeriod;
  129. awesome = correctFloat(shortSMA - longSMA);
  130. AO.push([xVal[j], awesome]);
  131. xData.push(xVal[j]);
  132. yData.push(awesome);
  133. shortLastIndex = j + 1 - shortPeriod;
  134. longLastIndex = j + 1 - longPeriod;
  135. shortSum = correctFloat(shortSum -
  136. (yVal[shortLastIndex][high] +
  137. yVal[shortLastIndex][low]) / 2);
  138. longSum = correctFloat(longSum -
  139. (yVal[longLastIndex][high] +
  140. yVal[longLastIndex][low]) / 2);
  141. }
  142. return {
  143. values: AO,
  144. xData: xData,
  145. yData: yData
  146. };
  147. }
  148. });
  149. /**
  150. * An `AO` series. If the [type](#series.ao.type)
  151. * option is not specified, it is inherited from [chart.type](#chart.type).
  152. *
  153. * @extends series,plotOptions.ao
  154. * @since 7.0.0
  155. * @product highstock
  156. * @excluding allAreas, colorAxis, dataParser, dataURL, joinBy, keys,
  157. * navigatorOptions, pointInterval, pointIntervalUnit,
  158. * pointPlacement, pointRange, pointStart, showInNavigator, stacking
  159. * @requires stock/indicators/indicators
  160. * @requires stock/indicators/ao
  161. * @apioption series.ao
  162. */
  163. ''; // for including the above in the doclets