Still think there is a bug in MarketInfo(Symbol(), MODE_MARGINREQUIRED)

 
Hi Rosh, this is related to the bug I described here: 'A couple questions from BugReport'

I read the "Azbuka..." article and your explanations on the russian forum ('AccountStopout.. : опубликуйте пожалуста более подробную документацию'), thank you - these were actually quite useful and now I understand some thinsg which were not completely clear.

However.


I still say that THERE IS A BUG in these functions:
AccountFreeMargin, AccountFreeMarginCheck and MarketInfo(Symbol(), MODE_MARGINREQUIRED)

They don't work properly in the Tester - they use current prices (i.e. variables Ask and Bid) in the Tester instead of using historical prices!

Please look at this screen shot:



The same expert was run in the Tester and on a Demo account. The expert uses your MarginCalculate function, Rosh, from the Azbuka article ('Азбука торговли валютами'). In theory MarginCalculate should return the same value as MarketInfo(Symbol(), MODE_MARGINREQUIRED).
However it DOES NOT! In the Tester the values are different and you can easily see that MarketInfo used the current Ask price when running in the Tester. When dragging the expert onto a real-time chart the expert displays the same results for both methods which is correct.

If you still insist there is no bug, then please explain these differences.

This is the expert:

#property copyright "Copyright © 2007, bla-bla"
#property link      ""
 
//+------------------------------------------------------------------+
// This function was copied from 'Азбука торговли валютами'
// Calculates the Free Margin required to have in your account to open the
// required volume for symbol
double MarginCalculate(string symbol, double volume)
{
    string first    = StringSubstr(symbol,0,3); // Base currency, e.g. EUR
    string second   = StringSubstr(symbol,3,3); // Quoted currency, e.g. CHF
    string currency = AccountCurrency();        // Account deposit currency, e.g USD
    double leverage = AccountLeverage();        // Account leverage, e.g. 100 for 100:1
 
    double fLotSize = MarketInfo(symbol, MODE_LOTSIZE);
    double bid      = MarketInfo(symbol, MODE_ASK);
 
    if(StringLen(symbol) != 6)
    {
        Print("MarginCalculate: '",symbol,"' must be standard forex symbol XXXYYY");
        return(0.0);
    }
 
    if(bid <= 0 || fLotSize <= 0) 
    {
        Print("MarginCalculate: no market information for '",symbol,"'");
        return(0.0);
    }
 
    // Are we lucky? Lets see if one of the pairs is also the deposit currency
    if(first == currency)   
    {
        Print("--- MarginCalculate: '", symbol,"' is the base currency");
        return(fLotSize*volume / leverage);           // USDxxx
    }
    if(second == currency)  
    {
        Print("--- MarginCalculate: '", symbol,"' is the quoted currency");
           return(fLotSize*bid*volume / leverage);       // xxxUSD
       }
 
    // Try a direct cross through account currency
    string base = currency + first;                   // USDxxx
    if(MarketInfo(base, MODE_BID) > 0) 
           return(fLotSize / MarketInfo(base, MODE_BID)*volume / leverage);
    base = first + currency;                          // xxxUSD
    if(MarketInfo(base, MODE_BID) > 0) 
           return(fLotSize*MarketInfo(base, MODE_BID)*volume / leverage);
 
    Print("MarginCalculate: can not convert '",symbol,"'");
    return(0.0);
}
//+------------------------------------------------------------------+
void init()
{
    Print( "Free Margin required to buy 1 Lot (Method 2) : ", MarginCalculate(Symbol(), 1));
    Print( "Free Margin required to buy 1 Lot (Method 1): ", MarketInfo(Symbol(), MODE_MARGINREQUIRED));
}
//+------------------------------------------------------------------+
//| script program start function                                    |
//+------------------------------------------------------------------+
int start()
{
   return(0);
}
//+------------------------------------------------------------------+
 

Move these 2 lines from the init function to the start function and see results

 
We've moved initial settings before init function.

Please download latest build 203 dated Mar 12 and try again.
Reason: