zigzag.src.js 8.5 KB

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