supertrend.src.js 22 KB

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