ppo.src.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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/ppo', ['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/indicator-required.js', [_modules['parts/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, else throw error.
  46. * @private
  47. * @param {Highcharts.Indicator} indicator
  48. * Indicator constructor function.
  49. * @param {string} requiredIndicator
  50. * Required indicator type.
  51. * @param {string} type
  52. * Type of indicator where function was called (parent).
  53. * @param {Highcharts.IndicatorCallbackFunction} callback
  54. * Callback which is triggered if the given indicator is loaded.
  55. * Takes indicator as an argument.
  56. * @param {string} errMessage
  57. * Error message that will be logged in console.
  58. * @return {boolean}
  59. * Returns false when there is no required indicator loaded.
  60. */
  61. isParentLoaded: function (indicator, requiredIndicator, type, callback, errMessage) {
  62. if (indicator) {
  63. return callback ? callback(indicator) : true;
  64. }
  65. error(errMessage || this.generateMessage(type, requiredIndicator));
  66. return false;
  67. },
  68. /**
  69. * @private
  70. * @param {string} indicatorType
  71. * Indicator type
  72. * @param {string} required
  73. * Required indicator
  74. * @return {string}
  75. * Error message
  76. */
  77. generateMessage: function (indicatorType, required) {
  78. return 'Error: "' + indicatorType +
  79. '" indicator type requires "' + required +
  80. '" indicator loaded before. Please read docs: ' +
  81. 'https://api.highcharts.com/highstock/plotOptions.' +
  82. indicatorType;
  83. }
  84. };
  85. return requiredIndicatorMixin;
  86. });
  87. _registerModule(_modules, 'indicators/ppo.src.js', [_modules['parts/Globals.js'], _modules['parts/Utilities.js'], _modules['mixins/indicator-required.js']], function (H, U, requiredIndicatorMixin) {
  88. /* *
  89. *
  90. * License: www.highcharts.com/license
  91. *
  92. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  93. *
  94. * */
  95. var correctFloat = U.correctFloat, error = U.error, seriesType = U.seriesType;
  96. var EMA = H.seriesTypes.ema, requiredIndicator = requiredIndicatorMixin;
  97. /**
  98. * The PPO series type.
  99. *
  100. * @private
  101. * @class
  102. * @name Highcharts.seriesTypes.ppo
  103. *
  104. * @augments Highcharts.Series
  105. */
  106. seriesType('ppo', 'ema',
  107. /**
  108. * Percentage Price Oscillator. This series requires the
  109. * `linkedTo` option to be set and should be loaded after the
  110. * `stock/indicators/indicators.js` and `stock/indicators/ema.js`.
  111. *
  112. * @sample {highstock} stock/indicators/ppo
  113. * Percentage Price Oscillator
  114. *
  115. * @extends plotOptions.ema
  116. * @since 7.0.0
  117. * @product highstock
  118. * @excluding allAreas, colorAxis, joinBy, keys, navigatorOptions,
  119. * pointInterval, pointIntervalUnit, pointPlacement,
  120. * pointRange, pointStart, showInNavigator, stacking
  121. * @requires stock/indicators/indicators
  122. * @requires stock/indicators/ema
  123. * @requires stock/indicators/ppo
  124. * @optionparent plotOptions.ppo
  125. */
  126. {
  127. /**
  128. * Paramters used in calculation of Percentage Price Oscillator series
  129. * points.
  130. *
  131. * @excluding period
  132. */
  133. params: {
  134. /**
  135. * Periods for Percentage Price Oscillator calculations.
  136. *
  137. * @type {Array<number>}
  138. * @default [12, 26]
  139. */
  140. periods: [12, 26]
  141. }
  142. },
  143. /**
  144. * @lends Highcharts.Series.prototype
  145. */
  146. {
  147. nameBase: 'PPO',
  148. nameComponents: ['periods'],
  149. init: function () {
  150. var args = arguments, ctx = this;
  151. requiredIndicator.isParentLoaded(EMA, 'ema', ctx.type, function (indicator) {
  152. indicator.prototype.init.apply(ctx, args);
  153. return;
  154. });
  155. },
  156. getValues: function (series, params) {
  157. var periods = params.periods, index = params.index,
  158. // 0- date, 1- Percentage Price Oscillator
  159. PPO = [], xData = [], yData = [], periodsOffset,
  160. // Shorter Period EMA
  161. SPE,
  162. // Longer Period EMA
  163. LPE, oscillator, i;
  164. // Check if periods are correct
  165. if (periods.length !== 2 || periods[1] <= periods[0]) {
  166. error('Error: "PPO requires two periods. Notice, first period ' +
  167. 'should be lower than the second one."');
  168. return;
  169. }
  170. SPE = EMA.prototype.getValues.call(this, series, {
  171. index: index,
  172. period: periods[0]
  173. });
  174. LPE = EMA.prototype.getValues.call(this, series, {
  175. index: index,
  176. period: periods[1]
  177. });
  178. // Check if ema is calculated properly, if not skip
  179. if (!SPE || !LPE) {
  180. return;
  181. }
  182. periodsOffset = periods[1] - periods[0];
  183. for (i = 0; i < LPE.yData.length; i++) {
  184. oscillator = correctFloat((SPE.yData[i + periodsOffset] -
  185. LPE.yData[i]) /
  186. LPE.yData[i] *
  187. 100);
  188. PPO.push([LPE.xData[i], oscillator]);
  189. xData.push(LPE.xData[i]);
  190. yData.push(oscillator);
  191. }
  192. return {
  193. values: PPO,
  194. xData: xData,
  195. yData: yData
  196. };
  197. }
  198. });
  199. /**
  200. * A `Percentage Price Oscillator` series. If the [type](#series.ppo.type)
  201. * option is not specified, it is inherited from [chart.type](#chart.type).
  202. *
  203. * @extends series,plotOptions.ppo
  204. * @since 7.0.0
  205. * @product highstock
  206. * @excluding allAreas, colorAxis, dataParser, dataURL, joinBy, keys,
  207. * navigatorOptions, pointInterval, pointIntervalUnit,
  208. * pointPlacement, pointRange, pointStart, showInNavigator, stacking
  209. * @requires stock/indicators/indicators
  210. * @requires stock/indicators/ema
  211. * @requires stock/indicators/ppo
  212. * @apioption series.ppo
  213. */
  214. ''; // to include the above in the js output
  215. });
  216. _registerModule(_modules, 'masters/indicators/ppo.src.js', [], function () {
  217. });
  218. }));