PivotPointsIndicator.js 10.0 KB

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