stochastic.src.js 18 KB

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