zigzag.src.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /**
  2. * @license Highstock JS v8.1.2 (2020-06-16)
  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, 'indicators/zigzag.src.js', [_modules['parts/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, highIndex = params.highIndex, deviation = params.deviation / 100, deviations = {
  107. 'low': 1 + deviation,
  108. 'high': 1 - deviation
  109. }, xVal = series.xData, yVal = series.yData, yValLen = yVal ? yVal.length : 0, zigzag = [], xData = [], yData = [], i, j, zigzagPoint, firstZigzagLow, firstZigzagHigh, directionUp, zigzagLen, exitLoop = false, yIndex = false;
  110. // Exit if not enught points or no low or high values
  111. if (!xVal || xVal.length <= 1 ||
  112. (yValLen &&
  113. (yVal[0][lowIndex] === UNDEFINED ||
  114. yVal[0][highIndex] === UNDEFINED))) {
  115. return;
  116. }
  117. // Set first zigzag point candidate
  118. firstZigzagLow = yVal[0][lowIndex];
  119. firstZigzagHigh = yVal[0][highIndex];
  120. // Search for a second zigzag point candidate,
  121. // this will also set first zigzag point
  122. for (i = 1; i < yValLen; i++) {
  123. // requried change to go down
  124. if (yVal[i][lowIndex] <= firstZigzagHigh * deviations.high) {
  125. zigzag.push([xVal[0], firstZigzagHigh]);
  126. // second zigzag point candidate
  127. zigzagPoint = [xVal[i], yVal[i][lowIndex]];
  128. // next line will be going up
  129. directionUp = true;
  130. exitLoop = true;
  131. // requried change to go up
  132. }
  133. else if (yVal[i][highIndex] >= firstZigzagLow * deviations.low) {
  134. zigzag.push([xVal[0], firstZigzagLow]);
  135. // second zigzag point candidate
  136. zigzagPoint = [xVal[i], yVal[i][highIndex]];
  137. // next line will be going down
  138. directionUp = false;
  139. exitLoop = true;
  140. }
  141. if (exitLoop) {
  142. xData.push(zigzag[0][0]);
  143. yData.push(zigzag[0][1]);
  144. j = i++;
  145. i = yValLen;
  146. }
  147. }
  148. // Search for next zigzags
  149. for (i = j; i < yValLen; i++) {
  150. if (directionUp) { // next line up
  151. // lower when going down -> change zigzag candidate
  152. if (yVal[i][lowIndex] <= zigzagPoint[1]) {
  153. zigzagPoint = [xVal[i], yVal[i][lowIndex]];
  154. }
  155. // requried change to go down -> new zigzagpoint and
  156. // direction change
  157. if (yVal[i][highIndex] >=
  158. zigzagPoint[1] * deviations.low) {
  159. yIndex = highIndex;
  160. }
  161. }
  162. else { // next line down
  163. // higher when going up -> change zigzag candidate
  164. if (yVal[i][highIndex] >= zigzagPoint[1]) {
  165. zigzagPoint = [xVal[i], yVal[i][highIndex]];
  166. }
  167. // requried change to go down -> new zigzagpoint and
  168. // direction change
  169. if (yVal[i][lowIndex] <=
  170. zigzagPoint[1] * deviations.high) {
  171. yIndex = lowIndex;
  172. }
  173. }
  174. if (yIndex !== false) { // new zigzag point and direction change
  175. zigzag.push(zigzagPoint);
  176. xData.push(zigzagPoint[0]);
  177. yData.push(zigzagPoint[1]);
  178. zigzagPoint = [xVal[i], yVal[i][yIndex]];
  179. directionUp = !directionUp;
  180. yIndex = false;
  181. }
  182. }
  183. zigzagLen = zigzag.length;
  184. // no zigzag for last point
  185. if (zigzagLen !== 0 &&
  186. zigzag[zigzagLen - 1][0] < xVal[yValLen - 1]) {
  187. // set last point from zigzag candidate
  188. zigzag.push(zigzagPoint);
  189. xData.push(zigzagPoint[0]);
  190. yData.push(zigzagPoint[1]);
  191. }
  192. return {
  193. values: zigzag,
  194. xData: xData,
  195. yData: yData
  196. };
  197. }
  198. });
  199. /**
  200. * A `Zig Zag` series. If the [type](#series.zigzag.type) option is not
  201. * specified, it is inherited from [chart.type](#chart.type).
  202. *
  203. * @extends series,plotOptions.zigzag
  204. * @since 6.0.0
  205. * @product highstock
  206. * @excluding dataParser, dataURL
  207. * @requires stock/indicators/indicators
  208. * @requires stock/indicators/zigzag
  209. * @apioption series.zigzag
  210. */
  211. ''; // adds doclets above to transpiled file
  212. });
  213. _registerModule(_modules, 'masters/indicators/zigzag.src.js', [], function () {
  214. });
  215. }));