# 计算当日赢亏状况函数代码

MT4版：

double TodayProfit()
{
int i; double yl=0,hyl=0;
if(OrdersTotal()>0)                                  //当前持仓订单浮盈亏
{
for(i=OrdersTotal()-1;i>=0;i--)
{
{
if(TimeDay(OrderOpenTime())==TimeDay(CurTime()) && TimeMonth(OrderOpenTime())==TimeMonth(CurTime())
&& TimeYear(OrderOpenTime())==TimeYear(CurTime()))
yl+=OrderProfit();
}
}
}
if(OrdersHistoryTotal()>0)                         // 已了结的订单实盈亏
{
for(i=OrdersHistoryTotal()-1;i>=0;i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY))
{
if(TimeDay(OrderCloseTime())==TimeDay(CurTime()) && TimeMonth(OrderCloseTime())==TimeMonth(CurTime())
&& TimeYear(OrderCloseTime())==TimeYear(CurTime()))
hyl+=OrderProfit();
if(TimeDay(OrderCloseTime())!=TimeDay(CurTime()))  break;
}
}
}
return(yl+hyl);                                         //实盈亏与浮盈亏合计
}

MT5版：

double TodayProfit()
{
MqlDateTime stm,tm;
TimeToStruct(TimeCurrent(),tm);
int i;
ulong Tick;
double yl=0,hyl=0;
if(PositionsTotal()>0)                                   //当前持仓订单浮盈亏
{
for(i=PositionsTotal()-1;i>=0;i--)
{
if(PositionSelectByTicket(PositionGetTicket(i)))
{
TimeToStruct(PositionGetInteger(POSITION_TIME),stm);
if(stm.day==tm.day && stm.mon==tm.mon && stm.year==tm.year)
yl+=PositionGetDouble(POSITION_PROFIT);
}
}
}
HistorySelect(0,TimeCurrent());
if(HistoryDealsTotal()>0)                               // 已了结的订单实盈亏
{
for(i=HistoryDealsTotal()-1;i>=0;i--)
{
Tick=HistoryDealGetTicket(i);
if(HistoryDealGetInteger(Tick,DEAL_ENTRY)==1)
{
TimeToStruct(HistoryDealGetInteger(Tick,DEAL_TIME),stm);
if(stm.day==tm.day && stm.mon==tm.mon && stm.year==tm.year)
hyl+=HistoryDealGetDouble(Tick,DEAL_PROFIT);
if(stm.day!=tm.day)  break;
}
}
}
return(yl+hyl);                                              //实盈亏与浮盈亏合计
}

Nianfu Li:

MT4版：

double TodayProfit()
{
int i; double yl=0,hyl=0;
if(OrdersTotal()>0)                                  //当前持仓订单浮盈亏
{
for(i=OrdersTotal()-1;i>=0;i--)
{
{
if(TimeDay(OrderOpenTime())==TimeDay(CurTime()) && TimeMonth(OrderOpenTime())==TimeMonth(CurTime())
&& TimeYear(OrderOpenTime())==TimeYear(CurTime()))
yl+=OrderProfit();
}
}
}
if(OrdersHistoryTotal()>0)                         // 已了结的订单实盈亏
{
for(i=OrdersHistoryTotal()-1;i>=0;i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY))
{
if(TimeDay(OrderCloseTime())==TimeDay(CurTime()) && TimeMonth(OrderCloseTime())==TimeMonth(CurTime())
&& TimeYear(OrderCloseTime())==TimeYear(CurTime()))
hyl+=OrderProfit();
if(TimeDay(OrderCloseTime())!=TimeDay(CurTime()))  break;
}
}
}
return(yl+hyl);                                         //实盈亏与浮盈亏合计
}

MT5版：

double TodayProfit()
{
MqlDateTime stm,tm;
TimeToStruct(TimeCurrent(),tm);
int i;
ulong Tick;
double yl=0,hyl=0;
if(PositionsTotal()>0)                                   //当前持仓订单浮盈亏
{
for(i=PositionsTotal()-1;i>=0;i--)
{
if(PositionSelectByTicket(PositionGetTicket(i)))
{
TimeToStruct(PositionGetInteger(POSITION_TIME),stm);
if(stm.day==tm.day && stm.mon==tm.mon && stm.year==tm.year)
yl+=PositionGetDouble(POSITION_PROFIT);
}
}
}
HistorySelect(0,TimeCurrent());
if(HistoryDealsTotal()>0)                               // 已了结的订单实盈亏
{
for(i=HistoryDealsTotal()-1;i>=0;i--)
{
Tick=HistoryDealGetTicket(i);
if(HistoryDealGetInteger(Tick,DEAL_ENTRY)==1)
{
TimeToStruct(HistoryDealGetInteger(Tick,DEAL_TIME),stm);
if(stm.day==tm.day && stm.mon==tm.mon && stm.year==tm.year)
hyl+=HistoryDealGetDouble(Tick,DEAL_PROFIT);
if(stm.day!=tm.day)  break;
}
}
}
return(yl+hyl);                                              //实盈亏与浮盈亏合计
}

double CostToPip(int type)//将手续费和隔夜利息转换为点值 0 代表多单 ， 1 代表空单

{

double point = 0;

double tick_value =0;

if(Digits() == 3 || Digits() == 5)

{

point = Point() * 10;

tick_value = SymbolInfoDouble(Symbol(), SYMBOL_TRADE_TICK_VALUE) * 10;

}

else

{

point = Point();

}

ulong ticket;

double pips = 0.0;

double close_total = HistoryDealsTotal();

double lot = 0.0;

double cost = 0.0;

ENUM_DEAL_TYPE deal_type = NULL;

switch(type)

{

case 0:

break;

case 1:

deal_type = DEAL_TYPE_SELL;

break;

}

if(close_total > 0)

{

for(int i = 0; i < close_total; i++)

{

ticket = HistoryDealGetTicket(i);

if(HistoryDealGetString(ticket, DEAL_SYMBOL) == Symbol()

&& HistoryDealGetInteger(ticket, DEAL_TYPE) == deal_type

&& HistoryDealGetInteger(ticket, DEAL_MAGIC) == Magic)

{

cost = HistoryDealGetDouble(ticket, DEAL_COMMISSION) + HistoryDealGetDouble(ticket, DEAL_SWAP);

lot = HistoryDealGetDouble(ticket, DEAL_VOLUME);

}

}

}

if(cost < 0)

{

pips = MathAbs(cost) / lot / tick_value * point;

}

else

pips = 0.0;

return(pips);

}

When you post code please use the CODE button (Alt-S)!