The trade cost of Forex Currency Part3

19 April 2018, 10:44
Yupeng Xiao
0
42

Content directory

  • Overview
  • The concept of point
  • Point value
  • Spread costs
  • Output the spread costs of all Forex symbols
  • Test
  • Conclusion


Output the spread costs of all Forex symbols

The fourth part describes how to output the spread cost of all Forex symbols in the platform. 


1.  Get the spread cost of all Forex symbols in the platform

In class CForexSymbols, CForexSymbols::FxSymbolsTotal() returns the total number of foreign exchange currency pairs in the platform. CForexSymbols::IsForex() is used to determine whether a given symbol is a currency pair. Since the gold XAU and silver XAG trading instrument are CFD spread contracts, their profit calculation formulas are the same as the foreign exchange varieties, and XAU and XAG are common trading varieties. Therefore, the XAU and XAG trading varieties are also counted within the total number of foreign currency pairs. The specific code is as follows:

//+------------------------------------------------------------------+
//| 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() is used to get the symbols of all foreign exchange currency pairs in the platform. If you need to obtain the total number of foreign exchange currency pairs in the platform at the same time, first determine whether the FxSymbolsTotal() function has been called, if it has been called directly using the value of FXSymbsTotalNum variables. The specific code is as follows:

//+------------------------------------------------------------------+
//| 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);
}

In class CSpreadCost, CSpreadCost::GetSymbolsSpreadCost() is used to obtain spreads and spread costs for a given set of foreign currency pairs. CSpreadCost::GetAllSymbolsSpreadCost() is used to get spreads and spread costs of all forex currency pairs in the platform. The specific code is as follows:

//+------------------------------------------------------------------+
//| 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.  Output spread cost

The method used in this paper is to output all the spread costs obtained to the specified file. The output path is "platform path\MQL5\Files\SpreadCostData". The file name is the current date, and the output path and file name are as follows:

文件名称

The specific code output to the file is as follows:

//+------------------------------------------------------------------+
//| 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);
}

Since not all trading symbols is showed by default in MT5 platform Market Watch, the MT5 Market Watch must be set to Show All before using this EA. The operation is as follows:

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

3.  Output display

1)  In the USD account, the trading volume is a standard lot, and the spreads and spread costs of all Forex symbols in the market watch

美元账户点差成本

Figure 2.  Spread costs in USD account


2) In the EUR account, the trading volume is a standard lot, and the spreads and spread costs of all Forex symbols in the market watch

欧元账户点差成本

Figure 3.  Spread costs in EUR account

Share it with friends: