Margin required calculations

 

Some questions regarding code from the article Forex Trading ABC:


//+------------------------------------------------------------------+
//| A very simple function to calculate margin for Forex symbols.    |
//| It automatically calculates in the account's base currency and   |
//| does not work for complicated rates that do not have direct      |
//| recalculation into the trade account's base currency.            |
//+------------------------------------------------------------------+
double MarginCalculate(string symbol, double volume)
  {
   string first    = StringSubstr(symbol,0,3); // the first symbol, for example,  EUR
   string second   = StringSubstr(symbol,3,3); // the second symbol, for example, USD
   string currency = AccountCurrency();        // deposit currency, for example,  USD
   double leverage = AccountLeverage();        // leverage, for example,          100
// contract size, for example, 100000
   double contract = MarketInfo(symbol, MODE_LOTSIZE);
   double bid      = MarketInfo(symbol, MODE_BID);      // Bid price
//---- allow only standard forex symbols like XXXYYY
   if(StringLen(symbol) != 6)
     {
      Print("MarginCalculate: '",symbol,"' must be standard forex symbol XXXYYY");
      return(0.0);
     }
//---- check for data availability
   if(bid <= 0 || contract <= 0) 
     {
      Print("MarginCalculate: no market information for '",symbol,"'");
      return(0.0);
     }
//---- check the simplest variations - without cross currencies
   if(first == currency)   
       return(contract*volume / leverage);           // USDxxx
   if(second == currency)  
       return(contract*bid*volume / leverage);       // xxxUSD
//---- check normal cross currencies, search for direct conversion through deposit currency
   string base = currency + first;                   // USDxxx
   if(MarketInfo(base, MODE_BID) > 0) 
       return(contract / MarketInfo(base, MODE_BID)*volume / leverage);
//---- try vice versa
   base = first + currency;                          // xxxUSD
   if(MarketInfo(base, MODE_BID) > 0) 
       return(contract*MarketInfo(base, MODE_BID)*volume / leverage);
//---- direct conversion is impossible
   Print("MarginCalculate: can not convert '",symbol,"'");
   return(0.0);
  }
1. Can I expand this for BUY orders using ASK price? Are the calculations the same?
2. What happens if I reach the '//---- direct conversion is impossible' point in the code? How do I calculate it?
3. For orders already opened, I use their Bid/Ask price when they were opened to calculate their required margin at the time?
4. I guess this won't work in th Tester because of cross currency prices read, is their some way around this?


 
1.Yes, yes.
2.It shouldn't. If your broker has gold as GOLD, you have to make additional calculations, but it's unreal.
3.Margin could be changed after order has opened, so calculate for open price only.
4.I don't know these ways, sorry.
 
Roger, Roger. Thanks.
 
I have another question - are there brokers that have a different calculation method for margin required? I think I found a broker that regardless of symbol the margin required = volume*contract/leverage. Is that possible? How can I know which broker has what method?
Reason: