price-envelopes.src.js 9.6 KB

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