supertrend.src.js 22 KB

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