The trade cost of Forex Currency Part2

19 April 2018, 10:04
Yupeng Xiao
0
43

Content directory

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


Spread cost

价格和点差

Figure 1. Buy and sell prices and spreads for some Forex symbols


The spread is the difference between Ask and Bid. The rightmost column in Figure 1 is the spread in point of each currency pair. Because of the spread, there will be an initial loss of buying and selling any currency pair. This loss is the trade cost of the currency pair.

1.  formula

According to formulas (1), (2), (3) and (4) above, the spread cost can be summarized in the following three situations:

1) The base currency of the direct currency pair of the profit currency is the account currency

The US dollar account is used as an example. For example: USDJPY, EURJPY, the profit currency are both JPY, the direct currency pair of JPY is USDJPY, and the base currency of USDJPY is the account currency, USD.

The spread cost in this case is:  成本公式1         (Formula 5)

among them:

  • Lots:trade volume
  • ContractSize:One standard contract size, foreign currency variety is 100,000 base currency
  • SymbPoint:the minimum price change for the currency pair (one pip)
  • Price:the current price of the currency pair

2) Profit currency is account currency

The US dollar account is used as an example. For example: EURUSD,GBPUSD, etc.

The spread cost in this case is:   成本公式2         (Formula 6)

3) The base currency of the direct currency pair of profit currency is non-account currency

The US dollar account is used as an example. For example: EURGBP, GBPAUD, etc.

The spread cost in this case is:   成本公式3         (Formula 7)

Among them: Price is the current price of the direct currency pair of the profit currency.

2.  Code

First define the class CSpreadCost. The class CSpreadCost is used to calculate the spread cost of the currency pair and other related operations.The code to calculate the spread cost is as follows, where CSpreadCost::GetSpread() is used to obtain the spread of the currency pair and CSpreadCost::GetSpreadCost() is used to calculate the spread cost resulting from the spread. The yellow background part is the spread cost calculation code corresponding to formula (6), and the green background part is the spread cost calculation code corresponding to formulas (5) and (7).

//+------------------------------------------------------------------+
//| Get spread                                                       |
//+------------------------------------------------------------------+
int CSpreadCost::GetSpread(string Symb)
{
   int spread;
   spread = (int)::SymbolInfoInteger(Symb,SYMBOL_SPREAD);
   return(spread);
}
//+------------------------------------------------------------------+
//| Get spread cost                                                  |
//+------------------------------------------------------------------+
double CSpreadCost::GetSpreadCost(string Symb)
{
   double price;
   double ContractSize = ::SymbolInfoDouble(Symb,SYMBOL_TRADE_CONTRACT_SIZE);
   double SymbPoint = ::SymbolInfoDouble(Symb,SYMBOL_POINT);
   string SymbProfit = ::SymbolInfoString(Symb,SYMBOL_CURRENCY_PROFIT);
   int spread = GetSpread(Symb);
   double spreadcost = 0;
   if(SymbProfit == AccountSymbol)
   {
      spreadcost = Lots*ContractSize*SymbPoint*spread;
   }
   else
   {
      price = oSymbs.FxSymbPrice(SymbProfit);
      spreadcost = Lots*ContractSize*SymbPoint*spread*price;
   }
   
   return(spreadcost);
}

In formulas (5) and (7), Price is the current price of the direct currency pair of profit currency, and formula (5) can be converted into:

成本公式4

Therefore, in CSpreadCost::GetSpread(), the spread cost calculation methods corresponding to equations (5) and (7) are unified into one formula.

Since there may be no quotation for the direct currency pair on the platform, for example, in GBP account, USDHKD’s profit currency is HKD, because the account currency is GBP, so its direct currency pair is GBPHKD, and the platform may not have a direct quote from GBPHKD, which deals with the following:

  • In the currency pair of profit currency and USD, USD is the base currency. In the currency pair of account currency and USD, USD is the base currency. In this case there are:组合价格1

  • In the currency pair of profit currency and USD, USD is the base currency. In the currency pair of account currency and USD, account currency is the base currency. In this case there are:组合价格2

  • In the currency pair of profit currency and USD, profit currency is the base currency. In the currency pair of account currency and USD, USD is the base currency. In this case there are:组合价格3

  • In the currency pair of profit currency and USD, profit currency is the base currency. In the currency pair of account currency and USD, account currency is the base currency. In this case there are:组合价格4

Where: ProfitCur. is the profit currency and AccountCur. is the account currency.

The calculation code for Price is implemented in the class CForexSymbols. The class CForexSymbols is used to obtain the currency pair's related operations. CForexSymbols::FxSymbPrice() is used to calculate the price of a direct currency pair based on the specified currency. The specific code is as follows:

//+------------------------------------------------------------------+
//| Get the Symbol price,                                            |
//| which base currency is SymbBase,                                 |
//| and the profit currency is AccountSymbol                         | 
//+------------------------------------------------------------------+
double CForexSymbols::FxSymbPrice(string SymbBase)
{
   MqlTick LatestPrice;
   string SymbProfitFullName;
   string SymbFullName1;
   string SymbFullName2;
   double price = -1;
   
   if(SymbBase == AccountSymbol)
   {
      ::Print(__FUNCTION__," > Parameters error!!");
      return(-1);
   }
   
   SymbProfitFullName = FxSymbFullName(SymbBase);
   
   if(SymbProfitFullName == "")//no currency contains SymbBase and AccountSymbol in the platform
   {
      SymbFullName1 = FxSymbFullName(SymbBase,"USD");
      SymbFullName2 = FxSymbFullName(AccountSymbol,"USD");
      if(FxSymbolType(SymbFullName1,"USD") == 0)
      {
         ::SymbolInfoTick(SymbFullName1,LatestPrice);
         price = 1/LatestPrice.bid;
         if(FxSymbolType(SymbFullName2,"USD") == 0)
         {
            ::SymbolInfoTick(SymbFullName2,LatestPrice);
            price = price*LatestPrice.bid;
         }
         else if(FxSymbolType(SymbFullName2,"USD") == 1)
         {
            ::SymbolInfoTick(SymbFullName2,LatestPrice);
            price = price/LatestPrice.bid;
         }
      }
      else if(FxSymbolType(SymbFullName1,"USD") == 1)
      {
         ::SymbolInfoTick(SymbFullName1,LatestPrice);
         price = LatestPrice.bid;
         if(FxSymbolType(SymbFullName2,"USD") == 0)
         {
            ::SymbolInfoTick(SymbFullName2,LatestPrice);
            price = price*LatestPrice.bid;
         }
         else if(FxSymbolType(SymbFullName2,"USD") == 1)
         {
            ::SymbolInfoTick(SymbFullName2,LatestPrice);
            price = price/LatestPrice.bid;
         }  
      }
   }
   else
   {
      if(FxSymbolType(SymbProfitFullName) == 0)//AccountSymbol is base currency
      {
         ::SymbolInfoTick(SymbProfitFullName,LatestPrice);
         price = 1/LatestPrice.bid;
      }
      else if(FxSymbolType(SymbProfitFullName) == 1)//SymbBase is base currency
      {
         ::SymbolInfoTick(SymbProfitFullName,LatestPrice);
         price = LatestPrice.bid;
      }
   }
   return(price);
}

The green background part is code realization for calculating the price of direct currency pair for profit currency which does not exist in the platform. In the above code, FxSymbFullName() is used to: 1. return the direct currency pair for the specified currency; 2. return the currency pair consisting of two currencies; 3. return the direct currency pair for base currency or profit currency. FxSymbolType() is used to determine the type of specified currency pair or currency pair consisting of two currencies. The specific code is as follows:

//+------------------------------------------------------------------+
//| get the symbol type when account currency is USD                 |
//| 0: like USDJPY,USDCAD  1: like EURUSD,GBPUSD                     |
//| 2-cross symbols: like EURAUD,CADJPY                              |
//| when account currency is EUR                                     |
//| 0: like EURUSD,EURJPY  1: don't exist                            |
//| 2-cross: like GBPUSD,USDJPY                                      |
//| Other account currency is similar                                |
//+------------------------------------------------------------------+  
int CForexSymbols::FxSymbolType(string FxSymb)
{
   if(::StringSubstr(FxSymb,0,3)==AccountSymbol) return(0);
   else if(::StringSubstr(FxSymb,3,3)==AccountSymbol) return(1);
   else return(2);
}
//+------------------------------------------------------------------+
//| get the postion of HalfSymb in the FxSymb                        |
//+------------------------------------------------------------------+  
int CForexSymbols::FxSymbolType(string FxSymb,string HalfSymb)
{
   if(::StringSubstr(FxSymb,0,3)==HalfSymb) return(0);
   else if(::StringSubstr(FxSymb,3,3)==HalfSymb) return(1);
   else return(-1);
}
//+------------------------------------------------------------------+
//| Judge whether this symbol is exist                               |
//+------------------------------------------------------------------+  
bool CForexSymbols::IsSymbolExist(string symb)
{
   double rvalue;//receiving the value of the requested property.
   if(::SymbolInfoDouble(symb,SYMBOL_BID,rvalue))//Symbol Exist
   {
      return(true);
   }
   else
   {
      if(::GetLastError() == ERR_MARKET_UNKNOWN_SYMBOL)
      {
         return(false);
      }
      return(true);
   }
}

CForexSymbols::IsSymbolExist() is used to determine if there is a quote for a given currency pair in the platform.


Share it with friends: