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!
PositionGetInteger(POSITION_TIME)
and
CPositionInfo position;
datetime posTime = position.Time();

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hello for all i need just to add start time and close time to this code please if any one can help me.