Download MetaTrader 5

how to calculate the lots volume?

To add comments, please log in or register
The entire MQL5.community history on a single page!
dudenukem
57
dudenukem 2011.10.04 13:13 

for a given account with 100 US$ balance, leverage 10:1

 what is the size of the lot for opening a position without getting a not enough moeny error?

Jimmy
490
Jimmy 2011.10.04 14:02  
dudenukem:

for a given account with 100 US$ balance, leverage 10:1

 what is the size of the lot for opening a position without getting a not enough moeny error?


Clearly also depends on the currency pair being traded, the direction of the trade (long/short), and the time it is trade (ie. the current market price).

100$ balance and leverage 10:1 means you can control a position upto equivalent of 100$ x10 = 1000 in USD.

eg. if you're going long on GBPUSD, when the ask price is 1.5389 then you can control 1000/1.5389 = 649 worth of GBP or 649/100,000 lot ~= 0.006 lot GBPUSD

dudenukem
57
dudenukem 2011.10.04 14:37  

do you need to substract the margin as well?

 if I print the account info I receive zero margine (this is during testing) 

Documentation on MQL5: Standard Constants, Enumerations and Structures / Environment State / Account Properties
  • www.mql5.com
Standard Constants, Enumerations and Structures / Environment State / Account Properties - Documentation on MQL5
dudenukem
57
dudenukem 2011.10.05 12:30  

here is my code:

 

         trReq.type=ORDER_TYPE_BUY;
         trReq.price=SymbolInfoDouble(trReq.symbol,SYMBOL_ASK);
         trReq.sl=GetSL(true);
         trReq.tp=GetTP(true);
         trReq.volume=AccountInfoDouble(ACCOUNT_BALANCE)*(double)AccountInfoInteger(ACCOUNT_LEVERAGE)/trReq.price-AccountInfoDouble(ACCOUNT_FREEMARGIN);
         if(trReq.volume)
         {        
         trReq.volume=NormalizeDouble(trReq.volume/m_stdLot,5);
         Print("ASK price:", trReq.price);
         printf("ACCOUNT_EQUITY =  %G",AccountInfoDouble(ACCOUNT_EQUITY));
         printf("ACCOUNT_FREEMARGIN =  %G",AccountInfoDouble(ACCOUNT_FREEMARGIN));
         Print("Lots to buy: ",trReq.volume);
         }else  Print("Problem to buy: ",trReq.volume);
          

the code for sell is identical with the respective parameters replaced;

 the Journal Log during execution is

 

     Execution of new order failed! Not enough funds! Error = 4753
    Lots to sell: 0.00637
    ACCOUNT_FREEMARGIN =  100
    ACCOUNT_EQUITY =  100
    BID price:1.356
        --------------------------------->
        ACCOUNT_MARGIN_SO_SO = 20
        ACCOUNT_MARGIN_SO_CALL = 50
        ACCOUNT_MARGIN_LEVEL =  0
        ACCOUNT_FREEMARGIN =  100
        ACCOUNT_MARGIN =  0
        ACCOUNT_EQUITY =  100
        ACCOUNT_PROFIT =  0
        ACCOUNT_CREDIT =  0
        ACCOUNT_BALANCE =  100
        --------------------------------->

 any clues?

 

I don't know why the A/C_MARGIN is 0...otherwise I could calculate the volume using the balance the margine and the price... 

Documentation on MQL5: Standard Constants, Enumerations and Structures / Environment State / Account Properties
  • www.mql5.com
Standard Constants, Enumerations and Structures / Environment State / Account Properties - Documentation on MQL5
Jimmy
490
Jimmy 2011.10.06 13:35  
dudenukem:
 trReq.volume=AccountInfoDouble(ACCOUNT_BALANCE)*(double)AccountInfoInteger(ACCOUNT_LEVERAGE)/trReq.price-AccountInfoDouble(ACCOUNT_FREEMARGIN);
         if(trReq.volume)
         {         
         trReq.volume=NormalizeDouble(trReq.volume/m_stdLot,5);
trReq.volume = AccountInfoDouble(ACCOUNT_FREEMARGIN) / trReq.price; // don't forget to NormalizeDouble

dudenukem
57
dudenukem 2011.10.07 10:51  

still the same error after the new code:

trReq.price=SymbolInfoDouble(trReq.symbol,SYMBOL_ASK);
trReq.sl=GetSL(true);
trReq.tp=GetTP(true);
trReq.volume=AccountInfoDouble(ACCOUNT_FREEMARGIN/trReq.price;
if(trReq.volume)
{
   trReq.volume=NormalizeDouble(trReq.volume/m_stdLot,5);//m_stdLot=100000

while tested with deposit 100 and leverage 1:10 the result is always not enough money...

 very puzzled; 

 

rodesh
115
rodesh 2011.10.07 13:43  

 

   trReq.type=ORDER_TYPE_BUY;
   trReq.price=SymbolInfoDouble(trReq.symbol,SYMBOL_ASK);
   double required_margin=0.0;
   if(OrderCalcMargin(trReq.type,trReq.symbol,1.0,trReq.price,required_margin))  // Calculate margin required on 1 lot
   {
      trReq.volume = NormalizeDouble(AccountInfoDouble(ACCOUNT_FREEMARGIN)/required_margin,2); // Maximal lots that could be opened with free margin  
   }
   if(trReq.volume) 
     {
    ...

* Adjust the digits to normalize, if your broker allows for different volume than 2 decimal places (#.##), may not be nesessary to normalize in this way if you properly consider SYMBOL_VOLUME_STEP and normalize from there (see comment below)
* Scale the volume, if you only want to use a percentage of free margin


Using the example from fireflies above (GBPUSD @ 1.5389 ASK, given $100 Free Margin and 10:1 Leverage)
Depending on the account type  (Standard, Mini, or Micro) 
~0.006498 Standard Lots (100,000), result = 0
~0.06498 Mini Lots (10,000), result = 0.06
~0.6498 Micro Lots (1,000) , result = 0.64

I think you'll find that $100 free margin with 10:1 leverage may not be enough margin to complete any deals, depending on the type of your account (Standard, Mini, or Micro) and the volume limits/step for your broker/account.  You'll want to take into account the volume limits (Minimum, Maximum, and Step) allowed for a deal, and normalize the volume to conform, as nesessary.

SymbolInfoDouble(trReq.symbol,SYMBOL_VOLUME_MIN);
SymbolInfoDouble(trReq.symbol,SYMBOL_VOLUME_MAX);
SymbolInfoDouble(trReq.symbol,SYMBOL_VOLUME_STEP);

 Hope that helps streer you in the right direction.

To add comments, please log in or register