disparity-index.src.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /**
  2. * @license Highstock JS v9.1.1 (2021-06-04)
  3. *
  4. * Indicator series type for Highstock
  5. *
  6. * (c) 2010-2021 Rafal Sebestjanski
  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/disparity-index', ['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/DisparityIndex/DisparityIndexIndicator.js', [_modules['Mixins/IndicatorRequired.js'], _modules['Core/Series/SeriesRegistry.js'], _modules['Core/Utilities.js']], function (RequiredIndicatorMixin, SeriesRegistry, U) {
  93. /* *
  94. * (c) 2010-2021 Rafal Sebestjanski
  95. *
  96. * Disparity Index technical indicator for Highcharts Stock
  97. *
  98. * License: www.highcharts.com/license
  99. *
  100. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  101. *
  102. * */
  103. var __extends = (this && this.__extends) || (function () {
  104. var extendStatics = function (d,
  105. b) {
  106. extendStatics = Object.setPrototypeOf ||
  107. ({ __proto__: [] } instanceof Array && function (d,
  108. b) { d.__proto__ = b; }) ||
  109. function (d,
  110. b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  111. return extendStatics(d, b);
  112. };
  113. return function (d, b) {
  114. extendStatics(d, b);
  115. function __() { this.constructor = d; }
  116. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  117. };
  118. })();
  119. var SMAIndicator = SeriesRegistry.seriesTypes.sma;
  120. var correctFloat = U.correctFloat,
  121. defined = U.defined,
  122. extend = U.extend,
  123. isArray = U.isArray,
  124. merge = U.merge;
  125. /* *
  126. *
  127. * Class
  128. *
  129. * */
  130. /**
  131. * The Disparity Index series type.
  132. *
  133. * @private
  134. * @class
  135. * @name Highcharts.seriesTypes.disparityindex
  136. *
  137. * @augments Highcharts.Series
  138. */
  139. var DisparityIndexIndicator = /** @class */ (function (_super) {
  140. __extends(DisparityIndexIndicator, _super);
  141. function DisparityIndexIndicator() {
  142. var _this = _super !== null && _super.apply(this,
  143. arguments) || this;
  144. /* *
  145. *
  146. * Properties
  147. *
  148. * */
  149. _this.averageIndicator = void 0;
  150. _this.data = void 0;
  151. _this.options = void 0;
  152. _this.points = void 0;
  153. return _this;
  154. }
  155. /* *
  156. *
  157. * Functions
  158. *
  159. * */
  160. DisparityIndexIndicator.prototype.init = function () {
  161. var args = arguments,
  162. ctx = this, // Disparity Index indicator
  163. params = args[1].params, // options.params
  164. averageType = params && params.average ? params.average : void 0;
  165. ctx.averageIndicator =
  166. SeriesRegistry.seriesTypes[averageType] || SMAIndicator;
  167. // Check if the required average indicator modules is loaded
  168. RequiredIndicatorMixin.isParentLoaded(ctx.averageIndicator, averageType, ctx.type, function (indicator) {
  169. indicator.prototype.init.apply(ctx, args);
  170. return;
  171. });
  172. };
  173. DisparityIndexIndicator.prototype.calculateDisparityIndex = function (curPrice, periodAverage) {
  174. return correctFloat(curPrice - periodAverage) / periodAverage * 100;
  175. };
  176. DisparityIndexIndicator.prototype.getValues = function (series, params) {
  177. var index = params.index,
  178. xVal = series.xData,
  179. yVal = series.yData,
  180. yValLen = yVal ? yVal.length : 0,
  181. disparityIndexPoint = [],
  182. xData = [],
  183. yData = [],
  184. // "as any" because getValues doesn't exist on typeof Series
  185. averageIndicator = this.averageIndicator,
  186. isOHLC = isArray(yVal[0]),
  187. // Get the average indicator's values
  188. values = averageIndicator.prototype.getValues(series,
  189. params),
  190. yValues = values.yData,
  191. start = xVal.indexOf(values.xData[0]);
  192. // Check period, if bigger than points length, skip
  193. if (!yValues || yValues.length === 0 ||
  194. !defined(index) ||
  195. yVal.length <= start) {
  196. return;
  197. }
  198. // Get the Disparity Index indicator's values
  199. for (var i = start; i < yValLen; i++) {
  200. var disparityIndexValue = this.calculateDisparityIndex(isOHLC ? yVal[i][index] : yVal[i],
  201. yValues[i - start]);
  202. disparityIndexPoint.push([
  203. xVal[i],
  204. disparityIndexValue
  205. ]);
  206. xData.push(xVal[i]);
  207. yData.push(disparityIndexValue);
  208. }
  209. return {
  210. values: disparityIndexPoint,
  211. xData: xData,
  212. yData: yData
  213. };
  214. };
  215. /**
  216. * Disparity Index.
  217. * This series requires the `linkedTo` option to be set and should
  218. * be loaded after the `stock/indicators/indicators.js` file.
  219. *
  220. * @sample stock/indicators/disparity-index
  221. * Disparity Index indicator
  222. *
  223. * @extends plotOptions.sma
  224. * @since 9.1.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/disparity-index
  231. * @optionparent plotOptions.disparityindex
  232. */
  233. DisparityIndexIndicator.defaultOptions = merge(SMAIndicator.defaultOptions, {
  234. params: {
  235. /**
  236. * The average used to calculate the Disparity Index indicator.
  237. * By default it uses SMA. To use other averages, e.g. EMA,
  238. * the `stock/indicators/ema.js` file needs to be loaded.
  239. *
  240. * If value is different than ema|dema|tema|wma, then sma is used.
  241. */
  242. average: 'sma',
  243. index: 3
  244. },
  245. marker: {
  246. enabled: false
  247. },
  248. dataGrouping: {
  249. approximation: 'averages'
  250. }
  251. });
  252. return DisparityIndexIndicator;
  253. }(SMAIndicator));
  254. extend(DisparityIndexIndicator.prototype, {
  255. nameBase: 'Disparity Index',
  256. nameComponents: ['period', 'average']
  257. });
  258. SeriesRegistry.registerSeriesType('disparityindex', DisparityIndexIndicator);
  259. /* *
  260. *
  261. * Default Export
  262. *
  263. * */
  264. /**
  265. * The Disparity Index indicator series.
  266. * If the [type](#series.disparityindex.type) option is not
  267. * specified, it is inherited from [chart.type](#chart.type).
  268. *
  269. * @extends series,plotOptions.disparityindex
  270. * @since 9.1.0
  271. * @product highstock
  272. * @excluding allAreas, colorAxis, dataParser, dataURL, joinBy, keys,
  273. * navigatorOptions, pointInterval, pointIntervalUnit,
  274. * pointPlacement, pointRange, pointStart, showInNavigator, stacking
  275. * @requires stock/indicators/indicators
  276. * @requires stock/indicators/disparity-index
  277. * @apioption series.disparityindex
  278. */
  279. ''; // to include the above in the js output
  280. return DisparityIndexIndicator;
  281. });
  282. _registerModule(_modules, 'masters/indicators/disparity-index.src.js', [], function () {
  283. });
  284. }));