stochastic.src.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. /**
  2. * @license Highstock JS v8.1.2 (2020-06-16)
  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/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/stochastic.src.js', [_modules['parts/Globals.js'], _modules['parts/Utilities.js'], _modules['mixins/reduce-array.js'], _modules['mixins/multipe-lines.js']], function (H, 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 isArray = U.isArray, merge = U.merge, seriesType = U.seriesType;
  268. var SMA = H.seriesTypes.sma, getArrayExtremes = reduceArrayMixin.getArrayExtremes;
  269. /**
  270. * The Stochastic series type.
  271. *
  272. * @private
  273. * @class
  274. * @name Highcharts.seriesTypes.stochastic
  275. *
  276. * @augments Highcharts.Series
  277. */
  278. seriesType('stochastic', 'sma',
  279. /**
  280. * Stochastic oscillator. This series requires the `linkedTo` option to be
  281. * set and should be loaded after the `stock/indicators/indicators.js` file.
  282. *
  283. * @sample stock/indicators/stochastic
  284. * Stochastic oscillator
  285. *
  286. * @extends plotOptions.sma
  287. * @since 6.0.0
  288. * @product highstock
  289. * @excluding allAreas, colorAxis, joinBy, keys, navigatorOptions,
  290. * pointInterval, pointIntervalUnit, pointPlacement,
  291. * pointRange, pointStart, showInNavigator, stacking
  292. * @requires stock/indicators/indicators
  293. * @requires stock/indicators/stochastic
  294. * @optionparent plotOptions.stochastic
  295. */
  296. {
  297. /**
  298. * @excluding index, period
  299. */
  300. params: {
  301. /**
  302. * Periods for Stochastic oscillator: [%K, %D].
  303. *
  304. * @type {Array<number,number>}
  305. * @default [14, 3]
  306. */
  307. periods: [14, 3]
  308. },
  309. marker: {
  310. enabled: false
  311. },
  312. tooltip: {
  313. pointFormat: '<span style="color:{point.color}">\u25CF</span><b> {series.name}</b><br/>%K: {point.y}<br/>%D: {point.smoothed}<br/>'
  314. },
  315. /**
  316. * Smoothed line options.
  317. */
  318. smoothedLine: {
  319. /**
  320. * Styles for a smoothed line.
  321. */
  322. styles: {
  323. /**
  324. * Pixel width of the line.
  325. */
  326. lineWidth: 1,
  327. /**
  328. * Color of the line. If not set, it's inherited from
  329. * [plotOptions.stochastic.color
  330. * ](#plotOptions.stochastic.color).
  331. *
  332. * @type {Highcharts.ColorString}
  333. */
  334. lineColor: void 0
  335. }
  336. },
  337. dataGrouping: {
  338. approximation: 'averages'
  339. }
  340. },
  341. /**
  342. * @lends Highcharts.Series#
  343. */
  344. merge(multipleLinesMixin, {
  345. nameComponents: ['periods'],
  346. nameBase: 'Stochastic',
  347. pointArrayMap: ['y', 'smoothed'],
  348. parallelArrays: ['x', 'y', 'smoothed'],
  349. pointValKey: 'y',
  350. linesApiNames: ['smoothedLine'],
  351. init: function () {
  352. SMA.prototype.init.apply(this, arguments);
  353. // Set default color for lines:
  354. this.options = merge({
  355. smoothedLine: {
  356. styles: {
  357. lineColor: this.color
  358. }
  359. }
  360. }, this.options);
  361. },
  362. getValues: function (series, params) {
  363. var periodK = params.periods[0], periodD = params.periods[1], xVal = series.xData, yVal = series.yData, yValLen = yVal ? yVal.length : 0,
  364. // 0- date, 1-%K, 2-%D
  365. SO = [], xData = [], yData = [], slicedY, close = 3, low = 2, high = 1, CL, HL, LL, K, D = null, points, extremes, i;
  366. // Stochastic requires close value
  367. if (yValLen < periodK ||
  368. !isArray(yVal[0]) ||
  369. yVal[0].length !== 4) {
  370. return;
  371. }
  372. // For a N-period, we start from N-1 point, to calculate Nth point
  373. // That is why we later need to comprehend slice() elements list
  374. // with (+1)
  375. for (i = periodK - 1; i < yValLen; i++) {
  376. slicedY = yVal.slice(i - periodK + 1, i + 1);
  377. // Calculate %K
  378. extremes = getArrayExtremes(slicedY, low, high);
  379. LL = extremes[0]; // Lowest low in %K periods
  380. CL = yVal[i][close] - LL;
  381. HL = extremes[1] - LL;
  382. K = CL / HL * 100;
  383. xData.push(xVal[i]);
  384. yData.push([K, null]);
  385. // Calculate smoothed %D, which is SMA of %K
  386. if (i >= (periodK - 1) + (periodD - 1)) {
  387. points = SMA.prototype.getValues.call(this, {
  388. xData: xData.slice(-periodD),
  389. yData: yData.slice(-periodD)
  390. }, {
  391. period: periodD
  392. });
  393. D = points.yData[0];
  394. }
  395. SO.push([xVal[i], K, D]);
  396. yData[yData.length - 1][1] = D;
  397. }
  398. return {
  399. values: SO,
  400. xData: xData,
  401. yData: yData
  402. };
  403. }
  404. }));
  405. /**
  406. * A Stochastic indicator. If the [type](#series.stochastic.type) option is not
  407. * specified, it is inherited from [chart.type](#chart.type).
  408. *
  409. * @extends series,plotOptions.stochastic
  410. * @since 6.0.0
  411. * @product highstock
  412. * @excluding allAreas, colorAxis, dataParser, dataURL, joinBy, keys,
  413. * navigatorOptions, pointInterval, pointIntervalUnit,
  414. * pointPlacement, pointRange, pointStart, showInNavigator, stacking
  415. * @requires stock/indicators/indicators
  416. * @requires stock/indicators/stochastic
  417. * @apioption series.stochastic
  418. */
  419. ''; // to include the above in the js output
  420. });
  421. _registerModule(_modules, 'masters/indicators/stochastic.src.js', [], function () {
  422. });
  423. }));