KeltnerChannelsIndicator.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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, EMA = H.seriesTypes.ema, ATR = H.seriesTypes.atr;
  14. /**
  15. * The Keltner Channels series type.
  16. *
  17. * @private
  18. * @class
  19. * @name Highcharts.seriesTypes.keltnerchannels
  20. *
  21. * @augments Highcharts.Series
  22. */
  23. seriesType('keltnerchannels', 'sma',
  24. /**
  25. * Keltner Channels. This series requires the `linkedTo` option to be set
  26. * and should be loaded after the `stock/indicators/indicators.js`,
  27. * `stock/indicators/atr.js`, and `stock/ema/.js`.
  28. *
  29. * @sample {highstock} stock/indicators/keltner-channels
  30. * Keltner Channels
  31. *
  32. * @extends plotOptions.sma
  33. * @since 7.0.0
  34. * @product highstock
  35. * @excluding allAreas, colorAxis, compare, compareBase, joinBy, keys,
  36. * navigatorOptions, pointInterval, pointIntervalUnit,
  37. * pointPlacement, pointRange, pointStart,showInNavigator,
  38. * stacking
  39. * @requires stock/indicators/indicators
  40. * @requires stock/indicators/keltner-channels
  41. * @optionparent plotOptions.keltnerchannels
  42. */
  43. {
  44. params: {
  45. period: 20,
  46. /**
  47. * The ATR period.
  48. */
  49. periodATR: 10,
  50. /**
  51. * The ATR multiplier.
  52. */
  53. multiplierATR: 2
  54. },
  55. /**
  56. * Bottom line options.
  57. *
  58. */
  59. bottomLine: {
  60. /**
  61. * Styles for a bottom line.
  62. *
  63. */
  64. styles: {
  65. /**
  66. * Pixel width of the line.
  67. */
  68. lineWidth: 1,
  69. /**
  70. * Color of the line. If not set, it's inherited from
  71. * `plotOptions.keltnerchannels.color`
  72. */
  73. lineColor: void 0
  74. }
  75. },
  76. /**
  77. * Top line options.
  78. *
  79. * @extends plotOptions.keltnerchannels.bottomLine
  80. */
  81. topLine: {
  82. styles: {
  83. lineWidth: 1,
  84. lineColor: void 0
  85. }
  86. },
  87. tooltip: {
  88. pointFormat: '<span style="color:{point.color}">\u25CF</span><b> {series.name}</b><br/>Upper Channel: {point.top}<br/>EMA({series.options.params.period}): {point.middle}<br/>Lower Channel: {point.bottom}<br/>'
  89. },
  90. marker: {
  91. enabled: false
  92. },
  93. dataGrouping: {
  94. approximation: 'averages'
  95. },
  96. lineWidth: 1
  97. },
  98. /**
  99. * @lends Highcharts.Series#
  100. */
  101. merge(multipleLinesMixin, {
  102. pointArrayMap: ['top', 'middle', 'bottom'],
  103. pointValKey: 'middle',
  104. nameBase: 'Keltner Channels',
  105. nameComponents: ['period', 'periodATR', 'multiplierATR'],
  106. linesApiNames: ['topLine', 'bottomLine'],
  107. requiredIndicators: ['ema', 'atr'],
  108. init: function () {
  109. SMA.prototype.init.apply(this, arguments);
  110. // Set default color for lines:
  111. this.options = merge({
  112. topLine: {
  113. styles: {
  114. lineColor: this.color
  115. }
  116. },
  117. bottomLine: {
  118. styles: {
  119. lineColor: this.color
  120. }
  121. }
  122. }, this.options);
  123. },
  124. getValues: function (series, params) {
  125. var period = params.period, periodATR = params.periodATR, multiplierATR = params.multiplierATR, index = params.index, yVal = series.yData, yValLen = yVal ? yVal.length : 0,
  126. // Keltner Channels array structure:
  127. // 0-date, 1-top line, 2-middle line, 3-bottom line
  128. KC = [],
  129. // middle line, top line and bottom lineI
  130. ML, TL, BL, date, seriesEMA = EMA.prototype.getValues(series, {
  131. period: period,
  132. index: index
  133. }), seriesATR = ATR.prototype.getValues(series, {
  134. period: periodATR
  135. }), pointEMA, pointATR, xData = [], yData = [], i;
  136. if (yValLen < period) {
  137. return;
  138. }
  139. for (i = period; i <= yValLen; i++) {
  140. pointEMA = seriesEMA.values[i - period];
  141. pointATR = seriesATR.values[i - periodATR];
  142. date = pointEMA[0];
  143. TL = correctFloat(pointEMA[1] + (multiplierATR * pointATR[1]));
  144. BL = correctFloat(pointEMA[1] - (multiplierATR * pointATR[1]));
  145. ML = pointEMA[1];
  146. KC.push([date, TL, ML, BL]);
  147. xData.push(date);
  148. yData.push([TL, ML, BL]);
  149. }
  150. return {
  151. values: KC,
  152. xData: xData,
  153. yData: yData
  154. };
  155. }
  156. }));
  157. /**
  158. * A Keltner Channels indicator. If the [type](#series.keltnerchannels.type)
  159. * option is not specified, it is inherited from[chart.type](#chart.type).
  160. *
  161. * @extends series,plotOptions.keltnerchannels
  162. * @since 7.0.0
  163. * @product highstock
  164. * @excluding allAreas, colorAxis, compare, compareBase, dataParser, dataURL,
  165. * joinBy, keys, navigatorOptions, pointInterval,
  166. * pointIntervalUnit, pointPlacement, pointRange, pointStart,
  167. * stacking, showInNavigator
  168. * @requires stock/indicators/indicators
  169. * @requires stock/indicators/keltner-channels
  170. * @apioption series.keltnerchannels
  171. */
  172. ''; // to include the above in the js output