zigzag.src.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /**
  2. * @license Highstock JS v8.2.0 (2020-08-20)
  3. *
  4. * Indicator series type for Highstock
  5. *
  6. * (c) 2010-2019 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/ZigzagIndicator.js', [_modules['Core/Utilities.js']], function (U) {
  32. /* *
  33. *
  34. * (c) 2010-2020 Kacper Madej
  35. *
  36. * License: www.highcharts.com/license
  37. *
  38. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  39. *
  40. * */
  41. var seriesType = U.seriesType;
  42. var UNDEFINED;
  43. /**
  44. * The Zig Zag series type.
  45. *
  46. * @private
  47. * @class
  48. * @name Highcharts.seriesTypes.zigzag
  49. *
  50. * @augments Highcharts.Series
  51. */
  52. seriesType('zigzag', 'sma',
  53. /**
  54. * Zig Zag indicator.
  55. *
  56. * This series requires `linkedTo` option to be set.
  57. *
  58. * @sample stock/indicators/zigzag
  59. * Zig Zag indicator
  60. *
  61. * @extends plotOptions.sma
  62. * @since 6.0.0
  63. * @product highstock
  64. * @requires stock/indicators/indicators
  65. * @requires stock/indicators/zigzag
  66. * @optionparent plotOptions.zigzag
  67. */
  68. {
  69. /**
  70. * @excluding index, period
  71. */
  72. params: {
  73. /**
  74. * The point index which indicator calculations will base - low
  75. * value.
  76. *
  77. * For example using OHLC data, index=2 means the indicator will be
  78. * calculated using Low values.
  79. */
  80. lowIndex: 2,
  81. /**
  82. * The point index which indicator calculations will base - high
  83. * value.
  84. *
  85. * For example using OHLC data, index=1 means the indicator will be
  86. * calculated using High values.
  87. */
  88. highIndex: 1,
  89. /**
  90. * The threshold for the value change.
  91. *
  92. * For example deviation=1 means the indicator will ignore all price
  93. * movements less than 1%.
  94. */
  95. deviation: 1
  96. }
  97. },
  98. /**
  99. * @lends Highcharts.Series#
  100. */
  101. {
  102. nameComponents: ['deviation'],
  103. nameSuffixes: ['%'],
  104. nameBase: 'Zig Zag',
  105. getValues: function (series, params) {
  106. var lowIndex = params.lowIndex,
  107. highIndex = params.highIndex,
  108. deviation = params.deviation / 100,
  109. deviations = {
  110. 'low': 1 + deviation,
  111. 'high': 1 - deviation
  112. },
  113. xVal = series.xData,
  114. yVal = series.yData,
  115. yValLen = yVal ? yVal.length : 0,
  116. zigzag = [],
  117. xData = [],
  118. yData = [],
  119. i,
  120. j,
  121. zigzagPoint,
  122. firstZigzagLow,
  123. firstZigzagHigh,
  124. directionUp,
  125. zigzagLen,
  126. exitLoop = false,
  127. yIndex = false;
  128. // Exit if not enught points or no low or high values
  129. if (!xVal || xVal.length <= 1 ||
  130. (yValLen &&
  131. (yVal[0][lowIndex] === UNDEFINED ||
  132. yVal[0][highIndex] === UNDEFINED))) {
  133. return;
  134. }
  135. // Set first zigzag point candidate
  136. firstZigzagLow = yVal[0][lowIndex];
  137. firstZigzagHigh = yVal[0][highIndex];
  138. // Search for a second zigzag point candidate,
  139. // this will also set first zigzag point
  140. for (i = 1; i < yValLen; i++) {
  141. // requried change to go down
  142. if (yVal[i][lowIndex] <= firstZigzagHigh * deviations.high) {
  143. zigzag.push([xVal[0], firstZigzagHigh]);
  144. // second zigzag point candidate
  145. zigzagPoint = [xVal[i], yVal[i][lowIndex]];
  146. // next line will be going up
  147. directionUp = true;
  148. exitLoop = true;
  149. // requried change to go up
  150. }
  151. else if (yVal[i][highIndex] >= firstZigzagLow * deviations.low) {
  152. zigzag.push([xVal[0], firstZigzagLow]);
  153. // second zigzag point candidate
  154. zigzagPoint = [xVal[i], yVal[i][highIndex]];
  155. // next line will be going down
  156. directionUp = false;
  157. exitLoop = true;
  158. }
  159. if (exitLoop) {
  160. xData.push(zigzag[0][0]);
  161. yData.push(zigzag[0][1]);
  162. j = i++;
  163. i = yValLen;
  164. }
  165. }
  166. // Search for next zigzags
  167. for (i = j; i < yValLen; i++) {
  168. if (directionUp) { // next line up
  169. // lower when going down -> change zigzag candidate
  170. if (yVal[i][lowIndex] <= zigzagPoint[1]) {
  171. zigzagPoint = [xVal[i], yVal[i][lowIndex]];
  172. }
  173. // requried change to go down -> new zigzagpoint and
  174. // direction change
  175. if (yVal[i][highIndex] >=
  176. zigzagPoint[1] * deviations.low) {
  177. yIndex = highIndex;
  178. }
  179. }
  180. else { // next line down
  181. // higher when going up -> change zigzag candidate
  182. if (yVal[i][highIndex] >= zigzagPoint[1]) {
  183. zigzagPoint = [xVal[i], yVal[i][highIndex]];
  184. }
  185. // requried change to go down -> new zigzagpoint and
  186. // direction change
  187. if (yVal[i][lowIndex] <=
  188. zigzagPoint[1] * deviations.high) {
  189. yIndex = lowIndex;
  190. }
  191. }
  192. if (yIndex !== false) { // new zigzag point and direction change
  193. zigzag.push(zigzagPoint);
  194. xData.push(zigzagPoint[0]);
  195. yData.push(zigzagPoint[1]);
  196. zigzagPoint = [xVal[i], yVal[i][yIndex]];
  197. directionUp = !directionUp;
  198. yIndex = false;
  199. }
  200. }
  201. zigzagLen = zigzag.length;
  202. // no zigzag for last point
  203. if (zigzagLen !== 0 &&
  204. zigzag[zigzagLen - 1][0] < xVal[yValLen - 1]) {
  205. // set last point from zigzag candidate
  206. zigzag.push(zigzagPoint);
  207. xData.push(zigzagPoint[0]);
  208. yData.push(zigzagPoint[1]);
  209. }
  210. return {
  211. values: zigzag,
  212. xData: xData,
  213. yData: yData
  214. };
  215. }
  216. });
  217. /**
  218. * A `Zig Zag` series. If the [type](#series.zigzag.type) option is not
  219. * specified, it is inherited from [chart.type](#chart.type).
  220. *
  221. * @extends series,plotOptions.zigzag
  222. * @since 6.0.0
  223. * @product highstock
  224. * @excluding dataParser, dataURL
  225. * @requires stock/indicators/indicators
  226. * @requires stock/indicators/zigzag
  227. * @apioption series.zigzag
  228. */
  229. ''; // adds doclets above to transpiled file
  230. });
  231. _registerModule(_modules, 'masters/indicators/zigzag.src.js', [], function () {
  232. });
  233. }));