BBIndicator.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 multipleLinesMixin from '../../Mixins/MultipleLines.js';
  13. var SMA = H.seriesTypes.sma;
  14. /* eslint-disable valid-jsdoc */
  15. // Utils:
  16. /**
  17. * @private
  18. */
  19. function getStandardDeviation(arr, index, isOHLC, mean) {
  20. var variance = 0, arrLen = arr.length, std = 0, i = 0, value;
  21. for (; i < arrLen; i++) {
  22. value = (isOHLC ? arr[i][index] : arr[i]) - mean;
  23. variance += value * value;
  24. }
  25. variance = variance / (arrLen - 1);
  26. std = Math.sqrt(variance);
  27. return std;
  28. }
  29. /* eslint-enable valid-jsdoc */
  30. /**
  31. * Bollinger Bands series type.
  32. *
  33. * @private
  34. * @class
  35. * @name Highcharts.seriesTypes.bb
  36. *
  37. * @augments Highcharts.Series
  38. */
  39. seriesType('bb', 'sma',
  40. /**
  41. * Bollinger bands (BB). This series requires the `linkedTo` option to be
  42. * set and should be loaded after the `stock/indicators/indicators.js` file.
  43. *
  44. * @sample stock/indicators/bollinger-bands
  45. * Bollinger bands
  46. *
  47. * @extends plotOptions.sma
  48. * @since 6.0.0
  49. * @product highstock
  50. * @requires stock/indicators/indicators
  51. * @requires stock/indicators/bollinger-bands
  52. * @optionparent plotOptions.bb
  53. */
  54. {
  55. params: {
  56. period: 20,
  57. /**
  58. * Standard deviation for top and bottom bands.
  59. */
  60. standardDeviation: 2,
  61. index: 3
  62. },
  63. /**
  64. * Bottom line options.
  65. */
  66. bottomLine: {
  67. /**
  68. * Styles for a bottom line.
  69. */
  70. styles: {
  71. /**
  72. * Pixel width of the line.
  73. */
  74. lineWidth: 1,
  75. /**
  76. * Color of the line. If not set, it's inherited from
  77. * [plotOptions.bb.color](#plotOptions.bb.color).
  78. *
  79. * @type {Highcharts.ColorString}
  80. */
  81. lineColor: void 0
  82. }
  83. },
  84. /**
  85. * Top line options.
  86. *
  87. * @extends plotOptions.bb.bottomLine
  88. */
  89. topLine: {
  90. styles: {
  91. lineWidth: 1,
  92. /**
  93. * @type {Highcharts.ColorString}
  94. */
  95. lineColor: void 0
  96. }
  97. },
  98. tooltip: {
  99. pointFormat: '<span style="color:{point.color}">\u25CF</span><b> {series.name}</b><br/>Top: {point.top}<br/>Middle: {point.middle}<br/>Bottom: {point.bottom}<br/>'
  100. },
  101. marker: {
  102. enabled: false
  103. },
  104. dataGrouping: {
  105. approximation: 'averages'
  106. }
  107. },
  108. /**
  109. * @lends Highcharts.Series#
  110. */
  111. merge(multipleLinesMixin, {
  112. pointArrayMap: ['top', 'middle', 'bottom'],
  113. pointValKey: 'middle',
  114. nameComponents: ['period', 'standardDeviation'],
  115. linesApiNames: ['topLine', 'bottomLine'],
  116. init: function () {
  117. SMA.prototype.init.apply(this, arguments);
  118. // Set default color for lines:
  119. this.options = merge({
  120. topLine: {
  121. styles: {
  122. lineColor: this.color
  123. }
  124. },
  125. bottomLine: {
  126. styles: {
  127. lineColor: this.color
  128. }
  129. }
  130. }, this.options);
  131. },
  132. getValues: function (series, params) {
  133. var period = params.period, standardDeviation = params.standardDeviation, xVal = series.xData, yVal = series.yData, yValLen = yVal ? yVal.length : 0,
  134. // 0- date, 1-middle line, 2-top line, 3-bottom line
  135. BB = [],
  136. // middle line, top line and bottom line
  137. ML, TL, BL, date, xData = [], yData = [], slicedX, slicedY, stdDev, isOHLC, point, i;
  138. if (xVal.length < period) {
  139. return;
  140. }
  141. isOHLC = isArray(yVal[0]);
  142. for (i = period; i <= yValLen; i++) {
  143. slicedX = xVal.slice(i - period, i);
  144. slicedY = yVal.slice(i - period, i);
  145. point = SMA.prototype.getValues.call(this, {
  146. xData: slicedX,
  147. yData: slicedY
  148. }, params);
  149. date = point.xData[0];
  150. ML = point.yData[0];
  151. stdDev = getStandardDeviation(slicedY, params.index, isOHLC, ML);
  152. TL = ML + standardDeviation * stdDev;
  153. BL = ML - standardDeviation * stdDev;
  154. BB.push([date, TL, ML, BL]);
  155. xData.push(date);
  156. yData.push([TL, ML, BL]);
  157. }
  158. return {
  159. values: BB,
  160. xData: xData,
  161. yData: yData
  162. };
  163. }
  164. }));
  165. /**
  166. * A bollinger bands indicator. If the [type](#series.bb.type) option is not
  167. * specified, it is inherited from [chart.type](#chart.type).
  168. *
  169. * @extends series,plotOptions.bb
  170. * @since 6.0.0
  171. * @excluding dataParser, dataURL
  172. * @product highstock
  173. * @requires stock/indicators/indicators
  174. * @requires stock/indicators/bollinger-bands
  175. * @apioption series.bb
  176. */
  177. ''; // to include the above in the js output