How to close all trades and no longer open any new trades at a certain time? Beginner

konohamaru17
32
konohamaru17  

Hello, i have been learning mql5 for about a week now and i have a super simple RSI bot that opens a trade when it hits either the 70 or 30 and then closes the trade when it hits the stop loss or take profit. But I need to add a time function i guess is what you call it. I want to add some lines of code that says it has to close any open trades and that it cant open any new trades at 5pm

if the current time> 5pm close all trades and do not open new any trades. something like that so it doesn't open or hold trades over night.


I also want to add some lines of code that say when its allowed to open trades for example it can only open and close trades between 9:05 am and 5:00 pm

if current time > 9:05 am it is allowed to open trades


Thank you for anyone that can help.

Ferhat Mutlu
12118
Ferhat Mutlu  
konohamaru17:

Hello, i have been learning mql5 for about a week now and i have a super simple RSI bot that opens a trade when it hits either the 70 or 30 and then closes the trade when it hits the stop loss or take profit. But I need to add a time function i guess is what you call it. I want to add some lines of code that says it has to close any open trades and that it cant open any new trades at 5pm

if the current time> 5pm close all trades and do not open new any trades. something like that so it doesn't open or hold trades over night.


I also want to add some lines of code that say when its allowed to open trades for example it can only open and close trades between 9:05 am and 5:00 pm

if current time > 9:05 am it is allowed to open trades


Thank you for anyone that can help.

Share some code that you write. Without that its like asking the job from others to make instead of you
William Roeder
25051
William Roeder  
konohamaru17: I also want to add some lines of code that say when its allowed to open trades for example it can only open and close trades between 9:05 am and 5:00 pm

Just find out what time it is.

#define HR0905 32700 // (9*60+5)*60
#define HR1700 61200 // 5 pm = 1700 hours
SECONDS tod = time();
bool canTrade = HR0905 <= tod && tod < HR1700;

          Find bar of the same time one day ago - MQL4 programming forum 2017.10.06

konohamaru17
32
konohamaru17  
William Roeder:

Just find out what time it is.

          Find bar of the same time one day ago - MQL4 programming forum 2017.10.06

Thank you for this, even though i dont understand this perfectly I always try to understand. 


Im guessing #define is defining the hours that we will use, so HR0905 is 9:05 am.

32700 is the sum of  (9*60+5)*60. 

Not sure why you have to put that there, 


61200 is the result of (17*60)*60 which we use for HR1700.


SECONDS tod i guess is seconds Time Of Day not sure why we put time(); i also dont know what SECONDS tod= time(); does but im guessing that its declaring something not sure what though.

but i know that the empty () means its void and the ; is like a period.


bool is a boolean statement, I think that canTrade is a function or variable but probably a function because i think it would have to be declared before in order  for it to be a variable


the bool canTrade is saying that if its greater than 9:05am  and between/until a time greater than 17:00 pm than it may trade.


Am I correct?

Sorry for making you write the code for me but im just trying to learn as much as possible.


EDIT1: When i copy and paste and then compile it doesent work lol.

Also, could i make it trade during 2 time during the day?

Like it can trade from 9:05 am until 11:00 am and then it stops at 11:00am


and then I want it to start trading again at 2:00 Pm until 3:00 Pm

Could i do this? Thank you.


Heres my bots code, it uses Ctrade, idk if that makes any difference. Its in MQL5


   #include<Trade\Trade.mqh>
   
   
   CTrade trade;
   
   
void OnTick()
  {
   double Ask=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);
   
   double Bid=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);
   
   string signal="";
   
   double myRSIArray[];
   
   ArraySetAsSeries(myRSIArray,true);
   
   int myRSIDefinition = iRSI(_Symbol,_Period,14,PRICE_CLOSE); 

   CopyBuffer(myRSIDefinition,0,0,3,myRSIArray);
   
   double myRSIValue=NormalizeDouble(myRSIArray[0],2);
   
   if (myRSIValue>70)
   signal="sell";
  
   if (myRSIValue<30)
   signal="buy";
   
   if (signal =="sell" && PositionsTotal()<1)
      trade.Sell(1,NULL,Bid,(Bid+200 * _Point),(Bid- 200* _Point),NULL);

      
   if (signal =="buy" && PositionsTotal()<1)
      trade.Buy(1,NULL,Ask,(Ask-200 *_Point),(Ask+ 200* _Point),NULL);
 
   Comment ("The signal is now: ",signal);
   
  }
konohamaru17
32
konohamaru17  
Ferhat Mutlu:
Share some code that you write. Without that its like asking the job from others to make instead of you

Of course,


Heres the my bots code. Its pretty basic as you can see

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


void OnTick()
  {
  


  
  
   double Ask=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);
   
   double Bid=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);
   
   string signal="";
   
   double myRSIArray[];
   
   ArraySetAsSeries(myRSIArray,true);
   
   int myRSIDefinition = iRSI(_Symbol,_Period,14,PRICE_CLOSE); 

   CopyBuffer(myRSIDefinition,0,0,3,myRSIArray);
   
   double myRSIValue=NormalizeDouble(myRSIArray[0],2);
   
   if (myRSIValue>70)
   signal="sell";
  
   if (myRSIValue<30)
   signal="buy";
   
   if (signal =="sell" && PositionsTotal()<1)
      trade.Sell(1,NULL,Bid,(Bid+
200  * _Point),(Bid- 
200 * _Point),NULL);

      
   if (signal =="buy" && PositionsTotal()<1)
      trade.Buy(1,NULL,Ask,(Ask-
200  *_Point),(Ask+ 
200 * _Point),NULL);
 
   Comment ("The signal is now: ",signal);
   
  }

I was given some code by William Roeder, but when i copy and paste and then compile I get error messages. Im not sure where i should start looking in order to create this code that i need, thats why i posted here

#define HR0905 32700 // (9*60+5)*60
#define HR1700 61200 // 5 pm = 1700 hours
SECONDS tod = time();
bool canTrade = HR0905 <= tod && tod < HR1700;