bollinger-bands.src.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. /**
  2. * @license Highstock JS v8.1.2 (2020-06-16)
  3. *
  4. * Indicator series type for Highstock
  5. *
  6. * (c) 2010-2019 Paweł Fus
  7. *
  8. * License: www.highcharts.com/license
  9. */
  10. 'use strict';
  11. (function (factory) {
  12. if (typeof module === 'object' && module.exports) {
  13. factory['default'] = factory;
  14. module.exports = factory;
  15. } else if (typeof define === 'function' && define.amd) {
  16. define('highcharts/indicators/bollinger-bands', ['highcharts', 'highcharts/modules/stock'], function (Highcharts) {
  17. factory(Highcharts);
  18. factory.Highcharts = Highcharts;
  19. return factory;
  20. });
  21. } else {
  22. factory(typeof Highcharts !== 'undefined' ? Highcharts : undefined);
  23. }
  24. }(function (Highcharts) {
  25. var _modules = Highcharts ? Highcharts._modules : {};
  26. function _registerModule(obj, path, args, fn) {
  27. if (!obj.hasOwnProperty(path)) {
  28. obj[path] = fn.apply(null, args);
  29. }
  30. }
  31. _registerModule(_modules, 'mixins/multipe-lines.js', [_modules['parts/Globals.js'], _modules['parts/Utilities.js']], function (H, U) {
  32. /**
  33. *
  34. * (c) 2010-2020 Wojciech Chmiel
  35. *
  36. * License: www.highcharts.com/license
  37. *
  38. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  39. *
  40. * */
  41. var defined = U.defined, error = U.error, merge = U.merge;
  42. var SMA = H.seriesTypes.sma;
  43. /**
  44. * Mixin useful for all indicators that have more than one line.
  45. * Merge it with your implementation where you will provide
  46. * getValues method appropriate to your indicator and pointArrayMap,
  47. * pointValKey, linesApiNames properites. Notice that pointArrayMap
  48. * should be consistent with amount of lines calculated in getValues method.
  49. *
  50. * @private
  51. * @mixin multipleLinesMixin
  52. */
  53. var multipleLinesMixin = {
  54. /* eslint-disable valid-jsdoc */
  55. /**
  56. * Lines ids. Required to plot appropriate amount of lines.
  57. * Notice that pointArrayMap should have more elements than
  58. * linesApiNames, because it contains main line and additional lines ids.
  59. * Also it should be consistent with amount of lines calculated in
  60. * getValues method from your implementation.
  61. *
  62. * @private
  63. * @name multipleLinesMixin.pointArrayMap
  64. * @type {Array<string>}
  65. */
  66. pointArrayMap: ['top', 'bottom'],
  67. /**
  68. * Main line id.
  69. *
  70. * @private
  71. * @name multipleLinesMixin.pointValKey
  72. * @type {string}
  73. */
  74. pointValKey: 'top',
  75. /**
  76. * Additional lines DOCS names. Elements of linesApiNames array should
  77. * be consistent with DOCS line names defined in your implementation.
  78. * Notice that linesApiNames should have decreased amount of elements
  79. * relative to pointArrayMap (without pointValKey).
  80. *
  81. * @private
  82. * @name multipleLinesMixin.linesApiNames
  83. * @type {Array<string>}
  84. */
  85. linesApiNames: ['bottomLine'],
  86. /**
  87. * Create translatedLines Collection based on pointArrayMap.
  88. *
  89. * @private
  90. * @function multipleLinesMixin.getTranslatedLinesNames
  91. * @param {string} [excludedValue]
  92. * Main line id
  93. * @return {Array<string>}
  94. * Returns translated lines names without excluded value.
  95. */
  96. getTranslatedLinesNames: function (excludedValue) {
  97. var translatedLines = [];
  98. (this.pointArrayMap || []).forEach(function (propertyName) {
  99. if (propertyName !== excludedValue) {
  100. translatedLines.push('plot' +
  101. propertyName.charAt(0).toUpperCase() +
  102. propertyName.slice(1));
  103. }
  104. });
  105. return translatedLines;
  106. },
  107. /**
  108. * @private
  109. * @function multipleLinesMixin.toYData
  110. * @param {Highcharts.Point} point
  111. * Indicator point
  112. * @return {Array<number>}
  113. * Returns point Y value for all lines
  114. */
  115. toYData: function (point) {
  116. var pointColl = [];
  117. (this.pointArrayMap || []).forEach(function (propertyName) {
  118. pointColl.push(point[propertyName]);
  119. });
  120. return pointColl;
  121. },
  122. /**
  123. * Add lines plot pixel values.
  124. *
  125. * @private
  126. * @function multipleLinesMixin.translate
  127. * @return {void}
  128. */
  129. translate: function () {
  130. var indicator = this, pointArrayMap = indicator.pointArrayMap, LinesNames = [], value;
  131. LinesNames = indicator.getTranslatedLinesNames();
  132. SMA.prototype.translate.apply(indicator, arguments);
  133. indicator.points.forEach(function (point) {
  134. pointArrayMap.forEach(function (propertyName, i) {
  135. value = point[propertyName];
  136. if (value !== null) {
  137. point[LinesNames[i]] = indicator.yAxis.toPixels(value, true);
  138. }
  139. });
  140. });
  141. },
  142. /**
  143. * Draw main and additional lines.
  144. *
  145. * @private
  146. * @function multipleLinesMixin.drawGraph
  147. * @return {void}
  148. */
  149. drawGraph: function () {
  150. var indicator = this, pointValKey = indicator.pointValKey, linesApiNames = indicator.linesApiNames, mainLinePoints = indicator.points, pointsLength = mainLinePoints.length, mainLineOptions = indicator.options, mainLinePath = indicator.graph, gappedExtend = {
  151. options: {
  152. gapSize: mainLineOptions.gapSize
  153. }
  154. },
  155. // additional lines point place holders:
  156. secondaryLines = [], secondaryLinesNames = indicator.getTranslatedLinesNames(pointValKey), point;
  157. // Generate points for additional lines:
  158. secondaryLinesNames.forEach(function (plotLine, index) {
  159. // create additional lines point place holders
  160. secondaryLines[index] = [];
  161. while (pointsLength--) {
  162. point = mainLinePoints[pointsLength];
  163. secondaryLines[index].push({
  164. x: point.x,
  165. plotX: point.plotX,
  166. plotY: point[plotLine],
  167. isNull: !defined(point[plotLine])
  168. });
  169. }
  170. pointsLength = mainLinePoints.length;
  171. });
  172. // Modify options and generate additional lines:
  173. linesApiNames.forEach(function (lineName, i) {
  174. if (secondaryLines[i]) {
  175. indicator.points = secondaryLines[i];
  176. if (mainLineOptions[lineName]) {
  177. indicator.options = merge(mainLineOptions[lineName].styles, gappedExtend);
  178. }
  179. else {
  180. error('Error: "There is no ' + lineName +
  181. ' in DOCS options declared. Check if linesApiNames' +
  182. ' are consistent with your DOCS line names."' +
  183. ' at mixin/multiple-line.js:34');
  184. }
  185. indicator.graph = indicator['graph' + lineName];
  186. SMA.prototype.drawGraph.call(indicator);
  187. // Now save lines:
  188. indicator['graph' + lineName] = indicator.graph;
  189. }
  190. else {
  191. error('Error: "' + lineName + ' doesn\'t have equivalent ' +
  192. 'in pointArrayMap. To many elements in linesApiNames ' +
  193. 'relative to pointArrayMap."');
  194. }
  195. });
  196. // Restore options and draw a main line:
  197. indicator.points = mainLinePoints;
  198. indicator.options = mainLineOptions;
  199. indicator.graph = mainLinePath;
  200. SMA.prototype.drawGraph.call(indicator);
  201. }
  202. };
  203. return multipleLinesMixin;
  204. });
  205. _registerModule(_modules, 'indicators/bollinger-bands.src.js', [_modules['parts/Globals.js'], _modules['parts/Utilities.js'], _modules['mixins/multipe-lines.js']], function (H, U, multipleLinesMixin) {
  206. /**
  207. *
  208. * License: www.highcharts.com/license
  209. *
  210. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  211. *
  212. * */
  213. var isArray = U.isArray, merge = U.merge, seriesType = U.seriesType;
  214. var SMA = H.seriesTypes.sma;
  215. /* eslint-disable valid-jsdoc */
  216. // Utils:
  217. /**
  218. * @private
  219. */
  220. function getStandardDeviation(arr, index, isOHLC, mean) {
  221. var variance = 0, arrLen = arr.length, std = 0, i = 0, value;
  222. for (; i < arrLen; i++) {
  223. value = (isOHLC ? arr[i][index] : arr[i]) - mean;
  224. variance += value * value;
  225. }
  226. variance = variance / (arrLen - 1);
  227. std = Math.sqrt(variance);
  228. return std;
  229. }
  230. /* eslint-enable valid-jsdoc */
  231. /**
  232. * Bollinger Bands series type.
  233. *
  234. * @private
  235. * @class
  236. * @name Highcharts.seriesTypes.bb
  237. *
  238. * @augments Highcharts.Series
  239. */
  240. seriesType('bb', 'sma',
  241. /**
  242. * Bollinger bands (BB). This series requires the `linkedTo` option to be
  243. * set and should be loaded after the `stock/indicators/indicators.js` file.
  244. *
  245. * @sample stock/indicators/bollinger-bands
  246. * Bollinger bands
  247. *
  248. * @extends plotOptions.sma
  249. * @since 6.0.0
  250. * @product highstock
  251. * @requires stock/indicators/indicators
  252. * @requires stock/indicators/bollinger-bands
  253. * @optionparent plotOptions.bb
  254. */
  255. {
  256. params: {
  257. period: 20,
  258. /**
  259. * Standard deviation for top and bottom bands.
  260. */
  261. standardDeviation: 2,
  262. index: 3
  263. },
  264. /**
  265. * Bottom line options.
  266. */
  267. bottomLine: {
  268. /**
  269. * Styles for a bottom line.
  270. */
  271. styles: {
  272. /**
  273. * Pixel width of the line.
  274. */
  275. lineWidth: 1,
  276. /**
  277. * Color of the line. If not set, it's inherited from
  278. * [plotOptions.bb.color](#plotOptions.bb.color).
  279. *
  280. * @type {Highcharts.ColorString}
  281. */
  282. lineColor: void 0
  283. }
  284. },
  285. /**
  286. * Top line options.
  287. *
  288. * @extends plotOptions.bb.bottomLine
  289. */
  290. topLine: {
  291. styles: {
  292. lineWidth: 1,
  293. /**
  294. * @type {Highcharts.ColorString}
  295. */
  296. lineColor: void 0
  297. }
  298. },
  299. tooltip: {
  300. 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/>'
  301. },
  302. marker: {
  303. enabled: false
  304. },
  305. dataGrouping: {
  306. approximation: 'averages'
  307. }
  308. },
  309. /**
  310. * @lends Highcharts.Series#
  311. */
  312. merge(multipleLinesMixin, {
  313. pointArrayMap: ['top', 'middle', 'bottom'],
  314. pointValKey: 'middle',
  315. nameComponents: ['period', 'standardDeviation'],
  316. linesApiNames: ['topLine', 'bottomLine'],
  317. init: function () {
  318. SMA.prototype.init.apply(this, arguments);
  319. // Set default color for lines:
  320. this.options = merge({
  321. topLine: {
  322. styles: {
  323. lineColor: this.color
  324. }
  325. },
  326. bottomLine: {
  327. styles: {
  328. lineColor: this.color
  329. }
  330. }
  331. }, this.options);
  332. },
  333. getValues: function (series, params) {
  334. var period = params.period, standardDeviation = params.standardDeviation, xVal = series.xData, yVal = series.yData, yValLen = yVal ? yVal.length : 0,
  335. // 0- date, 1-middle line, 2-top line, 3-bottom line
  336. BB = [],
  337. // middle line, top line and bottom line
  338. ML, TL, BL, date, xData = [], yData = [], slicedX, slicedY, stdDev, isOHLC, point, i;
  339. if (xVal.length < period) {
  340. return;
  341. }
  342. isOHLC = isArray(yVal[0]);
  343. for (i = period; i <= yValLen; i++) {
  344. slicedX = xVal.slice(i - period, i);
  345. slicedY = yVal.slice(i - period, i);
  346. point = SMA.prototype.getValues.call(this, {
  347. xData: slicedX,
  348. yData: slicedY
  349. }, params);
  350. date = point.xData[0];
  351. ML = point.yData[0];
  352. stdDev = getStandardDeviation(slicedY, params.index, isOHLC, ML);
  353. TL = ML + standardDeviation * stdDev;
  354. BL = ML - standardDeviation * stdDev;
  355. BB.push([date, TL, ML, BL]);
  356. xData.push(date);
  357. yData.push([TL, ML, BL]);
  358. }
  359. return {
  360. values: BB,
  361. xData: xData,
  362. yData: yData
  363. };
  364. }
  365. }));
  366. /**
  367. * A bollinger bands indicator. If the [type](#series.bb.type) option is not
  368. * specified, it is inherited from [chart.type](#chart.type).
  369. *
  370. * @extends series,plotOptions.bb
  371. * @since 6.0.0
  372. * @excluding dataParser, dataURL
  373. * @product highstock
  374. * @requires stock/indicators/indicators
  375. * @requires stock/indicators/bollinger-bands
  376. * @apioption series.bb
  377. */
  378. ''; // to include the above in the js output
  379. });
  380. _registerModule(_modules, 'masters/indicators/bollinger-bands.src.js', [], function () {
  381. });
  382. }));