ao.src.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 '../parts/Globals.js';
  10. import U from '../parts/Utilities.js';
  11. var correctFloat = U.correctFloat, isArray = U.isArray;
  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. H.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. states: {
  69. hover: {
  70. halo: {
  71. size: 0
  72. }
  73. }
  74. }
  75. },
  76. /**
  77. * @lends Highcharts.Series#
  78. */
  79. {
  80. nameBase: 'AO',
  81. nameComponents: false,
  82. // Columns support:
  83. markerAttribs: noop,
  84. getColumnMetrics: H.seriesTypes.column.prototype.getColumnMetrics,
  85. crispCol: H.seriesTypes.column.prototype.crispCol,
  86. translate: H.seriesTypes.column.prototype.translate,
  87. drawPoints: H.seriesTypes.column.prototype.drawPoints,
  88. drawGraph: function () {
  89. var indicator = this, options = indicator.options, points = indicator.points, userColor = indicator.userOptions.color, positiveColor = options.greaterBarColor, negativeColor = options.lowerBarColor, firstPoint = points[0], i;
  90. if (!userColor && firstPoint) {
  91. firstPoint.color = positiveColor;
  92. for (i = 1; i < points.length; i++) {
  93. if (points[i].y > points[i - 1].y) {
  94. points[i].color = positiveColor;
  95. }
  96. else if (points[i].y < points[i - 1].y) {
  97. points[i].color = negativeColor;
  98. }
  99. else {
  100. points[i].color = points[i - 1].color;
  101. }
  102. }
  103. }
  104. },
  105. getValues: function (series) {
  106. var shortPeriod = 5, longPeriod = 34, xVal = series.xData || [], yVal = series.yData || [], yValLen = yVal.length, AO = [], // 0- date, 1- Awesome Oscillator
  107. xData = [], yData = [], high = 1, low = 2, shortSum = 0, longSum = 0, shortSMA, // Shorter Period SMA
  108. longSMA, // Longer Period SMA
  109. awesome, shortLastIndex, longLastIndex, price, i, j;
  110. if (xVal.length <= longPeriod ||
  111. !isArray(yVal[0]) ||
  112. yVal[0].length !== 4) {
  113. return;
  114. }
  115. for (i = 0; i < longPeriod - 1; i++) {
  116. price = (yVal[i][high] + yVal[i][low]) / 2;
  117. if (i >= longPeriod - shortPeriod) {
  118. shortSum = correctFloat(shortSum + price);
  119. }
  120. longSum = correctFloat(longSum + price);
  121. }
  122. for (j = longPeriod - 1; j < yValLen; j++) {
  123. price = (yVal[j][high] + yVal[j][low]) / 2;
  124. shortSum = correctFloat(shortSum + price);
  125. longSum = correctFloat(longSum + price);
  126. shortSMA = shortSum / shortPeriod;
  127. longSMA = longSum / longPeriod;
  128. awesome = correctFloat(shortSMA - longSMA);
  129. AO.push([xVal[j], awesome]);
  130. xData.push(xVal[j]);
  131. yData.push(awesome);
  132. shortLastIndex = j + 1 - shortPeriod;
  133. longLastIndex = j + 1 - longPeriod;
  134. shortSum = correctFloat(shortSum -
  135. (yVal[shortLastIndex][high] +
  136. yVal[shortLastIndex][low]) / 2);
  137. longSum = correctFloat(longSum -
  138. (yVal[longLastIndex][high] +
  139. yVal[longLastIndex][low]) / 2);
  140. }
  141. return {
  142. values: AO,
  143. xData: xData,
  144. yData: yData
  145. };
  146. }
  147. });
  148. /**
  149. * An `AO` series. If the [type](#series.ao.type)
  150. * option is not specified, it is inherited from [chart.type](#chart.type).
  151. *
  152. * @extends series,plotOptions.ao
  153. * @since 7.0.0
  154. * @product highstock
  155. * @excluding allAreas, colorAxis, dataParser, dataURL, joinBy, keys,
  156. * navigatorOptions, pointInterval, pointIntervalUnit,
  157. * pointPlacement, pointRange, pointStart, showInNavigator, stacking
  158. * @requires stock/indicators/indicators
  159. * @requires stock/indicators/ao
  160. * @apioption series.ao
  161. */
  162. ''; // for including the above in the doclets