dema.src.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /**
  2. * @license Highstock JS v8.2.0 (2020-08-20)
  3. *
  4. * Indicator series type for Highstock
  5. *
  6. * (c) 2010-2019 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-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/DEMAIndicator.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 DEMA series Type
  106. *
  107. * @private
  108. * @class
  109. * @name Highcharts.seriesTypes.dema
  110. *
  111. * @augments Highcharts.Series
  112. */
  113. seriesType('dema', 'ema',
  114. /**
  115. * Double exponential moving average (DEMA) 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/dema
  120. * DEMA 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/dema
  132. * @optionparent plotOptions.dema
  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. getValues: function (series, params) {
  151. var period = params.period,
  152. doubledPeriod = 2 * period,
  153. xVal = series.xData,
  154. yVal = series.yData,
  155. yValLen = yVal ? yVal.length : 0,
  156. index = -1,
  157. accumulatePeriodPoints = 0,
  158. SMA = 0,
  159. DEMA = [],
  160. xDataDema = [],
  161. yDataDema = [],
  162. EMA = 0,
  163. // EMA(EMA)
  164. EMAlevel2,
  165. // EMA of previous point
  166. prevEMA,
  167. prevEMAlevel2,
  168. // EMA values array
  169. EMAvalues = [],
  170. i,
  171. DEMAPoint;
  172. series.EMApercent = (2 / (period + 1));
  173. // Check period, if bigger than EMA points length, skip
  174. if (yValLen < 2 * period - 1) {
  175. return;
  176. }
  177. // Switch index for OHLC / Candlestick / Arearange
  178. if (isArray(yVal[0])) {
  179. index = params.index ? params.index : 0;
  180. }
  181. // Accumulate first N-points
  182. accumulatePeriodPoints =
  183. EMAindicator.prototype.accumulatePeriodPoints(period, index, yVal);
  184. // first point
  185. SMA = accumulatePeriodPoints / period;
  186. accumulatePeriodPoints = 0;
  187. // Calculate value one-by-one for each period in visible data
  188. for (i = period; i < yValLen + 2; i++) {
  189. if (i < yValLen + 1) {
  190. EMA = this.getEMA(yVal, prevEMA, SMA, index, i)[1];
  191. EMAvalues.push(EMA);
  192. }
  193. prevEMA = EMA;
  194. // Summing first period points for EMA(EMA)
  195. if (i < doubledPeriod) {
  196. accumulatePeriodPoints += EMA;
  197. }
  198. else {
  199. // Calculate DEMA
  200. // First DEMA point
  201. if (i === doubledPeriod) {
  202. SMA = accumulatePeriodPoints / period;
  203. }
  204. EMA = EMAvalues[i - period - 1];
  205. EMAlevel2 = this.getEMA([EMA], prevEMAlevel2, SMA)[1];
  206. DEMAPoint = [
  207. xVal[i - 2],
  208. correctFloat(2 * EMA - EMAlevel2)
  209. ];
  210. DEMA.push(DEMAPoint);
  211. xDataDema.push(DEMAPoint[0]);
  212. yDataDema.push(DEMAPoint[1]);
  213. prevEMAlevel2 = EMAlevel2;
  214. }
  215. }
  216. return {
  217. values: DEMA,
  218. xData: xDataDema,
  219. yData: yDataDema
  220. };
  221. }
  222. });
  223. /**
  224. * A `DEMA` series. If the [type](#series.ema.type) option is not
  225. * specified, it is inherited from [chart.type](#chart.type).
  226. *
  227. * @extends series,plotOptions.ema
  228. * @since 7.0.0
  229. * @product highstock
  230. * @excluding allAreas, colorAxis, compare, compareBase, dataParser, dataURL,
  231. * joinBy, keys, navigatorOptions, pointInterval, pointIntervalUnit,
  232. * pointPlacement, pointRange, pointStart, showInNavigator, stacking
  233. * @requires stock/indicators/indicators
  234. * @requires stock/indicators/ema
  235. * @requires stock/indicators/dema
  236. * @apioption series.dema
  237. */
  238. ''; // adds doclet above to the transpiled file
  239. });
  240. _registerModule(_modules, 'masters/indicators/dema.src.js', [], function () {
  241. });
  242. }));