ABIndicator.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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, merge = U.merge, seriesType = U.seriesType;
  12. import multipleLinesMixin from '../../Mixins/MultipleLines.js';
  13. var SMA = H.seriesTypes.sma;
  14. /* eslint-disable valid-jsdoc */
  15. /**
  16. * @private
  17. */
  18. function getBaseForBand(low, high, factor) {
  19. return (((correctFloat(high - low)) /
  20. ((correctFloat(high + low)) / 2)) * 1000) * factor;
  21. }
  22. /**
  23. * @private
  24. */
  25. function getPointUB(high, base) {
  26. return high * (correctFloat(1 + 2 * base));
  27. }
  28. /**
  29. * @private
  30. */
  31. function getPointLB(low, base) {
  32. return low * (correctFloat(1 - 2 * base));
  33. }
  34. /* eslint-enable valid-jsdoc */
  35. /**
  36. * The ABands series type
  37. *
  38. * @private
  39. * @class
  40. * @name Highcharts.seriesTypes.abands
  41. *
  42. * @augments Highcharts.Series
  43. */
  44. seriesType('abands', 'sma',
  45. /**
  46. * Acceleration bands (ABANDS). This series requires the `linkedTo` option
  47. * to be set and should be loaded after the
  48. * `stock/indicators/indicators.js`.
  49. *
  50. * @sample {highstock} stock/indicators/acceleration-bands
  51. * Acceleration Bands
  52. *
  53. * @extends plotOptions.sma
  54. * @mixes Highcharts.MultipleLinesMixin
  55. * @since 7.0.0
  56. * @product highstock
  57. * @excluding allAreas, colorAxis, compare, compareBase, joinBy, keys,
  58. * navigatorOptions, pointInterval, pointIntervalUnit,
  59. * pointPlacement, pointRange, pointStart, showInNavigator,
  60. * stacking,
  61. * @requires stock/indicators/indicators
  62. * @requires stock/indicators/acceleration-bands
  63. * @optionparent plotOptions.abands
  64. */
  65. {
  66. params: {
  67. period: 20,
  68. /**
  69. * The algorithms factor value used to calculate bands.
  70. *
  71. * @product highstock
  72. */
  73. factor: 0.001,
  74. index: 3
  75. },
  76. lineWidth: 1,
  77. topLine: {
  78. styles: {
  79. /**
  80. * Pixel width of the line.
  81. */
  82. lineWidth: 1
  83. }
  84. },
  85. bottomLine: {
  86. styles: {
  87. /**
  88. * Pixel width of the line.
  89. */
  90. lineWidth: 1
  91. }
  92. },
  93. dataGrouping: {
  94. approximation: 'averages'
  95. }
  96. },
  97. /**
  98. * @lends Highcharts.Series#
  99. */
  100. merge(multipleLinesMixin, {
  101. pointArrayMap: ['top', 'middle', 'bottom'],
  102. pointValKey: 'middle',
  103. nameBase: 'Acceleration Bands',
  104. nameComponents: ['period', 'factor'],
  105. linesApiNames: ['topLine', 'bottomLine'],
  106. getValues: function (series, params) {
  107. var period = params.period, factor = params.factor, index = params.index, xVal = series.xData, yVal = series.yData, yValLen = yVal ? yVal.length : 0,
  108. // Upperbands
  109. UB = [],
  110. // Lowerbands
  111. LB = [],
  112. // ABANDS array structure:
  113. // 0-date, 1-top line, 2-middle line, 3-bottom line
  114. ABANDS = [],
  115. // middle line, top line and bottom line
  116. ML, TL, BL, date, bandBase, pointSMA, ubSMA, lbSMA, low = 2, high = 1, xData = [], yData = [], slicedX, slicedY, i;
  117. if (yValLen < period) {
  118. return;
  119. }
  120. for (i = 0; i <= yValLen; i++) {
  121. // Get UB and LB values of every point. This condition
  122. // is necessary, because there is a need to calculate current
  123. // UB nad LB values simultaneously with given period SMA
  124. // in one for loop.
  125. if (i < yValLen) {
  126. bandBase = getBaseForBand(yVal[i][low], yVal[i][high], factor);
  127. UB.push(getPointUB(yVal[i][high], bandBase));
  128. LB.push(getPointLB(yVal[i][low], bandBase));
  129. }
  130. if (i >= period) {
  131. slicedX = xVal.slice(i - period, i);
  132. slicedY = yVal.slice(i - period, i);
  133. ubSMA = SMA.prototype.getValues.call(this, {
  134. xData: slicedX,
  135. yData: UB.slice(i - period, i)
  136. }, {
  137. period: period
  138. });
  139. lbSMA = SMA.prototype.getValues.call(this, {
  140. xData: slicedX,
  141. yData: LB.slice(i - period, i)
  142. }, {
  143. period: period
  144. });
  145. pointSMA = SMA.prototype.getValues.call(this, {
  146. xData: slicedX,
  147. yData: slicedY
  148. }, {
  149. period: period,
  150. index: index
  151. });
  152. date = pointSMA.xData[0];
  153. TL = ubSMA.yData[0];
  154. BL = lbSMA.yData[0];
  155. ML = pointSMA.yData[0];
  156. ABANDS.push([date, TL, ML, BL]);
  157. xData.push(date);
  158. yData.push([TL, ML, BL]);
  159. }
  160. }
  161. return {
  162. values: ABANDS,
  163. xData: xData,
  164. yData: yData
  165. };
  166. }
  167. }));
  168. /**
  169. * An Acceleration bands indicator. If the [type](#series.abands.type) option is not
  170. * specified, it is inherited from [chart.type](#chart.type).
  171. *
  172. * @extends series,plotOptions.abands
  173. * @since 7.0.0
  174. * @product highstock
  175. * @excluding allAreas, colorAxis, compare, compareBase, dataParser, dataURL,
  176. * joinBy, keys, navigatorOptions, pointInterval,
  177. * pointIntervalUnit, pointPlacement, pointRange, pointStart,
  178. * stacking, showInNavigator,
  179. * @requires stock/indicators/indicators
  180. * @requires stock/indicators/acceleration-bands
  181. * @apioption series.abands
  182. */
  183. ''; // to include the above in jsdoc