double CheckProfitSofar()
{
datetime today = iTime(_Symbol,PERIOD_D1,0);
int dDay=TimeDay(today);
int dMonth = TimeMonth(today);
int dYear = TimeYear(today);
int todayYear, todayMonth, todayDay;
datetime opnOrdTime = 0;
double opnOrdProfit =0;
double addLoss = 0;
for(int s=0 ; s<OrdersHistoryTotal(); s++)
{
if ( OrderSelect( s, SELECT_BY_POS, MODE_HISTORY ) == true )
opnOrdTime = OrderCloseTime();
todayYear=TimeYear(opnOrdTime);
todayMonth =TimeMonth(opnOrdTime);
todayDay=TimeDay(opnOrdTime);
if ( dYear==todayYear && dMonth==todayMonth && dDay==todayDay )
totalLoss_Profit += (OrderProfit() + OrderSwap() + OrderCommission());
}
Comment(dYear," ",dMonth," ",dDay," ",todayYear," ",todayMonth," ",todayDay);
return totalLoss_Profit;
}
What i want to is that i want to calculate the profit (whether +ve or -ve) for a particular day. e.g today. i used the "datetime today" to get the opening time of the first candle of today, then follows. It is not returning the accurate value. Please help out. Thanks in advance.
You just need start of that particular day received by datetime timeStart = iTime(_Symbol,PERIOD_D1,i)
and end of that day datetime timeEnd=timeStart+PeriodSeconds(PERIOD_D1);
. Then loop over closed deals and filter them out if OrderClosePrice()
is not in range.
double getClosedPnlOfDay(const int indexDay)
{
const datetime timeStart=iTime(_Symbol,PERIOD_D1,indexDay),
timeEnd = timeStart+PeriodSeconds(PERIOD_D1);
double result=0.;
for(int i=OrdersHistoryTotal()-1;i>=0;i--)
{
if(!OrderSelect(i,SELECT_BY_POS,MODE_HISTORY))continue;
//filter by OrderSymbol() and OrderMagicNumber() here
if(OrderCloseTime()<timeStart || OrderCloseTime()>=timeEnd) continue;
result+=OrderProfit() + OrderCommission() + OrderSwap();
}
return result;
}