price-channel.src.js 16 KB

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