regressions.src.js 17 KB

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