RegressionIndicators.js 13 KB

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