chaikin.src.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /**
  2. * @license Highstock JS v8.1.2 (2020-06-16)
  3. *
  4. * Indicator series type for Highstock
  5. *
  6. * (c) 2010-2019 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, 'indicators/accumulation-distribution.src.js', [_modules['parts/Utilities.js']], function (U) {
  32. /* *
  33. *
  34. * License: www.highcharts.com/license
  35. *
  36. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  37. * */
  38. var error = U.error, seriesType = U.seriesType;
  39. /* eslint-disable valid-jsdoc */
  40. // Utils:
  41. /**
  42. * @private
  43. */
  44. function populateAverage(xVal, yVal, yValVolume, i) {
  45. var high = yVal[i][1], low = yVal[i][2], close = yVal[i][3], volume = yValVolume[i], adY = close === high && close === low || high === low ?
  46. 0 :
  47. ((2 * close - low - high) / (high - low)) * volume, adX = xVal[i];
  48. return [adX, adY];
  49. }
  50. /* eslint-enable valid-jsdoc */
  51. /**
  52. * The AD series type.
  53. *
  54. * @private
  55. * @class
  56. * @name Highcharts.seriesTypes.ad
  57. *
  58. * @augments Highcharts.Series
  59. */
  60. seriesType('ad', 'sma',
  61. /**
  62. * Accumulation Distribution (AD). This series requires `linkedTo` option to
  63. * be set.
  64. *
  65. * @sample stock/indicators/accumulation-distribution
  66. * Accumulation/Distribution indicator
  67. *
  68. * @extends plotOptions.sma
  69. * @since 6.0.0
  70. * @product highstock
  71. * @requires stock/indicators/indicators
  72. * @requires stock/indicators/accumulation-distribution
  73. * @optionparent plotOptions.ad
  74. */
  75. {
  76. params: {
  77. /**
  78. * The id of volume series which is mandatory.
  79. * For example using OHLC data, volumeSeriesID='volume' means
  80. * the indicator will be calculated using OHLC and volume values.
  81. *
  82. * @since 6.0.0
  83. */
  84. volumeSeriesID: 'volume'
  85. }
  86. },
  87. /**
  88. * @lends Highcharts.Series#
  89. */
  90. {
  91. nameComponents: false,
  92. nameBase: 'Accumulation/Distribution',
  93. getValues: function (series, params) {
  94. var period = params.period, xVal = series.xData, yVal = series.yData, volumeSeriesID = params.volumeSeriesID, volumeSeries = series.chart.get(volumeSeriesID), yValVolume = volumeSeries && volumeSeries.yData, yValLen = yVal ? yVal.length : 0, AD = [], xData = [], yData = [], len, i, ADPoint;
  95. if (xVal.length <= period &&
  96. yValLen &&
  97. yVal[0].length !== 4) {
  98. return;
  99. }
  100. if (!volumeSeries) {
  101. error('Series ' +
  102. volumeSeriesID +
  103. ' not found! Check `volumeSeriesID`.', true, series.chart);
  104. return;
  105. }
  106. // i = period <-- skip first N-points
  107. // Calculate value one-by-one for each period in visible data
  108. for (i = period; i < yValLen; i++) {
  109. len = AD.length;
  110. ADPoint = populateAverage(xVal, yVal, yValVolume, i, period);
  111. if (len > 0) {
  112. ADPoint[1] += AD[len - 1][1];
  113. }
  114. AD.push(ADPoint);
  115. xData.push(ADPoint[0]);
  116. yData.push(ADPoint[1]);
  117. }
  118. return {
  119. values: AD,
  120. xData: xData,
  121. yData: yData
  122. };
  123. }
  124. });
  125. /**
  126. * A `AD` series. If the [type](#series.ad.type) option is not
  127. * specified, it is inherited from [chart.type](#chart.type).
  128. *
  129. * @extends series,plotOptions.ad
  130. * @since 6.0.0
  131. * @excluding dataParser, dataURL
  132. * @product highstock
  133. * @requires stock/indicators/indicators
  134. * @requires stock/indicators/accumulation-distribution
  135. * @apioption series.ad
  136. */
  137. ''; // add doclet above to transpiled file
  138. });
  139. _registerModule(_modules, 'mixins/indicator-required.js', [_modules['parts/Utilities.js']], function (U) {
  140. /**
  141. *
  142. * (c) 2010-2020 Daniel Studencki
  143. *
  144. * License: www.highcharts.com/license
  145. *
  146. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  147. *
  148. * */
  149. var error = U.error;
  150. /* eslint-disable no-invalid-this, valid-jsdoc */
  151. var requiredIndicatorMixin = {
  152. /**
  153. * Check whether given indicator is loaded, else throw error.
  154. * @private
  155. * @param {Highcharts.Indicator} indicator
  156. * Indicator constructor function.
  157. * @param {string} requiredIndicator
  158. * Required indicator type.
  159. * @param {string} type
  160. * Type of indicator where function was called (parent).
  161. * @param {Highcharts.IndicatorCallbackFunction} callback
  162. * Callback which is triggered if the given indicator is loaded.
  163. * Takes indicator as an argument.
  164. * @param {string} errMessage
  165. * Error message that will be logged in console.
  166. * @return {boolean}
  167. * Returns false when there is no required indicator loaded.
  168. */
  169. isParentLoaded: function (indicator, requiredIndicator, type, callback, errMessage) {
  170. if (indicator) {
  171. return callback ? callback(indicator) : true;
  172. }
  173. error(errMessage || this.generateMessage(type, requiredIndicator));
  174. return false;
  175. },
  176. /**
  177. * @private
  178. * @param {string} indicatorType
  179. * Indicator type
  180. * @param {string} required
  181. * Required indicator
  182. * @return {string}
  183. * Error message
  184. */
  185. generateMessage: function (indicatorType, required) {
  186. return 'Error: "' + indicatorType +
  187. '" indicator type requires "' + required +
  188. '" indicator loaded before. Please read docs: ' +
  189. 'https://api.highcharts.com/highstock/plotOptions.' +
  190. indicatorType;
  191. }
  192. };
  193. return requiredIndicatorMixin;
  194. });
  195. _registerModule(_modules, 'indicators/chaikin.src.js', [_modules['parts/Globals.js'], _modules['parts/Utilities.js'], _modules['mixins/indicator-required.js']], function (H, U, requiredIndicatorMixin) {
  196. /* *
  197. *
  198. * License: www.highcharts.com/license
  199. *
  200. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  201. *
  202. * */
  203. var correctFloat = U.correctFloat, error = U.error, seriesType = U.seriesType;
  204. var EMA = H.seriesTypes.ema, AD = H.seriesTypes.ad, requiredIndicator = requiredIndicatorMixin;
  205. /**
  206. * The Chaikin series type.
  207. *
  208. * @private
  209. * @class
  210. * @name Highcharts.seriesTypes.chaikin
  211. *
  212. * @augments Highcharts.Series
  213. */
  214. seriesType('chaikin', 'ema',
  215. /**
  216. * Chaikin Oscillator. This series requires the `linkedTo` option to
  217. * be set and should be loaded after the `stock/indicators/indicators.js`
  218. * and `stock/indicators/ema.js`.
  219. *
  220. * @sample {highstock} stock/indicators/chaikin
  221. * Chaikin Oscillator
  222. *
  223. * @extends plotOptions.ema
  224. * @since 7.0.0
  225. * @product highstock
  226. * @excluding allAreas, colorAxis, joinBy, keys, navigatorOptions,
  227. * pointInterval, pointIntervalUnit, pointPlacement,
  228. * pointRange, pointStart, showInNavigator, stacking
  229. * @requires stock/indicators/indicators
  230. * @requires stock/indicators/ema
  231. * @requires stock/indicators/chaikin
  232. * @optionparent plotOptions.chaikin
  233. */
  234. {
  235. /**
  236. * Paramters used in calculation of Chaikin Oscillator
  237. * series points.
  238. *
  239. * @excluding index, period
  240. */
  241. params: {
  242. /**
  243. * The id of volume series which is mandatory.
  244. * For example using OHLC data, volumeSeriesID='volume' means
  245. * the indicator will be calculated using OHLC and volume values.
  246. */
  247. volumeSeriesID: 'volume',
  248. /**
  249. * Periods for Chaikin Oscillator calculations.
  250. *
  251. * @type {Array<number>}
  252. * @default [3, 10]
  253. */
  254. periods: [3, 10]
  255. }
  256. },
  257. /**
  258. * @lends Highcharts.Series#
  259. */
  260. {
  261. nameBase: 'Chaikin Osc',
  262. nameComponents: ['periods'],
  263. init: function () {
  264. var args = arguments, ctx = this;
  265. requiredIndicator.isParentLoaded(EMA, 'ema', ctx.type, function (indicator) {
  266. indicator.prototype.init.apply(ctx, args);
  267. return;
  268. });
  269. },
  270. getValues: function (series, params) {
  271. var periods = params.periods, period = params.period,
  272. // Accumulation Distribution Line data
  273. ADL,
  274. // 0- date, 1- Chaikin Oscillator
  275. CHA = [], xData = [], yData = [], periodsOffset,
  276. // Shorter Period EMA
  277. SPE,
  278. // Longer Period EMA
  279. LPE, oscillator, i;
  280. // Check if periods are correct
  281. if (periods.length !== 2 || periods[1] <= periods[0]) {
  282. error('Error: "Chaikin requires two periods. Notice, first ' +
  283. 'period should be lower than the second one."');
  284. return;
  285. }
  286. ADL = AD.prototype.getValues.call(this, series, {
  287. volumeSeriesID: params.volumeSeriesID,
  288. period: period
  289. });
  290. // Check if adl is calculated properly, if not skip
  291. if (!ADL) {
  292. return;
  293. }
  294. SPE = EMA.prototype.getValues.call(this, ADL, {
  295. period: periods[0]
  296. });
  297. LPE = EMA.prototype.getValues.call(this, ADL, {
  298. period: periods[1]
  299. });
  300. // Check if ema is calculated properly, if not skip
  301. if (!SPE || !LPE) {
  302. return;
  303. }
  304. periodsOffset = periods[1] - periods[0];
  305. for (i = 0; i < LPE.yData.length; i++) {
  306. oscillator = correctFloat(SPE.yData[i + periodsOffset] -
  307. LPE.yData[i]);
  308. CHA.push([LPE.xData[i], oscillator]);
  309. xData.push(LPE.xData[i]);
  310. yData.push(oscillator);
  311. }
  312. return {
  313. values: CHA,
  314. xData: xData,
  315. yData: yData
  316. };
  317. }
  318. });
  319. /**
  320. * A `Chaikin Oscillator` series. If the [type](#series.chaikin.type)
  321. * option is not specified, it is inherited from [chart.type](#chart.type).
  322. *
  323. * @extends series,plotOptions.chaikin
  324. * @since 7.0.0
  325. * @product highstock
  326. * @excluding allAreas, colorAxis, dataParser, dataURL, joinBy, keys,
  327. * navigatorOptions, pointInterval, pointIntervalUnit,
  328. * pointPlacement, pointRange, pointStart, stacking, showInNavigator
  329. * @requires stock/indicators/indicators
  330. * @requires stock/indicators/ema
  331. * @requires stock/indicators/chaikin
  332. * @apioption series.chaikin
  333. */
  334. ''; // to include the above in the js output
  335. });
  336. _registerModule(_modules, 'masters/indicators/chaikin.src.js', [], function () {
  337. });
  338. }));