regressions.src.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. /**
  2. * @license Highstock JS v8.2.0 (2020-08-20)
  3. *
  4. * Indicator series type for Highstock
  5. *
  6. * (c) 2010-2019 Kamil Kulig
  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/regressions', ['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, 'Stock/Indicators/RegressionIndicators.js', [_modules['Core/Utilities.js']], function (U) {
  32. /**
  33. *
  34. * (c) 2010-2020 Kamil Kulig
  35. *
  36. * License: www.highcharts.com/license
  37. *
  38. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  39. *
  40. * */
  41. var isArray = U.isArray,
  42. seriesType = U.seriesType;
  43. /**
  44. * Linear regression series type.
  45. *
  46. * @private
  47. * @class
  48. * @name Highcharts.seriesTypes.linearregression
  49. *
  50. * @augments Highcharts.Series
  51. */
  52. seriesType('linearRegression', 'sma',
  53. /**
  54. * Linear regression indicator. This series requires `linkedTo` option to be
  55. * set.
  56. *
  57. * @sample {highstock} stock/indicators/linear-regression
  58. * Linear regression indicator
  59. *
  60. * @extends plotOptions.sma
  61. * @since 7.0.0
  62. * @product highstock
  63. * @requires stock/indicators/indicators
  64. * @requires stock/indicators/regressions
  65. * @optionparent plotOptions.linearregression
  66. */
  67. {
  68. params: {
  69. /**
  70. * Unit (in milliseconds) for the x axis distances used to compute
  71. * the regression line paramters (slope & intercept) for every
  72. * range. In Highstock the x axis values are always represented in
  73. * milliseconds which may cause that distances between points are
  74. * "big" integer numbers.
  75. *
  76. * Highstock's linear regression algorithm (least squares method)
  77. * will utilize these "big" integers for finding the slope and the
  78. * intercept of the regression line for each period. In consequence,
  79. * this value may be a very "small" decimal number that's hard to
  80. * interpret by a human.
  81. *
  82. * For instance: `xAxisUnit` equealed to `86400000` ms (1 day)
  83. * forces the algorithm to treat `86400000` as `1` while computing
  84. * the slope and the intercept. This may enchance the legiblitity of
  85. * the indicator's values.
  86. *
  87. * Default value is the closest distance between two data points.
  88. *
  89. * @sample {highstock} stock/plotoptions/linear-regression-xaxisunit
  90. * xAxisUnit set to 1 minute
  91. *
  92. * @example
  93. * // In Liniear Regression Slope Indicator series `xAxisUnit` is
  94. * // `86400000` (1 day) and period is `3`. There're 3 points in the
  95. * // base series:
  96. *
  97. * data: [
  98. * [Date.UTC(2020, 0, 1), 1],
  99. * [Date.UTC(2020, 0, 2), 3],
  100. * [Date.UTC(2020, 0, 3), 5]
  101. * ]
  102. *
  103. * // This will produce one point in the indicator series that has a
  104. * // `y` value of `2` (slope of the regression line). If we change
  105. * // the `xAxisUnit` to `1` (ms) the value of the indicator's point
  106. * // will be `2.3148148148148148e-8` which is harder to interpert
  107. * // for a human.
  108. *
  109. * @type {number}
  110. * @product highstock
  111. */
  112. xAxisUnit: void 0
  113. },
  114. tooltip: {
  115. valueDecimals: 4
  116. }
  117. },
  118. /**
  119. * @lends Highcharts.Series#
  120. */
  121. {
  122. nameBase: 'Linear Regression Indicator',
  123. /**
  124. * Return the slope and intercept of a straight line function.
  125. * @private
  126. * @param {Highcharts.LinearRegressionIndicator} this indicator to use
  127. * @param {Array<number>} xData - list of all x coordinates in a period
  128. * @param {Array<number>} yData - list of all y coordinates in a period
  129. * @return {Highcharts.RegressionLineParametersObject}
  130. * object that contains the slope and the intercept
  131. * of a straight line function
  132. */
  133. getRegressionLineParameters: function (xData, yData) {
  134. // least squares method
  135. var yIndex = this.options.params.index,
  136. getSingleYValue = function (yValue,
  137. yIndex) {
  138. return isArray(yValue) ? yValue[yIndex] : yValue;
  139. }, xSum = xData.reduce(function (accX, val) {
  140. return val + accX;
  141. }, 0), ySum = yData.reduce(function (accY, val) {
  142. return getSingleYValue(val, yIndex) + accY;
  143. }, 0), xMean = xSum / xData.length, yMean = ySum / yData.length, xError, yError, formulaNumerator = 0, formulaDenominator = 0, i, slope;
  144. for (i = 0; i < xData.length; i++) {
  145. xError = xData[i] - xMean;
  146. yError = getSingleYValue(yData[i], yIndex) - yMean;
  147. formulaNumerator += xError * yError;
  148. formulaDenominator += Math.pow(xError, 2);
  149. }
  150. slope = formulaDenominator ?
  151. formulaNumerator / formulaDenominator : 0; // don't divide by 0
  152. return {
  153. slope: slope,
  154. intercept: yMean - slope * xMean
  155. };
  156. },
  157. /**
  158. * Return the y value on a straight line.
  159. * @private
  160. * @param {Highcharts.RegressionLineParametersObject} lineParameters
  161. * object that contains the slope and the intercept
  162. * of a straight line function
  163. * @param {number} endPointX - x coordinate of the point
  164. * @return {number} - y value of the point that lies on the line
  165. */
  166. getEndPointY: function (lineParameters, endPointX) {
  167. return lineParameters.slope * endPointX + lineParameters.intercept;
  168. },
  169. /**
  170. * Transform the coordinate system so that x values start at 0 and
  171. * apply xAxisUnit.
  172. * @private
  173. * @param {Array<number>} xData - list of all x coordinates in a period
  174. * @param {number} xAxisUnit - option (see the API)
  175. * @return {Array<number>} - array of transformed x data
  176. */
  177. transformXData: function (xData, xAxisUnit) {
  178. var xOffset = xData[0];
  179. return xData.map(function (xValue) {
  180. return (xValue - xOffset) / xAxisUnit;
  181. });
  182. },
  183. /**
  184. * Find the closest distance between points in the base series.
  185. * @private
  186. * @param {Array<number>} xData
  187. list of all x coordinates in the base series
  188. * @return {number} - closest distance between points in the base series
  189. */
  190. findClosestDistance: function (xData) {
  191. var distance,
  192. closestDistance,
  193. i;
  194. for (i = 1; i < xData.length - 1; i++) {
  195. distance = xData[i] - xData[i - 1];
  196. if (distance > 0 &&
  197. (typeof closestDistance === 'undefined' ||
  198. distance < closestDistance)) {
  199. closestDistance = distance;
  200. }
  201. }
  202. return closestDistance;
  203. },
  204. // Required to be implemented - starting point for indicator's logic
  205. getValues: function (baseSeries, regressionSeriesParams) {
  206. var xData = baseSeries.xData,
  207. yData = baseSeries.yData,
  208. period = regressionSeriesParams.period,
  209. lineParameters,
  210. i,
  211. periodStart,
  212. periodEnd,
  213. // format required to be returned
  214. indicatorData = {
  215. xData: [],
  216. yData: [],
  217. values: []
  218. },
  219. endPointX,
  220. endPointY,
  221. periodXData,
  222. periodYData,
  223. periodTransformedXData,
  224. xAxisUnit = this.options.params.xAxisUnit ||
  225. this.findClosestDistance(xData);
  226. // Iteration logic: x value of the last point within the period
  227. // (end point) is used to represent the y value (regression)
  228. // of the entire period.
  229. for (i = period - 1; i <= xData.length - 1; i++) {
  230. periodStart = i - period + 1; // adjusted for slice() function
  231. periodEnd = i + 1; // (as above)
  232. endPointX = xData[i];
  233. periodXData = xData.slice(periodStart, periodEnd);
  234. periodYData = yData.slice(periodStart, periodEnd);
  235. periodTransformedXData = this.transformXData(periodXData, xAxisUnit);
  236. lineParameters = this.getRegressionLineParameters(periodTransformedXData, periodYData);
  237. endPointY = this.getEndPointY(lineParameters, periodTransformedXData[periodTransformedXData.length - 1]);
  238. // @todo this is probably not used anywhere
  239. indicatorData.values.push({
  240. regressionLineParameters: lineParameters,
  241. x: endPointX,
  242. y: endPointY
  243. });
  244. indicatorData.xData.push(endPointX);
  245. indicatorData.yData.push(endPointY);
  246. }
  247. return indicatorData;
  248. }
  249. });
  250. /**
  251. * A linear regression series. If the [type](#series.linearregression.type)
  252. * option is not specified, it is inherited from [chart.type](#chart.type).
  253. *
  254. * @extends series,plotOptions.linearregression
  255. * @since 7.0.0
  256. * @product highstock
  257. * @excluding dataParser,dataURL
  258. * @requires stock/indicators/indicators
  259. * @requires stock/indicators/regressions
  260. * @apioption series.linearregression
  261. */
  262. /* ************************************************************************** */
  263. /**
  264. * The Linear Regression Slope series type.
  265. *
  266. * @private
  267. * @class
  268. * @name Highcharts.seriesTypes.linearRegressionSlope
  269. *
  270. * @augments Highcharts.Series
  271. */
  272. seriesType('linearRegressionSlope', 'linearRegression',
  273. /**
  274. * Linear regression slope indicator. This series requires `linkedTo`
  275. * option to be set.
  276. *
  277. * @sample {highstock} stock/indicators/linear-regression-slope
  278. * Linear regression slope indicator
  279. *
  280. * @extends plotOptions.linearregression
  281. * @since 7.0.0
  282. * @product highstock
  283. * @requires stock/indicators/indicators
  284. * @requires stock/indicators/regressions
  285. * @optionparent plotOptions.linearregressionslope
  286. */
  287. {},
  288. /**
  289. * @lends Highcharts.Series#
  290. */
  291. {
  292. nameBase: 'Linear Regression Slope Indicator',
  293. getEndPointY: function (lineParameters) {
  294. return lineParameters.slope;
  295. }
  296. });
  297. /**
  298. * A linear regression slope series. If the
  299. * [type](#series.linearregressionslope.type) option is not specified, it is
  300. * inherited from [chart.type](#chart.type).
  301. *
  302. * @extends series,plotOptions.linearregressionslope
  303. * @since 7.0.0
  304. * @product highstock
  305. * @excluding dataParser,dataURL
  306. * @requires stock/indicators/indicators
  307. * @requires stock/indicators/regressions
  308. * @apioption series.linearregressionslope
  309. */
  310. /* ************************************************************************** */
  311. /**
  312. * The Linear Regression Intercept series type.
  313. *
  314. * @private
  315. * @class
  316. * @name Highcharts.seriesTypes.linearRegressionIntercept
  317. *
  318. * @augments Highcharts.Series
  319. */
  320. seriesType('linearRegressionIntercept', 'linearRegression',
  321. /**
  322. * Linear regression intercept indicator. This series requires `linkedTo`
  323. * option to be set.
  324. *
  325. * @sample {highstock} stock/indicators/linear-regression-intercept
  326. * Linear intercept slope indicator
  327. *
  328. * @extends plotOptions.linearregression
  329. * @since 7.0.0
  330. * @product highstock
  331. * @requires stock/indicators/indicators
  332. * @requires stock/indicators/regressions
  333. * @optionparent plotOptions.linearregressionintercept
  334. */
  335. {},
  336. /**
  337. * @lends Highcharts.Series#
  338. */
  339. {
  340. nameBase: 'Linear Regression Intercept Indicator',
  341. getEndPointY: function (lineParameters) {
  342. return lineParameters.intercept;
  343. }
  344. });
  345. /**
  346. * A linear regression intercept series. If the
  347. * [type](#series.linearregressionintercept.type) option is not specified, it is
  348. * inherited from [chart.type](#chart.type).
  349. *
  350. * @extends series,plotOptions.linearregressionintercept
  351. * @since 7.0.0
  352. * @product highstock
  353. * @excluding dataParser,dataURL
  354. * @requires stock/indicators/indicators
  355. * @requires stock/indicators/regressions
  356. * @apioption series.linearregressionintercept
  357. */
  358. /* ************************************************************************** */
  359. /**
  360. * The Linear Regression Angle series type.
  361. *
  362. * @private
  363. * @class
  364. * @name Highcharts.seriesTypes.linearRegressionAngle
  365. *
  366. * @augments Highcharts.Series
  367. */
  368. seriesType('linearRegressionAngle', 'linearRegression',
  369. /**
  370. * Linear regression angle indicator. This series requires `linkedTo`
  371. * option to be set.
  372. *
  373. * @sample {highstock} stock/indicators/linear-regression-angle
  374. * Linear intercept angle indicator
  375. *
  376. * @extends plotOptions.linearregression
  377. * @since 7.0.0
  378. * @product highstock
  379. * @requires stock/indicators/indicators
  380. * @requires stock/indicators/regressions
  381. * @optionparent plotOptions.linearregressionangle
  382. */
  383. {
  384. tooltip: {
  385. pointFormat: '<span style="color:{point.color}">\u25CF</span>' +
  386. '{series.name}: <b>{point.y}°</b><br/>'
  387. }
  388. },
  389. /**
  390. * @lends Highcharts.Series#
  391. */
  392. {
  393. nameBase: 'Linear Regression Angle Indicator',
  394. /**
  395. * Convert a slope of a line to angle (in degrees) between
  396. * the line and x axis
  397. * @private
  398. * @param {number} slope of the straight line function
  399. * @return {number} angle in degrees
  400. */
  401. slopeToAngle: function (slope) {
  402. return Math.atan(slope) * (180 / Math.PI); // rad to deg
  403. },
  404. getEndPointY: function (lineParameters) {
  405. return this.slopeToAngle(lineParameters.slope);
  406. }
  407. });
  408. /**
  409. * A linear regression intercept series. If the
  410. * [type](#series.linearregressionangle.type) option is not specified, it is
  411. * inherited from [chart.type](#chart.type).
  412. *
  413. * @extends series,plotOptions.linearregressionangle
  414. * @since 7.0.0
  415. * @product highstock
  416. * @excluding dataParser,dataURL
  417. * @requires stock/indicators/indicators
  418. * @requires stock/indicators/regressions
  419. * @apioption series.linearregressionangle
  420. */
  421. ''; // to include the above in the js output
  422. });
  423. _registerModule(_modules, 'masters/indicators/regressions.src.js', [], function () {
  424. });
  425. }));