StochasticIndicator.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 isArray = U.isArray, merge = U.merge, seriesType = U.seriesType;
  12. import reduceArrayMixin from '../../Mixins/ReduceArray.js';
  13. import multipleLinesMixin from '../../Mixins/MultipleLines.js';
  14. var SMA = H.seriesTypes.sma, getArrayExtremes = reduceArrayMixin.getArrayExtremes;
  15. /**
  16. * The Stochastic series type.
  17. *
  18. * @private
  19. * @class
  20. * @name Highcharts.seriesTypes.stochastic
  21. *
  22. * @augments Highcharts.Series
  23. */
  24. seriesType('stochastic', 'sma',
  25. /**
  26. * Stochastic oscillator. This series requires the `linkedTo` option to be
  27. * set and should be loaded after the `stock/indicators/indicators.js` file.
  28. *
  29. * @sample stock/indicators/stochastic
  30. * Stochastic oscillator
  31. *
  32. * @extends plotOptions.sma
  33. * @since 6.0.0
  34. * @product highstock
  35. * @excluding allAreas, colorAxis, joinBy, keys, navigatorOptions,
  36. * pointInterval, pointIntervalUnit, pointPlacement,
  37. * pointRange, pointStart, showInNavigator, stacking
  38. * @requires stock/indicators/indicators
  39. * @requires stock/indicators/stochastic
  40. * @optionparent plotOptions.stochastic
  41. */
  42. {
  43. /**
  44. * @excluding index, period
  45. */
  46. params: {
  47. /**
  48. * Periods for Stochastic oscillator: [%K, %D].
  49. *
  50. * @type {Array<number,number>}
  51. * @default [14, 3]
  52. */
  53. periods: [14, 3]
  54. },
  55. marker: {
  56. enabled: false
  57. },
  58. tooltip: {
  59. pointFormat: '<span style="color:{point.color}">\u25CF</span><b> {series.name}</b><br/>%K: {point.y}<br/>%D: {point.smoothed}<br/>'
  60. },
  61. /**
  62. * Smoothed line options.
  63. */
  64. smoothedLine: {
  65. /**
  66. * Styles for a smoothed line.
  67. */
  68. styles: {
  69. /**
  70. * Pixel width of the line.
  71. */
  72. lineWidth: 1,
  73. /**
  74. * Color of the line. If not set, it's inherited from
  75. * [plotOptions.stochastic.color
  76. * ](#plotOptions.stochastic.color).
  77. *
  78. * @type {Highcharts.ColorString}
  79. */
  80. lineColor: void 0
  81. }
  82. },
  83. dataGrouping: {
  84. approximation: 'averages'
  85. }
  86. },
  87. /**
  88. * @lends Highcharts.Series#
  89. */
  90. merge(multipleLinesMixin, {
  91. nameComponents: ['periods'],
  92. nameBase: 'Stochastic',
  93. pointArrayMap: ['y', 'smoothed'],
  94. parallelArrays: ['x', 'y', 'smoothed'],
  95. pointValKey: 'y',
  96. linesApiNames: ['smoothedLine'],
  97. init: function () {
  98. SMA.prototype.init.apply(this, arguments);
  99. // Set default color for lines:
  100. this.options = merge({
  101. smoothedLine: {
  102. styles: {
  103. lineColor: this.color
  104. }
  105. }
  106. }, this.options);
  107. },
  108. getValues: function (series, params) {
  109. var periodK = params.periods[0], periodD = params.periods[1], xVal = series.xData, yVal = series.yData, yValLen = yVal ? yVal.length : 0,
  110. // 0- date, 1-%K, 2-%D
  111. SO = [], xData = [], yData = [], slicedY, close = 3, low = 2, high = 1, CL, HL, LL, K, D = null, points, extremes, i;
  112. // Stochastic requires close value
  113. if (yValLen < periodK ||
  114. !isArray(yVal[0]) ||
  115. yVal[0].length !== 4) {
  116. return;
  117. }
  118. // For a N-period, we start from N-1 point, to calculate Nth point
  119. // That is why we later need to comprehend slice() elements list
  120. // with (+1)
  121. for (i = periodK - 1; i < yValLen; i++) {
  122. slicedY = yVal.slice(i - periodK + 1, i + 1);
  123. // Calculate %K
  124. extremes = getArrayExtremes(slicedY, low, high);
  125. LL = extremes[0]; // Lowest low in %K periods
  126. CL = yVal[i][close] - LL;
  127. HL = extremes[1] - LL;
  128. K = CL / HL * 100;
  129. xData.push(xVal[i]);
  130. yData.push([K, null]);
  131. // Calculate smoothed %D, which is SMA of %K
  132. if (i >= (periodK - 1) + (periodD - 1)) {
  133. points = SMA.prototype.getValues.call(this, {
  134. xData: xData.slice(-periodD),
  135. yData: yData.slice(-periodD)
  136. }, {
  137. period: periodD
  138. });
  139. D = points.yData[0];
  140. }
  141. SO.push([xVal[i], K, D]);
  142. yData[yData.length - 1][1] = D;
  143. }
  144. return {
  145. values: SO,
  146. xData: xData,
  147. yData: yData
  148. };
  149. }
  150. }));
  151. /**
  152. * A Stochastic indicator. If the [type](#series.stochastic.type) option is not
  153. * specified, it is inherited from [chart.type](#chart.type).
  154. *
  155. * @extends series,plotOptions.stochastic
  156. * @since 6.0.0
  157. * @product highstock
  158. * @excluding allAreas, colorAxis, dataParser, dataURL, joinBy, keys,
  159. * navigatorOptions, pointInterval, pointIntervalUnit,
  160. * pointPlacement, pointRange, pointStart, showInNavigator, stacking
  161. * @requires stock/indicators/indicators
  162. * @requires stock/indicators/stochastic
  163. * @apioption series.stochastic
  164. */
  165. ''; // to include the above in the js output