cci.src.js 4.8 KB

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