Trade time filter by hour

 

Hi

I thought this would be easy, but after a whole day trying different options, i don't seem to be any closer :(

I just want to have an external variable where I enter a list of the hours to exclude from trading. (just based on server time)

I have created a string array variable, and found some code that I thought might help, and modified that, but I don't seem to be able to pull the values out of the array to compare to TimeHour(TimeCurrent()).

I put the split string code in the init () function as i didn't want this to calculate every tick.

How can I access the values in the array from another function (CheckSessionTime()) which is called in Start()?

As a workaround I created a whole bunch of variables that read from the array, but that seems messy and didn't work anyway.

extern bool      SessionTime=true; 
extern string    NonTradingHours = "23,0,1,6,7,8,9,10";

string stri[];
int NTI;
int hournow;
int NTHvalue1= 0;
int NTHvalue2= 0;
int NTHvalue3= 0;
int NTHvalue4= 0;
int NTHvalue5= 0;
int NTHvalue6= 0;
int NTHvalue7= 0;
int NTHvalue8= 0;

int init()
  {
/////Split string code


int char[],pos;


for(NTI=0;NTI<StringLen(NonTradingHours);NTI++) ///NTI - NonTradingIndex
   {
   if(StringGetChar(NonTradingHours,NTI)==44)//code ","
      {
      pos++;
      ArrayResize(char,pos);
      char[pos-1]=NTI;
      //Print (i);
      }
   }
ArrayResize(stri,pos+1);
for(NTI=0;NTI<=pos;NTI++)
   {
   if(NTI==0)stri[0]=StringSubstr(NonTradingHours,0,char[NTI]);
   else if(NTI==pos)stri[NTI]=StringSubstr(NonTradingHours,char[NTI-1]+1);
   else stri[NTI]=StringSubstr(NonTradingHours,char[NTI-1]+1,char[NTI]-char[NTI-1]-1);
   NTHvalue1 = StrToInteger(stri[0]);  // all these variables were my last workaround attempt
   NTHvalue2 = StrToInteger(stri[1]); // but ideally i just want to get the values from stri[NTI]
   NTHvalue3 = StrToInteger(stri[2]);
   NTHvalue4 = StrToInteger(stri[3]);
   NTHvalue5 = StrToInteger(stri[4]);
   NTHvalue6 = StrToInteger(stri[5]);
   NTHvalue7 = StrToInteger(stri[6]);
   NTHvalue8 = StrToInteger(stri[7]);
   
   }



int start()
  {
if (AllowTrading()==true)

Execute code......

  return(0);
  }






///Allow trading function - if the extern variable is true then the times are checked otherwise trading is allowed anyway.
bool AllowTrading()
{
bool TradeAllowed=false;

if (SessionTime==true)
{
  
   if (CheckSessionTime()==true)
   {
      TradeAllowed=true;
   }
      else TradeAllowed=false;
}
else TradeAllowed=true;

return(TradeAllowed);
//Print(CheckSessionTime());
}  

///Business days function - sets the business days that can be traded ie Monday to Friday.  
bool BusinessDay()
{
if(DayOfWeek()>=1 || DayOfWeek()<6) 

return(1);
} 

/// CheckSession time function - checks the current time to extern variables .
bool CheckSessionTime ()
{
hournow = TimeHour(TimeCurrent());
if (BusinessDay()==true)
   if (NTHvalue1 != hournow || NTHvalue2 != hournow || NTHvalue3 != hournow || NTHvalue4 != hournow ||
      NTHvalue5 != hournow || NTHvalue6 != hournow || NTHvalue7 != hournow || NTHvalue8 != hournow)
      
  return(1);
  
}
 
simoncs:

Hi

I thought this would be easy, but after a whole day trying different options, i don't seem to be any closer :(

I just want to have an external variable where I enter a list of the hours to exclude from trading. (just based on server time)

I have created a string array variable, and found some code that I thought might help, and modified that, but I don't seem to be able to pull the values out of the array to compare to TimeHour(TimeCurrent()).

I put the split string code in the init () function as i didn't want this to calculate every tick.

How can I access the values in the array from another function (CheckSessionTime()) which is called in Start()?

As a workaround I created a whole bunch of variables that read from the array, but that seems messy and didn't work anyway.

Declare your array globally not local to int() then you can access it from anywhere . . . while you are at it, change the name of your array, char is a type in the new mql4 . . .
 
RaptorUK:
Declare your array globally not local to int() then you can access it from anywhere . . . while you are at it, change the name of your array, char is a type in the new mql4 . . .


Thanks, made those changes but I still can't seem to get the values.

string stri[];
int NTI;
int AR[];
int pos;
int hournow;

int init()
  {

for(NTI=0;NTI<StringLen(NonTradingHours);NTI++) ///NTI - NonTradingIndex
   {
   if(StringGetChar(NonTradingHours,NTI)==44)//code ","
      {
      pos++;
      ArrayResize(AR,pos);
      AR[pos-1]=NTI;
      //Print (i);
      }
   }
ArrayResize(stri,pos+1);
for(NTI=0;NTI<=pos;NTI++)
   {
   if(NTI==0)stri[0]=StringSubstr(NonTradingHours,0,AR[NTI]);
   else if(NTI==pos)stri[NTI]=StringSubstr(NonTradingHours,AR[NTI-1]+1);
   else stri[NTI]=StringSubstr(NonTradingHours,AR[NTI-1]+1,AR[NTI]-AR[NTI-1]-1);
   NTHvalue1 = StrToInteger(stri[0]);
   NTHvalue2 = StrToInteger(stri[1]);
   NTHvalue3 = StrToInteger(stri[2]);
   NTHvalue4 = StrToInteger(stri[3]);
   NTHvalue5 = StrToInteger(stri[4]);
   NTHvalue6 = StrToInteger(stri[5]);
   NTHvalue7 = StrToInteger(stri[6]);
   NTHvalue8 = StrToInteger(stri[7]);
   
   }
   

I tried to amend my function...

/// CheckSession time function - checks the current time to the start and end time extern variables.
bool CheckSessionTime ()
{
hournow = TimeHour(TimeCurrent());
if (BusinessDay()==true)
   if (hournow != StrToInteger(stri[NTI]))
   //(NTHvalue1 != hournow || NTHvalue2 != hournow || NTHvalue3 != hournow || NTHvalue4 != hournow ||
     // NTHvalue5 != hournow || NTHvalue6 != hournow || NTHvalue7 != hournow || NTHvalue8 != hournow)
      
  return(1);
  
}

which didn't work and i also added Print statements to the start, but it doesn't output the value from the array.

Print("i - ",NTI," str - ",stri[NTI], "hournow = ", hournow);
 
simoncs:


Thanks, made those changes but I still can't seem to get the values.

I tried to amend my function...

which didn't work and i also added Print statements to the start, but it doesn't output the value from the array.

Can't you do something like this . . .

/// TradeThisHour function - returns true if it's OK to trade during this hour
bool TradeThisHour()
   {
   hournow = TimeHour(TimeCurrent());
   if( BusinessDay() )
      if( StringFind( NonTradingHours, DoubleToStr(hournow, 0) ) == -1 ) return(true);  // hour not found
      
   return(false);
   }
 
RaptorUK:

Can't you do something like this . . .


that looks a lot cleaner, but haven't managed to get it to work yet.

i integrated that function into mine...

///Allow trading function - if the extern variable is true then the times are checked otherwise trading is allowed anyway.
bool AllowTrading()
{
bool TradeAllowed=false;

if (SessionTime==true)
{
  
   if(TradeThisHour()==true)//if (CheckSessionTime()==true) 
   {
      TradeAllowed=true;
   }
      else TradeAllowed=false;
}
else TradeAllowed=true;

return(TradeAllowed);
//Print(CheckSessionTime());
}  

interestingly i still get trades during the nontrading hours, but i don't get any print messages for those hours.

 if (AllowTrading()==true)
    Print( "hournow = ", hournow," TradeThisHour =",TradeThisHour());
 
simoncs:


that looks a lot cleaner, but haven't managed to get it to work yet.

i integrated that function into mine...

interestingly i still get trades during the nontrading hours, but i don't get any print messages for those hours.

Each time you place a trade also print the current hour, the NonTradingHours string, the value of TradeAllowed and then when you have a trade that shouldn't have happened check the variables printed and find out what is going wrong.
 
RaptorUK:
Each time you place a trade also print the current hour, the NonTradingHours string, the value of TradeAllowed and then when you have a trade that shouldn't have happened check the variables printed and find out what is going wrong.


Thanks mate - it worked a treat!

No idea what was preventing it from running - just added the print statements, and removed my coding attempt, and it worked fine this morning. I'll just put it down to a late night!

thanks for your help - this is a much cleaner solution than mine.

Reason: