An example of how to work with ZigZag:
Forum on trading, automated trading systems and testing trading strategies
Vladimir Karputov, 2018.12.27 11:32
An example of working with the ZigZag indicator
Code: ZigZag Example.mq5
Pay attention to the extremum search algorithm: if the value in the indicator buffer is not equal to "0.0" and not equal to "PLOT_EMPTY_VALUE" - it means that We have detected an extremum.
The extremum search goes to " ZigZag: how many candles to check back " bars.
Algorithm of work standard: ONCE in OnInit () we create an indicator.
//--- int handle_iCustom; // variable for storing the handle of the iCustom indicator //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit() { //--- create handle of the indicator iCustom handle_iCustom=iCustom(Symbol(),Period(),"Examples\\ZigZag",InpDepth,InpDeviation,InptBackstep); //--- if the handle is not created if(handle_iCustom==INVALID_HANDLE) { //--- tell about the failure and output the error code PrintFormat("Failed to create handle of the iCustom indicator for the symbol %s/%s, error code %d", Symbol(), EnumToString(Period()), GetLastError()); //--- the indicator is stopped early return(INIT_FAILED); } //--- return(INIT_SUCCEEDED); }
Next in OnTick (), we make a copy of the indicator data, while using the indicator handle.
//+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { static long counter=0; counter++; if(counter>=15) counter=0; else return; //--- double ZigzagBuffer[]; ArraySetAsSeries(ZigzagBuffer,true); int start_pos=0,count=InpCandlesCheck+1; if(!iGetArray(handle_iCustom,0,start_pos,count,ZigzagBuffer)) return; string text=""; for(int i=0;i<count;i++) { if(ZigzagBuffer[i]!=PLOT_EMPTY_VALUE && ZigzagBuffer[i]!=0.0) text=text+"\n"+IntegerToString(i)+": "+DoubleToString(ZigzagBuffer[i],Digits()); } Comment(text); }
The function by which the data is copied indicator:
//+------------------------------------------------------------------+ //| Get value of buffers | //+------------------------------------------------------------------+ double iGetArray(const int handle,const int buffer,const int start_pos,const int count,double &arr_buffer[]) { bool result=true; if(!ArrayIsDynamic(arr_buffer)) { Print("This a no dynamic array!"); return(false); } ArrayFree(arr_buffer); //--- reset error code ResetLastError(); //--- fill a part of the iBands array with values from the indicator buffer int copied=CopyBuffer(handle,buffer,start_pos,count,arr_buffer); if(copied!=count) { //--- if the copying fails, tell the error code PrintFormat("Failed to copy data from the indicator, error code %d",GetLastError()); //--- quit with zero result - it means that the indicator is considered as not calculated return(false); } return(result); }
Result of work:
Thank you for your answer But my question and problem is that I can use monthly time frame in icustom, but in daily time frame backtest, I also used zigzag separator, not the usual zigzag
ZigZag separate link:
- www.mql5.com
The details are as follows when I select the daily timeframe tester strategy:
2021.06.04 18:24:25.639 login (build 2940) 2021.06.04 18:24:25.670 account info found with currency RLS 2021.06.04 18:24:25.674 expert file added: Experts\mn_bo_price.ex5. 30229 bytes loaded 2021.06.04 18:24:25.695 calculate profit in pips, initial deposit 100000, leverage 1:100 2021.06.04 18:24:25.695 successfully initialized 2021.06.04 18:24:25.695 29 Kb of total initialization data received 2021.06.04 18:24:25.695 Intel Core i3 M 330 @ 2.13GHz, 3956 MB 2021.06.04 18:24:25.717 BTCUSD: symbol to be synchronized 2021.06.04 18:24:25.718 BTCUSD: symbol synchronized already, 18 bytes received 2021.06.04 18:24:25.719 BTCUSD: history synchronization started 2021.06.04 18:24:25.726 BTCUSD: load 27 bytes of history data to synchronize in 0:00:00.004 2021.06.04 18:24:25.726 BTCUSD: history synchronized from 2011.03.24 to 2021.06.03 2021.06.04 18:24:26.473 BTCUSD,Daily: history cache allocated for 802 bars and contains 627 bars from 2019.01.01 00:00 to 2020.09.30 00:00 2021.06.04 18:24:26.473 BTCUSD,Daily: history begins from 2019.01.01 00:00 2021.06.04 18:24:26.476 BTCUSD,Daily (MofidSecurities-Server): OHLC bar states generating. OnTick executed on the bar begin only 2021.06.04 18:24:26.476 BTCUSD,Daily: testing of Experts\mn_bo_price.ex5 from 2020.10.01 00:00 to 2021.06.03 00:00 started 2021.06.04 18:24:26.488 program file added: \Indicators\ZigZag (separate).ex5. 27955 bytes loaded 2021.06.04 18:24:27.236 BTCUSD,Monthly: history cache allocated for 31 bars and contains 21 bars from 2019.01.01 00:00 to 2020.09.01 00:00 2021.06.04 18:24:27.237 BTCUSD,Monthly: history begins from 2019.01.01 00:00 2021.06.04 18:24:27.598 2020.10.01 00:00:00 array out of range in 'mn_bo_price.mq5' (104,12) 2021.06.04 18:24:27.598 OnTick critical error 2021.06.04 18:24:27.606 BTCUSD,Daily: 1 ticks, 1 bars generated. Environment synchronized in 0:00:00.082. Test passed in 0:00:01.885 (including ticks preprocessing 0:00:00.359). 2021.06.04 18:24:27.606 BTCUSD,Daily: total time from login to stop testing 0:00:01.967 (including 0:00:00.082 for history data synchronization) 2021.06.04 18:24:27.606 303 Mb memory used including 0.94 Mb of history data, 64 Mb of tick data 2021.06.04 18:24:27.606 log file "C:\Users\hheid\AppData\Roaming\MetaQuotes\Tester\2506E8E7E4116548D478CE2C3598FAB1\Agent-127.0.0.1-3000\logs\20210604.log" written 2021.06.04 18:24:27.606 test Experts\mn_bo_price.ex5 on BTCUSD,Daily thread finished 2021.06.04 18:24:27.713 prepare for shutdown
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hello friends, I hope you are healthy. Excuse me, my English is very weak. The problem is that when I test the daily timeframe in icustom, it returns zero and does not enter the deal, but it works properly in the monthly timeframe. What is the problem?