//+------------------------------------------------------------------+ //| 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" //--- parámetros de entrada input string other_symbol="USDJPY"; //+------------------------------------------------------------------+ //| Función de inicialización del Experto | //+------------------------------------------------------------------+ int OnInit() { //--- comprueba el símbolo if(_Symbol==other_symbol) { PrintFormat("Debe especificar otro símbolo en los parámetros de entrada o seleccionar otro símbolo en el Probador de Estrategia!"); //--- forzamos la detención de la prueba return(-1); } //--- return(0); } //+------------------------------------------------------------------+ //| Función de ticks del Experto | //+------------------------------------------------------------------+ void OnTick() { //--- variable estática, se usa para almacenar la hora de la última barra static datetime last_bar_time=0; //--- marca de sincronización static bool synchonized=false; //--- si la variable estática no ha sido inicializada if(last_bar_time==0) { //--- es la primera llamada, guardamos la hora de la barra y salimos last_bar_time=(datetime)SeriesInfoInteger(_Symbol,Period(),SERIES_LASTBAR_DATE); PrintFormat("La variable last_bar_time ha sido inicializada con el valor %s",TimeToString(last_bar_time)); } //--- obtenemos la hora de apertura de la última barra del símbolo en el gráfico datetime curr_time=(datetime)SeriesInfoInteger(Symbol(),Period(),SERIES_LASTBAR_DATE); //--- si las horas son diferentes if(curr_time!=last_bar_time) { //--- guardamos la hora de apertura en la variable estática last_bar_time=curr_time; //--- no están sincronizados synchonized=false; //--- imprimimos el mensaje PrintFormat("Se ha formado una nueva barra en el símbolo %s a las %s",_Symbol,TimeToString(TimeCurrent())); } //--- hora de apertura de la barra del otro símbolo datetime other_time; //--- ciclo hasta que la hora de apertura del otro símbolo se iguala a curr_time while(!(curr_time==(other_time=(datetime)SeriesInfoInteger(other_symbol,Period(),SERIES_LASTBAR_DATE)) && !synchonized)) { PrintFormat("Esperando 5 segundos.."); //--- esperamos 5 segundos y llamamos SeriesInfoInteger(other_symbol,Period(),SERIES_LASTBAR_DATE) Sleep(5000); } //--- las barras están sincronizadas synchonized=true; PrintFormat("La hora de apertura de la barra del símbolo del gráfico %s: es %s",_Symbol,TimeToString(last_bar_time)); PrintFormat("La hora de apertura de la barra del símbolo %s: es %s",other_symbol,TimeToString(other_time)); //--- TimeCurrent() no es útil, use TimeTradeServer() Print("Las barras están sincronizadas a las ",TimeToString(TimeTradeServer(),TIME_SECONDS)); } //+------------------------------------------------------------------+