stochastic.src.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. /**
  2. * @license Highstock JS v9.1.1 (2021-06-04)
  3. *
  4. * Indicator series type for Highcharts Stock
  5. *
  6. * (c) 2010-2021 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/stochastic', ['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/MultipleLines.js', [_modules['Core/Globals.js'], _modules['Core/Utilities.js']], function (H, U) {
  32. /**
  33. *
  34. * (c) 2010-2021 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,
  42. error = U.error,
  43. merge = U.merge;
  44. var SMA = H.seriesTypes.sma;
  45. /**
  46. * Mixin useful for all indicators that have more than one line.
  47. * Merge it with your implementation where you will provide
  48. * getValues method appropriate to your indicator and pointArrayMap,
  49. * pointValKey, linesApiNames properites. Notice that pointArrayMap
  50. * should be consistent with amount of lines calculated in getValues method.
  51. *
  52. * @private
  53. * @mixin multipleLinesMixin
  54. */
  55. var multipleLinesMixin = {
  56. /* eslint-disable valid-jsdoc */
  57. /**
  58. * Lines ids. Required to plot appropriate amount of lines.
  59. * Notice that pointArrayMap should have more elements than
  60. * linesApiNames, because it contains main line and additional lines ids.
  61. * Also it should be consistent with amount of lines calculated in
  62. * getValues method from your implementation.
  63. *
  64. * @private
  65. * @name multipleLinesMixin.pointArrayMap
  66. * @type {Array<string>}
  67. */
  68. pointArrayMap: ['top', 'bottom'],
  69. /**
  70. * Main line id.
  71. *
  72. * @private
  73. * @name multipleLinesMixin.pointValKey
  74. * @type {string}
  75. */
  76. pointValKey: 'top',
  77. /**
  78. * Additional lines DOCS names. Elements of linesApiNames array should
  79. * be consistent with DOCS line names defined in your implementation.
  80. * Notice that linesApiNames should have decreased amount of elements
  81. * relative to pointArrayMap (without pointValKey).
  82. *
  83. * @private
  84. * @name multipleLinesMixin.linesApiNames
  85. * @type {Array<string>}
  86. */
  87. linesApiNames: ['bottomLine'],
  88. /**
  89. * Create translatedLines Collection based on pointArrayMap.
  90. *
  91. * @private
  92. * @function multipleLinesMixin.getTranslatedLinesNames
  93. * @param {string} [excludedValue]
  94. * Main line id
  95. * @return {Array<string>}
  96. * Returns translated lines names without excluded value.
  97. */
  98. getTranslatedLinesNames: function (excludedValue) {
  99. var translatedLines = [];
  100. (this.pointArrayMap || []).forEach(function (propertyName) {
  101. if (propertyName !== excludedValue) {
  102. translatedLines.push('plot' +
  103. propertyName.charAt(0).toUpperCase() +
  104. propertyName.slice(1));
  105. }
  106. });
  107. return translatedLines;
  108. },
  109. /**
  110. * @private
  111. * @function multipleLinesMixin.toYData
  112. * @param {Highcharts.Point} point
  113. * Indicator point
  114. * @return {Array<number>}
  115. * Returns point Y value for all lines
  116. */
  117. toYData: function (point) {
  118. var pointColl = [];
  119. (this.pointArrayMap || []).forEach(function (propertyName) {
  120. pointColl.push(point[propertyName]);
  121. });
  122. return pointColl;
  123. },
  124. /**
  125. * Add lines plot pixel values.
  126. *
  127. * @private
  128. * @function multipleLinesMixin.translate
  129. * @return {void}
  130. */
  131. translate: function () {
  132. var indicator = this,
  133. pointArrayMap = indicator.pointArrayMap,
  134. LinesNames = [],
  135. value;
  136. LinesNames = indicator.getTranslatedLinesNames();
  137. SMA.prototype.translate.apply(indicator, arguments);
  138. indicator.points.forEach(function (point) {
  139. pointArrayMap.forEach(function (propertyName, i) {
  140. value = point[propertyName];
  141. if (value !== null) {
  142. point[LinesNames[i]] = indicator.yAxis.toPixels(value, true);
  143. }
  144. });
  145. });
  146. },
  147. /**
  148. * Draw main and additional lines.
  149. *
  150. * @private
  151. * @function multipleLinesMixin.drawGraph
  152. * @return {void}
  153. */
  154. drawGraph: function () {
  155. var indicator = this,
  156. pointValKey = indicator.pointValKey,
  157. linesApiNames = indicator.linesApiNames,
  158. mainLinePoints = indicator.points,
  159. pointsLength = mainLinePoints.length,
  160. mainLineOptions = indicator.options,
  161. mainLinePath = indicator.graph,
  162. gappedExtend = {
  163. options: {
  164. gapSize: mainLineOptions.gapSize
  165. }
  166. },
  167. // additional lines point place holders:
  168. secondaryLines = [],
  169. secondaryLinesNames = indicator.getTranslatedLinesNames(pointValKey),
  170. point;
  171. // Generate points for additional lines:
  172. secondaryLinesNames.forEach(function (plotLine, index) {
  173. // create additional lines point place holders
  174. secondaryLines[index] = [];
  175. while (pointsLength--) {
  176. point = mainLinePoints[pointsLength];
  177. secondaryLines[index].push({
  178. x: point.x,
  179. plotX: point.plotX,
  180. plotY: point[plotLine],
  181. isNull: !defined(point[plotLine])
  182. });
  183. }
  184. pointsLength = mainLinePoints.length;
  185. });
  186. // Modify options and generate additional lines:
  187. linesApiNames.forEach(function (lineName, i) {
  188. if (secondaryLines[i]) {
  189. indicator.points = secondaryLines[i];
  190. if (mainLineOptions[lineName]) {
  191. indicator.options = merge(mainLineOptions[lineName].styles, gappedExtend);
  192. }
  193. else {
  194. error('Error: "There is no ' + lineName +
  195. ' in DOCS options declared. Check if linesApiNames' +
  196. ' are consistent with your DOCS line names."' +
  197. ' at mixin/multiple-line.js:34');
  198. }
  199. indicator.graph = indicator['graph' + lineName];
  200. SMA.prototype.drawGraph.call(indicator);
  201. // Now save lines:
  202. indicator['graph' + lineName] = indicator.graph;
  203. }
  204. else {
  205. error('Error: "' + lineName + ' doesn\'t have equivalent ' +
  206. 'in pointArrayMap. To many elements in linesApiNames ' +
  207. 'relative to pointArrayMap."');
  208. }
  209. });
  210. // Restore options and draw a main line:
  211. indicator.points = mainLinePoints;
  212. indicator.options = mainLineOptions;
  213. indicator.graph = mainLinePath;
  214. SMA.prototype.drawGraph.call(indicator);
  215. }
  216. };
  217. return multipleLinesMixin;
  218. });
  219. _registerModule(_modules, 'Mixins/ReduceArray.js', [], function () {
  220. /**
  221. *
  222. * (c) 2010-2021 Pawel Fus & Daniel Studencki
  223. *
  224. * License: www.highcharts.com/license
  225. *
  226. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  227. *
  228. * */
  229. var reduceArrayMixin = {
  230. /**
  231. * Get min value of array filled by OHLC data.
  232. * @private
  233. * @param {Array<*>} arr Array of OHLC points (arrays).
  234. * @param {string} index Index of "low" value in point array.
  235. * @return {number} Returns min value.
  236. */
  237. minInArray: function (arr,
  238. index) {
  239. return arr.reduce(function (min,
  240. target) {
  241. return Math.min(min,
  242. target[index]);
  243. }, Number.MAX_VALUE);
  244. },
  245. /**
  246. * Get max value of array filled by OHLC data.
  247. * @private
  248. * @param {Array<*>} arr Array of OHLC points (arrays).
  249. * @param {string} index Index of "high" value in point array.
  250. * @return {number} Returns max value.
  251. */
  252. maxInArray: function (arr, index) {
  253. return arr.reduce(function (max, target) {
  254. return Math.max(max, target[index]);
  255. }, -Number.MAX_VALUE);
  256. },
  257. /**
  258. * Get extremes of array filled by OHLC data.
  259. * @private
  260. * @param {Array<*>} arr Array of OHLC points (arrays).
  261. * @param {string} minIndex Index of "low" value in point array.
  262. * @param {string} maxIndex Index of "high" value in point array.
  263. * @return {Array<number,number>} Returns array with min and max value.
  264. */
  265. getArrayExtremes: function (arr, minIndex, maxIndex) {
  266. return arr.reduce(function (prev, target) {
  267. return [
  268. Math.min(prev[0], target[minIndex]),
  269. Math.max(prev[1], target[maxIndex])
  270. ];
  271. }, [Number.MAX_VALUE, -Number.MAX_VALUE]);
  272. }
  273. };
  274. return reduceArrayMixin;
  275. });
  276. _registerModule(_modules, 'Stock/Indicators/Stochastic/StochasticIndicator.js', [_modules['Mixins/MultipleLines.js'], _modules['Mixins/ReduceArray.js'], _modules['Core/Series/SeriesRegistry.js'], _modules['Core/Utilities.js']], function (MultipleLinesMixin, ReduceArrayMixin, SeriesRegistry, U) {
  277. /* *
  278. *
  279. * License: www.highcharts.com/license
  280. *
  281. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  282. *
  283. * */
  284. var __extends = (this && this.__extends) || (function () {
  285. var extendStatics = function (d,
  286. b) {
  287. extendStatics = Object.setPrototypeOf ||
  288. ({ __proto__: [] } instanceof Array && function (d,
  289. b) { d.__proto__ = b; }) ||
  290. function (d,
  291. b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  292. return extendStatics(d, b);
  293. };
  294. return function (d, b) {
  295. extendStatics(d, b);
  296. function __() { this.constructor = d; }
  297. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  298. };
  299. })();
  300. var SMAIndicator = SeriesRegistry.seriesTypes.sma;
  301. var extend = U.extend,
  302. isArray = U.isArray,
  303. merge = U.merge;
  304. /**
  305. * The Stochastic series type.
  306. *
  307. * @private
  308. * @class
  309. * @name Highcharts.seriesTypes.stochastic
  310. *
  311. * @augments Highcharts.Series
  312. */
  313. var StochasticIndicator = /** @class */ (function (_super) {
  314. __extends(StochasticIndicator, _super);
  315. function StochasticIndicator() {
  316. var _this = _super !== null && _super.apply(this,
  317. arguments) || this;
  318. _this.data = void 0;
  319. _this.options = void 0;
  320. _this.points = void 0;
  321. return _this;
  322. }
  323. StochasticIndicator.prototype.init = function () {
  324. SeriesRegistry.seriesTypes.sma.prototype.init.apply(this, arguments);
  325. // Set default color for lines:
  326. this.options = merge({
  327. smoothedLine: {
  328. styles: {
  329. lineColor: this.color
  330. }
  331. }
  332. }, this.options);
  333. };
  334. StochasticIndicator.prototype.getValues = function (series, params) {
  335. var periodK = params.periods[0],
  336. periodD = params.periods[1],
  337. xVal = series.xData,
  338. yVal = series.yData,
  339. yValLen = yVal ? yVal.length : 0,
  340. // 0- date, 1-%K, 2-%D
  341. SO = [],
  342. xData = [],
  343. yData = [],
  344. slicedY,
  345. close = 3,
  346. low = 2,
  347. high = 1,
  348. CL,
  349. HL,
  350. LL,
  351. K,
  352. D = null,
  353. points,
  354. extremes,
  355. i;
  356. // Stochastic requires close value
  357. if (yValLen < periodK ||
  358. !isArray(yVal[0]) ||
  359. yVal[0].length !== 4) {
  360. return;
  361. }
  362. // For a N-period, we start from N-1 point, to calculate Nth point
  363. // That is why we later need to comprehend slice() elements list
  364. // with (+1)
  365. for (i = periodK - 1; i < yValLen; i++) {
  366. slicedY = yVal.slice(i - periodK + 1, i + 1);
  367. // Calculate %K
  368. extremes = ReduceArrayMixin.getArrayExtremes(slicedY, low, high);
  369. LL = extremes[0]; // Lowest low in %K periods
  370. CL = yVal[i][close] - LL;
  371. HL = extremes[1] - LL;
  372. K = CL / HL * 100;
  373. xData.push(xVal[i]);
  374. yData.push([K, null]);
  375. // Calculate smoothed %D, which is SMA of %K
  376. if (i >= (periodK - 1) + (periodD - 1)) {
  377. points = SeriesRegistry.seriesTypes.sma.prototype.getValues.call(this, {
  378. xData: xData.slice(-periodD),
  379. yData: yData.slice(-periodD)
  380. }, {
  381. period: periodD
  382. });
  383. D = points.yData[0];
  384. }
  385. SO.push([xVal[i], K, D]);
  386. yData[yData.length - 1][1] = D;
  387. }
  388. return {
  389. values: SO,
  390. xData: xData,
  391. yData: yData
  392. };
  393. };
  394. /**
  395. * Stochastic oscillator. This series requires the `linkedTo` option to be
  396. * set and should be loaded after the `stock/indicators/indicators.js` file.
  397. *
  398. * @sample stock/indicators/stochastic
  399. * Stochastic oscillator
  400. *
  401. * @extends plotOptions.sma
  402. * @since 6.0.0
  403. * @product highstock
  404. * @excluding allAreas, colorAxis, joinBy, keys, navigatorOptions,
  405. * pointInterval, pointIntervalUnit, pointPlacement,
  406. * pointRange, pointStart, showInNavigator, stacking
  407. * @requires stock/indicators/indicators
  408. * @requires stock/indicators/stochastic
  409. * @optionparent plotOptions.stochastic
  410. */
  411. StochasticIndicator.defaultOptions = merge(SMAIndicator.defaultOptions, {
  412. /**
  413. * @excluding index, period
  414. */
  415. params: {
  416. // Index and period are unchangeable, do not inherit (#15362)
  417. index: void 0,
  418. period: void 0,
  419. /**
  420. * Periods for Stochastic oscillator: [%K, %D].
  421. *
  422. * @type {Array<number,number>}
  423. * @default [14, 3]
  424. */
  425. periods: [14, 3]
  426. },
  427. marker: {
  428. enabled: false
  429. },
  430. tooltip: {
  431. pointFormat: '<span style="color:{point.color}">\u25CF</span><b> {series.name}</b><br/>%K: {point.y}<br/>%D: {point.smoothed}<br/>'
  432. },
  433. /**
  434. * Smoothed line options.
  435. */
  436. smoothedLine: {
  437. /**
  438. * Styles for a smoothed line.
  439. */
  440. styles: {
  441. /**
  442. * Pixel width of the line.
  443. */
  444. lineWidth: 1,
  445. /**
  446. * Color of the line. If not set, it's inherited from
  447. * [plotOptions.stochastic.color
  448. * ](#plotOptions.stochastic.color).
  449. *
  450. * @type {Highcharts.ColorString}
  451. */
  452. lineColor: void 0
  453. }
  454. },
  455. dataGrouping: {
  456. approximation: 'averages'
  457. }
  458. });
  459. return StochasticIndicator;
  460. }(SMAIndicator));
  461. extend(StochasticIndicator.prototype, {
  462. nameComponents: ['periods'],
  463. nameBase: 'Stochastic',
  464. pointArrayMap: ['y', 'smoothed'],
  465. parallelArrays: ['x', 'y', 'smoothed'],
  466. pointValKey: 'y',
  467. linesApiNames: ['smoothedLine'],
  468. drawGraph: MultipleLinesMixin.drawGraph,
  469. getTranslatedLinesNames: MultipleLinesMixin.getTranslatedLinesNames,
  470. translate: MultipleLinesMixin.translate,
  471. toYData: MultipleLinesMixin.toYData
  472. });
  473. SeriesRegistry.registerSeriesType('stochastic', StochasticIndicator);
  474. /* *
  475. *
  476. * Default Export
  477. *
  478. * */
  479. /**
  480. * A Stochastic indicator. If the [type](#series.stochastic.type) option is not
  481. * specified, it is inherited from [chart.type](#chart.type).
  482. *
  483. * @extends series,plotOptions.stochastic
  484. * @since 6.0.0
  485. * @product highstock
  486. * @excluding allAreas, colorAxis, dataParser, dataURL, joinBy, keys,
  487. * navigatorOptions, pointInterval, pointIntervalUnit,
  488. * pointPlacement, pointRange, pointStart, showInNavigator, stacking
  489. * @requires stock/indicators/indicators
  490. * @requires stock/indicators/stochastic
  491. * @apioption series.stochastic
  492. */
  493. ''; // to include the above in the js output
  494. return StochasticIndicator;
  495. });
  496. _registerModule(_modules, 'masters/indicators/stochastic.src.js', [], function () {
  497. });
  498. }));