zigzag.src.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /**
  2. * @license Highstock JS v9.1.1 (2021-06-04)
  3. *
  4. * Indicator series type for Highcharts Stock
  5. *
  6. * (c) 2010-2021 Kacper Madej
  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/zigzag', ['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/Zigzag/ZigzagIndicator.js', [_modules['Core/Series/SeriesRegistry.js'], _modules['Core/Utilities.js']], function (SeriesRegistry, U) {
  32. /* *
  33. *
  34. * (c) 2010-2021 Kacper Madej
  35. *
  36. * License: www.highcharts.com/license
  37. *
  38. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  39. *
  40. * */
  41. var __extends = (this && this.__extends) || (function () {
  42. var extendStatics = function (d,
  43. b) {
  44. extendStatics = Object.setPrototypeOf ||
  45. ({ __proto__: [] } instanceof Array && function (d,
  46. b) { d.__proto__ = b; }) ||
  47. function (d,
  48. b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  49. return extendStatics(d, b);
  50. };
  51. return function (d, b) {
  52. extendStatics(d, b);
  53. function __() { this.constructor = d; }
  54. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  55. };
  56. })();
  57. var SMAIndicator = SeriesRegistry.seriesTypes.sma;
  58. var merge = U.merge,
  59. extend = U.extend;
  60. /* *
  61. *
  62. * Class
  63. *
  64. * */
  65. /**
  66. * The Zig Zag series type.
  67. *
  68. * @private
  69. * @class
  70. * @name Highcharts.seriesTypes.zigzag
  71. *
  72. * @augments Highcharts.Series
  73. */
  74. var ZigzagIndicator = /** @class */ (function (_super) {
  75. __extends(ZigzagIndicator, _super);
  76. function ZigzagIndicator() {
  77. var _this = _super !== null && _super.apply(this,
  78. arguments) || this;
  79. /* *
  80. *
  81. * Properties
  82. *
  83. * */
  84. _this.data = void 0;
  85. _this.points = void 0;
  86. _this.options = void 0;
  87. return _this;
  88. }
  89. /* *
  90. *
  91. * Functions
  92. *
  93. * */
  94. ZigzagIndicator.prototype.getValues = function (series, params) {
  95. var lowIndex = params.lowIndex,
  96. highIndex = params.highIndex,
  97. deviation = params.deviation / 100,
  98. deviations = {
  99. 'low': 1 + deviation,
  100. 'high': 1 - deviation
  101. },
  102. xVal = series.xData,
  103. yVal = series.yData,
  104. yValLen = yVal ? yVal.length : 0,
  105. zigzag = [],
  106. xData = [],
  107. yData = [],
  108. i,
  109. j,
  110. zigzagPoint,
  111. firstZigzagLow,
  112. firstZigzagHigh,
  113. directionUp,
  114. zigzagLen,
  115. exitLoop = false,
  116. yIndex = false;
  117. // Exit if not enught points or no low or high values
  118. if (!xVal || xVal.length <= 1 ||
  119. (yValLen &&
  120. (typeof yVal[0][lowIndex] === 'undefined' ||
  121. typeof yVal[0][highIndex] === 'undefined'))) {
  122. return;
  123. }
  124. // Set first zigzag point candidate
  125. firstZigzagLow = yVal[0][lowIndex];
  126. firstZigzagHigh = yVal[0][highIndex];
  127. // Search for a second zigzag point candidate,
  128. // this will also set first zigzag point
  129. for (i = 1; i < yValLen; i++) {
  130. // requried change to go down
  131. if (yVal[i][lowIndex] <= firstZigzagHigh * deviations.high) {
  132. zigzag.push([xVal[0], firstZigzagHigh]);
  133. // second zigzag point candidate
  134. zigzagPoint = [xVal[i], yVal[i][lowIndex]];
  135. // next line will be going up
  136. directionUp = true;
  137. exitLoop = true;
  138. // requried change to go up
  139. }
  140. else if (yVal[i][highIndex] >= firstZigzagLow * deviations.low) {
  141. zigzag.push([xVal[0], firstZigzagLow]);
  142. // second zigzag point candidate
  143. zigzagPoint = [xVal[i], yVal[i][highIndex]];
  144. // next line will be going down
  145. directionUp = false;
  146. exitLoop = true;
  147. }
  148. if (exitLoop) {
  149. xData.push(zigzag[0][0]);
  150. yData.push(zigzag[0][1]);
  151. j = i++;
  152. i = yValLen;
  153. }
  154. }
  155. // Search for next zigzags
  156. for (i = j; i < yValLen; i++) {
  157. if (directionUp) { // next line up
  158. // lower when going down -> change zigzag candidate
  159. if (yVal[i][lowIndex] <= zigzagPoint[1]) {
  160. zigzagPoint = [xVal[i], yVal[i][lowIndex]];
  161. }
  162. // requried change to go down -> new zigzagpoint and
  163. // direction change
  164. if (yVal[i][highIndex] >=
  165. zigzagPoint[1] * deviations.low) {
  166. yIndex = highIndex;
  167. }
  168. }
  169. else { // next line down
  170. // higher when going up -> change zigzag candidate
  171. if (yVal[i][highIndex] >= zigzagPoint[1]) {
  172. zigzagPoint = [xVal[i], yVal[i][highIndex]];
  173. }
  174. // requried change to go down -> new zigzagpoint and
  175. // direction change
  176. if (yVal[i][lowIndex] <=
  177. zigzagPoint[1] * deviations.high) {
  178. yIndex = lowIndex;
  179. }
  180. }
  181. if (yIndex !== false) { // new zigzag point and direction change
  182. zigzag.push(zigzagPoint);
  183. xData.push(zigzagPoint[0]);
  184. yData.push(zigzagPoint[1]);
  185. zigzagPoint = [xVal[i], yVal[i][yIndex]];
  186. directionUp = !directionUp;
  187. yIndex = false;
  188. }
  189. }
  190. zigzagLen = zigzag.length;
  191. // no zigzag for last point
  192. if (zigzagLen !== 0 &&
  193. zigzag[zigzagLen - 1][0] < xVal[yValLen - 1]) {
  194. // set last point from zigzag candidate
  195. zigzag.push(zigzagPoint);
  196. xData.push(zigzagPoint[0]);
  197. yData.push(zigzagPoint[1]);
  198. }
  199. return {
  200. values: zigzag,
  201. xData: xData,
  202. yData: yData
  203. };
  204. };
  205. /**
  206. * Zig Zag indicator.
  207. *
  208. * This series requires `linkedTo` option to be set.
  209. *
  210. * @sample stock/indicators/zigzag
  211. * Zig Zag indicator
  212. *
  213. * @extends plotOptions.sma
  214. * @since 6.0.0
  215. * @product highstock
  216. * @requires stock/indicators/indicators
  217. * @requires stock/indicators/zigzag
  218. * @optionparent plotOptions.zigzag
  219. */
  220. ZigzagIndicator.defaultOptions = merge(SMAIndicator.defaultOptions, {
  221. /**
  222. * @excluding index, period
  223. */
  224. params: {
  225. // Index and period are unchangeable, do not inherit (#15362)
  226. index: void 0,
  227. period: void 0,
  228. /**
  229. * The point index which indicator calculations will base - low
  230. * value.
  231. *
  232. * For example using OHLC data, index=2 means the indicator will be
  233. * calculated using Low values.
  234. */
  235. lowIndex: 2,
  236. /**
  237. * The point index which indicator calculations will base - high
  238. * value.
  239. *
  240. * For example using OHLC data, index=1 means the indicator will be
  241. * calculated using High values.
  242. */
  243. highIndex: 1,
  244. /**
  245. * The threshold for the value change.
  246. *
  247. * For example deviation=1 means the indicator will ignore all price
  248. * movements less than 1%.
  249. */
  250. deviation: 1
  251. }
  252. });
  253. return ZigzagIndicator;
  254. }(SMAIndicator));
  255. extend(ZigzagIndicator.prototype, {
  256. nameComponents: ['deviation'],
  257. nameSuffixes: ['%'],
  258. nameBase: 'Zig Zag'
  259. });
  260. SeriesRegistry.registerSeriesType('zigzag', ZigzagIndicator);
  261. /* *
  262. *
  263. * Default Export
  264. *
  265. * */
  266. /**
  267. * A `Zig Zag` series. If the [type](#series.zigzag.type) option is not
  268. * specified, it is inherited from [chart.type](#chart.type).
  269. *
  270. * @extends series,plotOptions.zigzag
  271. * @since 6.0.0
  272. * @product highstock
  273. * @excluding dataParser, dataURL
  274. * @requires stock/indicators/indicators
  275. * @requires stock/indicators/zigzag
  276. * @apioption series.zigzag
  277. */
  278. ''; // adds doclets above to transpiled file
  279. return ZigzagIndicator;
  280. });
  281. _registerModule(_modules, 'masters/indicators/zigzag.src.js', [], function () {
  282. });
  283. }));