stochastic.src.js 18 KB

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