ZigzagIndicator.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /* *
  2. *
  3. * (c) 2010-2020 Kacper Madej
  4. *
  5. * License: www.highcharts.com/license
  6. *
  7. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  8. *
  9. * */
  10. 'use strict';
  11. import U from '../../Core/Utilities.js';
  12. var seriesType = U.seriesType;
  13. var UNDEFINED;
  14. /**
  15. * The Zig Zag series type.
  16. *
  17. * @private
  18. * @class
  19. * @name Highcharts.seriesTypes.zigzag
  20. *
  21. * @augments Highcharts.Series
  22. */
  23. seriesType('zigzag', 'sma',
  24. /**
  25. * Zig Zag indicator.
  26. *
  27. * This series requires `linkedTo` option to be set.
  28. *
  29. * @sample stock/indicators/zigzag
  30. * Zig Zag indicator
  31. *
  32. * @extends plotOptions.sma
  33. * @since 6.0.0
  34. * @product highstock
  35. * @requires stock/indicators/indicators
  36. * @requires stock/indicators/zigzag
  37. * @optionparent plotOptions.zigzag
  38. */
  39. {
  40. /**
  41. * @excluding index, period
  42. */
  43. params: {
  44. /**
  45. * The point index which indicator calculations will base - low
  46. * value.
  47. *
  48. * For example using OHLC data, index=2 means the indicator will be
  49. * calculated using Low values.
  50. */
  51. lowIndex: 2,
  52. /**
  53. * The point index which indicator calculations will base - high
  54. * value.
  55. *
  56. * For example using OHLC data, index=1 means the indicator will be
  57. * calculated using High values.
  58. */
  59. highIndex: 1,
  60. /**
  61. * The threshold for the value change.
  62. *
  63. * For example deviation=1 means the indicator will ignore all price
  64. * movements less than 1%.
  65. */
  66. deviation: 1
  67. }
  68. },
  69. /**
  70. * @lends Highcharts.Series#
  71. */
  72. {
  73. nameComponents: ['deviation'],
  74. nameSuffixes: ['%'],
  75. nameBase: 'Zig Zag',
  76. getValues: function (series, params) {
  77. var lowIndex = params.lowIndex, highIndex = params.highIndex, deviation = params.deviation / 100, deviations = {
  78. 'low': 1 + deviation,
  79. 'high': 1 - deviation
  80. }, 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;
  81. // Exit if not enught points or no low or high values
  82. if (!xVal || xVal.length <= 1 ||
  83. (yValLen &&
  84. (yVal[0][lowIndex] === UNDEFINED ||
  85. yVal[0][highIndex] === UNDEFINED))) {
  86. return;
  87. }
  88. // Set first zigzag point candidate
  89. firstZigzagLow = yVal[0][lowIndex];
  90. firstZigzagHigh = yVal[0][highIndex];
  91. // Search for a second zigzag point candidate,
  92. // this will also set first zigzag point
  93. for (i = 1; i < yValLen; i++) {
  94. // requried change to go down
  95. if (yVal[i][lowIndex] <= firstZigzagHigh * deviations.high) {
  96. zigzag.push([xVal[0], firstZigzagHigh]);
  97. // second zigzag point candidate
  98. zigzagPoint = [xVal[i], yVal[i][lowIndex]];
  99. // next line will be going up
  100. directionUp = true;
  101. exitLoop = true;
  102. // requried change to go up
  103. }
  104. else if (yVal[i][highIndex] >= firstZigzagLow * deviations.low) {
  105. zigzag.push([xVal[0], firstZigzagLow]);
  106. // second zigzag point candidate
  107. zigzagPoint = [xVal[i], yVal[i][highIndex]];
  108. // next line will be going down
  109. directionUp = false;
  110. exitLoop = true;
  111. }
  112. if (exitLoop) {
  113. xData.push(zigzag[0][0]);
  114. yData.push(zigzag[0][1]);
  115. j = i++;
  116. i = yValLen;
  117. }
  118. }
  119. // Search for next zigzags
  120. for (i = j; i < yValLen; i++) {
  121. if (directionUp) { // next line up
  122. // lower when going down -> change zigzag candidate
  123. if (yVal[i][lowIndex] <= zigzagPoint[1]) {
  124. zigzagPoint = [xVal[i], yVal[i][lowIndex]];
  125. }
  126. // requried change to go down -> new zigzagpoint and
  127. // direction change
  128. if (yVal[i][highIndex] >=
  129. zigzagPoint[1] * deviations.low) {
  130. yIndex = highIndex;
  131. }
  132. }
  133. else { // next line down
  134. // higher when going up -> change zigzag candidate
  135. if (yVal[i][highIndex] >= zigzagPoint[1]) {
  136. zigzagPoint = [xVal[i], yVal[i][highIndex]];
  137. }
  138. // requried change to go down -> new zigzagpoint and
  139. // direction change
  140. if (yVal[i][lowIndex] <=
  141. zigzagPoint[1] * deviations.high) {
  142. yIndex = lowIndex;
  143. }
  144. }
  145. if (yIndex !== false) { // new zigzag point and direction change
  146. zigzag.push(zigzagPoint);
  147. xData.push(zigzagPoint[0]);
  148. yData.push(zigzagPoint[1]);
  149. zigzagPoint = [xVal[i], yVal[i][yIndex]];
  150. directionUp = !directionUp;
  151. yIndex = false;
  152. }
  153. }
  154. zigzagLen = zigzag.length;
  155. // no zigzag for last point
  156. if (zigzagLen !== 0 &&
  157. zigzag[zigzagLen - 1][0] < xVal[yValLen - 1]) {
  158. // set last point from zigzag candidate
  159. zigzag.push(zigzagPoint);
  160. xData.push(zigzagPoint[0]);
  161. yData.push(zigzagPoint[1]);
  162. }
  163. return {
  164. values: zigzag,
  165. xData: xData,
  166. yData: yData
  167. };
  168. }
  169. });
  170. /**
  171. * A `Zig Zag` series. If the [type](#series.zigzag.type) option is not
  172. * specified, it is inherited from [chart.type](#chart.type).
  173. *
  174. * @extends series,plotOptions.zigzag
  175. * @since 6.0.0
  176. * @product highstock
  177. * @excluding dataParser, dataURL
  178. * @requires stock/indicators/indicators
  179. * @requires stock/indicators/zigzag
  180. * @apioption series.zigzag
  181. */
  182. ''; // adds doclets above to transpiled file