acceleration-bands.src.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. /**
  2. * @license Highstock JS v8.2.0 (2020-08-20)
  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/MultipleLines.js', [_modules['Core/Globals.js'], _modules['Core/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,
  42. error = U.error,
  43. merge = U.merge;
  44. var SMA = H.seriesTypes.sma;
  45. /**
  46. * Mixin useful for all indicators that have more than one line.
  47. * Merge it with your implementation where you will provide
  48. * getValues method appropriate to your indicator and pointArrayMap,
  49. * pointValKey, linesApiNames properites. Notice that pointArrayMap
  50. * should be consistent with amount of lines calculated in getValues method.
  51. *
  52. * @private
  53. * @mixin multipleLinesMixin
  54. */
  55. var multipleLinesMixin = {
  56. /* eslint-disable valid-jsdoc */
  57. /**
  58. * Lines ids. Required to plot appropriate amount of lines.
  59. * Notice that pointArrayMap should have more elements than
  60. * linesApiNames, because it contains main line and additional lines ids.
  61. * Also it should be consistent with amount of lines calculated in
  62. * getValues method from your implementation.
  63. *
  64. * @private
  65. * @name multipleLinesMixin.pointArrayMap
  66. * @type {Array<string>}
  67. */
  68. pointArrayMap: ['top', 'bottom'],
  69. /**
  70. * Main line id.
  71. *
  72. * @private
  73. * @name multipleLinesMixin.pointValKey
  74. * @type {string}
  75. */
  76. pointValKey: 'top',
  77. /**
  78. * Additional lines DOCS names. Elements of linesApiNames array should
  79. * be consistent with DOCS line names defined in your implementation.
  80. * Notice that linesApiNames should have decreased amount of elements
  81. * relative to pointArrayMap (without pointValKey).
  82. *
  83. * @private
  84. * @name multipleLinesMixin.linesApiNames
  85. * @type {Array<string>}
  86. */
  87. linesApiNames: ['bottomLine'],
  88. /**
  89. * Create translatedLines Collection based on pointArrayMap.
  90. *
  91. * @private
  92. * @function multipleLinesMixin.getTranslatedLinesNames
  93. * @param {string} [excludedValue]
  94. * Main line id
  95. * @return {Array<string>}
  96. * Returns translated lines names without excluded value.
  97. */
  98. getTranslatedLinesNames: function (excludedValue) {
  99. var translatedLines = [];
  100. (this.pointArrayMap || []).forEach(function (propertyName) {
  101. if (propertyName !== excludedValue) {
  102. translatedLines.push('plot' +
  103. propertyName.charAt(0).toUpperCase() +
  104. propertyName.slice(1));
  105. }
  106. });
  107. return translatedLines;
  108. },
  109. /**
  110. * @private
  111. * @function multipleLinesMixin.toYData
  112. * @param {Highcharts.Point} point
  113. * Indicator point
  114. * @return {Array<number>}
  115. * Returns point Y value for all lines
  116. */
  117. toYData: function (point) {
  118. var pointColl = [];
  119. (this.pointArrayMap || []).forEach(function (propertyName) {
  120. pointColl.push(point[propertyName]);
  121. });
  122. return pointColl;
  123. },
  124. /**
  125. * Add lines plot pixel values.
  126. *
  127. * @private
  128. * @function multipleLinesMixin.translate
  129. * @return {void}
  130. */
  131. translate: function () {
  132. var indicator = this,
  133. pointArrayMap = indicator.pointArrayMap,
  134. LinesNames = [],
  135. value;
  136. LinesNames = indicator.getTranslatedLinesNames();
  137. SMA.prototype.translate.apply(indicator, arguments);
  138. indicator.points.forEach(function (point) {
  139. pointArrayMap.forEach(function (propertyName, i) {
  140. value = point[propertyName];
  141. if (value !== null) {
  142. point[LinesNames[i]] = indicator.yAxis.toPixels(value, true);
  143. }
  144. });
  145. });
  146. },
  147. /**
  148. * Draw main and additional lines.
  149. *
  150. * @private
  151. * @function multipleLinesMixin.drawGraph
  152. * @return {void}
  153. */
  154. drawGraph: function () {
  155. var indicator = this,
  156. pointValKey = indicator.pointValKey,
  157. linesApiNames = indicator.linesApiNames,
  158. mainLinePoints = indicator.points,
  159. pointsLength = mainLinePoints.length,
  160. mainLineOptions = indicator.options,
  161. mainLinePath = indicator.graph,
  162. gappedExtend = {
  163. options: {
  164. gapSize: mainLineOptions.gapSize
  165. }
  166. },
  167. // additional lines point place holders:
  168. secondaryLines = [],
  169. secondaryLinesNames = indicator.getTranslatedLinesNames(pointValKey),
  170. point;
  171. // Generate points for additional lines:
  172. secondaryLinesNames.forEach(function (plotLine, index) {
  173. // create additional lines point place holders
  174. secondaryLines[index] = [];
  175. while (pointsLength--) {
  176. point = mainLinePoints[pointsLength];
  177. secondaryLines[index].push({
  178. x: point.x,
  179. plotX: point.plotX,
  180. plotY: point[plotLine],
  181. isNull: !defined(point[plotLine])
  182. });
  183. }
  184. pointsLength = mainLinePoints.length;
  185. });
  186. // Modify options and generate additional lines:
  187. linesApiNames.forEach(function (lineName, i) {
  188. if (secondaryLines[i]) {
  189. indicator.points = secondaryLines[i];
  190. if (mainLineOptions[lineName]) {
  191. indicator.options = merge(mainLineOptions[lineName].styles, gappedExtend);
  192. }
  193. else {
  194. error('Error: "There is no ' + lineName +
  195. ' in DOCS options declared. Check if linesApiNames' +
  196. ' are consistent with your DOCS line names."' +
  197. ' at mixin/multiple-line.js:34');
  198. }
  199. indicator.graph = indicator['graph' + lineName];
  200. SMA.prototype.drawGraph.call(indicator);
  201. // Now save lines:
  202. indicator['graph' + lineName] = indicator.graph;
  203. }
  204. else {
  205. error('Error: "' + lineName + ' doesn\'t have equivalent ' +
  206. 'in pointArrayMap. To many elements in linesApiNames ' +
  207. 'relative to pointArrayMap."');
  208. }
  209. });
  210. // Restore options and draw a main line:
  211. indicator.points = mainLinePoints;
  212. indicator.options = mainLineOptions;
  213. indicator.graph = mainLinePath;
  214. SMA.prototype.drawGraph.call(indicator);
  215. }
  216. };
  217. return multipleLinesMixin;
  218. });
  219. _registerModule(_modules, 'Stock/Indicators/ABIndicator.js', [_modules['Core/Globals.js'], _modules['Core/Utilities.js'], _modules['Mixins/MultipleLines.js']], function (H, U, multipleLinesMixin) {
  220. /* *
  221. *
  222. * License: www.highcharts.com/license
  223. *
  224. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  225. *
  226. * */
  227. var correctFloat = U.correctFloat,
  228. merge = U.merge,
  229. seriesType = U.seriesType;
  230. var SMA = H.seriesTypes.sma;
  231. /* eslint-disable valid-jsdoc */
  232. /**
  233. * @private
  234. */
  235. function getBaseForBand(low, high, factor) {
  236. return (((correctFloat(high - low)) /
  237. ((correctFloat(high + low)) / 2)) * 1000) * factor;
  238. }
  239. /**
  240. * @private
  241. */
  242. function getPointUB(high, base) {
  243. return high * (correctFloat(1 + 2 * base));
  244. }
  245. /**
  246. * @private
  247. */
  248. function getPointLB(low, base) {
  249. return low * (correctFloat(1 - 2 * base));
  250. }
  251. /* eslint-enable valid-jsdoc */
  252. /**
  253. * The ABands series type
  254. *
  255. * @private
  256. * @class
  257. * @name Highcharts.seriesTypes.abands
  258. *
  259. * @augments Highcharts.Series
  260. */
  261. seriesType('abands', 'sma',
  262. /**
  263. * Acceleration bands (ABANDS). This series requires the `linkedTo` option
  264. * to be set and should be loaded after the
  265. * `stock/indicators/indicators.js`.
  266. *
  267. * @sample {highstock} stock/indicators/acceleration-bands
  268. * Acceleration Bands
  269. *
  270. * @extends plotOptions.sma
  271. * @mixes Highcharts.MultipleLinesMixin
  272. * @since 7.0.0
  273. * @product highstock
  274. * @excluding allAreas, colorAxis, compare, compareBase, joinBy, keys,
  275. * navigatorOptions, pointInterval, pointIntervalUnit,
  276. * pointPlacement, pointRange, pointStart, showInNavigator,
  277. * stacking,
  278. * @requires stock/indicators/indicators
  279. * @requires stock/indicators/acceleration-bands
  280. * @optionparent plotOptions.abands
  281. */
  282. {
  283. params: {
  284. period: 20,
  285. /**
  286. * The algorithms factor value used to calculate bands.
  287. *
  288. * @product highstock
  289. */
  290. factor: 0.001,
  291. index: 3
  292. },
  293. lineWidth: 1,
  294. topLine: {
  295. styles: {
  296. /**
  297. * Pixel width of the line.
  298. */
  299. lineWidth: 1
  300. }
  301. },
  302. bottomLine: {
  303. styles: {
  304. /**
  305. * Pixel width of the line.
  306. */
  307. lineWidth: 1
  308. }
  309. },
  310. dataGrouping: {
  311. approximation: 'averages'
  312. }
  313. },
  314. /**
  315. * @lends Highcharts.Series#
  316. */
  317. merge(multipleLinesMixin, {
  318. pointArrayMap: ['top', 'middle', 'bottom'],
  319. pointValKey: 'middle',
  320. nameBase: 'Acceleration Bands',
  321. nameComponents: ['period', 'factor'],
  322. linesApiNames: ['topLine', 'bottomLine'],
  323. getValues: function (series, params) {
  324. var period = params.period,
  325. factor = params.factor,
  326. index = params.index,
  327. xVal = series.xData,
  328. yVal = series.yData,
  329. yValLen = yVal ? yVal.length : 0,
  330. // Upperbands
  331. UB = [],
  332. // Lowerbands
  333. LB = [],
  334. // ABANDS array structure:
  335. // 0-date, 1-top line, 2-middle line, 3-bottom line
  336. ABANDS = [],
  337. // middle line, top line and bottom line
  338. ML,
  339. TL,
  340. BL,
  341. date,
  342. bandBase,
  343. pointSMA,
  344. ubSMA,
  345. lbSMA,
  346. low = 2,
  347. high = 1,
  348. xData = [],
  349. yData = [],
  350. slicedX,
  351. slicedY,
  352. i;
  353. if (yValLen < period) {
  354. return;
  355. }
  356. for (i = 0; i <= yValLen; i++) {
  357. // Get UB and LB values of every point. This condition
  358. // is necessary, because there is a need to calculate current
  359. // UB nad LB values simultaneously with given period SMA
  360. // in one for loop.
  361. if (i < yValLen) {
  362. bandBase = getBaseForBand(yVal[i][low], yVal[i][high], factor);
  363. UB.push(getPointUB(yVal[i][high], bandBase));
  364. LB.push(getPointLB(yVal[i][low], bandBase));
  365. }
  366. if (i >= period) {
  367. slicedX = xVal.slice(i - period, i);
  368. slicedY = yVal.slice(i - period, i);
  369. ubSMA = SMA.prototype.getValues.call(this, {
  370. xData: slicedX,
  371. yData: UB.slice(i - period, i)
  372. }, {
  373. period: period
  374. });
  375. lbSMA = SMA.prototype.getValues.call(this, {
  376. xData: slicedX,
  377. yData: LB.slice(i - period, i)
  378. }, {
  379. period: period
  380. });
  381. pointSMA = SMA.prototype.getValues.call(this, {
  382. xData: slicedX,
  383. yData: slicedY
  384. }, {
  385. period: period,
  386. index: index
  387. });
  388. date = pointSMA.xData[0];
  389. TL = ubSMA.yData[0];
  390. BL = lbSMA.yData[0];
  391. ML = pointSMA.yData[0];
  392. ABANDS.push([date, TL, ML, BL]);
  393. xData.push(date);
  394. yData.push([TL, ML, BL]);
  395. }
  396. }
  397. return {
  398. values: ABANDS,
  399. xData: xData,
  400. yData: yData
  401. };
  402. }
  403. }));
  404. /**
  405. * An Acceleration bands indicator. If the [type](#series.abands.type) option is not
  406. * specified, it is inherited from [chart.type](#chart.type).
  407. *
  408. * @extends series,plotOptions.abands
  409. * @since 7.0.0
  410. * @product highstock
  411. * @excluding allAreas, colorAxis, compare, compareBase, dataParser, dataURL,
  412. * joinBy, keys, navigatorOptions, pointInterval,
  413. * pointIntervalUnit, pointPlacement, pointRange, pointStart,
  414. * stacking, showInNavigator,
  415. * @requires stock/indicators/indicators
  416. * @requires stock/indicators/acceleration-bands
  417. * @apioption series.abands
  418. */
  419. ''; // to include the above in jsdoc
  420. });
  421. _registerModule(_modules, 'masters/indicators/acceleration-bands.src.js', [], function () {
  422. });
  423. }));