//+------------------------------------------------------------------+ //| Synchronize_Bars_Use_Sleep.mq5 | //| Copyright 2011, MetaQuotes Software Corp. | //| http://www.mql5.com | //+------------------------------------------------------------------+ #property copyright "Copyright 2011, MetaQuotes Software Corp." #property link "http://www.mql5.com" #property version "1.00" //--- input parameters input string other_symbol="USDJPY"; //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit() { //--- check symbol if(_Symbol==other_symbol) { PrintFormat("You have to specify the other symbol in input parameters or select other symbol in Strategy Tester!"); //--- forced stop testing return(-1); } //--- return(0); } //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { //--- static variable, used for storage of last bar time static datetime last_bar_time=0; //--- sync flag static bool synchonized=false; //--- if static variable isn't initialized if(last_bar_time==0) { //--- it's first call, save bar time and exit last_bar_time=(datetime)SeriesInfoInteger(_Symbol,Period(),SERIES_LASTBAR_DATE); PrintFormat("The last_bar_time variable is initialized with value %s",TimeToString(last_bar_time)); } //--- get open time of the last bar of chart symbol datetime curr_time=(datetime)SeriesInfoInteger(Symbol(),Period(),SERIES_LASTBAR_DATE); //--- if times aren't equal if(curr_time!=last_bar_time) { //--- save open bar time to the static variable last_bar_time=curr_time; //--- not synchronized synchonized=false; //--- print message PrintFormat("A new bar has appeared on symbol %s at %s",_Symbol,TimeToString(TimeCurrent())); } //--- open time of the other symbol's bar datetime other_time; //--- loop until the open time of other symbol become equal to curr_time while(!(curr_time==(other_time=(datetime)SeriesInfoInteger(other_symbol,Period(),SERIES_LASTBAR_DATE)) && !synchonized)) { PrintFormat("Waiting 5 seconds.."); //--- wait 5 seconds and call SeriesInfoInteger(other_symbol,Period(),SERIES_LASTBAR_DATE) Sleep(5000); } //--- bars are synchronized synchonized=true; PrintFormat("Open bar time of the chart symbol %s: is %s",_Symbol,TimeToString(last_bar_time)); PrintFormat("Open bar time of the symbol %s: is %s",other_symbol,TimeToString(other_time)); //--- TimeCurrent() is not useful, use TimeTradeServer() Print("The bars are synchronized at ",TimeToString(TimeTradeServer(),TIME_SECONDS)); } //+------------------------------------------------------------------+