cci.src.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /* *
  2. *
  3. * License: www.highcharts.com/license
  4. *
  5. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  6. * */
  7. 'use strict';
  8. import H from '../parts/Globals.js';
  9. import U from '../parts/Utilities.js';
  10. var isArray = U.isArray;
  11. var seriesType = H.seriesType;
  12. /* eslint-disable valid-jsdoc */
  13. // Utils:
  14. /**
  15. * @private
  16. */
  17. function sumArray(array) {
  18. return array.reduce(function (prev, cur) {
  19. return prev + cur;
  20. }, 0);
  21. }
  22. /**
  23. * @private
  24. */
  25. function meanDeviation(arr, sma) {
  26. var len = arr.length, sum = 0, i;
  27. for (i = 0; i < len; i++) {
  28. sum += Math.abs(sma - (arr[i]));
  29. }
  30. return sum;
  31. }
  32. /* eslint-enable valid-jsdoc */
  33. /**
  34. * The CCI series type.
  35. *
  36. * @private
  37. * @class
  38. * @name Highcharts.seriesTypes.cci
  39. *
  40. * @augments Highcharts.Series
  41. */
  42. seriesType('cci', 'sma',
  43. /**
  44. * Commodity Channel Index (CCI). This series requires `linkedTo` option to
  45. * be set.
  46. *
  47. * @sample stock/indicators/cci
  48. * CCI indicator
  49. *
  50. * @extends plotOptions.sma
  51. * @since 6.0.0
  52. * @product highstock
  53. * @requires stock/indicators/indicators
  54. * @requires stock/indicators/cci
  55. * @optionparent plotOptions.cci
  56. */
  57. {
  58. params: {
  59. period: 14
  60. }
  61. },
  62. /**
  63. * @lends Highcharts.Series#
  64. */
  65. {
  66. getValues: function (series, params) {
  67. var period = params.period, xVal = series.xData, yVal = series.yData, yValLen = yVal ? yVal.length : 0, TP = [], periodTP = [], range = 1, CCI = [], xData = [], yData = [], CCIPoint, p, len, smaTP, TPtemp, meanDev, i;
  68. // CCI requires close value
  69. if (xVal.length <= period ||
  70. !isArray(yVal[0]) ||
  71. yVal[0].length !== 4) {
  72. return;
  73. }
  74. // accumulate first N-points
  75. while (range < period) {
  76. p = yVal[range - 1];
  77. TP.push((p[1] + p[2] + p[3]) / 3);
  78. range++;
  79. }
  80. for (i = period; i <= yValLen; i++) {
  81. p = yVal[i - 1];
  82. TPtemp = (p[1] + p[2] + p[3]) / 3;
  83. len = TP.push(TPtemp);
  84. periodTP = TP.slice(len - period);
  85. smaTP = sumArray(periodTP) / period;
  86. meanDev = meanDeviation(periodTP, smaTP) / period;
  87. CCIPoint = ((TPtemp - smaTP) / (0.015 * meanDev));
  88. CCI.push([xVal[i - 1], CCIPoint]);
  89. xData.push(xVal[i - 1]);
  90. yData.push(CCIPoint);
  91. }
  92. return {
  93. values: CCI,
  94. xData: xData,
  95. yData: yData
  96. };
  97. }
  98. });
  99. /**
  100. * A `CCI` series. If the [type](#series.cci.type) option is not
  101. * specified, it is inherited from [chart.type](#chart.type).
  102. *
  103. * @extends series,plotOptions.cci
  104. * @since 6.0.0
  105. * @excluding dataParser, dataURL
  106. * @product highstock
  107. * @requires stock/indicators/indicators
  108. * @requires stock/indicators/cci
  109. * @apioption series.cci
  110. */
  111. ''; // to include the above in the js output