chaikin.src.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. /**
  2. * @license Highstock JS v9.1.1 (2021-06-04)
  3. *
  4. * Indicator series type for Highcharts Stock
  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. /**
  228. * @excluding index
  229. */
  230. params: {
  231. index: void 0,
  232. /**
  233. * The id of volume series which is mandatory.
  234. * For example using OHLC data, volumeSeriesID='volume' means
  235. * the indicator will be calculated using OHLC and volume values.
  236. *
  237. * @since 6.0.0
  238. */
  239. volumeSeriesID: 'volume'
  240. }
  241. });
  242. return ADIndicator;
  243. }(SMAIndicator));
  244. extend(ADIndicator.prototype, {
  245. nameComponents: false,
  246. nameBase: 'Accumulation/Distribution'
  247. });
  248. SeriesRegistry.registerSeriesType('ad', ADIndicator);
  249. /* *
  250. *
  251. * Default Export
  252. *
  253. * */
  254. /* *
  255. *
  256. * API Options
  257. *
  258. * */
  259. /**
  260. * A `AD` series. If the [type](#series.ad.type) option is not
  261. * specified, it is inherited from [chart.type](#chart.type).
  262. *
  263. * @extends series,plotOptions.ad
  264. * @since 6.0.0
  265. * @excluding dataParser, dataURL
  266. * @product highstock
  267. * @requires stock/indicators/indicators
  268. * @requires stock/indicators/accumulation-distribution
  269. * @apioption series.ad
  270. */
  271. ''; // add doclet above to transpiled file
  272. return ADIndicator;
  273. });
  274. _registerModule(_modules, 'Stock/Indicators/Chaikin/ChaikinIndicator.js', [_modules['Mixins/IndicatorRequired.js'], _modules['Core/Series/SeriesRegistry.js'], _modules['Core/Utilities.js']], function (RequiredIndicatorMixin, SeriesRegistry, U) {
  275. /* *
  276. *
  277. * License: www.highcharts.com/license
  278. *
  279. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  280. *
  281. * */
  282. var __extends = (this && this.__extends) || (function () {
  283. var extendStatics = function (d,
  284. b) {
  285. extendStatics = Object.setPrototypeOf ||
  286. ({ __proto__: [] } instanceof Array && function (d,
  287. b) { d.__proto__ = b; }) ||
  288. function (d,
  289. b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  290. return extendStatics(d, b);
  291. };
  292. return function (d, b) {
  293. extendStatics(d, b);
  294. function __() { this.constructor = d; }
  295. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  296. };
  297. })();
  298. var _a = SeriesRegistry.seriesTypes,
  299. AD = _a.ad,
  300. EMAIndicator = _a.ema;
  301. var correctFloat = U.correctFloat,
  302. extend = U.extend,
  303. merge = U.merge,
  304. error = U.error;
  305. /* *
  306. *
  307. * Class
  308. *
  309. * */
  310. /**
  311. * The Chaikin series type.
  312. *
  313. * @private
  314. * @class
  315. * @name Highcharts.seriesTypes.chaikin
  316. *
  317. * @augments Highcharts.Series
  318. */
  319. var ChaikinIndicator = /** @class */ (function (_super) {
  320. __extends(ChaikinIndicator, _super);
  321. function ChaikinIndicator() {
  322. var _this = _super !== null && _super.apply(this,
  323. arguments) || this;
  324. /* *
  325. *
  326. * Properties
  327. *
  328. * */
  329. _this.data = void 0;
  330. _this.options = void 0;
  331. _this.points = void 0;
  332. return _this;
  333. }
  334. /* *
  335. *
  336. * Functions
  337. *
  338. * */
  339. ChaikinIndicator.prototype.init = function () {
  340. var args = arguments,
  341. ctx = this;
  342. RequiredIndicatorMixin.isParentLoaded(EMAIndicator, 'ema', ctx.type, function (indicator) {
  343. indicator.prototype.init.apply(ctx, args);
  344. return;
  345. });
  346. };
  347. ChaikinIndicator.prototype.getValues = function (series, params) {
  348. var periods = params.periods,
  349. period = params.period,
  350. // Accumulation Distribution Line data
  351. ADL,
  352. // 0- date, 1- Chaikin Oscillator
  353. CHA = [],
  354. xData = [],
  355. yData = [],
  356. periodsOffset,
  357. // Shorter Period EMA
  358. SPE,
  359. // Longer Period EMA
  360. LPE,
  361. oscillator,
  362. i;
  363. // Check if periods are correct
  364. if (periods.length !== 2 || periods[1] <= periods[0]) {
  365. error('Error: "Chaikin requires two periods. Notice, first ' +
  366. 'period should be lower than the second one."');
  367. return;
  368. }
  369. ADL = AD.prototype.getValues.call(this, series, {
  370. volumeSeriesID: params.volumeSeriesID,
  371. period: period
  372. });
  373. // Check if adl is calculated properly, if not skip
  374. if (!ADL) {
  375. return;
  376. }
  377. SPE = EMAIndicator.prototype.getValues.call(this, ADL, {
  378. period: periods[0]
  379. });
  380. LPE = EMAIndicator.prototype.getValues.call(this, ADL, {
  381. period: periods[1]
  382. });
  383. // Check if ema is calculated properly, if not skip
  384. if (!SPE || !LPE) {
  385. return;
  386. }
  387. periodsOffset = periods[1] - periods[0];
  388. for (i = 0; i < LPE.yData.length; i++) {
  389. oscillator = correctFloat(SPE.yData[i + periodsOffset] -
  390. LPE.yData[i]);
  391. CHA.push([LPE.xData[i], oscillator]);
  392. xData.push(LPE.xData[i]);
  393. yData.push(oscillator);
  394. }
  395. return {
  396. values: CHA,
  397. xData: xData,
  398. yData: yData
  399. };
  400. };
  401. /**
  402. * Chaikin Oscillator. This series requires the `linkedTo` option to
  403. * be set and should be loaded after the `stock/indicators/indicators.js`
  404. * and `stock/indicators/ema.js`.
  405. *
  406. * @sample {highstock} stock/indicators/chaikin
  407. * Chaikin Oscillator
  408. *
  409. * @extends plotOptions.ema
  410. * @since 7.0.0
  411. * @product highstock
  412. * @excluding allAreas, colorAxis, joinBy, keys, navigatorOptions,
  413. * pointInterval, pointIntervalUnit, pointPlacement,
  414. * pointRange, pointStart, showInNavigator, stacking
  415. * @requires stock/indicators/indicators
  416. * @requires stock/indicators/ema
  417. * @requires stock/indicators/chaikin
  418. * @optionparent plotOptions.chaikin
  419. */
  420. ChaikinIndicator.defaultOptions = merge(EMAIndicator.defaultOptions, {
  421. /**
  422. * Paramters used in calculation of Chaikin Oscillator
  423. * series points.
  424. *
  425. * @excluding index
  426. */
  427. params: {
  428. index: void 0,
  429. /**
  430. * The id of volume series which is mandatory.
  431. * For example using OHLC data, volumeSeriesID='volume' means
  432. * the indicator will be calculated using OHLC and volume values.
  433. */
  434. volumeSeriesID: 'volume',
  435. /**
  436. * Parameter used indirectly for calculating the `AD` indicator.
  437. * Decides about the number of data points that are taken
  438. * into account for the indicator calculations.
  439. */
  440. period: 9,
  441. /**
  442. * Periods for Chaikin Oscillator calculations.
  443. *
  444. * @type {Array<number>}
  445. * @default [3, 10]
  446. */
  447. periods: [3, 10]
  448. }
  449. });
  450. return ChaikinIndicator;
  451. }(EMAIndicator));
  452. extend(ChaikinIndicator.prototype, {
  453. nameBase: 'Chaikin Osc',
  454. nameComponents: ['periods']
  455. });
  456. SeriesRegistry.registerSeriesType('chaikin', ChaikinIndicator);
  457. /* *
  458. *
  459. * Default Export
  460. *
  461. * */
  462. /**
  463. * A `Chaikin Oscillator` series. If the [type](#series.chaikin.type)
  464. * option is not specified, it is inherited from [chart.type](#chart.type).
  465. *
  466. * @extends series,plotOptions.chaikin
  467. * @since 7.0.0
  468. * @product highstock
  469. * @excluding allAreas, colorAxis, dataParser, dataURL, joinBy, keys,
  470. * navigatorOptions, pointInterval, pointIntervalUnit,
  471. * pointPlacement, pointRange, pointStart, stacking, showInNavigator
  472. * @requires stock/indicators/indicators
  473. * @requires stock/indicators/ema
  474. * @requires stock/indicators/chaikin
  475. * @apioption series.chaikin
  476. */
  477. ''; // to include the above in the js output
  478. return ChaikinIndicator;
  479. });
  480. _registerModule(_modules, 'masters/indicators/chaikin.src.js', [], function () {
  481. });
  482. }));