open trade at specific time

 

i wana open trade for exemple at 11:00:00 and 12:00:00 and 13:00:00

can some slove my expert plz

#property version   "1.00"
#property strict
#include <stdlib.mqh>
#define MAGIC 94949449


enum TRADE_TYPE
{
   BUY,
   SELL
};
//--- input parameters
input string   TimeToOpen1="11:15:00";
input string   TimeToOpen2="12:00:00";
input string   TimeToOpen3="13:00:00";

input TRADE_TYPE TradeType = BUY;  
input double   Lots=0.01;
input int      StopLoss=40;
input int      TakeProfit=40;

MqlDateTime time_to_open;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
  
   TimeToStruct( StringToTime( TimeToOpen1),time_to_open)     ;
  
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   MqlDateTime time;
   TimeCurrent(time);

   time.hour = time_to_open.hour;
   time.min = time_to_open.min;
   time.sec = time_to_open.sec;

   datetime time_current = TimeCurrent();
   
   datetime time_trade = StructToTime(time);
  
   if(time_current >= time_trade && time_current < time_trade+(12*PeriodSeconds(PERIOD_M1)) && CanTrade())
      if(!OpenTrade())
         Print(__FUNCTION__," <!!!> ",ErrorDescription(GetLastError()));
  }
//+------------------------------------------------------------------+

bool CanTrade()
{
   datetime last_trade = 0;
   for(int i=OrdersHistoryTotal()-1;i>=0;i--)
      if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)&&OrderSymbol()==_Symbol&&OrderMagicNumber()==MAGIC)
         if(OrderOpenTime() > last_trade)
            last_trade=OrderOpenTime();

   if(TimeCurrent()-last_trade < 12*PeriodSeconds(PERIOD_H1))
      return false;
   
   for(int i=OrdersTotal()-1;i>=0;i--)
      if(OrderSelect(i,SELECT_BY_POS)&&OrderSymbol()==_Symbol&&OrderMagicNumber()==MAGIC)
         return false;
  
   return true;
}

bool OpenTrade()
{
   int type;
   double price;
   double sl;
   double tp;
   if(TradeType == BUY)
   {
      type = OP_BUY;
      price = Ask;
      sl = StopLoss > 0 ? NormalizeDouble(price - (double)StopLoss*_Point,_Digits):0.0;
      tp = TakeProfit > 0 ? NormalizeDouble(price + (double)TakeProfit*_Point,_Digits):0.0;
   }
   else
   {
      type = OP_SELL;
      price = Bid;
      sl = StopLoss > 0 ? NormalizeDouble(price + (double)StopLoss*_Point,_Digits):0.0;
      tp = TakeProfit > 0 ? NormalizeDouble(price - (double)TakeProfit*_Point,_Digits):0.0;
   }
   return OrderSend(_Symbol,type,Lots,price,3,sl,tp,"Timed Trade",MAGIC,0,clrLime)>=0;
}
 
ALI AMRIOUI:

i wana open trade for exemple at 11:00:00 and 12:00:00 and 13:00:00

can some slove my expert plz

did some one have idea plz ??

im becoms realy crazy with this code 

 

You need a state machine.
          Multiple conditions for taking a trade. - Expert Advisors and Automated Trading - MQL5 programming forum (2023)

  1. On a new day, reset to waiting for time 11.
  2. Wait for time to arrive or pass. Open. Set to waiting for 12.
  3. Wait for time to arrive or pass. Open. Set to waiting for 13.
  4. Wait for time to arrive or pass. Open. Set to waiting forever.
 
William Roeder #:

You need a state machine.
          Multiple conditions for taking a trade. - Expert Advisors and Automated Trading - MQL5 programming forum (2023)

  1. On a new day, reset to waiting for time 11.
  2. Wait for time to arrive or pass. Open. Set to waiting for 12.
  3. Wait for time to arrive or pass. Open. Set to waiting for 13.
  4. Wait for time to arrive or pass. Open. Set to waiting forever.
//--- input parameters
input string   TimeToOpen1="11:00:00";
input string   TimeToOpen2="12:00:00";
input string   TimeToOpen3="13:00:00";

input TRADE_TYPE TradeType = BUY;  
input double   Lots=0.01;
input int      StopLoss=40;
input int      TakeProfit=40;

MqlDateTime time_to_open1 , time_to_open2   ;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---

   TimeToStruct( StringToTime( TimeToOpen1), time_to_open1 ) ;

    TimeToStruct( StringToTime( TimeToOpen2),time_to_open2) ; 
  
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   MqlDateTime time1,time2;
   
   TimeCurrent(time2);
   TimeCurrent(time1);
   
   time1.hour = time_to_open1.hour;
   time1.min = time_to_open1.min;
   time1.sec = time_to_open1.sec;
   
   time2.hour = time_to_open2.hour;
   time2.min = time_to_open2.min;
   time2.sec = time_to_open2.sec;
   

   datetime time_current1 = TimeCurrent();
   datetime time_current2 = TimeCurrent();

    
     datetime time_trade1 = StructToTime(time1);
     datetime time_trade2 = StructToTime(time2);
   
   
  
   if(   (time_current1 >= time_trade1 && time_current1 < time_trade1+(12*PeriodSeconds(PERIOD_M1)) && CanTrade())  || (time_current2 >= time_trade2 && time_current2 < time_trade1+(12*PeriodSeconds(PERIOD_M1)) && CanTrade()) )
      if(!OpenTrade())
         Print(__FUNCTION__," <!!!> ",ErrorDescription(GetLastError()));
  }
//+------------------------------------------------------------------+

like that for expemle also dont work

Reason: