tema.src.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /**
  2. * @license Highstock JS v9.1.1 (2021-06-04)
  3. *
  4. * Indicator series type for Highcharts Stock
  5. *
  6. * (c) 2010-2021 Rafal Sebestjanski
  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/tema', ['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, 'Mixins/IndicatorRequired.js', [_modules['Core/Utilities.js']], function (U) {
  32. /**
  33. *
  34. * (c) 2010-2021 Daniel Studencki
  35. *
  36. * License: www.highcharts.com/license
  37. *
  38. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  39. *
  40. * */
  41. var error = U.error;
  42. /* eslint-disable no-invalid-this, valid-jsdoc */
  43. var requiredIndicatorMixin = {
  44. /**
  45. * Check whether given indicator is loaded,
  46. else throw error.
  47. * @private
  48. * @param {Highcharts.Indicator} indicator
  49. * Indicator constructor function.
  50. * @param {string} requiredIndicator
  51. * Required indicator type.
  52. * @param {string} type
  53. * Type of indicator where function was called (parent).
  54. * @param {Highcharts.IndicatorCallbackFunction} callback
  55. * Callback which is triggered if the given indicator is loaded.
  56. * Takes indicator as an argument.
  57. * @param {string} errMessage
  58. * Error message that will be logged in console.
  59. * @return {boolean}
  60. * Returns false when there is no required indicator loaded.
  61. */
  62. isParentLoaded: function (indicator,
  63. requiredIndicator,
  64. type,
  65. callback,
  66. errMessage) {
  67. if (indicator) {
  68. return callback ? callback(indicator) : true;
  69. }
  70. error(errMessage || this.generateMessage(type, requiredIndicator));
  71. return false;
  72. },
  73. /**
  74. * @private
  75. * @param {string} indicatorType
  76. * Indicator type
  77. * @param {string} required
  78. * Required indicator
  79. * @return {string}
  80. * Error message
  81. */
  82. generateMessage: function (indicatorType, required) {
  83. return 'Error: "' + indicatorType +
  84. '" indicator type requires "' + required +
  85. '" indicator loaded before. Please read docs: ' +
  86. 'https://api.highcharts.com/highstock/plotOptions.' +
  87. indicatorType;
  88. }
  89. };
  90. return requiredIndicatorMixin;
  91. });
  92. _registerModule(_modules, 'Stock/Indicators/TEMA/TEMAIndicator.js', [_modules['Mixins/IndicatorRequired.js'], _modules['Core/Series/SeriesRegistry.js'], _modules['Core/Utilities.js']], function (RequiredIndicatorMixin, SeriesRegistry, U) {
  93. /* *
  94. *
  95. * License: www.highcharts.com/license
  96. *
  97. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  98. *
  99. * */
  100. var __extends = (this && this.__extends) || (function () {
  101. var extendStatics = function (d,
  102. b) {
  103. extendStatics = Object.setPrototypeOf ||
  104. ({ __proto__: [] } instanceof Array && function (d,
  105. b) { d.__proto__ = b; }) ||
  106. function (d,
  107. b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  108. return extendStatics(d, b);
  109. };
  110. return function (d, b) {
  111. extendStatics(d, b);
  112. function __() { this.constructor = d; }
  113. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  114. };
  115. })();
  116. var EMAIndicator = SeriesRegistry.seriesTypes.ema;
  117. var correctFloat = U.correctFloat,
  118. isArray = U.isArray,
  119. merge = U.merge;
  120. /**
  121. * The TEMA series type.
  122. *
  123. * @private
  124. * @class
  125. * @name Highcharts.seriesTypes.tema
  126. *
  127. * @augments Highcharts.Series
  128. */
  129. var TEMAIndicator = /** @class */ (function (_super) {
  130. __extends(TEMAIndicator, _super);
  131. function TEMAIndicator() {
  132. var _this = _super !== null && _super.apply(this,
  133. arguments) || this;
  134. _this.EMApercent = void 0;
  135. _this.data = void 0;
  136. _this.options = void 0;
  137. _this.points = void 0;
  138. return _this;
  139. }
  140. TEMAIndicator.prototype.init = function () {
  141. var args = arguments,
  142. ctx = this;
  143. RequiredIndicatorMixin.isParentLoaded(EMAIndicator, 'ema', ctx.type, function (indicator) {
  144. indicator.prototype.init.apply(ctx, args);
  145. return;
  146. });
  147. };
  148. TEMAIndicator.prototype.getEMA = function (yVal, prevEMA, SMA, index, i, xVal) {
  149. return EMAIndicator.prototype.calculateEma(xVal || [], yVal, typeof i === 'undefined' ? 1 : i, this.EMApercent, prevEMA, typeof index === 'undefined' ? -1 : index, SMA);
  150. };
  151. TEMAIndicator.prototype.getTemaPoint = function (xVal, tripledPeriod, EMAlevels, i) {
  152. var TEMAPoint = [
  153. xVal[i - 3],
  154. correctFloat(3 * EMAlevels.level1 -
  155. 3 * EMAlevels.level2 + EMAlevels.level3)
  156. ];
  157. return TEMAPoint;
  158. };
  159. TEMAIndicator.prototype.getValues = function (series, params) {
  160. var period = params.period,
  161. doubledPeriod = 2 * period,
  162. tripledPeriod = 3 * period,
  163. xVal = series.xData,
  164. yVal = series.yData,
  165. yValLen = yVal ? yVal.length : 0,
  166. index = -1,
  167. accumulatePeriodPoints = 0,
  168. SMA = 0,
  169. TEMA = [],
  170. xDataTema = [],
  171. yDataTema = [],
  172. // EMA of previous point
  173. prevEMA,
  174. prevEMAlevel2,
  175. // EMA values array
  176. EMAvalues = [],
  177. EMAlevel2values = [],
  178. i,
  179. TEMAPoint,
  180. // This object contains all EMA EMAlevels calculated like below
  181. // EMA = level1
  182. // EMA(EMA) = level2,
  183. // EMA(EMA(EMA)) = level3,
  184. EMAlevels = {};
  185. this.EMApercent = (2 / (period + 1));
  186. // Check period, if bigger than EMA points length, skip
  187. if (yValLen < 3 * period - 2) {
  188. return;
  189. }
  190. // Switch index for OHLC / Candlestick / Arearange
  191. if (isArray(yVal[0])) {
  192. index = params.index ? params.index : 0;
  193. }
  194. // Accumulate first N-points
  195. accumulatePeriodPoints =
  196. EMAIndicator.prototype.accumulatePeriodPoints(period, index, yVal);
  197. // first point
  198. SMA = accumulatePeriodPoints / period;
  199. accumulatePeriodPoints = 0;
  200. // Calculate value one-by-one for each period in visible data
  201. for (i = period; i < yValLen + 3; i++) {
  202. if (i < yValLen + 1) {
  203. EMAlevels.level1 = this.getEMA(yVal, prevEMA, SMA, index, i)[1];
  204. EMAvalues.push(EMAlevels.level1);
  205. }
  206. prevEMA = EMAlevels.level1;
  207. // Summing first period points for ema(ema)
  208. if (i < doubledPeriod) {
  209. accumulatePeriodPoints += EMAlevels.level1;
  210. }
  211. else {
  212. // Calculate dema
  213. // First dema point
  214. if (i === doubledPeriod) {
  215. SMA = accumulatePeriodPoints / period;
  216. accumulatePeriodPoints = 0;
  217. }
  218. EMAlevels.level1 = EMAvalues[i - period - 1];
  219. EMAlevels.level2 = this.getEMA([EMAlevels.level1], prevEMAlevel2, SMA)[1];
  220. EMAlevel2values.push(EMAlevels.level2);
  221. prevEMAlevel2 = EMAlevels.level2;
  222. // Summing first period points for ema(ema(ema))
  223. if (i < tripledPeriod) {
  224. accumulatePeriodPoints += EMAlevels.level2;
  225. }
  226. else {
  227. // Calculate tema
  228. // First tema point
  229. if (i === tripledPeriod) {
  230. SMA = accumulatePeriodPoints / period;
  231. }
  232. if (i === yValLen + 1) {
  233. // Calculate the last ema and emaEMA points
  234. EMAlevels.level1 = EMAvalues[i - period - 1];
  235. EMAlevels.level2 = this.getEMA([EMAlevels.level1], prevEMAlevel2, SMA)[1];
  236. EMAlevel2values.push(EMAlevels.level2);
  237. }
  238. EMAlevels.level1 = EMAvalues[i - period - 2];
  239. EMAlevels.level2 = EMAlevel2values[i - 2 * period - 1];
  240. EMAlevels.level3 = this.getEMA([EMAlevels.level2], EMAlevels.prevLevel3, SMA)[1];
  241. TEMAPoint = this.getTemaPoint(xVal, tripledPeriod, EMAlevels, i);
  242. // Make sure that point exists (for TRIX oscillator)
  243. if (TEMAPoint) {
  244. TEMA.push(TEMAPoint);
  245. xDataTema.push(TEMAPoint[0]);
  246. yDataTema.push(TEMAPoint[1]);
  247. }
  248. EMAlevels.prevLevel3 = EMAlevels.level3;
  249. }
  250. }
  251. }
  252. return {
  253. values: TEMA,
  254. xData: xDataTema,
  255. yData: yDataTema
  256. };
  257. };
  258. /**
  259. * Triple exponential moving average (TEMA) indicator. This series requires
  260. * `linkedTo` option to be set and should be loaded after the
  261. * `stock/indicators/indicators.js` and `stock/indicators/ema.js`.
  262. *
  263. * @sample {highstock} stock/indicators/tema
  264. * TEMA indicator
  265. *
  266. * @extends plotOptions.ema
  267. * @since 7.0.0
  268. * @product highstock
  269. * @excluding allAreas, colorAxis, compare, compareBase, joinBy, keys,
  270. * navigatorOptions, pointInterval, pointIntervalUnit,
  271. * pointPlacement, pointRange, pointStart, showInNavigator,
  272. * stacking
  273. * @requires stock/indicators/indicators
  274. * @requires stock/indicators/ema
  275. * @requires stock/indicators/tema
  276. * @optionparent plotOptions.tema
  277. */
  278. TEMAIndicator.defaultOptions = merge(EMAIndicator.defaultOptions);
  279. return TEMAIndicator;
  280. }(EMAIndicator));
  281. SeriesRegistry.registerSeriesType('tema', TEMAIndicator);
  282. /* *
  283. *
  284. * Default Export
  285. *
  286. * */
  287. /**
  288. * A `TEMA` series. If the [type](#series.tema.type) option is not
  289. * specified, it is inherited from [chart.type](#chart.type).
  290. *
  291. * @extends series,plotOptions.tema
  292. * @since 7.0.0
  293. * @product highstock
  294. * @excluding allAreas, colorAxis, compare, compareBase, dataParser, dataURL,
  295. * joinBy, keys, navigatorOptions, pointInterval, pointIntervalUnit,
  296. * pointPlacement, pointRange, pointStart, showInNavigator, stacking
  297. * @requires stock/indicators/indicators
  298. * @requires stock/indicators/ema
  299. * @requires stock/indicators/tema
  300. * @apioption series.tema
  301. */
  302. ''; // to include the above in the js output
  303. return TEMAIndicator;
  304. });
  305. _registerModule(_modules, 'masters/indicators/tema.src.js', [], function () {
  306. });
  307. }));