Please help me

 

Hello for all i need just to add start time and close time to this code please if any one can help me.

#include <Trade/Trade.mqh>



input int Magic = 2; // Mn.



int barsTotal;

CTrade trade;



int handles[];



string symbols[] = {

   "AUDUSD_",

   "EURUSD_",

   "GBPUSD_",

   "USDCAD_",

   "USDCHF_",

   "USDJPY_",

};



double volumes[] = {

   0.01,

   0.01,

   0.01,

   0.01,

   0.01,

   0.01,



};

int OnInit(){

   ArrayResize(handles,ArraySize(symbols));

   for(int i = 0; i < ArraySize(symbols); i++){

      handles[i] = iRSI(symbols[i],PERIOD_CURRENT,14,PRICE_CLOSE);

   }

   

   trade.SetExpertMagicNumber(Magic);



   return(INIT_SUCCEEDED);

}



void OnDeinit(const int reason){



   

}



void OnTick(){



   int bars = iBars(_Symbol,PERIOD_CURRENT);

   if(barsTotal < bars){

   barsTotal = bars;

   

      for(int i = 0; i < ArraySize(symbols); i++){

         doStuff(symbols[i],handles[i],volumes[i]);

      }

   }      

}



void doStuff(string symbol, int handle, double volume){

   double rsi[];

   CopyBuffer(handle,MAIN_LINE,1,1,rsi);

   

   bool isRsiBuy = rsi[0] < 30;

   bool isRsiSell = rsi[0] > 70;

   

   for(int i = PositionsTotal()-1; i >= 0; i--){

      ulong posTicket = PositionGetTicket(i);

      if(PositionGetString(POSITION_SYMBOL) == symbol && PositionGetInteger(POSITION_MAGIC) == Magic){

         if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY){

            if(isRsiSell){

               trade.PositionClose(posTicket);

            }

            isRsiSell = false;

         }else if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL){

            if(isRsiBuy){

               trade.PositionClose(posTicket);

            }

            isRsiSell = false;

         }

      }

   }

   

   if(isRsiSell){

      trade.Sell(volume,symbol);

   }else if(isRsiBuy){

      trade.Buy(volume,symbol);

   }

} 
Files:
 

Create an MqlDateTime variable for both open and close:

MqlDateTime opentime; 
MqlDateTime closetime;

When placing a buy or a sell update the opentime to the current time:

   if(isRsiSell){

      trade.Sell(volume,symbol);
      TimeCurrent(opentime);//set opentime to current time

   }else if(isRsiBuy){

      trade.Buy(volume,symbol);
      TimeCurrent(opentime); //set opentime to current time
   }

Add the same thing for closing a position:

 if(PositionGetInteger(POSITION_TYPE) == POSITIOSN_TYPE_BUY){
            if(isRsiSell){
               trade.PositionClose(posTicket);
		TimeCurrent(closetime); //set closetime to current time
            }
            isRsiSell = false;

         }else if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL){
            if(isRsiBuy){
               trade.PositionClose(posTicket);
		TimeCurrent(closetime); //set closetime to current time
            }
            isRsiSell = false;
         }

The data structure that we used, MqlDateTime has the following format:

struct MqlDateTime
  {
   int year;           // Year
   int mon;            // Month
   int day;            // Day
   int hour;           // Hour
   int min;            // Minutes
   int sec;            // Seconds
   int day_of_week;    // Day of week (0-Sunday, 1-Monday, ... ,6-Saturday)
   int day_of_year;    // Day number of the year (January 1st is assigned the number value of zero)
  };

So to get the year, month, day, hour, minute, second, day of week, or day of year, you would use opentime.year/mon/day/hour... Or for the close time closetime.year/mon...

There is another data structure you could use instead if you wish, the datetime data structure. Its value is the number of seconds since January 01, 1970. You can set it with TimeCurrent() like this: datetimevar = TimeCurrent(). There are also some functions to switch in between the two.

Most of this is from memory so if you can't get it to work or you need more help just let me know.

And for next time, I would try to make the title be your specific question instead of just asking for help. I think most people would see that and just scroll over it.

Good luck in your coding!

 
if you want to do things with a trade based on time there is also 
PositionGetInteger(POSITION_TIME)

and

   CPositionInfo position;
   
   datetime posTime = position.Time();
Reason: