supertrend.src.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. /**
  2. * @license Highstock JS v9.1.1 (2021-06-04)
  3. *
  4. * Indicator series type for Highcharts Stock
  5. *
  6. * (c) 2010-2021 Wojciech Chmiel
  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/supertrend', ['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/Supertrend/SupertrendIndicator.js', [_modules['Core/Color/Palette.js'], _modules['Core/Series/SeriesRegistry.js'], _modules['Core/Utilities.js']], function (palette, SeriesRegistry, U) {
  32. /* *
  33. *
  34. * License: www.highcharts.com/license
  35. *
  36. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  37. *
  38. * */
  39. var __extends = (this && this.__extends) || (function () {
  40. var extendStatics = function (d,
  41. b) {
  42. extendStatics = Object.setPrototypeOf ||
  43. ({ __proto__: [] } instanceof Array && function (d,
  44. b) { d.__proto__ = b; }) ||
  45. function (d,
  46. b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  47. return extendStatics(d, b);
  48. };
  49. return function (d, b) {
  50. extendStatics(d, b);
  51. function __() { this.constructor = d; }
  52. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  53. };
  54. })();
  55. var _a = SeriesRegistry.seriesTypes,
  56. ATRIndicator = _a.atr,
  57. SMAIndicator = _a.sma;
  58. var correctFloat = U.correctFloat,
  59. isArray = U.isArray,
  60. extend = U.extend,
  61. merge = U.merge,
  62. objectEach = U.objectEach;
  63. /* eslint-disable require-jsdoc */
  64. // Utils:
  65. function createPointObj(mainSeries, index, close) {
  66. return {
  67. index: index,
  68. close: mainSeries.yData[index][close],
  69. x: mainSeries.xData[index]
  70. };
  71. }
  72. /* eslint-enable require-jsdoc */
  73. /* *
  74. *
  75. * Class
  76. *
  77. * */
  78. /**
  79. * The Supertrend series type.
  80. *
  81. * @private
  82. * @class
  83. * @name Highcharts.seriesTypes.supertrend
  84. *
  85. * @augments Highcharts.Series
  86. */
  87. var SupertrendIndicator = /** @class */ (function (_super) {
  88. __extends(SupertrendIndicator, _super);
  89. function SupertrendIndicator() {
  90. var _this = _super !== null && _super.apply(this,
  91. arguments) || this;
  92. /* *
  93. *
  94. * Properties
  95. *
  96. * */
  97. _this.data = void 0;
  98. _this.linkedParent = void 0;
  99. _this.options = void 0;
  100. _this.points = void 0;
  101. return _this;
  102. }
  103. /* *
  104. *
  105. * Functions
  106. *
  107. * */
  108. SupertrendIndicator.prototype.init = function () {
  109. var options,
  110. parentOptions;
  111. SMAIndicator.prototype.init.apply(this, arguments);
  112. options = this.options;
  113. parentOptions = this.linkedParent.options;
  114. // Indicator cropThreshold has to be equal linked series one
  115. // reduced by period due to points comparison in drawGraph method
  116. // (#9787)
  117. options.cropThreshold = (parentOptions.cropThreshold -
  118. (options.params.period - 1));
  119. };
  120. SupertrendIndicator.prototype.drawGraph = function () {
  121. var indicator = this,
  122. indicOptions = indicator.options,
  123. // Series that indicator is linked to
  124. mainSeries = indicator.linkedParent,
  125. mainLinePoints = (mainSeries ? mainSeries.points : []),
  126. indicPoints = indicator.points,
  127. indicPath = indicator.graph,
  128. indicPointsLen = indicPoints.length,
  129. // Points offset between lines
  130. tempOffset = mainLinePoints.length - indicPointsLen,
  131. offset = tempOffset > 0 ? tempOffset : 0,
  132. // @todo: fix when ichi-moku indicator is merged to master.
  133. gappedExtend = {
  134. options: {
  135. gapSize: indicOptions.gapSize
  136. }
  137. },
  138. // Sorted supertrend points array
  139. groupedPoitns = {
  140. top: [],
  141. bottom: [],
  142. intersect: [] // Change trend line points
  143. },
  144. // Options for trend lines
  145. supertrendLineOptions = {
  146. top: {
  147. styles: {
  148. lineWidth: indicOptions.lineWidth,
  149. lineColor: (indicOptions.fallingTrendColor ||
  150. indicOptions.color),
  151. dashStyle: indicOptions.dashStyle
  152. }
  153. },
  154. bottom: {
  155. styles: {
  156. lineWidth: indicOptions.lineWidth,
  157. lineColor: (indicOptions.risingTrendColor ||
  158. indicOptions.color),
  159. dashStyle: indicOptions.dashStyle
  160. }
  161. },
  162. intersect: indicOptions.changeTrendLine
  163. },
  164. close = 3,
  165. // Supertrend line point
  166. point,
  167. // Supertrend line next point (has smaller x pos than point)
  168. nextPoint,
  169. // Main series points
  170. mainPoint,
  171. nextMainPoint,
  172. // Used when supertrend and main points are shifted
  173. // relative to each other
  174. prevMainPoint,
  175. prevPrevMainPoint,
  176. // Used when particular point color is set
  177. pointColor,
  178. // Temporary points that fill groupedPoitns array
  179. newPoint,
  180. newNextPoint;
  181. // Loop which sort supertrend points
  182. while (indicPointsLen--) {
  183. point = indicPoints[indicPointsLen];
  184. nextPoint = indicPoints[indicPointsLen - 1];
  185. mainPoint = mainLinePoints[indicPointsLen - 1 + offset];
  186. nextMainPoint = mainLinePoints[indicPointsLen - 2 + offset];
  187. prevMainPoint = mainLinePoints[indicPointsLen + offset];
  188. prevPrevMainPoint = mainLinePoints[indicPointsLen + offset + 1];
  189. pointColor = point.options.color;
  190. newPoint = {
  191. x: point.x,
  192. plotX: point.plotX,
  193. plotY: point.plotY,
  194. isNull: false
  195. };
  196. // When mainPoint is the last one (left plot area edge)
  197. // but supertrend has additional one
  198. if (!nextMainPoint &&
  199. mainPoint && mainSeries.yData[mainPoint.index - 1]) {
  200. nextMainPoint = createPointObj(mainSeries, mainPoint.index - 1, close);
  201. }
  202. // When prevMainPoint is the last one (right plot area edge)
  203. // but supertrend has additional one (and points are shifted)
  204. if (!prevPrevMainPoint &&
  205. prevMainPoint && mainSeries.yData[prevMainPoint.index + 1]) {
  206. prevPrevMainPoint = createPointObj(mainSeries, prevMainPoint.index + 1, close);
  207. }
  208. // When points are shifted (right or left plot area edge)
  209. if (!mainPoint &&
  210. nextMainPoint && mainSeries.yData[nextMainPoint.index + 1]) {
  211. mainPoint = createPointObj(mainSeries, nextMainPoint.index + 1, close);
  212. }
  213. else if (!mainPoint &&
  214. prevMainPoint && mainSeries.yData[prevMainPoint.index - 1]) {
  215. mainPoint = createPointObj(mainSeries, prevMainPoint.index - 1, close);
  216. }
  217. // Check if points are shifted relative to each other
  218. if (point &&
  219. mainPoint &&
  220. prevMainPoint &&
  221. nextMainPoint &&
  222. point.x !== mainPoint.x) {
  223. if (point.x === prevMainPoint.x) {
  224. nextMainPoint = mainPoint;
  225. mainPoint = prevMainPoint;
  226. }
  227. else if (point.x === nextMainPoint.x) {
  228. mainPoint = nextMainPoint;
  229. nextMainPoint = {
  230. close: mainSeries.yData[mainPoint.index - 1][close],
  231. x: mainSeries.xData[mainPoint.index - 1]
  232. };
  233. }
  234. else if (prevPrevMainPoint && point.x === prevPrevMainPoint.x) {
  235. mainPoint = prevPrevMainPoint;
  236. nextMainPoint = prevMainPoint;
  237. }
  238. }
  239. if (nextPoint && nextMainPoint && mainPoint) {
  240. newNextPoint = {
  241. x: nextPoint.x,
  242. plotX: nextPoint.plotX,
  243. plotY: nextPoint.plotY,
  244. isNull: false
  245. };
  246. if (point.y >= mainPoint.close &&
  247. nextPoint.y >= nextMainPoint.close) {
  248. point.color = (pointColor || indicOptions.fallingTrendColor ||
  249. indicOptions.color);
  250. groupedPoitns.top.push(newPoint);
  251. }
  252. else if (point.y < mainPoint.close &&
  253. nextPoint.y < nextMainPoint.close) {
  254. point.color = (pointColor || indicOptions.risingTrendColor ||
  255. indicOptions.color);
  256. groupedPoitns.bottom.push(newPoint);
  257. }
  258. else {
  259. groupedPoitns.intersect.push(newPoint);
  260. groupedPoitns.intersect.push(newNextPoint);
  261. // Additional null point to make a gap in line
  262. groupedPoitns.intersect.push(merge(newNextPoint, {
  263. isNull: true
  264. }));
  265. if (point.y >= mainPoint.close &&
  266. nextPoint.y < nextMainPoint.close) {
  267. point.color = (pointColor || indicOptions.fallingTrendColor ||
  268. indicOptions.color);
  269. nextPoint.color = (pointColor || indicOptions.risingTrendColor ||
  270. indicOptions.color);
  271. groupedPoitns.top.push(newPoint);
  272. groupedPoitns.top.push(merge(newNextPoint, {
  273. isNull: true
  274. }));
  275. }
  276. else if (point.y < mainPoint.close &&
  277. nextPoint.y >= nextMainPoint.close) {
  278. point.color = (pointColor || indicOptions.risingTrendColor ||
  279. indicOptions.color);
  280. nextPoint.color = (pointColor || indicOptions.fallingTrendColor ||
  281. indicOptions.color);
  282. groupedPoitns.bottom.push(newPoint);
  283. groupedPoitns.bottom.push(merge(newNextPoint, {
  284. isNull: true
  285. }));
  286. }
  287. }
  288. }
  289. else if (mainPoint) {
  290. if (point.y >= mainPoint.close) {
  291. point.color = (pointColor || indicOptions.fallingTrendColor ||
  292. indicOptions.color);
  293. groupedPoitns.top.push(newPoint);
  294. }
  295. else {
  296. point.color = (pointColor || indicOptions.risingTrendColor ||
  297. indicOptions.color);
  298. groupedPoitns.bottom.push(newPoint);
  299. }
  300. }
  301. }
  302. // Generate lines:
  303. objectEach(groupedPoitns, function (values, lineName) {
  304. indicator.points = values;
  305. indicator.options = merge(supertrendLineOptions[lineName].styles, gappedExtend);
  306. indicator.graph = indicator['graph' + lineName + 'Line'];
  307. SMAIndicator.prototype.drawGraph.call(indicator);
  308. // Now save line
  309. indicator['graph' + lineName + 'Line'] = indicator.graph;
  310. });
  311. // Restore options:
  312. indicator.points = indicPoints;
  313. indicator.options = indicOptions;
  314. indicator.graph = indicPath;
  315. };
  316. // Supertrend (Multiplier, Period) Formula:
  317. // BASIC UPPERBAND = (HIGH + LOW) / 2 + Multiplier * ATR(Period)
  318. // BASIC LOWERBAND = (HIGH + LOW) / 2 - Multiplier * ATR(Period)
  319. // FINAL UPPERBAND =
  320. // IF(
  321. // Current BASICUPPERBAND < Previous FINAL UPPERBAND AND
  322. // Previous Close > Previous FINAL UPPERBAND
  323. // ) THEN (Current BASIC UPPERBAND)
  324. // ELSE (Previous FINALUPPERBAND)
  325. // FINAL LOWERBAND =
  326. // IF(
  327. // Current BASIC LOWERBAND > Previous FINAL LOWERBAND AND
  328. // Previous Close < Previous FINAL LOWERBAND
  329. // ) THEN (Current BASIC LOWERBAND)
  330. // ELSE (Previous FINAL LOWERBAND)
  331. // SUPERTREND =
  332. // IF(
  333. // Previous Supertrend == Previous FINAL UPPERBAND AND
  334. // Current Close < Current FINAL UPPERBAND
  335. // ) THAN Current FINAL UPPERBAND
  336. // ELSE IF(
  337. // Previous Supertrend == Previous FINAL LOWERBAND AND
  338. // Current Close < Current FINAL LOWERBAND
  339. // ) THAN Current FINAL UPPERBAND
  340. // ELSE IF(
  341. // Previous Supertrend == Previous FINAL UPPERBAND AND
  342. // Current Close > Current FINAL UPPERBAND
  343. // ) THAN Current FINAL LOWERBAND
  344. // ELSE IF(
  345. // Previous Supertrend == Previous FINAL LOWERBAND AND
  346. // Current Close > Current FINAL LOWERBAND
  347. // ) THAN Current FINAL LOWERBAND
  348. SupertrendIndicator.prototype.getValues = function (series, params) {
  349. var period = params.period,
  350. multiplier = params.multiplier,
  351. xVal = series.xData,
  352. yVal = series.yData,
  353. ATRData = [],
  354. // 0- date, 1- Supertrend indicator
  355. ST = [],
  356. xData = [],
  357. yData = [],
  358. close = 3,
  359. low = 2,
  360. high = 1,
  361. periodsOffset = (period === 0) ? 0 : period - 1,
  362. basicUp,
  363. basicDown,
  364. finalUp = [],
  365. finalDown = [],
  366. supertrend,
  367. prevFinalUp,
  368. prevFinalDown,
  369. prevST, // previous Supertrend
  370. prevY,
  371. y,
  372. i;
  373. if ((xVal.length <= period) || !isArray(yVal[0]) ||
  374. yVal[0].length !== 4 || period < 0) {
  375. return;
  376. }
  377. ATRData = ATRIndicator.prototype.getValues.call(this, series, {
  378. period: period
  379. }).yData;
  380. for (i = 0; i < ATRData.length; i++) {
  381. y = yVal[periodsOffset + i];
  382. prevY = yVal[periodsOffset + i - 1] || [];
  383. prevFinalUp = finalUp[i - 1];
  384. prevFinalDown = finalDown[i - 1];
  385. prevST = yData[i - 1];
  386. if (i === 0) {
  387. prevFinalUp = prevFinalDown = prevST = 0;
  388. }
  389. basicUp = correctFloat((y[high] + y[low]) / 2 + multiplier * ATRData[i]);
  390. basicDown = correctFloat((y[high] + y[low]) / 2 - multiplier * ATRData[i]);
  391. if ((basicUp < prevFinalUp) ||
  392. (prevY[close] > prevFinalUp)) {
  393. finalUp[i] = basicUp;
  394. }
  395. else {
  396. finalUp[i] = prevFinalUp;
  397. }
  398. if ((basicDown > prevFinalDown) ||
  399. (prevY[close] < prevFinalDown)) {
  400. finalDown[i] = basicDown;
  401. }
  402. else {
  403. finalDown[i] = prevFinalDown;
  404. }
  405. if (prevST === prevFinalUp && y[close] < finalUp[i] ||
  406. prevST === prevFinalDown && y[close] < finalDown[i]) {
  407. supertrend = finalUp[i];
  408. }
  409. else if (prevST === prevFinalUp && y[close] > finalUp[i] ||
  410. prevST === prevFinalDown && y[close] > finalDown[i]) {
  411. supertrend = finalDown[i];
  412. }
  413. ST.push([xVal[periodsOffset + i], supertrend]);
  414. xData.push(xVal[periodsOffset + i]);
  415. yData.push(supertrend);
  416. }
  417. return {
  418. values: ST,
  419. xData: xData,
  420. yData: yData
  421. };
  422. };
  423. /**
  424. * Supertrend indicator. This series requires the `linkedTo` option to be
  425. * set and should be loaded after the `stock/indicators/indicators.js` and
  426. * `stock/indicators/sma.js`.
  427. *
  428. * @sample {highstock} stock/indicators/supertrend
  429. * Supertrend indicator
  430. *
  431. * @extends plotOptions.sma
  432. * @since 7.0.0
  433. * @product highstock
  434. * @excluding allAreas, cropThreshold, negativeColor, colorAxis, joinBy,
  435. * keys, navigatorOptions, pointInterval, pointIntervalUnit,
  436. * pointPlacement, pointRange, pointStart, showInNavigator,
  437. * stacking, threshold
  438. * @requires stock/indicators/indicators
  439. * @requires stock/indicators/supertrend
  440. * @optionparent plotOptions.supertrend
  441. */
  442. SupertrendIndicator.defaultOptions = merge(SMAIndicator.defaultOptions, {
  443. /**
  444. * Paramters used in calculation of Supertrend indicator series points.
  445. *
  446. * @excluding index
  447. */
  448. params: {
  449. index: void 0,
  450. /**
  451. * Multiplier for Supertrend Indicator.
  452. */
  453. multiplier: 3,
  454. /**
  455. * The base period for indicator Supertrend Indicator calculations.
  456. * This is the number of data points which are taken into account
  457. * for the indicator calculations.
  458. */
  459. period: 10
  460. },
  461. /**
  462. * Color of the Supertrend series line that is beneath the main series.
  463. *
  464. * @sample {highstock} stock/indicators/supertrend/
  465. * Example with risingTrendColor
  466. *
  467. * @type {Highcharts.ColorString|Highcharts.GradientColorObject|Highcharts.PatternObject}
  468. */
  469. risingTrendColor: palette.positiveColor,
  470. /**
  471. * Color of the Supertrend series line that is above the main series.
  472. *
  473. * @sample {highstock} stock/indicators/supertrend/
  474. * Example with fallingTrendColor
  475. *
  476. * @type {Highcharts.ColorString|Highcharts.GradientColorObject|Highcharts.PatternObject}
  477. */
  478. fallingTrendColor: palette.negativeColor,
  479. /**
  480. * The styles for the Supertrend line that intersect main series.
  481. *
  482. * @sample {highstock} stock/indicators/supertrend/
  483. * Example with changeTrendLine
  484. */
  485. changeTrendLine: {
  486. styles: {
  487. /**
  488. * Pixel width of the line.
  489. */
  490. lineWidth: 1,
  491. /**
  492. * Color of the line.
  493. *
  494. * @type {Highcharts.ColorString}
  495. */
  496. lineColor: palette.neutralColor80,
  497. /**
  498. * The dash or dot style of the grid lines. For possible
  499. * values, see
  500. * [this demonstration](https://jsfiddle.net/gh/get/library/pure/highcharts/highcharts/tree/master/samples/highcharts/plotoptions/series-dashstyle-all/).
  501. *
  502. * @sample {highcharts} highcharts/yaxis/gridlinedashstyle/
  503. * Long dashes
  504. * @sample {highstock} stock/xaxis/gridlinedashstyle/
  505. * Long dashes
  506. *
  507. * @type {Highcharts.DashStyleValue}
  508. * @since 7.0.0
  509. */
  510. dashStyle: 'LongDash'
  511. }
  512. }
  513. });
  514. return SupertrendIndicator;
  515. }(SMAIndicator));
  516. extend(SupertrendIndicator.prototype, {
  517. nameBase: 'Supertrend',
  518. nameComponents: ['multiplier', 'period'],
  519. requiredIndicators: ['atr']
  520. });
  521. SeriesRegistry.registerSeriesType('supertrend', SupertrendIndicator);
  522. /* *
  523. *
  524. * Default Export
  525. *
  526. * */
  527. /**
  528. * A `Supertrend indicator` series. If the [type](#series.supertrend.type)
  529. * option is not specified, it is inherited from [chart.type](#chart.type).
  530. *
  531. * @extends series,plotOptions.supertrend
  532. * @since 7.0.0
  533. * @product highstock
  534. * @excluding allAreas, colorAxis, cropThreshold, data, dataParser, dataURL,
  535. * joinBy, keys, navigatorOptions, negativeColor, pointInterval,
  536. * pointIntervalUnit, pointPlacement, pointRange, pointStart,
  537. * showInNavigator, stacking, threshold
  538. * @requires stock/indicators/indicators
  539. * @requires stock/indicators/supertrend
  540. * @apioption series.supertrend
  541. */
  542. ''; // to include the above in the js output
  543. return SupertrendIndicator;
  544. });
  545. _registerModule(_modules, 'masters/indicators/supertrend.src.js', [], function () {
  546. });
  547. }));