tema.src.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /**
  2. * @license Highstock JS v8.2.0 (2020-08-20)
  3. *
  4. * Indicator series type for Highstock
  5. *
  6. * (c) 2010-2019 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-2020 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/TEMAIndicator.js', [_modules['Core/Globals.js'], _modules['Core/Utilities.js'], _modules['Mixins/IndicatorRequired.js']], function (H, U, requiredIndicator) {
  93. /* *
  94. *
  95. * License: www.highcharts.com/license
  96. *
  97. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  98. *
  99. * */
  100. var correctFloat = U.correctFloat,
  101. isArray = U.isArray,
  102. seriesType = U.seriesType;
  103. var EMAindicator = H.seriesTypes.ema;
  104. /**
  105. * The TEMA series type.
  106. *
  107. * @private
  108. * @class
  109. * @name Highcharts.seriesTypes.tema
  110. *
  111. * @augments Highcharts.Series
  112. */
  113. seriesType('tema', 'ema',
  114. /**
  115. * Triple exponential moving average (TEMA) indicator. This series requires
  116. * `linkedTo` option to be set and should be loaded after the
  117. * `stock/indicators/indicators.js` and `stock/indicators/ema.js`.
  118. *
  119. * @sample {highstock} stock/indicators/tema
  120. * TEMA indicator
  121. *
  122. * @extends plotOptions.ema
  123. * @since 7.0.0
  124. * @product highstock
  125. * @excluding allAreas, colorAxis, compare, compareBase, joinBy, keys,
  126. * navigatorOptions, pointInterval, pointIntervalUnit,
  127. * pointPlacement, pointRange, pointStart, showInNavigator,
  128. * stacking
  129. * @requires stock/indicators/indicators
  130. * @requires stock/indicators/ema
  131. * @requires stock/indicators/tema
  132. * @optionparent plotOptions.tema
  133. */
  134. {},
  135. /**
  136. * @lends Highcharts.Series#
  137. */
  138. {
  139. init: function () {
  140. var args = arguments,
  141. ctx = this;
  142. requiredIndicator.isParentLoaded(EMAindicator, 'ema', ctx.type, function (indicator) {
  143. indicator.prototype.init.apply(ctx, args);
  144. return;
  145. });
  146. },
  147. getEMA: function (yVal, prevEMA, SMA, index, i, xVal) {
  148. return EMAindicator.prototype.calculateEma(xVal || [], yVal, typeof i === 'undefined' ? 1 : i, this.chart.series[0].EMApercent, prevEMA, typeof index === 'undefined' ? -1 : index, SMA);
  149. },
  150. getTemaPoint: function (xVal, tripledPeriod, EMAlevels, i) {
  151. var TEMAPoint = [
  152. xVal[i - 3],
  153. correctFloat(3 * EMAlevels.level1 -
  154. 3 * EMAlevels.level2 + EMAlevels.level3)
  155. ];
  156. return TEMAPoint;
  157. },
  158. getValues: function (series, params) {
  159. var period = params.period,
  160. doubledPeriod = 2 * period,
  161. tripledPeriod = 3 * period,
  162. xVal = series.xData,
  163. yVal = series.yData,
  164. yValLen = yVal ? yVal.length : 0,
  165. index = -1,
  166. accumulatePeriodPoints = 0,
  167. SMA = 0,
  168. TEMA = [],
  169. xDataTema = [],
  170. yDataTema = [],
  171. // EMA of previous point
  172. prevEMA,
  173. prevEMAlevel2,
  174. // EMA values array
  175. EMAvalues = [],
  176. EMAlevel2values = [],
  177. i,
  178. TEMAPoint,
  179. // This object contains all EMA EMAlevels calculated like below
  180. // EMA = level1
  181. // EMA(EMA) = level2,
  182. // EMA(EMA(EMA)) = level3,
  183. EMAlevels = {};
  184. series.EMApercent = (2 / (period + 1));
  185. // Check period, if bigger than EMA points length, skip
  186. if (yValLen < 3 * period - 2) {
  187. return;
  188. }
  189. // Switch index for OHLC / Candlestick / Arearange
  190. if (isArray(yVal[0])) {
  191. index = params.index ? params.index : 0;
  192. }
  193. // Accumulate first N-points
  194. accumulatePeriodPoints =
  195. EMAindicator.prototype.accumulatePeriodPoints(period, index, yVal);
  196. // first point
  197. SMA = accumulatePeriodPoints / period;
  198. accumulatePeriodPoints = 0;
  199. // Calculate value one-by-one for each period in visible data
  200. for (i = period; i < yValLen + 3; i++) {
  201. if (i < yValLen + 1) {
  202. EMAlevels.level1 = this.getEMA(yVal, prevEMA, SMA, index, i)[1];
  203. EMAvalues.push(EMAlevels.level1);
  204. }
  205. prevEMA = EMAlevels.level1;
  206. // Summing first period points for ema(ema)
  207. if (i < doubledPeriod) {
  208. accumulatePeriodPoints += EMAlevels.level1;
  209. }
  210. else {
  211. // Calculate dema
  212. // First dema point
  213. if (i === doubledPeriod) {
  214. SMA = accumulatePeriodPoints / period;
  215. accumulatePeriodPoints = 0;
  216. }
  217. EMAlevels.level1 = EMAvalues[i - period - 1];
  218. EMAlevels.level2 = this.getEMA([EMAlevels.level1], prevEMAlevel2, SMA)[1];
  219. EMAlevel2values.push(EMAlevels.level2);
  220. prevEMAlevel2 = EMAlevels.level2;
  221. // Summing first period points for ema(ema(ema))
  222. if (i < tripledPeriod) {
  223. accumulatePeriodPoints += EMAlevels.level2;
  224. }
  225. else {
  226. // Calculate tema
  227. // First tema point
  228. if (i === tripledPeriod) {
  229. SMA = accumulatePeriodPoints / period;
  230. }
  231. if (i === yValLen + 1) {
  232. // Calculate the last ema and emaEMA points
  233. EMAlevels.level1 = EMAvalues[i - period - 1];
  234. EMAlevels.level2 = this.getEMA([EMAlevels.level1], prevEMAlevel2, SMA)[1];
  235. EMAlevel2values.push(EMAlevels.level2);
  236. }
  237. EMAlevels.level1 = EMAvalues[i - period - 2];
  238. EMAlevels.level2 = EMAlevel2values[i - 2 * period - 1];
  239. EMAlevels.level3 = this.getEMA([EMAlevels.level2], EMAlevels.prevLevel3, SMA)[1];
  240. TEMAPoint = this.getTemaPoint(xVal, tripledPeriod, EMAlevels, i);
  241. // Make sure that point exists (for TRIX oscillator)
  242. if (TEMAPoint) {
  243. TEMA.push(TEMAPoint);
  244. xDataTema.push(TEMAPoint[0]);
  245. yDataTema.push(TEMAPoint[1]);
  246. }
  247. EMAlevels.prevLevel3 = EMAlevels.level3;
  248. }
  249. }
  250. }
  251. return {
  252. values: TEMA,
  253. xData: xDataTema,
  254. yData: yDataTema
  255. };
  256. }
  257. });
  258. /**
  259. * A `TEMA` series. If the [type](#series.ema.type) option is not
  260. * specified, it is inherited from [chart.type](#chart.type).
  261. *
  262. * @extends series,plotOptions.ema
  263. * @since 7.0.0
  264. * @product highstock
  265. * @excluding allAreas, colorAxis, compare, compareBase, dataParser, dataURL,
  266. * joinBy, keys, navigatorOptions, pointInterval, pointIntervalUnit,
  267. * pointPlacement, pointRange, pointStart, showInNavigator, stacking
  268. * @requires stock/indicators/indicators
  269. * @requires stock/indicators/ema
  270. * @requires stock/indicators/tema
  271. * @apioption series.tema
  272. */
  273. ''; // to include the above in the js output
  274. });
  275. _registerModule(_modules, 'masters/indicators/tema.src.js', [], function () {
  276. });
  277. }));