下载MetaTrader 5

求一段EA源代码,计算持仓同类品种的浮动盈亏

要添加评论,请登录注册
Haihua Luo
149
Haihua Luo 2016.03.05 17:16 

请MT4编程达人指点,帮忙写一段代码。在持仓多个不同货币对不同方向单子的情况下,单独计算出某一货币对所有多单(或空单)累加的浮动盈亏金额,待调用。谢谢。

Ziheng Zhuang
1769
Ziheng Zhuang 2016.03.06 18:45  
Haihua Luo:

请MT4编程达人指点,帮忙写一段代码。在持仓多个不同货币对不同方向单子的情况下,单独计算出某一货币对所有多单(或空单)累加的浮动盈亏金额,待调用。谢谢。

//+------------------------------------------------------------------+
//|                                                         test.mq4 |
//|                        Copyright 2016, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2016, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

input int MagicNumber = 888;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
       // 比如当前品种的多单
       double profit = 0.0;
       profit = CalcProfit(Symbol(),OP_BUY,MagicNumber); // 比如当前品种的多单
       
  }
//+------------------------------------------------------------------+

double CalcProfit(const string sym,const int ordertype,const int magic)
{
       double sum = 0.0;
       for(int i=OrdersTotal()-1;i>=0;i--){
          if(OrderSelect(i,SELECT_BY_POS)){
            if(OrderMagicNumber()==magic&&OrderSymbol()==sym&&OrderType()==ordertype){
                sum+=OrderProfit()+OrderCommission()+OrderSwap();
            } 
          }          
       }       
       return(sum);
}
Haihua Luo
149
Haihua Luo 2016.03.07 05:38  
Ziheng Zhuang:
受教了,谢谢
要添加评论,请登录注册