Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2018
739
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.26 KB | None | 0 0
  1. //+------------------------------------------------------------------+
  2. //| ZigZag.mq4 |
  3. //| Copyright 2006-2014, MetaQuotes Software Corp. |
  4. //| http://www.mql4.com |
  5. //+------------------------------------------------------------------+
  6. #property copyright "2006-2014, MetaQuotes Software Corp."
  7. #property link "http://www.mql4.com"
  8. #property strict
  9.  
  10. #property indicator_chart_window
  11. #property indicator_buffers 1
  12. #property indicator_color1 Red
  13. //---- indicator parameters
  14. input int InpDepth=12; // Depth
  15. input int InpDeviation=5; // Deviation
  16. input int InpBackstep=3; // Backstep
  17. //---- indicator buffers
  18. double ExtZigzagBuffer[];
  19. double ExtHighBuffer[];
  20. double ExtLowBuffer[];
  21. //--- globals
  22. int ExtLevel=3; // recounting's depth of extremums
  23. //+------------------------------------------------------------------+
  24. //| Custom indicator initialization function |
  25. //+------------------------------------------------------------------+
  26. int OnInit()
  27. {
  28. if(InpBackstep>=InpDepth)
  29. {
  30. Print("Backstep cannot be greater or equal to Depth");
  31. return(INIT_FAILED);
  32. }
  33. //--- 2 additional buffers
  34. IndicatorBuffers(3);
  35. //---- drawing settings
  36. SetIndexStyle(0,DRAW_SECTION);
  37. //---- indicator buffers
  38. SetIndexBuffer(0,ExtZigzagBuffer);
  39. SetIndexBuffer(1,ExtHighBuffer);
  40. SetIndexBuffer(2,ExtLowBuffer);
  41. SetIndexEmptyValue(0,0.0);
  42. //---- indicator short name
  43. IndicatorShortName("ZigZag("+string(InpDepth)+","+string(InpDeviation)+","+string(InpBackstep)+")");
  44. //---- initialization done
  45. return(INIT_SUCCEEDED);
  46. }
  47. //+------------------------------------------------------------------+
  48. //| |
  49. //+------------------------------------------------------------------+
  50. int OnCalculate(const int rates_total,
  51. const int prev_calculated,
  52. const datetime &time[],
  53. const double &open[],
  54. const double &high[],
  55. const double &low[],
  56. const double &close[],
  57. const long& tick_volume[],
  58. const long& volume[],
  59. const int& spread[])
  60. {
  61. int i,limit,counterZ,whatlookfor=0;
  62. int back,pos,lasthighpos=0,lastlowpos=0;
  63. double extremum;
  64. double curlow=0.0,curhigh=0.0,lasthigh=0.0,lastlow=0.0;
  65. //--- check for history and inputs
  66. if(rates_total<InpDepth || InpBackstep>=InpDepth)
  67. return(0);
  68. //--- first calculations
  69. if(prev_calculated==0)
  70. limit=InitializeAll();
  71. else
  72. {
  73. //--- find first extremum in the depth ExtLevel or 100 last bars
  74. i=counterZ=0;
  75. while(counterZ<ExtLevel && i<100)
  76. {
  77. if(ExtZigzagBuffer[i]!=0.0)
  78. counterZ++;
  79. i++;
  80. }
  81. //--- no extremum found - recounting all from begin
  82. if(counterZ==0)
  83. limit=InitializeAll();
  84. else
  85. {
  86. //--- set start position to found extremum position
  87. limit=i-1;
  88. //--- what kind of extremum?
  89. if(ExtLowBuffer[i]!=0.0)
  90. {
  91. //--- low extremum
  92. curlow=ExtLowBuffer[i];
  93. //--- will look for the next high extremum
  94. whatlookfor=1;
  95. }
  96. else
  97. {
  98. //--- high extremum
  99. curhigh=ExtHighBuffer[i];
  100. //--- will look for the next low extremum
  101. whatlookfor=-1;
  102. }
  103. //--- clear the rest data
  104. for(i=limit-1; i>=0; i--)
  105. {
  106. ExtZigzagBuffer[i]=0.0;
  107. ExtLowBuffer[i]=0.0;
  108. ExtHighBuffer[i]=0.0;
  109. }
  110. }
  111. }
  112. //--- main loop
  113. for(i=limit; i>=0; i--)
  114. {
  115. //--- find lowest low in depth of bars
  116. extremum=low[iLowest(NULL,0,MODE_LOW,InpDepth,i)];
  117. //--- this lowest has been found previously
  118. if(extremum==lastlow)
  119. extremum=0.0;
  120. else
  121. {
  122. //--- new last low
  123. lastlow=extremum;
  124. //--- discard extremum if current low is too high
  125. if(low[i]-extremum>InpDeviation*Point)
  126. extremum=0.0;
  127. else
  128. {
  129. //--- clear previous extremums in backstep bars
  130. for(back=1; back<=InpBackstep; back++)
  131. {
  132. pos=i+back;
  133. if(ExtLowBuffer[pos]!=0 && ExtLowBuffer[pos]>extremum)
  134. ExtLowBuffer[pos]=0.0;
  135. }
  136. }
  137. }
  138. //--- found extremum is current low
  139. if(low[i]==extremum)
  140. ExtLowBuffer[i]=extremum;
  141. else
  142. ExtLowBuffer[i]=0.0;
  143. //--- find highest high in depth of bars
  144. extremum=high[iHighest(NULL,0,MODE_HIGH,InpDepth,i)];
  145. //--- this highest has been found previously
  146. if(extremum==lasthigh)
  147. extremum=0.0;
  148. else
  149. {
  150. //--- new last high
  151. lasthigh=extremum;
  152. //--- discard extremum if current high is too low
  153. if(extremum-high[i]>InpDeviation*Point)
  154. extremum=0.0;
  155. else
  156. {
  157. //--- clear previous extremums in backstep bars
  158. for(back=1; back<=InpBackstep; back++)
  159. {
  160. pos=i+back;
  161. if(ExtHighBuffer[pos]!=0 && ExtHighBuffer[pos]<extremum)
  162. ExtHighBuffer[pos]=0.0;
  163. }
  164. }
  165. }
  166. //--- found extremum is current high
  167. if(high[i]==extremum)
  168. ExtHighBuffer[i]=extremum;
  169. else
  170. ExtHighBuffer[i]=0.0;
  171. }
  172. //--- final cutting
  173. if(whatlookfor==0)
  174. {
  175. lastlow=0.0;
  176. lasthigh=0.0;
  177. }
  178. else
  179. {
  180. lastlow=curlow;
  181. lasthigh=curhigh;
  182. }
  183. for(i=limit; i>=0; i--)
  184. {
  185. switch(whatlookfor)
  186. {
  187. case 0: // look for peak or lawn
  188. if(lastlow==0.0 && lasthigh==0.0)
  189. {
  190. if(ExtHighBuffer[i]!=0.0)
  191. {
  192. lasthigh=High[i];
  193. lasthighpos=i;
  194. whatlookfor=-1;
  195. ExtZigzagBuffer[i]=lasthigh;
  196. }
  197. if(ExtLowBuffer[i]!=0.0)
  198. {
  199. lastlow=Low[i];
  200. lastlowpos=i;
  201. whatlookfor=1;
  202. ExtZigzagBuffer[i]=lastlow;
  203. }
  204. }
  205. break;
  206. case 1: // look for peak
  207. if(ExtLowBuffer[i]!=0.0 && ExtLowBuffer[i]<lastlow && ExtHighBuffer[i]==0.0)
  208. {
  209. ExtZigzagBuffer[lastlowpos]=0.0;
  210. lastlowpos=i;
  211. lastlow=ExtLowBuffer[i];
  212. ExtZigzagBuffer[i]=lastlow;
  213. }
  214. if(ExtHighBuffer[i]!=0.0 && ExtLowBuffer[i]==0.0)
  215. {
  216. lasthigh=ExtHighBuffer[i];
  217. lasthighpos=i;
  218. ExtZigzagBuffer[i]=lasthigh;
  219. whatlookfor=-1;
  220. }
  221. break;
  222. case -1: // look for lawn
  223. if(ExtHighBuffer[i]!=0.0 && ExtHighBuffer[i]>lasthigh && ExtLowBuffer[i]==0.0)
  224. {
  225. ExtZigzagBuffer[lasthighpos]=0.0;
  226. lasthighpos=i;
  227. lasthigh=ExtHighBuffer[i];
  228. ExtZigzagBuffer[i]=lasthigh;
  229. }
  230. if(ExtLowBuffer[i]!=0.0 && ExtHighBuffer[i]==0.0)
  231. {
  232. lastlow=ExtLowBuffer[i];
  233. lastlowpos=i;
  234. ExtZigzagBuffer[i]=lastlow;
  235. whatlookfor=1;
  236. }
  237. break;
  238. }
  239. }
  240. //--- done
  241. return(rates_total);
  242. }
  243. //+------------------------------------------------------------------+
  244. //| |
  245. //+------------------------------------------------------------------+
  246. int InitializeAll()
  247. {
  248. ArrayInitialize(ExtZigzagBuffer,0.0);
  249. ArrayInitialize(ExtHighBuffer,0.0);
  250. ArrayInitialize(ExtLowBuffer,0.0);
  251. //--- first counting position
  252. return(Bars-InpDepth);
  253. }
  254. //+------------------------------------------------------------------+
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement