price-envelopes.src.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /**
  2. * @license Highstock JS v9.1.1 (2021-06-04)
  3. *
  4. * Indicator series type for Highcharts Stock
  5. *
  6. * (c) 2010-2021 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/price-envelopes', ['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, 'Stock/Indicators/PriceEnvelopes/PriceEnvelopesIndicator.js', [_modules['Core/Series/SeriesRegistry.js'], _modules['Core/Utilities.js']], function (SeriesRegistry, U) {
  32. /* *
  33. *
  34. * License: www.highcharts.com/license
  35. *
  36. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  37. *
  38. * */
  39. var __extends = (this && this.__extends) || (function () {
  40. var extendStatics = function (d,
  41. b) {
  42. extendStatics = Object.setPrototypeOf ||
  43. ({ __proto__: [] } instanceof Array && function (d,
  44. b) { d.__proto__ = b; }) ||
  45. function (d,
  46. b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  47. return extendStatics(d, b);
  48. };
  49. return function (d, b) {
  50. extendStatics(d, b);
  51. function __() { this.constructor = d; }
  52. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  53. };
  54. })();
  55. var SMAIndicator = SeriesRegistry.seriesTypes.sma;
  56. var extend = U.extend,
  57. isArray = U.isArray,
  58. merge = U.merge;
  59. /**
  60. * The Price Envelopes series type.
  61. *
  62. * @private
  63. * @class
  64. * @name Highcharts.seriesTypes.priceenvelopes
  65. *
  66. * @augments Highcharts.Series
  67. */
  68. var PriceEnvelopesIndicator = /** @class */ (function (_super) {
  69. __extends(PriceEnvelopesIndicator, _super);
  70. function PriceEnvelopesIndicator() {
  71. var _this = _super !== null && _super.apply(this,
  72. arguments) || this;
  73. _this.data = void 0;
  74. _this.options = void 0;
  75. _this.points = void 0;
  76. return _this;
  77. }
  78. PriceEnvelopesIndicator.prototype.init = function () {
  79. SeriesRegistry.seriesTypes.sma.prototype.init.apply(this, arguments);
  80. // Set default color for lines:
  81. this.options = merge({
  82. topLine: {
  83. styles: {
  84. lineColor: this.color
  85. }
  86. },
  87. bottomLine: {
  88. styles: {
  89. lineColor: this.color
  90. }
  91. }
  92. }, this.options);
  93. };
  94. PriceEnvelopesIndicator.prototype.toYData = function (point) {
  95. return [point.top, point.middle, point.bottom];
  96. };
  97. PriceEnvelopesIndicator.prototype.translate = function () {
  98. var indicator = this, translatedEnvelopes = ['plotTop', 'plotMiddle', 'plotBottom'];
  99. SeriesRegistry.seriesTypes.sma.prototype.translate.apply(indicator);
  100. indicator.points.forEach(function (point) {
  101. [point.top, point.middle, point.bottom].forEach(function (value, i) {
  102. if (value !== null) {
  103. point[translatedEnvelopes[i]] =
  104. indicator.yAxis.toPixels(value, true);
  105. }
  106. });
  107. });
  108. };
  109. PriceEnvelopesIndicator.prototype.drawGraph = function () {
  110. var indicator = this,
  111. middleLinePoints = indicator.points,
  112. pointsLength = middleLinePoints.length,
  113. middleLineOptions = (indicator.options),
  114. middleLinePath = indicator.graph,
  115. gappedExtend = {
  116. options: {
  117. gapSize: middleLineOptions.gapSize
  118. }
  119. },
  120. deviations = [[],
  121. []], // top and bottom point place holders
  122. point;
  123. // Generate points for top and bottom lines:
  124. while (pointsLength--) {
  125. point = middleLinePoints[pointsLength];
  126. deviations[0].push({
  127. plotX: point.plotX,
  128. plotY: point.plotTop,
  129. isNull: point.isNull
  130. });
  131. deviations[1].push({
  132. plotX: point.plotX,
  133. plotY: point.plotBottom,
  134. isNull: point.isNull
  135. });
  136. }
  137. // Modify options and generate lines:
  138. ['topLine', 'bottomLine'].forEach(function (lineName, i) {
  139. indicator.points = deviations[i];
  140. indicator.options = merge(middleLineOptions[lineName].styles, gappedExtend);
  141. indicator.graph = indicator['graph' + lineName];
  142. SeriesRegistry.seriesTypes.sma.prototype.drawGraph.call(indicator);
  143. // Now save lines:
  144. indicator['graph' + lineName] = indicator.graph;
  145. });
  146. // Restore options and draw a middle line:
  147. indicator.points = middleLinePoints;
  148. indicator.options = middleLineOptions;
  149. indicator.graph = middleLinePath;
  150. SeriesRegistry.seriesTypes.sma.prototype.drawGraph.call(indicator);
  151. };
  152. PriceEnvelopesIndicator.prototype.getValues = function (series, params) {
  153. var period = params.period,
  154. topPercent = params.topBand,
  155. botPercent = params.bottomBand,
  156. xVal = series.xData,
  157. yVal = series.yData,
  158. yValLen = yVal ? yVal.length : 0,
  159. // 0- date, 1-top line, 2-middle line, 3-bottom line
  160. PE = [],
  161. // middle line, top line and bottom line
  162. ML,
  163. TL,
  164. BL,
  165. date,
  166. xData = [],
  167. yData = [],
  168. slicedX,
  169. slicedY,
  170. point,
  171. i;
  172. // Price envelopes requires close value
  173. if (xVal.length < period ||
  174. !isArray(yVal[0]) ||
  175. yVal[0].length !== 4) {
  176. return;
  177. }
  178. for (i = period; i <= yValLen; i++) {
  179. slicedX = xVal.slice(i - period, i);
  180. slicedY = yVal.slice(i - period, i);
  181. point = SeriesRegistry.seriesTypes.sma.prototype.getValues.call(this, {
  182. xData: slicedX,
  183. yData: slicedY
  184. }, params);
  185. date = point.xData[0];
  186. ML = point.yData[0];
  187. TL = ML * (1 + topPercent);
  188. BL = ML * (1 - botPercent);
  189. PE.push([date, TL, ML, BL]);
  190. xData.push(date);
  191. yData.push([TL, ML, BL]);
  192. }
  193. return {
  194. values: PE,
  195. xData: xData,
  196. yData: yData
  197. };
  198. };
  199. /**
  200. * Price envelopes indicator based on [SMA](#plotOptions.sma) calculations.
  201. * This series requires the `linkedTo` option to be set and should be loaded
  202. * after the `stock/indicators/indicators.js` file.
  203. *
  204. * @sample stock/indicators/price-envelopes
  205. * Price envelopes
  206. *
  207. * @extends plotOptions.sma
  208. * @since 6.0.0
  209. * @product highstock
  210. * @requires stock/indicators/indicators
  211. * @requires stock/indicators/price-envelopes
  212. * @optionparent plotOptions.priceenvelopes
  213. */
  214. PriceEnvelopesIndicator.defaultOptions = merge(SMAIndicator.defaultOptions, {
  215. marker: {
  216. enabled: false
  217. },
  218. tooltip: {
  219. 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/>'
  220. },
  221. params: {
  222. period: 20,
  223. /**
  224. * Percentage above the moving average that should be displayed.
  225. * 0.1 means 110%. Relative to the calculated value.
  226. */
  227. topBand: 0.1,
  228. /**
  229. * Percentage below the moving average that should be displayed.
  230. * 0.1 means 90%. Relative to the calculated value.
  231. */
  232. bottomBand: 0.1
  233. },
  234. /**
  235. * Bottom line options.
  236. */
  237. bottomLine: {
  238. styles: {
  239. /**
  240. * Pixel width of the line.
  241. */
  242. lineWidth: 1,
  243. /**
  244. * Color of the line. If not set, it's inherited from
  245. * [plotOptions.priceenvelopes.color](
  246. * #plotOptions.priceenvelopes.color).
  247. *
  248. * @type {Highcharts.ColorString}
  249. */
  250. lineColor: void 0
  251. }
  252. },
  253. /**
  254. * Top line options.
  255. *
  256. * @extends plotOptions.priceenvelopes.bottomLine
  257. */
  258. topLine: {
  259. styles: {
  260. lineWidth: 1
  261. }
  262. },
  263. dataGrouping: {
  264. approximation: 'averages'
  265. }
  266. });
  267. return PriceEnvelopesIndicator;
  268. }(SMAIndicator));
  269. extend(PriceEnvelopesIndicator.prototype, {
  270. nameComponents: ['period', 'topBand', 'bottomBand'],
  271. nameBase: 'Price envelopes',
  272. pointArrayMap: ['top', 'middle', 'bottom'],
  273. parallelArrays: ['x', 'y', 'top', 'bottom'],
  274. pointValKey: 'middle'
  275. });
  276. SeriesRegistry.registerSeriesType('priceenvelopes', PriceEnvelopesIndicator);
  277. /* *
  278. *
  279. * Default Export
  280. *
  281. * */
  282. /**
  283. * A price envelopes indicator. If the [type](#series.priceenvelopes.type)
  284. * option is not specified, it is inherited from [chart.type](#chart.type).
  285. *
  286. * @extends series,plotOptions.priceenvelopes
  287. * @since 6.0.0
  288. * @excluding dataParser, dataURL
  289. * @product highstock
  290. * @requires stock/indicators/indicators
  291. * @requires stock/indicators/price-envelopes
  292. * @apioption series.priceenvelopes
  293. */
  294. ''; // to include the above in the js output
  295. return PriceEnvelopesIndicator;
  296. });
  297. _registerModule(_modules, 'masters/indicators/price-envelopes.src.js', [], function () {
  298. });
  299. }));