tema.src.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /**
  2. * @license Highstock JS v8.1.2 (2020-06-16)
  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/indicator-required.js', [_modules['parts/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, else throw error.
  46. * @private
  47. * @param {Highcharts.Indicator} indicator
  48. * Indicator constructor function.
  49. * @param {string} requiredIndicator
  50. * Required indicator type.
  51. * @param {string} type
  52. * Type of indicator where function was called (parent).
  53. * @param {Highcharts.IndicatorCallbackFunction} callback
  54. * Callback which is triggered if the given indicator is loaded.
  55. * Takes indicator as an argument.
  56. * @param {string} errMessage
  57. * Error message that will be logged in console.
  58. * @return {boolean}
  59. * Returns false when there is no required indicator loaded.
  60. */
  61. isParentLoaded: function (indicator, requiredIndicator, type, callback, errMessage) {
  62. if (indicator) {
  63. return callback ? callback(indicator) : true;
  64. }
  65. error(errMessage || this.generateMessage(type, requiredIndicator));
  66. return false;
  67. },
  68. /**
  69. * @private
  70. * @param {string} indicatorType
  71. * Indicator type
  72. * @param {string} required
  73. * Required indicator
  74. * @return {string}
  75. * Error message
  76. */
  77. generateMessage: function (indicatorType, required) {
  78. return 'Error: "' + indicatorType +
  79. '" indicator type requires "' + required +
  80. '" indicator loaded before. Please read docs: ' +
  81. 'https://api.highcharts.com/highstock/plotOptions.' +
  82. indicatorType;
  83. }
  84. };
  85. return requiredIndicatorMixin;
  86. });
  87. _registerModule(_modules, 'indicators/tema.src.js', [_modules['parts/Globals.js'], _modules['parts/Utilities.js'], _modules['mixins/indicator-required.js']], function (H, U, requiredIndicatorMixin) {
  88. /* *
  89. *
  90. * License: www.highcharts.com/license
  91. *
  92. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  93. *
  94. * */
  95. var correctFloat = U.correctFloat, isArray = U.isArray, seriesType = U.seriesType;
  96. var EMAindicator = H.seriesTypes.ema, requiredIndicator = requiredIndicatorMixin;
  97. /**
  98. * The TEMA series type.
  99. *
  100. * @private
  101. * @class
  102. * @name Highcharts.seriesTypes.tema
  103. *
  104. * @augments Highcharts.Series
  105. */
  106. seriesType('tema', 'ema',
  107. /**
  108. * Triple exponential moving average (TEMA) indicator. This series requires
  109. * `linkedTo` option to be set and should be loaded after the
  110. * `stock/indicators/indicators.js` and `stock/indicators/ema.js`.
  111. *
  112. * @sample {highstock} stock/indicators/tema
  113. * TEMA indicator
  114. *
  115. * @extends plotOptions.ema
  116. * @since 7.0.0
  117. * @product highstock
  118. * @excluding allAreas, colorAxis, compare, compareBase, joinBy, keys,
  119. * navigatorOptions, pointInterval, pointIntervalUnit,
  120. * pointPlacement, pointRange, pointStart, showInNavigator,
  121. * stacking
  122. * @requires stock/indicators/indicators
  123. * @requires stock/indicators/ema
  124. * @requires stock/indicators/tema
  125. * @optionparent plotOptions.tema
  126. */
  127. {},
  128. /**
  129. * @lends Highcharts.Series#
  130. */
  131. {
  132. init: function () {
  133. var args = arguments, ctx = this;
  134. requiredIndicator.isParentLoaded(EMAindicator, 'ema', ctx.type, function (indicator) {
  135. indicator.prototype.init.apply(ctx, args);
  136. return;
  137. });
  138. },
  139. getEMA: function (yVal, prevEMA, SMA, index, i, xVal) {
  140. return EMAindicator.prototype.calculateEma(xVal || [], yVal, typeof i === 'undefined' ? 1 : i, this.chart.series[0].EMApercent, prevEMA, typeof index === 'undefined' ? -1 : index, SMA);
  141. },
  142. getTemaPoint: function (xVal, tripledPeriod, EMAlevels, i) {
  143. var TEMAPoint = [
  144. xVal[i - 3],
  145. correctFloat(3 * EMAlevels.level1 -
  146. 3 * EMAlevels.level2 + EMAlevels.level3)
  147. ];
  148. return TEMAPoint;
  149. },
  150. getValues: function (series, params) {
  151. var period = params.period, doubledPeriod = 2 * period, tripledPeriod = 3 * period, xVal = series.xData, yVal = series.yData, yValLen = yVal ? yVal.length : 0, index = -1, accumulatePeriodPoints = 0, SMA = 0, TEMA = [], xDataTema = [], yDataTema = [],
  152. // EMA of previous point
  153. prevEMA, prevEMAlevel2,
  154. // EMA values array
  155. EMAvalues = [], EMAlevel2values = [], i, TEMAPoint,
  156. // This object contains all EMA EMAlevels calculated like below
  157. // EMA = level1
  158. // EMA(EMA) = level2,
  159. // EMA(EMA(EMA)) = level3,
  160. EMAlevels = {};
  161. series.EMApercent = (2 / (period + 1));
  162. // Check period, if bigger than EMA points length, skip
  163. if (yValLen < 3 * period - 2) {
  164. return;
  165. }
  166. // Switch index for OHLC / Candlestick / Arearange
  167. if (isArray(yVal[0])) {
  168. index = params.index ? params.index : 0;
  169. }
  170. // Accumulate first N-points
  171. accumulatePeriodPoints =
  172. EMAindicator.prototype.accumulatePeriodPoints(period, index, yVal);
  173. // first point
  174. SMA = accumulatePeriodPoints / period;
  175. accumulatePeriodPoints = 0;
  176. // Calculate value one-by-one for each period in visible data
  177. for (i = period; i < yValLen + 3; i++) {
  178. if (i < yValLen + 1) {
  179. EMAlevels.level1 = this.getEMA(yVal, prevEMA, SMA, index, i)[1];
  180. EMAvalues.push(EMAlevels.level1);
  181. }
  182. prevEMA = EMAlevels.level1;
  183. // Summing first period points for ema(ema)
  184. if (i < doubledPeriod) {
  185. accumulatePeriodPoints += EMAlevels.level1;
  186. }
  187. else {
  188. // Calculate dema
  189. // First dema point
  190. if (i === doubledPeriod) {
  191. SMA = accumulatePeriodPoints / period;
  192. accumulatePeriodPoints = 0;
  193. }
  194. EMAlevels.level1 = EMAvalues[i - period - 1];
  195. EMAlevels.level2 = this.getEMA([EMAlevels.level1], prevEMAlevel2, SMA)[1];
  196. EMAlevel2values.push(EMAlevels.level2);
  197. prevEMAlevel2 = EMAlevels.level2;
  198. // Summing first period points for ema(ema(ema))
  199. if (i < tripledPeriod) {
  200. accumulatePeriodPoints += EMAlevels.level2;
  201. }
  202. else {
  203. // Calculate tema
  204. // First tema point
  205. if (i === tripledPeriod) {
  206. SMA = accumulatePeriodPoints / period;
  207. }
  208. if (i === yValLen + 1) {
  209. // Calculate the last ema and emaEMA points
  210. EMAlevels.level1 = EMAvalues[i - period - 1];
  211. EMAlevels.level2 = this.getEMA([EMAlevels.level1], prevEMAlevel2, SMA)[1];
  212. EMAlevel2values.push(EMAlevels.level2);
  213. }
  214. EMAlevels.level1 = EMAvalues[i - period - 2];
  215. EMAlevels.level2 = EMAlevel2values[i - 2 * period - 1];
  216. EMAlevels.level3 = this.getEMA([EMAlevels.level2], EMAlevels.prevLevel3, SMA)[1];
  217. TEMAPoint = this.getTemaPoint(xVal, tripledPeriod, EMAlevels, i);
  218. // Make sure that point exists (for TRIX oscillator)
  219. if (TEMAPoint) {
  220. TEMA.push(TEMAPoint);
  221. xDataTema.push(TEMAPoint[0]);
  222. yDataTema.push(TEMAPoint[1]);
  223. }
  224. EMAlevels.prevLevel3 = EMAlevels.level3;
  225. }
  226. }
  227. }
  228. return {
  229. values: TEMA,
  230. xData: xDataTema,
  231. yData: yDataTema
  232. };
  233. }
  234. });
  235. /**
  236. * A `TEMA` series. If the [type](#series.ema.type) option is not
  237. * specified, it is inherited from [chart.type](#chart.type).
  238. *
  239. * @extends series,plotOptions.ema
  240. * @since 7.0.0
  241. * @product highstock
  242. * @excluding allAreas, colorAxis, compare, compareBase, dataParser, dataURL,
  243. * joinBy, keys, navigatorOptions, pointInterval, pointIntervalUnit,
  244. * pointPlacement, pointRange, pointStart, showInNavigator, stacking
  245. * @requires stock/indicators/indicators
  246. * @requires stock/indicators/ema
  247. * @requires stock/indicators/tema
  248. * @apioption series.tema
  249. */
  250. ''; // to include the above in the js output
  251. });
  252. _registerModule(_modules, 'masters/indicators/tema.src.js', [], function () {
  253. });
  254. }));