pivot-points.src.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. /**
  2. * @license Highstock JS v8.2.0 (2020-08-20)
  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/pivot-points', ['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/PivotPointsIndicator.js', [_modules['Core/Globals.js'], _modules['Core/Utilities.js']], function (H, U) {
  32. /* *
  33. *
  34. * License: www.highcharts.com/license
  35. *
  36. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  37. *
  38. * */
  39. var defined = U.defined,
  40. isArray = U.isArray,
  41. seriesType = U.seriesType;
  42. var SMA = H.seriesTypes.sma;
  43. /* eslint-disable valid-jsdoc */
  44. /**
  45. * @private
  46. */
  47. function destroyExtraLabels(point, functionName) {
  48. var props = point.series.pointArrayMap,
  49. prop,
  50. i = props.length;
  51. SMA.prototype.pointClass.prototype[functionName].call(point);
  52. while (i--) {
  53. prop = 'dataLabel' + props[i];
  54. // S4 dataLabel could be removed by parent method:
  55. if (point[prop] && point[prop].element) {
  56. point[prop].destroy();
  57. }
  58. point[prop] = null;
  59. }
  60. }
  61. /* eslint-enable valid-jsdoc */
  62. /**
  63. * The Pivot Points series type.
  64. *
  65. * @private
  66. * @class
  67. * @name Highcharts.seriesTypes.pivotpoints
  68. *
  69. * @augments Highcharts.Series
  70. */
  71. seriesType('pivotpoints', 'sma',
  72. /**
  73. * Pivot points indicator. This series requires the `linkedTo` option to be
  74. * set and should be loaded after `stock/indicators/indicators.js` file.
  75. *
  76. * @sample stock/indicators/pivot-points
  77. * Pivot points
  78. *
  79. * @extends plotOptions.sma
  80. * @since 6.0.0
  81. * @product highstock
  82. * @requires stock/indicators/indicators
  83. * @requires stock/indicators/pivotpoints
  84. * @optionparent plotOptions.pivotpoints
  85. */
  86. {
  87. /**
  88. * @excluding index
  89. */
  90. params: {
  91. period: 28,
  92. /**
  93. * Algorithm used to calculate ressistance and support lines based
  94. * on pivot points. Implemented algorithms: `'standard'`,
  95. * `'fibonacci'` and `'camarilla'`
  96. */
  97. algorithm: 'standard'
  98. },
  99. marker: {
  100. enabled: false
  101. },
  102. enableMouseTracking: false,
  103. dataLabels: {
  104. enabled: true,
  105. format: '{point.pivotLine}'
  106. },
  107. dataGrouping: {
  108. approximation: 'averages'
  109. }
  110. },
  111. /**
  112. * @lends Highcharts.Series#
  113. */
  114. {
  115. nameBase: 'Pivot Points',
  116. pointArrayMap: ['R4', 'R3', 'R2', 'R1', 'P', 'S1', 'S2', 'S3', 'S4'],
  117. pointValKey: 'P',
  118. toYData: function (point) {
  119. return [point.P]; // The rest should not affect extremes
  120. },
  121. translate: function () {
  122. var indicator = this;
  123. SMA.prototype.translate.apply(indicator);
  124. indicator.points.forEach(function (point) {
  125. indicator.pointArrayMap.forEach(function (value) {
  126. if (defined(point[value])) {
  127. point['plot' + value] = (indicator.yAxis.toPixels(point[value], true));
  128. }
  129. });
  130. });
  131. // Pivot points are rendered as horizontal lines
  132. // And last point start not from the next one (as it's the last one)
  133. // But from the approximated last position in a given range
  134. indicator.plotEndPoint = indicator.xAxis.toPixels(indicator.endPoint, true);
  135. },
  136. getGraphPath: function (points) {
  137. var indicator = this,
  138. pointsLength = points.length,
  139. allPivotPoints = ([[],
  140. [],
  141. [],
  142. [],
  143. [],
  144. [],
  145. [],
  146. [],
  147. []]),
  148. path = [],
  149. endPoint = indicator.plotEndPoint,
  150. pointArrayMapLength = indicator.pointArrayMap.length,
  151. position,
  152. point,
  153. i;
  154. while (pointsLength--) {
  155. point = points[pointsLength];
  156. for (i = 0; i < pointArrayMapLength; i++) {
  157. position = indicator.pointArrayMap[i];
  158. if (defined(point[position])) {
  159. allPivotPoints[i].push({
  160. // Start left:
  161. plotX: point.plotX,
  162. plotY: point['plot' + position],
  163. isNull: false
  164. }, {
  165. // Go to right:
  166. plotX: endPoint,
  167. plotY: point['plot' + position],
  168. isNull: false
  169. }, {
  170. // And add null points in path to generate breaks:
  171. plotX: endPoint,
  172. plotY: null,
  173. isNull: true
  174. });
  175. }
  176. }
  177. endPoint = point.plotX;
  178. }
  179. allPivotPoints.forEach(function (pivotPoints) {
  180. path = path.concat(SMA.prototype.getGraphPath.call(indicator, pivotPoints));
  181. });
  182. return path;
  183. },
  184. // TODO: Rewrite this logic to use multiple datalabels
  185. drawDataLabels: function () {
  186. var indicator = this,
  187. pointMapping = indicator.pointArrayMap,
  188. currentLabel,
  189. pointsLength,
  190. point,
  191. i;
  192. if (indicator.options.dataLabels.enabled) {
  193. pointsLength = indicator.points.length;
  194. // For every Ressitance/Support group we need to render labels.
  195. // Add one more item, which will just store dataLabels from
  196. // previous iteration
  197. pointMapping.concat([false]).forEach(function (position, k) {
  198. i = pointsLength;
  199. while (i--) {
  200. point = indicator.points[i];
  201. if (!position) {
  202. // Store S4 dataLabel too:
  203. point['dataLabel' + pointMapping[k - 1]] =
  204. point.dataLabel;
  205. }
  206. else {
  207. point.y = point[position];
  208. point.pivotLine = position;
  209. point.plotY = point['plot' + position];
  210. currentLabel = point['dataLabel' + position];
  211. // Store previous label
  212. if (k) {
  213. point['dataLabel' + pointMapping[k - 1]] = point.dataLabel;
  214. }
  215. if (!point.dataLabels) {
  216. point.dataLabels = [];
  217. }
  218. point.dataLabels[0] = point.dataLabel =
  219. currentLabel =
  220. currentLabel && currentLabel.element ?
  221. currentLabel :
  222. null;
  223. }
  224. }
  225. SMA.prototype.drawDataLabels.apply(indicator, arguments);
  226. });
  227. }
  228. },
  229. getValues: function (series, params) {
  230. var period = params.period,
  231. xVal = series.xData,
  232. yVal = series.yData,
  233. yValLen = yVal ? yVal.length : 0,
  234. placement = this[params.algorithm + 'Placement'],
  235. // 0- from, 1- to, 2- R1, 3- R2, 4- pivot, 5- S1 etc.
  236. PP = [],
  237. endTimestamp,
  238. xData = [],
  239. yData = [],
  240. slicedXLen,
  241. slicedX,
  242. slicedY,
  243. lastPP,
  244. pivot,
  245. avg,
  246. i;
  247. // Pivot Points requires high, low and close values
  248. if (xVal.length < period ||
  249. !isArray(yVal[0]) ||
  250. yVal[0].length !== 4) {
  251. return;
  252. }
  253. for (i = period + 1; i <= yValLen + period; i += period) {
  254. slicedX = xVal.slice(i - period - 1, i);
  255. slicedY = yVal.slice(i - period - 1, i);
  256. slicedXLen = slicedX.length;
  257. endTimestamp = slicedX[slicedXLen - 1];
  258. pivot = this.getPivotAndHLC(slicedY);
  259. avg = placement(pivot);
  260. lastPP = PP.push([endTimestamp]
  261. .concat(avg));
  262. xData.push(endTimestamp);
  263. yData.push(PP[lastPP - 1].slice(1));
  264. }
  265. // We don't know exact position in ordinal axis
  266. // So we use simple logic:
  267. // Get first point in last range, calculate visible average range
  268. // and multiply by period
  269. this.endPoint = slicedX[0] + ((endTimestamp - slicedX[0]) /
  270. slicedXLen) * period;
  271. return {
  272. values: PP,
  273. xData: xData,
  274. yData: yData
  275. };
  276. },
  277. getPivotAndHLC: function (values) {
  278. var high = -Infinity,
  279. low = Infinity,
  280. close = values[values.length - 1][3],
  281. pivot;
  282. values.forEach(function (p) {
  283. high = Math.max(high, p[1]);
  284. low = Math.min(low, p[2]);
  285. });
  286. pivot = (high + low + close) / 3;
  287. return [pivot, high, low, close];
  288. },
  289. standardPlacement: function (values) {
  290. var diff = values[1] - values[2],
  291. avg = [
  292. null,
  293. null,
  294. values[0] + diff,
  295. values[0] * 2 - values[2],
  296. values[0],
  297. values[0] * 2 - values[1],
  298. values[0] - diff,
  299. null,
  300. null
  301. ];
  302. return avg;
  303. },
  304. camarillaPlacement: function (values) {
  305. var diff = values[1] - values[2],
  306. avg = [
  307. values[3] + diff * 1.5,
  308. values[3] + diff * 1.25,
  309. values[3] + diff * 1.1666,
  310. values[3] + diff * 1.0833,
  311. values[0],
  312. values[3] - diff * 1.0833,
  313. values[3] - diff * 1.1666,
  314. values[3] - diff * 1.25,
  315. values[3] - diff * 1.5
  316. ];
  317. return avg;
  318. },
  319. fibonacciPlacement: function (values) {
  320. var diff = values[1] - values[2],
  321. avg = [
  322. null,
  323. values[0] + diff,
  324. values[0] + diff * 0.618,
  325. values[0] + diff * 0.382,
  326. values[0],
  327. values[0] - diff * 0.382,
  328. values[0] - diff * 0.618,
  329. values[0] - diff,
  330. null
  331. ];
  332. return avg;
  333. }
  334. },
  335. /**
  336. * @lends Highcharts.Point#
  337. */
  338. {
  339. // Destroy labels:
  340. // This method is called when cropping data:
  341. destroyElements: function () {
  342. destroyExtraLabels(this, 'destroyElements');
  343. },
  344. // This method is called when removing points, e.g. series.update()
  345. destroy: function () {
  346. destroyExtraLabels(this, 'destroyElements');
  347. }
  348. });
  349. /**
  350. * A pivot points indicator. If the [type](#series.pivotpoints.type) option is
  351. * not specified, it is inherited from [chart.type](#chart.type).
  352. *
  353. * @extends series,plotOptions.pivotpoints
  354. * @since 6.0.0
  355. * @product highstock
  356. * @excluding dataParser, dataURL
  357. * @requires stock/indicators/indicators
  358. * @requires stock/indicators/pivotpoints
  359. * @apioption series.pivotpoints
  360. */
  361. ''; // to include the above in the js output'
  362. });
  363. _registerModule(_modules, 'masters/indicators/pivot-points.src.js', [], function () {
  364. });
  365. }));