dema.src.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /**
  2. * @license Highstock JS v9.1.1 (2021-06-04)
  3. *
  4. * Indicator series type for Highcharts Stock
  5. *
  6. * (c) 2010-2021 Rafał Sebestjański
  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/dema', ['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/DEMA/DEMAIndicator.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 DEMA series Type
  122. *
  123. * @private
  124. * @class
  125. * @name Highcharts.seriesTypes.dema
  126. *
  127. * @augments Highcharts.Series
  128. */
  129. var DEMAIndicator = /** @class */ (function (_super) {
  130. __extends(DEMAIndicator, _super);
  131. function DEMAIndicator() {
  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. DEMAIndicator.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. DEMAIndicator.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. DEMAIndicator.prototype.getValues = function (series, params) {
  152. var period = params.period,
  153. doubledPeriod = 2 * period,
  154. xVal = series.xData,
  155. yVal = series.yData,
  156. yValLen = yVal ? yVal.length : 0,
  157. index = -1,
  158. accumulatePeriodPoints = 0,
  159. SMA = 0,
  160. DEMA = [],
  161. xDataDema = [],
  162. yDataDema = [],
  163. EMA = 0,
  164. // EMA(EMA)
  165. EMAlevel2,
  166. // EMA of previous point
  167. prevEMA,
  168. prevEMAlevel2,
  169. // EMA values array
  170. EMAvalues = [],
  171. i,
  172. DEMAPoint;
  173. this.EMApercent = (2 / (period + 1));
  174. // Check period, if bigger than EMA points length, skip
  175. if (yValLen < 2 * period - 1) {
  176. return;
  177. }
  178. // Switch index for OHLC / Candlestick / Arearange
  179. if (isArray(yVal[0])) {
  180. index = params.index ? params.index : 0;
  181. }
  182. // Accumulate first N-points
  183. accumulatePeriodPoints =
  184. EMAIndicator.prototype.accumulatePeriodPoints(period, index, yVal);
  185. // first point
  186. SMA = accumulatePeriodPoints / period;
  187. accumulatePeriodPoints = 0;
  188. // Calculate value one-by-one for each period in visible data
  189. for (i = period; i < yValLen + 2; i++) {
  190. if (i < yValLen + 1) {
  191. EMA = this.getEMA(yVal, prevEMA, SMA, index, i)[1];
  192. EMAvalues.push(EMA);
  193. }
  194. prevEMA = EMA;
  195. // Summing first period points for EMA(EMA)
  196. if (i < doubledPeriod) {
  197. accumulatePeriodPoints += EMA;
  198. }
  199. else {
  200. // Calculate DEMA
  201. // First DEMA point
  202. if (i === doubledPeriod) {
  203. SMA = accumulatePeriodPoints / period;
  204. }
  205. EMA = EMAvalues[i - period - 1];
  206. EMAlevel2 = this.getEMA([EMA], prevEMAlevel2, SMA)[1];
  207. DEMAPoint = [
  208. xVal[i - 2],
  209. correctFloat(2 * EMA - EMAlevel2)
  210. ];
  211. DEMA.push(DEMAPoint);
  212. xDataDema.push(DEMAPoint[0]);
  213. yDataDema.push(DEMAPoint[1]);
  214. prevEMAlevel2 = EMAlevel2;
  215. }
  216. }
  217. return {
  218. values: DEMA,
  219. xData: xDataDema,
  220. yData: yDataDema
  221. };
  222. };
  223. /**
  224. * Double exponential moving average (DEMA) indicator. This series requires
  225. * `linkedTo` option to be set and should be loaded after the
  226. * `stock/indicators/indicators.js` and `stock/indicators/ema.js`.
  227. *
  228. * @sample {highstock} stock/indicators/dema
  229. * DEMA indicator
  230. *
  231. * @extends plotOptions.ema
  232. * @since 7.0.0
  233. * @product highstock
  234. * @excluding allAreas, colorAxis, compare, compareBase, joinBy, keys,
  235. * navigatorOptions, pointInterval, pointIntervalUnit,
  236. * pointPlacement, pointRange, pointStart, showInNavigator,
  237. * stacking
  238. * @requires stock/indicators/indicators
  239. * @requires stock/indicators/ema
  240. * @requires stock/indicators/dema
  241. * @optionparent plotOptions.dema
  242. */
  243. DEMAIndicator.defaultOptions = merge(EMAIndicator.defaultOptions);
  244. return DEMAIndicator;
  245. }(EMAIndicator));
  246. SeriesRegistry.registerSeriesType('dema', DEMAIndicator);
  247. /* *
  248. *
  249. * Default Export
  250. *
  251. * */
  252. /**
  253. * A `DEMA` series. If the [type](#series.dema.type) option is not
  254. * specified, it is inherited from [chart.type](#chart.type).
  255. *
  256. * @extends series,plotOptions.dema
  257. * @since 7.0.0
  258. * @product highstock
  259. * @excluding allAreas, colorAxis, compare, compareBase, dataParser, dataURL,
  260. * joinBy, keys, navigatorOptions, pointInterval, pointIntervalUnit,
  261. * pointPlacement, pointRange, pointStart, showInNavigator, stacking
  262. * @requires stock/indicators/indicators
  263. * @requires stock/indicators/ema
  264. * @requires stock/indicators/dema
  265. * @apioption series.dema
  266. */
  267. ''; // adds doclet above to the transpiled file
  268. return DEMAIndicator;
  269. });
  270. _registerModule(_modules, 'masters/indicators/dema.src.js', [], function () {
  271. });
  272. }));