MQL5语言提供了多种函数获取历史订单,下面先分享一种,另一种之后补充。 下面函数的功能是获取账户下本币种的所有历史单子的盈亏。
- double GetProfitHis(datetime t)
-
- {
-
- double res=0;
-
- //--- 请求交易历史记录
-
- HistorySelect(0,TimeCurrent());
-
- //--- 创建物件
-
- uint total=HistoryDealsTotal();
-
- ulong ticket=0;
-
- double price;
-
- double profit;
-
- double comdt;
-
- double swap;
-
- double fee;
-
- datetime time;
-
- string symbol;
-
- long type;
-
- long entry;
-
- //--- 所有交易
-
- for(uint i=0; i<total; i++)
-
- {
-
- //--- 尽力获得交易报价
-
- if((ticket=HistoryDealGetTicket(i))>0)
-
- {
-
- //--- 获得交易属性
-
- price =HistoryDealGetDouble(ticket,DEAL_PRICE);
-
- time =(datetime)HistoryDealGetInteger(ticket,DEAL_TIME);
-
- symbol=HistoryDealGetString(ticket,DEAL_SYMBOL);
-
- type =HistoryDealGetInteger(ticket,DEAL_TYPE);
-
- entry =HistoryDealGetInteger(ticket,DEAL_ENTRY);
-
- profit=HistoryDealGetDouble(ticket,DEAL_PROFIT);
-
- comdt=HistoryDealGetDouble(ticket,DEAL_COMMISSION);
-
- swap=HistoryDealGetDouble(ticket,DEAL_SWAP);
-
- fee=HistoryDealGetDouble(ticket,DEAL_FEE);
-
-
-
- //--- 只对当前交易品种
-
- if(price && time && symbol==Symbol())
-
- {
-
- res+=profit+comdt+swap+fee;
-
- }
-
- }
-
- }
-
- return res;
-
- }
-
- 如果要获取当前持仓单的盈亏呢?下面提供函数。
-
- double GetProfit(datetime t)
-
- {
-
- double res=0;
-
- for(int i=0; i<PositionsTotal(); i++)
-
- {
-
- ulong ticket=PositionGetTicket(i);
-
- string ordsymbol = PositionGetString(POSITION_SYMBOL);
-
- long ordmagic = PositionGetInteger(POSITION_MAGIC);
-
- long ordtype = PositionGetInteger(POSITION_TYPE);
-
- double profit=PositionGetDouble(POSITION_PROFIT);
-
- double swap=PositionGetDouble(POSITION_SWAP);
-
- if(ordsymbol==Symbol() && PositionGetInteger(POSITION_MAGIC)==magic)
-
- {
-
- if(ordtype==POSITION_TYPE_BUY)
-
- {
-
- res+=profit+swap;
-
- }
-
- if(ordtype==POSITION_TYPE_SELL)
-
- {
-
- res+=profit+swap;
-
- }
-
- }
-
- }
-
- return res;
-
- }
-
-
- //+------------------------------------------------------------------+
-
复制代码
|