外汇货币对的交易成本(The trade cost of Forex Currency) Part3

12 四月 2018, 05:39
Yupeng Xiao
0
95

内容目录

  • 概述
  • 点的概念
  • 点值
  • 点差成本
  • 输出全部外汇货币对的点差成本
  • 测试
  • 结论


输出全部外汇货币对的点差成本

第四部分介绍了如何输出平台报价中所有外汇货币对的点差成本。


1.  获取平台报价中全部外汇货币对的点差成本

CForexSymbols类中,CForexSymbols::FxSymbolsTotal()用于返回平台报价中外汇货币对的总数。 CForexSymbols::IsForex()用于判断指定交易品种是否为汇货币对。 由于黄金XAU和白银XAG的交易品种是CFD价差合约,其利润计算公式与外汇品种相同,且XAU、XAG为常用交易品种,因此这里也将XAU、XAG的交易品种统计在外汇货币对总数之内。 具体代码如下所示:

//+------------------------------------------------------------------+
//| get total forex symbols number                                   |
//+------------------------------------------------------------------+  
int CForexSymbols::FxSymbolsTotal(void)
{
   int i;
   string symbol;
//--it returns the total number of all symbols.
   int SymbsTotalNum = ::SymbolsTotal(false);   
   FXSymbsTotalNum = 0;
   ::ArrayResize(AllSymbols,SymbsTotalNum);
   
   for(i = 0; i < SymbsTotalNum; i++)
   {
      symbol = ::SymbolName(i,false);
      if(!IsForex(symbol)) continue;
      
      AllSymbols[FXSymbsTotalNum] = symbol;
      FXSymbsTotalNum++;
   }
   
   return(FXSymbsTotalNum);  
}
//+------------------------------------------------------------------+
//| Judge whether this symbol is Forex or not                        |
//+------------------------------------------------------------------+
bool CForexSymbols::IsForex(string symb)
{
   long CalcMode = ::SymbolInfoInteger(symb,SYMBOL_TRADE_CALC_MODE);
   if(CalcMode != SYMBOL_CALC_MODE_FOREX)
   {
      if(FxSymbolType(symb,"XAU") == -1 && FxSymbolType(symb,"XAG") == -1) return(false);
   } 
   
   return(true);
}

CForexSymbols::GetFxSymbols()用于获得平台报价中所有外汇货币对的符号,若需要同时获得平台报价中外汇货币对的总数,先判断是否已调用过FxSymbolsTotal()函数,若已调用直接使用外汇货币对总数FXSymbsTotalNum变量值。   具体代码如下所示:

//+------------------------------------------------------------------+
//| get all forex symbols                                            |
//+------------------------------------------------------------------+
bool CForexSymbols::GetFxSymbols(string &FxSymb[])
{
   uint array_size;
//---check whether if the fuction ForexSymbolsTotal() has been called
   if(FXSymbsTotalNum == -1) 
   {
      array_size = FxSymbolsTotal();
   }
   else
   {
      array_size = FXSymbsTotalNum;
   }
   
   ::ArrayResize(FxSymb,array_size);
   
   if(::ArrayCopy(FxSymb,AllSymbols,0,0,array_size) != array_size)
   {
      ::Print(__FUNCTION__," > Array copy error !!");
      return(false);
   }   
   return(true);
}
//+------------------------------------------------------------------+
//| get all forex symbols                                            |
//+------------------------------------------------------------------+
bool CForexSymbols::GetFxSymbols(string &FxSymb[],int &FxSymbsTNum)
{
//---check whether if the fuction ForexSymbolsTotal() has been called
   if(FXSymbsTotalNum == -1) 
   {
      FxSymbsTNum = FxSymbolsTotal();
   }
   else
   {
      FxSymbsTNum = FXSymbsTotalNum;
   }
   
   ::ArrayResize(FxSymb,FxSymbsTNum);
   
   if(::ArrayCopy(FxSymb,AllSymbols,0,0,FxSymbsTNum) != FxSymbsTNum)
   {
      ::Print(__FUNCTION__," > Array copy error !!");
      return(false);
   }   
   return(true);
}

CSpreadCost类中,CSpreadCost::GetSymbolsSpreadCost()用于获得一组指定外汇货币对的点差及点差成本。CSpreadCost::GetAllSymbolsSpreadCost()用于获得平台报价中所有外汇货币对的点差及点差成本。具体代码如下所示:

//+------------------------------------------------------------------+
//| Get Symbols Spread Cost                                          |
//+------------------------------------------------------------------+
bool CSpreadCost::GetSymbolsSpreadCost(const string &Symb[],int &Spread[],double &Cost[])
{
    int i;
    int SymbsNum; 
    
    SymbsNum = ::ArraySize(Symb);
    
    for(i = 0; i < SymbsNum; i++)
    {
      
      Spread[i] = GetSpread(Symb[i]);
      Cost[i] = GetSpreadCost(Symb[i]);
    }
    
    return(true);
}
//+------------------------------------------------------------------+
//| Get All Symbols Spread Cost                                      |
//+------------------------------------------------------------------+
bool CSpreadCost::GetAllSymbolsSpreadCost(string &AllSymb[],int &AllSpread[],double &AllCost[],int &SymbsTotalNum)
{
    if(!oSymbs.GetFxSymbols(AllSymb,SymbsTotalNum))
    {
      return(false);
    }
    
    ::ArrayResize(AllSpread,SymbsTotalNum);
    ::ArrayResize(AllCost,SymbsTotalNum);
    
    for(int i = 0; i < SymbsTotalNum; i++)
    {
      
      AllSpread[i] = GetSpread(AllSymb[i]);
      AllCost[i] = GetSpreadCost(AllSymb[i]);
    }
    
    return(true);
}


2.  输出点差成本

本文采用的方法是将获取的全部点差成本输出到指定文件,输出的路径为“ 平台安装路径\MQL5\Files\SpreadCostData ”,文件名称是当前日期,输出路径及文件名称如下所示:

文件名称

输出到文件的具体代码如下所示:

//+------------------------------------------------------------------+
//| Output Trade Cost to file                                        |
//+------------------------------------------------------------------+
bool Output()
{
   int totalnum;
   string symbols[];
   int spread[];
   double spread_cost[];
   string FileName;
   string date = TimeToString(TimeLocal(),TIME_DATE);
   string minute = TimeToString(TimeLocal(),TIME_MINUTES);
   
   if(StringReplace(minute,":","h") == -1)
   {
      Print("StringReplace error!");
      return(false);
   }
   FileName = date+" "+minute+"m.csv";
   oSpreadCost.GetAllSymbolsSpreadCost(symbols,spread,spread_cost,totalnum);
   
   
   int file_handle=FileOpen(DirectoryName+"\\"+FileName,FILE_WRITE|FILE_CSV); 
   PrintFormat("%s file is available for writing",FileName); 
      
   FileWrite(file_handle,"ForexSymbol","Spread","TradeCost("+AccountInfoString(ACCOUNT_CURRENCY)
             +"/"+DoubleToString(Extlots,2)+" lot(s))"); 
   if(file_handle!=INVALID_HANDLE) 
   { 
      for(int i=0;i<totalnum;i++) 
      {
         FileWrite(file_handle,symbols[i],spread[i],NormalizeDouble(spread_cost[i],2)); 
      }
      //--- close the file 
      FileClose(file_handle); 
      PrintFormat("Data is written, %s file is closed",FileName); 
   } 
   else
      PrintFormat("Failed to open %s file, Error code = %d",FileName,GetLastError());
   
   return(true);
}

由于MT5平台市场报价中默认不会显示所有交易品种,因此在使用此EA前需将MT5的市场报价设置为显示全部,操作如下所示:

显示全部品种中文      显示全部品种英文

3.  输出结果展示

1)  美元账户下,交易量为一标准手,市场报价中所有外汇货币对的点差及点差成本

美元账户点差成本

图2.  美元账户点差成本


2) 欧元账户下,交易量为一标准手,市场报价中所有外汇货币对的点差及点差成本

欧元账户点差成本

图3.  欧元账户点差成本

在社交网络与好友分享: