keltner-channels.src.js 15 KB

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