price-channel.src.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. /**
  2. * @license Highstock JS v8.0.0 (2019-12-10)
  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', [_modules['parts/Globals.js']], function (H) {
  32. /**
  33. *
  34. * (c) 2010-2019 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 reduce = H.reduce;
  42. var reduceArrayMixin = {
  43. /**
  44. * Get min value of array filled by OHLC data.
  45. * @private
  46. * @param {Array<*>} arr Array of OHLC points (arrays).
  47. * @param {string} index Index of "low" value in point array.
  48. * @return {number} Returns min value.
  49. */
  50. minInArray: function (arr, index) {
  51. return reduce(arr, function (min, target) {
  52. return Math.min(min, target[index]);
  53. }, Number.MAX_VALUE);
  54. },
  55. /**
  56. * Get max value of array filled by OHLC data.
  57. * @private
  58. * @param {Array<*>} arr Array of OHLC points (arrays).
  59. * @param {string} index Index of "high" value in point array.
  60. * @return {number} Returns max value.
  61. */
  62. maxInArray: function (arr, index) {
  63. return reduce(arr, function (max, target) {
  64. return Math.max(max, target[index]);
  65. }, -Number.MAX_VALUE);
  66. },
  67. /**
  68. * Get extremes of array filled by OHLC data.
  69. * @private
  70. * @param {Array<*>} arr Array of OHLC points (arrays).
  71. * @param {string} minIndex Index of "low" value in point array.
  72. * @param {string} maxIndex Index of "high" value in point array.
  73. * @return {Array<number,number>} Returns array with min and max value.
  74. */
  75. getArrayExtremes: function (arr, minIndex, maxIndex) {
  76. return reduce(arr, function (prev, target) {
  77. return [
  78. Math.min(prev[0], target[minIndex]),
  79. Math.max(prev[1], target[maxIndex])
  80. ];
  81. }, [Number.MAX_VALUE, -Number.MAX_VALUE]);
  82. }
  83. };
  84. return reduceArrayMixin;
  85. });
  86. _registerModule(_modules, 'mixins/multipe-lines.js', [_modules['parts/Globals.js'], _modules['parts/Utilities.js']], function (H, U) {
  87. /**
  88. *
  89. * (c) 2010-2019 Wojciech Chmiel
  90. *
  91. * License: www.highcharts.com/license
  92. *
  93. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  94. *
  95. * */
  96. var defined = U.defined;
  97. var each = H.each, merge = H.merge, error = H.error, SMA = H.seriesTypes.sma;
  98. /**
  99. * Mixin useful for all indicators that have more than one line.
  100. * Merge it with your implementation where you will provide
  101. * getValues method appropriate to your indicator and pointArrayMap,
  102. * pointValKey, linesApiNames properites. Notice that pointArrayMap
  103. * should be consistent with amount of lines calculated in getValues method.
  104. *
  105. * @private
  106. * @mixin multipleLinesMixin
  107. */
  108. var multipleLinesMixin = {
  109. /* eslint-disable valid-jsdoc */
  110. /**
  111. * Lines ids. Required to plot appropriate amount of lines.
  112. * Notice that pointArrayMap should have more elements than
  113. * linesApiNames, because it contains main line and additional lines ids.
  114. * Also it should be consistent with amount of lines calculated in
  115. * getValues method from your implementation.
  116. *
  117. * @private
  118. * @name multipleLinesMixin.pointArrayMap
  119. * @type {Array<string>}
  120. */
  121. pointArrayMap: ['top', 'bottom'],
  122. /**
  123. * Main line id.
  124. *
  125. * @private
  126. * @name multipleLinesMixin.pointValKey
  127. * @type {string}
  128. */
  129. pointValKey: 'top',
  130. /**
  131. * Additional lines DOCS names. Elements of linesApiNames array should
  132. * be consistent with DOCS line names defined in your implementation.
  133. * Notice that linesApiNames should have decreased amount of elements
  134. * relative to pointArrayMap (without pointValKey).
  135. *
  136. * @private
  137. * @name multipleLinesMixin.linesApiNames
  138. * @type {Array<string>}
  139. */
  140. linesApiNames: ['bottomLine'],
  141. /**
  142. * Create translatedLines Collection based on pointArrayMap.
  143. *
  144. * @private
  145. * @function multipleLinesMixin.getTranslatedLinesNames
  146. * @param {string} [excludedValue]
  147. * Main line id
  148. * @return {Array<string>}
  149. * Returns translated lines names without excluded value.
  150. */
  151. getTranslatedLinesNames: function (excludedValue) {
  152. var translatedLines = [];
  153. each(this.pointArrayMap, function (propertyName) {
  154. if (propertyName !== excludedValue) {
  155. translatedLines.push('plot' +
  156. propertyName.charAt(0).toUpperCase() +
  157. propertyName.slice(1));
  158. }
  159. });
  160. return translatedLines;
  161. },
  162. /**
  163. * @private
  164. * @function multipleLinesMixin.toYData
  165. * @param {Highcharts.Point} point
  166. * Indicator point
  167. * @return {Array<number>}
  168. * Returns point Y value for all lines
  169. */
  170. toYData: function (point) {
  171. var pointColl = [];
  172. each(this.pointArrayMap, function (propertyName) {
  173. pointColl.push(point[propertyName]);
  174. });
  175. return pointColl;
  176. },
  177. /**
  178. * Add lines plot pixel values.
  179. *
  180. * @private
  181. * @function multipleLinesMixin.translate
  182. * @return {void}
  183. */
  184. translate: function () {
  185. var indicator = this, pointArrayMap = indicator.pointArrayMap, LinesNames = [], value;
  186. LinesNames = indicator.getTranslatedLinesNames();
  187. SMA.prototype.translate.apply(indicator, arguments);
  188. each(indicator.points, function (point) {
  189. each(pointArrayMap, function (propertyName, i) {
  190. value = point[propertyName];
  191. if (value !== null) {
  192. point[LinesNames[i]] = indicator.yAxis.toPixels(value, true);
  193. }
  194. });
  195. });
  196. },
  197. /**
  198. * Draw main and additional lines.
  199. *
  200. * @private
  201. * @function multipleLinesMixin.drawGraph
  202. * @return {void}
  203. */
  204. drawGraph: function () {
  205. var indicator = this, pointValKey = indicator.pointValKey, linesApiNames = indicator.linesApiNames, mainLinePoints = indicator.points, pointsLength = mainLinePoints.length, mainLineOptions = indicator.options, mainLinePath = indicator.graph, gappedExtend = {
  206. options: {
  207. gapSize: mainLineOptions.gapSize
  208. }
  209. },
  210. // additional lines point place holders:
  211. secondaryLines = [], secondaryLinesNames = indicator.getTranslatedLinesNames(pointValKey), point;
  212. // Generate points for additional lines:
  213. each(secondaryLinesNames, function (plotLine, index) {
  214. // create additional lines point place holders
  215. secondaryLines[index] = [];
  216. while (pointsLength--) {
  217. point = mainLinePoints[pointsLength];
  218. secondaryLines[index].push({
  219. x: point.x,
  220. plotX: point.plotX,
  221. plotY: point[plotLine],
  222. isNull: !defined(point[plotLine])
  223. });
  224. }
  225. pointsLength = mainLinePoints.length;
  226. });
  227. // Modify options and generate additional lines:
  228. each(linesApiNames, function (lineName, i) {
  229. if (secondaryLines[i]) {
  230. indicator.points = secondaryLines[i];
  231. if (mainLineOptions[lineName]) {
  232. indicator.options = merge(mainLineOptions[lineName].styles, gappedExtend);
  233. }
  234. else {
  235. error('Error: "There is no ' + lineName +
  236. ' in DOCS options declared. Check if linesApiNames' +
  237. ' are consistent with your DOCS line names."' +
  238. ' at mixin/multiple-line.js:34');
  239. }
  240. indicator.graph = indicator['graph' + lineName];
  241. SMA.prototype.drawGraph.call(indicator);
  242. // Now save lines:
  243. indicator['graph' + lineName] = indicator.graph;
  244. }
  245. else {
  246. error('Error: "' + lineName + ' doesn\'t have equivalent ' +
  247. 'in pointArrayMap. To many elements in linesApiNames ' +
  248. 'relative to pointArrayMap."');
  249. }
  250. });
  251. // Restore options and draw a main line:
  252. indicator.points = mainLinePoints;
  253. indicator.options = mainLineOptions;
  254. indicator.graph = mainLinePath;
  255. SMA.prototype.drawGraph.call(indicator);
  256. }
  257. };
  258. return multipleLinesMixin;
  259. });
  260. _registerModule(_modules, 'indicators/price-channel.src.js', [_modules['parts/Globals.js'], _modules['mixins/reduce-array.js'], _modules['mixins/multipe-lines.js']], function (H, reduceArrayMixin, multipleLinesMixin) {
  261. /* *
  262. *
  263. * License: www.highcharts.com/license
  264. *
  265. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  266. *
  267. * */
  268. var getArrayExtremes = reduceArrayMixin.getArrayExtremes, merge = H.merge;
  269. /**
  270. * The Price Channel series type.
  271. *
  272. * @private
  273. * @class
  274. * @name Highcharts.seriesTypes.pc
  275. *
  276. * @augments Highcharts.Series
  277. */
  278. H.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. }));