chaikin.src.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. /**
  2. * @license Highstock JS v9.0.1 (2021-02-16)
  3. *
  4. * Indicator series type for Highstock
  5. *
  6. * (c) 2010-2021 Wojciech Chmiel
  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/chaikin', ['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/AD/ADIndicator.js', [_modules['Core/Series/SeriesRegistry.js'], _modules['Core/Utilities.js']], function (SeriesRegistry, U) {
  93. /* *
  94. *
  95. * License: www.highcharts.com/license
  96. *
  97. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  98. * */
  99. var __extends = (this && this.__extends) || (function () {
  100. var extendStatics = function (d,
  101. b) {
  102. extendStatics = Object.setPrototypeOf ||
  103. ({ __proto__: [] } instanceof Array && function (d,
  104. b) { d.__proto__ = b; }) ||
  105. function (d,
  106. b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  107. return extendStatics(d, b);
  108. };
  109. return function (d, b) {
  110. extendStatics(d, b);
  111. function __() { this.constructor = d; }
  112. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  113. };
  114. })();
  115. var SMAIndicator = SeriesRegistry.seriesTypes.sma;
  116. var error = U.error,
  117. extend = U.extend,
  118. merge = U.merge;
  119. /**
  120. * The AD series type.
  121. *
  122. * @private
  123. * @class
  124. * @name Highcharts.seriesTypes.ad
  125. *
  126. * @augments Highcharts.Series
  127. */
  128. var ADIndicator = /** @class */ (function (_super) {
  129. __extends(ADIndicator, _super);
  130. function ADIndicator() {
  131. /* *
  132. *
  133. * Static Properties
  134. *
  135. * */
  136. var _this = _super !== null && _super.apply(this,
  137. arguments) || this;
  138. /* *
  139. *
  140. * Properties
  141. *
  142. * */
  143. _this.data = void 0;
  144. _this.options = void 0;
  145. _this.points = void 0;
  146. return _this;
  147. }
  148. /* *
  149. *
  150. * Static Functions
  151. *
  152. * */
  153. ADIndicator.populateAverage = function (xVal, yVal, yValVolume, i, _period) {
  154. var high = yVal[i][1],
  155. low = yVal[i][2],
  156. close = yVal[i][3],
  157. volume = yValVolume[i],
  158. adY = close === high && close === low || high === low ?
  159. 0 :
  160. ((2 * close - low - high) / (high - low)) * volume,
  161. adX = xVal[i];
  162. return [adX, adY];
  163. };
  164. /* *
  165. *
  166. * Functions
  167. *
  168. * */
  169. ADIndicator.prototype.getValues = function (series, params) {
  170. var period = params.period,
  171. xVal = series.xData,
  172. yVal = series.yData,
  173. volumeSeriesID = params.volumeSeriesID,
  174. volumeSeries = series.chart.get(volumeSeriesID),
  175. yValVolume = volumeSeries && volumeSeries.yData,
  176. yValLen = yVal ? yVal.length : 0,
  177. AD = [],
  178. xData = [],
  179. yData = [],
  180. len,
  181. i,
  182. ADPoint;
  183. if (xVal.length <= period &&
  184. yValLen &&
  185. yVal[0].length !== 4) {
  186. return;
  187. }
  188. if (!volumeSeries) {
  189. error('Series ' +
  190. volumeSeriesID +
  191. ' not found! Check `volumeSeriesID`.', true, series.chart);
  192. return;
  193. }
  194. // i = period <-- skip first N-points
  195. // Calculate value one-by-one for each period in visible data
  196. for (i = period; i < yValLen; i++) {
  197. len = AD.length;
  198. ADPoint = ADIndicator.populateAverage(xVal, yVal, yValVolume, i, period);
  199. if (len > 0) {
  200. ADPoint[1] += AD[len - 1][1];
  201. }
  202. AD.push(ADPoint);
  203. xData.push(ADPoint[0]);
  204. yData.push(ADPoint[1]);
  205. }
  206. return {
  207. values: AD,
  208. xData: xData,
  209. yData: yData
  210. };
  211. };
  212. /**
  213. * Accumulation Distribution (AD). This series requires `linkedTo` option to
  214. * be set.
  215. *
  216. * @sample stock/indicators/accumulation-distribution
  217. * Accumulation/Distribution indicator
  218. *
  219. * @extends plotOptions.sma
  220. * @since 6.0.0
  221. * @product highstock
  222. * @requires stock/indicators/indicators
  223. * @requires stock/indicators/accumulation-distribution
  224. * @optionparent plotOptions.ad
  225. */
  226. ADIndicator.defaultOptions = merge(SMAIndicator.defaultOptions, {
  227. params: {
  228. /**
  229. * The id of volume series which is mandatory.
  230. * For example using OHLC data, volumeSeriesID='volume' means
  231. * the indicator will be calculated using OHLC and volume values.
  232. *
  233. * @since 6.0.0
  234. */
  235. volumeSeriesID: 'volume'
  236. }
  237. });
  238. return ADIndicator;
  239. }(SMAIndicator));
  240. extend(ADIndicator.prototype, {
  241. nameComponents: false,
  242. nameBase: 'Accumulation/Distribution'
  243. });
  244. SeriesRegistry.registerSeriesType('ad', ADIndicator);
  245. /* *
  246. *
  247. * Default Export
  248. *
  249. * */
  250. /* *
  251. *
  252. * API Options
  253. *
  254. * */
  255. /**
  256. * A `AD` series. If the [type](#series.ad.type) option is not
  257. * specified, it is inherited from [chart.type](#chart.type).
  258. *
  259. * @extends series,plotOptions.ad
  260. * @since 6.0.0
  261. * @excluding dataParser, dataURL
  262. * @product highstock
  263. * @requires stock/indicators/indicators
  264. * @requires stock/indicators/accumulation-distribution
  265. * @apioption series.ad
  266. */
  267. ''; // add doclet above to transpiled file
  268. return ADIndicator;
  269. });
  270. _registerModule(_modules, 'Stock/Indicators/Chaikin/ChaikinIndicator.js', [_modules['Mixins/IndicatorRequired.js'], _modules['Core/Series/SeriesRegistry.js'], _modules['Core/Utilities.js']], function (RequiredIndicatorMixin, SeriesRegistry, U) {
  271. /* *
  272. *
  273. * License: www.highcharts.com/license
  274. *
  275. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  276. *
  277. * */
  278. var __extends = (this && this.__extends) || (function () {
  279. var extendStatics = function (d,
  280. b) {
  281. extendStatics = Object.setPrototypeOf ||
  282. ({ __proto__: [] } instanceof Array && function (d,
  283. b) { d.__proto__ = b; }) ||
  284. function (d,
  285. b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  286. return extendStatics(d, b);
  287. };
  288. return function (d, b) {
  289. extendStatics(d, b);
  290. function __() { this.constructor = d; }
  291. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  292. };
  293. })();
  294. var _a = SeriesRegistry.seriesTypes,
  295. AD = _a.ad,
  296. EMAIndicator = _a.ema;
  297. var correctFloat = U.correctFloat,
  298. extend = U.extend,
  299. merge = U.merge,
  300. error = U.error;
  301. /* *
  302. *
  303. * Class
  304. *
  305. * */
  306. /**
  307. * The Chaikin series type.
  308. *
  309. * @private
  310. * @class
  311. * @name Highcharts.seriesTypes.chaikin
  312. *
  313. * @augments Highcharts.Series
  314. */
  315. var ChaikinIndicator = /** @class */ (function (_super) {
  316. __extends(ChaikinIndicator, _super);
  317. function ChaikinIndicator() {
  318. var _this = _super !== null && _super.apply(this,
  319. arguments) || this;
  320. /* *
  321. *
  322. * Properties
  323. *
  324. * */
  325. _this.data = void 0;
  326. _this.options = void 0;
  327. _this.points = void 0;
  328. return _this;
  329. }
  330. /* *
  331. *
  332. * Functions
  333. *
  334. * */
  335. ChaikinIndicator.prototype.init = function () {
  336. var args = arguments,
  337. ctx = this;
  338. RequiredIndicatorMixin.isParentLoaded(EMAIndicator, 'ema', ctx.type, function (indicator) {
  339. indicator.prototype.init.apply(ctx, args);
  340. return;
  341. });
  342. };
  343. ChaikinIndicator.prototype.getValues = function (series, params) {
  344. var periods = params.periods,
  345. period = params.period,
  346. // Accumulation Distribution Line data
  347. ADL,
  348. // 0- date, 1- Chaikin Oscillator
  349. CHA = [],
  350. xData = [],
  351. yData = [],
  352. periodsOffset,
  353. // Shorter Period EMA
  354. SPE,
  355. // Longer Period EMA
  356. LPE,
  357. oscillator,
  358. i;
  359. // Check if periods are correct
  360. if (periods.length !== 2 || periods[1] <= periods[0]) {
  361. error('Error: "Chaikin requires two periods. Notice, first ' +
  362. 'period should be lower than the second one."');
  363. return;
  364. }
  365. ADL = AD.prototype.getValues.call(this, series, {
  366. volumeSeriesID: params.volumeSeriesID,
  367. period: period
  368. });
  369. // Check if adl is calculated properly, if not skip
  370. if (!ADL) {
  371. return;
  372. }
  373. SPE = EMAIndicator.prototype.getValues.call(this, ADL, {
  374. period: periods[0]
  375. });
  376. LPE = EMAIndicator.prototype.getValues.call(this, ADL, {
  377. period: periods[1]
  378. });
  379. // Check if ema is calculated properly, if not skip
  380. if (!SPE || !LPE) {
  381. return;
  382. }
  383. periodsOffset = periods[1] - periods[0];
  384. for (i = 0; i < LPE.yData.length; i++) {
  385. oscillator = correctFloat(SPE.yData[i + periodsOffset] -
  386. LPE.yData[i]);
  387. CHA.push([LPE.xData[i], oscillator]);
  388. xData.push(LPE.xData[i]);
  389. yData.push(oscillator);
  390. }
  391. return {
  392. values: CHA,
  393. xData: xData,
  394. yData: yData
  395. };
  396. };
  397. /**
  398. * Chaikin Oscillator. This series requires the `linkedTo` option to
  399. * be set and should be loaded after the `stock/indicators/indicators.js`
  400. * and `stock/indicators/ema.js`.
  401. *
  402. * @sample {highstock} stock/indicators/chaikin
  403. * Chaikin Oscillator
  404. *
  405. * @extends plotOptions.ema
  406. * @since 7.0.0
  407. * @product highstock
  408. * @excluding allAreas, colorAxis, joinBy, keys, navigatorOptions,
  409. * pointInterval, pointIntervalUnit, pointPlacement,
  410. * pointRange, pointStart, showInNavigator, stacking
  411. * @requires stock/indicators/indicators
  412. * @requires stock/indicators/ema
  413. * @requires stock/indicators/chaikin
  414. * @optionparent plotOptions.chaikin
  415. */
  416. ChaikinIndicator.defaultOptions = merge(EMAIndicator.defaultOptions, {
  417. /**
  418. * Paramters used in calculation of Chaikin Oscillator
  419. * series points.
  420. *
  421. * @excluding index, period
  422. */
  423. params: {
  424. /**
  425. * The id of volume series which is mandatory.
  426. * For example using OHLC data, volumeSeriesID='volume' means
  427. * the indicator will be calculated using OHLC and volume values.
  428. */
  429. volumeSeriesID: 'volume',
  430. /**
  431. * Periods for Chaikin Oscillator calculations.
  432. *
  433. * @type {Array<number>}
  434. * @default [3, 10]
  435. */
  436. periods: [3, 10]
  437. }
  438. });
  439. return ChaikinIndicator;
  440. }(EMAIndicator));
  441. extend(ChaikinIndicator.prototype, {
  442. nameBase: 'Chaikin Osc',
  443. nameComponents: ['periods']
  444. });
  445. SeriesRegistry.registerSeriesType('chaikin', ChaikinIndicator);
  446. /* *
  447. *
  448. * Default Export
  449. *
  450. * */
  451. /**
  452. * A `Chaikin Oscillator` series. If the [type](#series.chaikin.type)
  453. * option is not specified, it is inherited from [chart.type](#chart.type).
  454. *
  455. * @extends series,plotOptions.chaikin
  456. * @since 7.0.0
  457. * @product highstock
  458. * @excluding allAreas, colorAxis, dataParser, dataURL, joinBy, keys,
  459. * navigatorOptions, pointInterval, pointIntervalUnit,
  460. * pointPlacement, pointRange, pointStart, stacking, showInNavigator
  461. * @requires stock/indicators/indicators
  462. * @requires stock/indicators/ema
  463. * @requires stock/indicators/chaikin
  464. * @apioption series.chaikin
  465. */
  466. ''; // to include the above in the js output
  467. return ChaikinIndicator;
  468. });
  469. _registerModule(_modules, 'masters/indicators/chaikin.src.js', [], function () {
  470. });
  471. }));