price-envelopes.src.js 10 KB

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