Timefilter doesn't work

 

Hi!

I can't figure out why my timefilter doesn't work. Let's say I would like to trade between 7:35-11:30 and 14:30-22:30 and I don't want to trade on Friday.

The time filter only works when I create a simple EA with only a trade.Buy function and no other conditions.

The more complex EA should only buy/sell when the vaule of the Supertrend indicator becomes higher/lower than the price and only in the given time intervals.

It should close the position at the next sell/buy signal (if it was a buy then it should be closed at the next 'sell' signal' ). When closing positions the time interval shouldn't matter.

The 'TradingIsAllowed' variable should return 'true' when the current time is between the allowed time interval but it always returns false and I  can't figure out why. I rewrote my code 3 times already..

Could you please help me?


#include  <Trade\Trade.mqh> 
CTrade trade;  

ulong posTicket;
input double Lots=0.1;
int stHandle;
int totalBars;

input ENUM_TIMEFRAMES Timeframe = PERIOD_CURRENT;
input int Periods =12;
input double Multiplier = 3.0;

//for the timefilter
input string StartTradingTime="07:35";
input string StopTradingTime="11:30";
input string StartTradingTime2="14:35";
input string StopTradingTime2="22:30";
string CurrentTime;
bool TradingIsAllowed=false;
bool TradingIsAllowed2=false;


int OnInit(){
   totalBars=iBars(_Symbol,Timeframe);
   stHandle = iCustom(_Symbol, Timeframe, "Supertrend.ex5", Periods, Multiplier);               
   return(INIT_SUCCEEDED);
}

void OnDeinit(const int reason){
}


void OnTick(){
     //for the timefilter 
     datetime LocalTime=TimeLocal();
     string HoursAndMinutes=TimeToString(LocalTime,TIME_MINUTES);
     string YearAndDate=TimeToString(LocalTime, TIME_DATE);
     MqlDateTime DateTimeStructure;
     TimeToStruct(LocalTime, DateTimeStructure);
     int DayOfWeek=DateTimeStructure.day_of_week;

     datetime time = TimeLocal();
     CurrentTime=TimeToString(time,TIME_MINUTES);    
       
   //this should only run if there is a new bar
   int bars=iBars(_Symbol, Timeframe);  
   if(totalBars !=bars){                        
      totalBars=bars;
      double st[];
      CopyBuffer(stHandle,0,0,3,st); 
 
      double close1 = iClose(_Symbol, Timeframe, 1); 
      double close2 = iClose(_Symbol, Timeframe, 2); 
      
      //BUY CONDITION
       if(close1 > st[1] && close2 < st[0]){
           if(posTicket > 0 ){
                 if(PositionSelectByTicket(posTicket)){
                     if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL){
                        if (trade.PositionClose(posTicket)){  
                             Print(__FUNCTION__," > Pos ", posTicket, "was closed..");
                         }
                       }
                    }
              }

            if(CheckTradingTime()==true || CheckTradingTime2()==true){
               if(PositionsTotal()==0 && DayOfWeek!=5){
                  Print(__FUNCTION__, " > BOUGHT");
                  if(trade.Buy(Lots, _Symbol)){  
                     if(trade.ResultRetcode() == TRADE_RETCODE_DONE){
                     posTicket= trade.ResultOrder();
                     } 
                    }
                }
              }
           }
            
         else if(close1 < st[1] && close2 > st[0]){
               if(posTicket > 0 ){
                  if(PositionSelectByTicket(posTicket)){
                     if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY){
                        if (trade.PositionClose(posTicket))  {  
                        Print(__FUNCTION__," > Pos ", posTicket, "was closed..");
                        }
                     }
                   }
                 }


               if(CheckTradingTime()==true || CheckTradingTime2()==true){
                    if(PositionsTotal()==0 && DayOfWeek!=5){
                        Print(__FUNCTION__, " > SOLD");
                        if(trade.Sell(Lots, _Symbol)){
                           if(trade.ResultRetcode() == TRADE_RETCODE_DONE){
                           posTicket= trade.ResultOrder();
                            }
                         }
                    }
               }
             }
}

 Comment (
            "TradingIsAllowed", TradingIsAllowed, TradingIsAllowed2, "\n",              //TradingIsAllowed always returns false..
            "Current Time=", CurrentTime,"\n",
            "Trading Session1=", StartTradingTime,"-" ,StopTradingTime, "\n",
            "Trading Session2=", StartTradingTime2, "-", StopTradingTime2,"\n",
            "Day of Week", DayOfWeek
            );

}


//trading session 1
bool CheckTradingTime()
   {
   if(StringSubstr(CurrentTime,0,5)==StartTradingTime)
   TradingIsAllowed=true;
   
   if(StringSubstr(CurrentTime,0,5)==StopTradingTime)
   TradingIsAllowed=false;
   
   return TradingIsAllowed;
   }
   
//trading session 2
bool CheckTradingTime2()
   {
   if(StringSubstr(CurrentTime,0,5)==StartTradingTime2)
   TradingIsAllowed2=true;
   
   if(StringSubstr(CurrentTime,0,5)==StopTradingTime2)
   TradingIsAllowed2=false;
   
   return TradingIsAllowed2;
   }
 
qwer qwer:

Hi!

I can't figure out why my timefilter doesn't work. Let's say I would like to trade between 7:35-11:30 and 14:30-22:30 and I don't want to trade on Friday.

The time filter only works when I create a simple EA with only a trade.Buy function and no other conditions.

The more complex EA should only buy/sell when the vaule of the Supertrend indicator becomes higher/lower than the price and only in the given time intervals.

It should close the position at the next sell/buy signal (if it was a buy then it should be closed at the next 'sell' signal' ). When closing positions the time interval shouldn't matter.

The 'TradingIsAllowed' variable should return 'true' when the current time is between the allowed time interval but it always returns false and I  can't figure out why. I rewrote my code 3 times already..

Could you please help me?


To don't trade on friday:

if(time.dayOfWeek>4){Filter==false;}
Reason: