cmo.src.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /**
  2. * @license Highstock JS v9.1.1 (2021-06-04)
  3. *
  4. * Indicator series type for Highcharts Stock
  5. *
  6. * (c) 2010-2021 Pawel Lysy
  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/cmo', ['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/CMO/CMOIndicator.js', [_modules['Core/Series/SeriesRegistry.js'], _modules['Core/Utilities.js']], function (SeriesRegistry, U) {
  32. /* *
  33. *
  34. * License: www.highcharts.com/license
  35. *
  36. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  37. *
  38. * */
  39. var __extends = (this && this.__extends) || (function () {
  40. var extendStatics = function (d,
  41. b) {
  42. extendStatics = Object.setPrototypeOf ||
  43. ({ __proto__: [] } instanceof Array && function (d,
  44. b) { d.__proto__ = b; }) ||
  45. function (d,
  46. b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  47. return extendStatics(d, b);
  48. };
  49. return function (d, b) {
  50. extendStatics(d, b);
  51. function __() { this.constructor = d; }
  52. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  53. };
  54. })();
  55. var SMAIndicator = SeriesRegistry.seriesTypes.sma;
  56. var isNumber = U.isNumber,
  57. merge = U.merge;
  58. /* eslint-enable require-jsdoc */
  59. /**
  60. * The CMO series type.
  61. *
  62. * @private
  63. * @class
  64. * @name Highcharts.seriesTypes.cmo
  65. *
  66. * @augments Highcharts.Series
  67. */
  68. var CMOIndicator = /** @class */ (function (_super) {
  69. __extends(CMOIndicator, _super);
  70. function CMOIndicator() {
  71. var _this = _super !== null && _super.apply(this,
  72. arguments) || this;
  73. /* *
  74. *
  75. * Properties
  76. *
  77. * */
  78. _this.data = void 0;
  79. _this.options = void 0;
  80. _this.points = void 0;
  81. return _this;
  82. }
  83. /* *
  84. *
  85. * Functions
  86. *
  87. * */
  88. CMOIndicator.prototype.getValues = function (series, params) {
  89. var period = params.period,
  90. xVal = series.xData,
  91. yVal = series.yData,
  92. yValLen = yVal ? yVal.length : 0,
  93. CMO = [],
  94. xData = [],
  95. yData = [];
  96. var i,
  97. index = params.index,
  98. values;
  99. if (xVal.length < period) {
  100. return;
  101. }
  102. if (isNumber(yVal[0])) {
  103. values = yVal;
  104. }
  105. else {
  106. // In case of the situation, where the series type has data length
  107. // shorter then 4 (HLC, range), this ensures that we are not trying
  108. // to reach the index out of bounds
  109. index = Math.min(index, yVal[0].length - 1);
  110. values = yVal.map(function (value) { return value[index]; });
  111. }
  112. var firstAddedSum = 0,
  113. sumOfHigherValues = 0,
  114. sumOfLowerValues = 0,
  115. y;
  116. // Calculate first point, check if the first value
  117. // was added to sum of higher/lower values, and what was the value.
  118. for (var j = period; j > 0; j--) {
  119. if (values[j] > values[j - 1]) {
  120. sumOfHigherValues += values[j] - values[j - 1];
  121. }
  122. else if (values[j] < values[j - 1]) {
  123. sumOfLowerValues += values[j - 1] - values[j];
  124. }
  125. }
  126. // You might devide by 0 if all values are equal,
  127. // so return 0 in this case.
  128. y =
  129. sumOfHigherValues + sumOfLowerValues > 0 ?
  130. (100 * (sumOfHigherValues - sumOfLowerValues)) /
  131. (sumOfHigherValues + sumOfLowerValues) :
  132. 0;
  133. xData.push(xVal[period]);
  134. yData.push(y);
  135. CMO.push([xVal[period], y]);
  136. for (i = period + 1; i < yValLen; i++) {
  137. firstAddedSum = Math.abs(values[i - period - 1] - values[i - period]);
  138. if (values[i] > values[i - 1]) {
  139. sumOfHigherValues += values[i] - values[i - 1];
  140. }
  141. else if (values[i] < values[i - 1]) {
  142. sumOfLowerValues += values[i - 1] - values[i];
  143. }
  144. // Check, to which sum was the first value added to,
  145. // and substract this value from given sum.
  146. if (values[i - period] > values[i - period - 1]) {
  147. sumOfHigherValues -= firstAddedSum;
  148. }
  149. else {
  150. sumOfLowerValues -= firstAddedSum;
  151. }
  152. // Same as above.
  153. y =
  154. sumOfHigherValues + sumOfLowerValues > 0 ?
  155. (100 * (sumOfHigherValues - sumOfLowerValues)) /
  156. (sumOfHigherValues + sumOfLowerValues) :
  157. 0;
  158. xData.push(xVal[i]);
  159. yData.push(y);
  160. CMO.push([xVal[i], y]);
  161. }
  162. return {
  163. values: CMO,
  164. xData: xData,
  165. yData: yData
  166. };
  167. };
  168. /**
  169. * Chande Momentum Oscilator (CMO) technical indicator. This series
  170. * requires the `linkedTo` option to be set and should be loaded after
  171. * the `stock/indicators/indicators.js` file.
  172. *
  173. * @sample stock/indicators/cmo
  174. * CMO indicator
  175. *
  176. * @extends plotOptions.sma
  177. * @since 9.1.0
  178. * @product highstock
  179. * @requires stock/indicators/indicators
  180. * @requires stock/indicators/cmo
  181. * @optionparent plotOptions.cmo
  182. */
  183. CMOIndicator.defaultOptions = merge(SMAIndicator.defaultOptions, {
  184. params: {
  185. period: 20,
  186. index: 3
  187. }
  188. });
  189. return CMOIndicator;
  190. }(SMAIndicator));
  191. SeriesRegistry.registerSeriesType('cmo', CMOIndicator);
  192. /* *
  193. *
  194. * Default Export
  195. *
  196. * */
  197. /**
  198. * A `CMO` series. If the [type](#series.cmo.type) option is not
  199. * specified, it is inherited from [chart.type](#chart.type).
  200. *
  201. * @extends series,plotOptions.cmo
  202. * @since 9.1.0
  203. * @product highstock
  204. * @excluding dataParser, dataURL
  205. * @requires stock/indicators/indicators
  206. * @requires stock/indicators/cmo
  207. * @apioption series.cmo
  208. */
  209. (''); // to include the above in the js output
  210. return CMOIndicator;
  211. });
  212. _registerModule(_modules, 'masters/indicators/cmo.src.js', [], function () {
  213. });
  214. }));