cci.src.js 4.7 KB

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