CCIIndicator.js 2.8 KB

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