acceleration-bands.src.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. /**
  2. * @license Highstock JS v8.1.2 (2020-06-16)
  3. *
  4. * Indicator series type for Highstock
  5. *
  6. * (c) 2010-2019 Daniel Studencki
  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/acceleration-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/acceleration-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 correctFloat = U.correctFloat, merge = U.merge, seriesType = U.seriesType;
  214. var SMA = H.seriesTypes.sma;
  215. /* eslint-disable valid-jsdoc */
  216. /**
  217. * @private
  218. */
  219. function getBaseForBand(low, high, factor) {
  220. return (((correctFloat(high - low)) /
  221. ((correctFloat(high + low)) / 2)) * 1000) * factor;
  222. }
  223. /**
  224. * @private
  225. */
  226. function getPointUB(high, base) {
  227. return high * (correctFloat(1 + 2 * base));
  228. }
  229. /**
  230. * @private
  231. */
  232. function getPointLB(low, base) {
  233. return low * (correctFloat(1 - 2 * base));
  234. }
  235. /* eslint-enable valid-jsdoc */
  236. /**
  237. * The ABands series type
  238. *
  239. * @private
  240. * @class
  241. * @name Highcharts.seriesTypes.abands
  242. *
  243. * @augments Highcharts.Series
  244. */
  245. seriesType('abands', 'sma',
  246. /**
  247. * Acceleration bands (ABANDS). This series requires the `linkedTo` option
  248. * to be set and should be loaded after the
  249. * `stock/indicators/indicators.js`.
  250. *
  251. * @sample {highstock} stock/indicators/acceleration-bands
  252. * Acceleration Bands
  253. *
  254. * @extends plotOptions.sma
  255. * @mixes Highcharts.MultipleLinesMixin
  256. * @since 7.0.0
  257. * @product highstock
  258. * @excluding allAreas, colorAxis, compare, compareBase, joinBy, keys,
  259. * navigatorOptions, pointInterval, pointIntervalUnit,
  260. * pointPlacement, pointRange, pointStart, showInNavigator,
  261. * stacking,
  262. * @requires stock/indicators/indicators
  263. * @requires stock/indicators/acceleration-bands
  264. * @optionparent plotOptions.abands
  265. */
  266. {
  267. params: {
  268. period: 20,
  269. /**
  270. * The algorithms factor value used to calculate bands.
  271. *
  272. * @product highstock
  273. */
  274. factor: 0.001,
  275. index: 3
  276. },
  277. lineWidth: 1,
  278. topLine: {
  279. styles: {
  280. /**
  281. * Pixel width of the line.
  282. */
  283. lineWidth: 1
  284. }
  285. },
  286. bottomLine: {
  287. styles: {
  288. /**
  289. * Pixel width of the line.
  290. */
  291. lineWidth: 1
  292. }
  293. },
  294. dataGrouping: {
  295. approximation: 'averages'
  296. }
  297. },
  298. /**
  299. * @lends Highcharts.Series#
  300. */
  301. merge(multipleLinesMixin, {
  302. pointArrayMap: ['top', 'middle', 'bottom'],
  303. pointValKey: 'middle',
  304. nameBase: 'Acceleration Bands',
  305. nameComponents: ['period', 'factor'],
  306. linesApiNames: ['topLine', 'bottomLine'],
  307. getValues: function (series, params) {
  308. var period = params.period, factor = params.factor, index = params.index, xVal = series.xData, yVal = series.yData, yValLen = yVal ? yVal.length : 0,
  309. // Upperbands
  310. UB = [],
  311. // Lowerbands
  312. LB = [],
  313. // ABANDS array structure:
  314. // 0-date, 1-top line, 2-middle line, 3-bottom line
  315. ABANDS = [],
  316. // middle line, top line and bottom line
  317. ML, TL, BL, date, bandBase, pointSMA, ubSMA, lbSMA, low = 2, high = 1, xData = [], yData = [], slicedX, slicedY, i;
  318. if (yValLen < period) {
  319. return;
  320. }
  321. for (i = 0; i <= yValLen; i++) {
  322. // Get UB and LB values of every point. This condition
  323. // is necessary, because there is a need to calculate current
  324. // UB nad LB values simultaneously with given period SMA
  325. // in one for loop.
  326. if (i < yValLen) {
  327. bandBase = getBaseForBand(yVal[i][low], yVal[i][high], factor);
  328. UB.push(getPointUB(yVal[i][high], bandBase));
  329. LB.push(getPointLB(yVal[i][low], bandBase));
  330. }
  331. if (i >= period) {
  332. slicedX = xVal.slice(i - period, i);
  333. slicedY = yVal.slice(i - period, i);
  334. ubSMA = SMA.prototype.getValues.call(this, {
  335. xData: slicedX,
  336. yData: UB.slice(i - period, i)
  337. }, {
  338. period: period
  339. });
  340. lbSMA = SMA.prototype.getValues.call(this, {
  341. xData: slicedX,
  342. yData: LB.slice(i - period, i)
  343. }, {
  344. period: period
  345. });
  346. pointSMA = SMA.prototype.getValues.call(this, {
  347. xData: slicedX,
  348. yData: slicedY
  349. }, {
  350. period: period,
  351. index: index
  352. });
  353. date = pointSMA.xData[0];
  354. TL = ubSMA.yData[0];
  355. BL = lbSMA.yData[0];
  356. ML = pointSMA.yData[0];
  357. ABANDS.push([date, TL, ML, BL]);
  358. xData.push(date);
  359. yData.push([TL, ML, BL]);
  360. }
  361. }
  362. return {
  363. values: ABANDS,
  364. xData: xData,
  365. yData: yData
  366. };
  367. }
  368. }));
  369. /**
  370. * An Acceleration bands indicator. If the [type](#series.abands.type) option is not
  371. * specified, it is inherited from [chart.type](#chart.type).
  372. *
  373. * @extends series,plotOptions.abands
  374. * @since 7.0.0
  375. * @product highstock
  376. * @excluding allAreas, colorAxis, compare, compareBase, dataParser, dataURL,
  377. * joinBy, keys, navigatorOptions, pointInterval,
  378. * pointIntervalUnit, pointPlacement, pointRange, pointStart,
  379. * stacking, showInNavigator,
  380. * @requires stock/indicators/indicators
  381. * @requires stock/indicators/acceleration-bands
  382. * @apioption series.abands
  383. */
  384. ''; // to include the above in jsdoc
  385. });
  386. _registerModule(_modules, 'masters/indicators/acceleration-bands.src.js', [], function () {
  387. });
  388. }));