trading within a time

 
Hello everyone, I wanted to ask how I can set up an ea to open operations only from one hour to another. For example, I would like you to open long or short transactions only if a maximum or a minimum is broken between 2 and 3 pm

 
https://www.mql5.com/en/docs/dateandtime/timetostruct
Documentation on MQL5: Date and Time / TimeToStruct
Documentation on MQL5: Date and Time / TimeToStruct
  • www.mql5.com
Date and Time / TimeToStruct - Reference on algorithmic/automated trading language for MetaTrader 5
 
I tried to create my code, it does not return any error but if I try to do the backtest it does not start


//+------------------------------------------------------------------+
//|                                                 Orario Borse.mq4 |
//|                        Copyright 2018, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2018, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+

datetime OrarioAperturaoperazioni=D'2000.01.01 14:00:00';
datetime OrarioFineoperazioni=D'2000.01.01 15:00:00';
datetime CurrentTime;
double lotto = 0.1;
extern int stop_loss=70;
extern int take_profit=120;
double TP=take_profit*10*Point;
double SL=stop_loss*10*Point;



int OnInit()
  {
//---
   
//---
   return(INIT_SUCCEEDED);
  } 
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
double DailyHigh,DailyLow,Highfast,Lowfast;
void OnTick()
  {
//---
   if((TimeCurrent()>=OrarioAperturaoperazioni && TimeCurrent()<=OrarioFineoperazioni)) {
   DailyHigh = iHigh("USD/JPY",PERIOD_D1,1);
   DailyLow = iLow("USD/JPY",PERIOD_D1,1);
   Highfast = iHigh("USD/JPY",PERIOD_M5,0);
   Lowfast = iLow("USD/JPY",PERIOD_M5,0);
   if (CondizioneLong()) EseguiOrdineLong();
   if (CondizioneShort()) EseguiOrdineShort();
   }
   
   
   
   if(TimeCurrent()>OrarioAperturaoperazioni) {
   while(TimeCurrent()>OrarioAperturaoperazioni) {
   OrarioAperturaoperazioni=OrarioAperturaoperazioni+86400;
    }
     if(TimeCurrent()>OrarioFineoperazioni) {
   while(TimeCurrent()>OrarioFineoperazioni) {
   OrarioAperturaoperazioni=OrarioAperturaoperazioni+86400;
    }
  }
  }
  }
  
//+------------------------------------------------------------------+

bool CondizioneLong(){
     if(Highfast > DailyHigh) return true;
     else return false;
     }
     
bool CondizioneShort() {
     if(Lowfast < DailyLow) return true;
     else return false;
     }
     
void EseguiOrdineLong() {
     OrderSend(Symbol(),OP_BUY,lotto,Ask,0,Ask-SL,Ask+TP,"Ordine Long eseguito");
     }

void EseguiOrdineShort() {
     OrderSend(Symbol(),OP_SELL,lotto,Bid,0,Bid+SL,Bid-TP,"Ordine Short eseguito"); 
     }


Discover new MetaTrader 5 opportunities with MQL5 community and services
Discover new MetaTrader 5 opportunities with MQL5 community and services
  • www.mql5.com
Ask questions on technical analysis, discuss trading systems and improve your MQL5 programming skills to develop your own trading strategies. Communicate and share your experience with traders from anywhere in the world, answer questions and help beginners — MQL5.community is developing along with you. Which DB engine for MT5 Hi there. to...
 
  1. Roberto Danilo: I try to do the backtest it does not start
    Use the debugger or print out your variables, including _LastError and find out why.

  2. DailyHigh = iHigh("USD/JPY",PERIOD_D1,1);
    On MT4: Unless the current chart is that specific pair/TF referenced, you must handle 4066/4073 errors.
              Download history in MQL4 EA - MQL4 and MetaTrader 4 - MQL4 programming forum
 
//+------------------------------------------------------------------+
//|                                                 Orario Borse.mq4 |
//|                        Copyright 2018, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2018, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+

datetime OrarioAperturaoperazioni=D'2000.01.01 14:00:00';
datetime OrarioFineoperazioni=D'2000.01.01 15:00:00';
double lotto = 0.1;
extern int stop_loss=70;
extern int take_profit=120;
double TP=take_profit*10*Point;
double SL=stop_loss*10*Point;
extern int e_start_hour = 14;
extern int e_start_minute = 00;
extern int e_end_hour = 15;
extern int e_end_minute = 00;


int OnInit()
  {
//---
   
//---
   return(INIT_SUCCEEDED);
  } 
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
double DailyHigh,DailyLow,Highfast,Lowfast;
void OnTick()
  {
   DailyHigh = iHigh("USD/JPY",PERIOD_D1,0);
   DailyLow = iLow("USD/JPY",PERIOD_D1,0);
   Highfast = iHigh("USD/JPY",PERIOD_M5,0);
   Lowfast = iLow("USD/JPY",PERIOD_M5,0);
   
   if( isTradingTime() ) {
   if (CondizioneLong()) EseguiOrdineLong();
   if (CondizioneShort()) EseguiOrdineShort();
   else Alert("Errore time current");
   }
   }
   
 
  
//+------------------------------------------------------------------+

bool CondizioneLong(){
     if(Highfast > DailyHigh) return true;
     else return false;
     }
     
bool CondizioneShort() {
     if(Lowfast < DailyLow) return true;
     else return false;
     }
     
void EseguiOrdineLong() {
     OrderSend(Symbol(),OP_BUY,lotto,Ask,0,Ask-SL,Ask+TP,"Ordine Long eseguito");
     }

void EseguiOrdineShort() {
     OrderSend(Symbol(),OP_SELL,lotto,Bid,0,Bid+SL,Bid-TP,"Ordine Short eseguito"); 
     }
     
bool isTradingTime(){
//TRUE trading consentito
//FALSE trading non consentito

bool v_tradingTime = false;

//Controllo validità dati in ingresso
if (e_start_hour < 0 || e_start_hour > 23){
Print("e_start_hour deve essere compreso tra 0 e 23");
return(false);
}
if (e_end_hour < 0 || e_end_hour > 23){
Print("e_end_hour deve essere compreso tra 0 e 23");
return(false);
} 
if (e_start_minute < 0 || e_start_minute > 59){
Print("e_start_minute deve essere compreso tra 0 e 59");
return(false);
} 
if (e_end_minute < 0 || e_end_minute > 59){
Print("e_end_minute deve essere compreso tra 0 e 59");
return(false);
} 

//verifica se l'orario attuale è compresto nella finestra
if (Hour() >= e_start_hour && Hour() <= e_end_hour)
 v_tradingTime = true;
  else return(false);

 if (Hour() == e_start_hour)
 if (Minute() >= e_start_minute)
v_tradingTime = true;
else return(false);

if (Hour() == e_end_hour) 
if (Minute() <= e_end_minute)
 v_tradingTime = true;
 else return(false);

 return (v_tradingTime);
}

  
Discover new MetaTrader 5 opportunities with MQL5 community and services
Discover new MetaTrader 5 opportunities with MQL5 community and services
  • www.mql5.com
Ask questions on technical analysis, discuss trading systems and improve your MQL5 programming skills to develop your own trading strategies. Communicate and share your experience with traders from anywhere in the world, answer questions and help beginners — MQL5.community is developing along with you. analyse log files programatically hello...
Reason: