//--- input parameters input group "The date can be entered in any of the formats:" input group "yyyy.mm.dd [hh:mi], yyyy.mm.dd [hh:mi:ss]" input group "yyyymmdd [hh:mi:ss], yyyymmdd [hhmiss]" input group "yyyy/mm/dd [hh:mi:ss], yyyy-mm-dd [hh:mi:ss]" input string InpDateStr; // Please enter the date here as a string //+------------------------------------------------------------------+ //| Script program start function | //+------------------------------------------------------------------+ void OnStart() { //--- convertimos el tiempo introducido en los parámetros de entrada como una cadena en un valor datetime datetime time=StringToTime(InpDateStr); //--- mostramos en el registro la cadena introducida y la hora obtenida PrintFormat("Date entered as a string in the form '%s' is converted to datetime in the form '%s'", InpDateStr, TimeToString(time, TIME_DATE|TIME_MINUTES|TIME_SECONDS)); //--- creamos una línea vertical en la fecha y hora resultante y desplazamos el gráfico a esta ubicación if(CreateVLine(time)) ChartNavigateToTime(time); /* resultado: Date entered as a string in the form '' is converted to datetime in the form '1970.01.01 00:00:00' Date entered as a string in the form '2024' is converted to datetime in the form '2024.02.24 20:24:00' Date entered as a string in the form '202400' is converted to datetime in the form '2024.02.24 20:24:00' Date entered as a string in the form '20240000' is converted to datetime in the form '2024.02.24 00:00:00' Date entered as a string in the form '2024022410' is converted to datetime in the form '2030.09.06 00:00:00' Date entered as a string in the form '20240224 10' is converted to datetime in the form '2024.02.24 10:00:00' Date entered as a string in the form '20240224 01' is converted to datetime in the form '2024.02.24 01:00:00' Date entered as a string in the form '20240224 0030' is converted to datetime in the form '2024.02.24 23:00:00' Date entered as a string in the form '20240224 0100' is converted to datetime in the form '2024.02.24 01:00:00' */ } //+------------------------------------------------------------------+ //| Crea un objeto de línea vertical | //+------------------------------------------------------------------+ bool CreateVLine(const datetime line_time) { ResetLastError(); string name=MQLInfoString(MQL_PROGRAM_NAME)+"_VLINE"; if(!ObjectCreate(0, name, OBJ_VLINE, 0, line_time, 0)) { Print("ObjectCreate() failed. Error code: ", GetLastError()); return(false); } ObjectSetInteger(0, name, OBJPROP_STYLE, STYLE_DOT); ObjectSetInteger(0, name, OBJPROP_SELECTABLE, true); return(true); } //+------------------------------------------------------------------+ //| Desplaza el gráfico a la hora indicada de apertura de la barra | //+------------------------------------------------------------------+ bool ChartNavigateToTime(const datetime time) { ChartSetInteger(0, CHART_AUTOSCROLL, false); ResetLastError(); int bar=iBarShift(_Symbol, PERIOD_CURRENT, time); if(bar<0) { PrintFormat("%s: iBarShift() failed. Error code: %d", __FUNCTION__, GetLastError()); return(false); } long first=0; if(!ChartGetInteger(0, CHART_FIRST_VISIBLE_BAR, 0, first)) { PrintFormat("%s: ChartGetInteger() failed. Error code: %d", __FUNCTION__, GetLastError()); return(false); } return(ChartNavigate(0, CHART_CURRENT_POS, (int)first-bar)); } |