cci.src.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /**
  2. * @license Highstock JS v8.2.0 (2020-08-20)
  3. *
  4. * Indicator series type for Highstock
  5. *
  6. * (c) 2010-2019 Sebastian Bochan
  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/cci', ['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, 'Stock/Indicators/CCIIndicator.js', [_modules['Core/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 isArray = U.isArray,
  39. seriesType = U.seriesType;
  40. /* eslint-disable valid-jsdoc */
  41. // Utils:
  42. /**
  43. * @private
  44. */
  45. function sumArray(array) {
  46. return array.reduce(function (prev, cur) {
  47. return prev + cur;
  48. }, 0);
  49. }
  50. /**
  51. * @private
  52. */
  53. function meanDeviation(arr, sma) {
  54. var len = arr.length,
  55. sum = 0,
  56. i;
  57. for (i = 0; i < len; i++) {
  58. sum += Math.abs(sma - (arr[i]));
  59. }
  60. return sum;
  61. }
  62. /* eslint-enable valid-jsdoc */
  63. /**
  64. * The CCI series type.
  65. *
  66. * @private
  67. * @class
  68. * @name Highcharts.seriesTypes.cci
  69. *
  70. * @augments Highcharts.Series
  71. */
  72. seriesType('cci', 'sma',
  73. /**
  74. * Commodity Channel Index (CCI). This series requires `linkedTo` option to
  75. * be set.
  76. *
  77. * @sample stock/indicators/cci
  78. * CCI indicator
  79. *
  80. * @extends plotOptions.sma
  81. * @since 6.0.0
  82. * @product highstock
  83. * @requires stock/indicators/indicators
  84. * @requires stock/indicators/cci
  85. * @optionparent plotOptions.cci
  86. */
  87. {
  88. params: {
  89. period: 14
  90. }
  91. },
  92. /**
  93. * @lends Highcharts.Series#
  94. */
  95. {
  96. getValues: function (series, params) {
  97. var period = params.period,
  98. xVal = series.xData,
  99. yVal = series.yData,
  100. yValLen = yVal ? yVal.length : 0,
  101. TP = [],
  102. periodTP = [],
  103. range = 1,
  104. CCI = [],
  105. xData = [],
  106. yData = [],
  107. CCIPoint,
  108. p,
  109. len,
  110. smaTP,
  111. TPtemp,
  112. meanDev,
  113. i;
  114. // CCI requires close value
  115. if (xVal.length <= period ||
  116. !isArray(yVal[0]) ||
  117. yVal[0].length !== 4) {
  118. return;
  119. }
  120. // accumulate first N-points
  121. while (range < period) {
  122. p = yVal[range - 1];
  123. TP.push((p[1] + p[2] + p[3]) / 3);
  124. range++;
  125. }
  126. for (i = period; i <= yValLen; i++) {
  127. p = yVal[i - 1];
  128. TPtemp = (p[1] + p[2] + p[3]) / 3;
  129. len = TP.push(TPtemp);
  130. periodTP = TP.slice(len - period);
  131. smaTP = sumArray(periodTP) / period;
  132. meanDev = meanDeviation(periodTP, smaTP) / period;
  133. CCIPoint = ((TPtemp - smaTP) / (0.015 * meanDev));
  134. CCI.push([xVal[i - 1], CCIPoint]);
  135. xData.push(xVal[i - 1]);
  136. yData.push(CCIPoint);
  137. }
  138. return {
  139. values: CCI,
  140. xData: xData,
  141. yData: yData
  142. };
  143. }
  144. });
  145. /**
  146. * A `CCI` series. If the [type](#series.cci.type) option is not
  147. * specified, it is inherited from [chart.type](#chart.type).
  148. *
  149. * @extends series,plotOptions.cci
  150. * @since 6.0.0
  151. * @excluding dataParser, dataURL
  152. * @product highstock
  153. * @requires stock/indicators/indicators
  154. * @requires stock/indicators/cci
  155. * @apioption series.cci
  156. */
  157. ''; // to include the above in the js output
  158. });
  159. _registerModule(_modules, 'masters/indicators/cci.src.js', [], function () {
  160. });
  161. }));