How to get maximum volume per position

To add comments, please log in or register
Jhojan Alberto Tobon Monsalve
293
Jhojan Alberto Tobon Monsalve  

Hello everyone, it must be something easy but I don´t know, I would like knowing, how I can get the maximum volume per order before the common message "not enough money" being shown.


I tried with the function  SYMBOL_VOLUME_MAX  but for instance  my demo account is (Type: STANDARD, Leaverage: 1:100, and Balance: 1.000 USD) and the function shows a 50.0 value. ¿What does it mean? ¿Is there another function that shows 0.7, 0.6 ... (maximum volume to be executed) or something like that? 

 

Thank you

Fernando Carreiro
4766
Fernando Carreiro  

jatm: Hello everyone, it must be something easy but I don´t know, I would like knowing, how I can get the maximum volume per order before the common message "not enough money" being shown.

I tried with the function  SYMBOL_VOLUME_MAX  but for instance  my demo account is (Type: STANDARD, Leaverage: 1:100, and Balance: 1.000 USD) and the function shows a 50.0 value. ¿What does it mean? ¿Is there another function that shows 0.7, 0.6 ... (maximum volume to be executed) or something like that?

You must learn to calculate your Volume (lots) based on margin and risk (such as stop size), and not on "what is the maximum allowed". If you don't do this, you will end up getting a Margin call and wiping out your entire deposit/balance in a single trade!

I sincerely urge you to first learn about the basics of trading and all its metrics, long before trying to code anything. You will need to do months (at least) of research and demo/paper trading before you should consider anything else.

Jhojan Alberto Tobon Monsalve
293
Jhojan Alberto Tobon Monsalve  
Fernando Carreiro:

You must learn to calculate your Volume (lots) based on margin and risk (such as stop size), and not on "what is the maximum allowed". If you don't do this, you will end up getting a Margin call and wiping out your entire deposit/balance in a single trade!

I sincerely urge you to first learn about the basics of trading and all its metrics, long before trying to code anything. You will need to do months (at least) of research and demo/paper trading before you should consider anything else.

I don´t know why you say that, you don´t know me and if you don´t know the answer you shouldn´t reply.  I supose this forum is to learn about the code, and I imagined if the program MT5 can show this mesagge it must be because there is some logical process behind. I suposed that this value could be get with  some function but if not then I will calculate it.  

But I would appreciate if anyone else  knows the answer, Thank you very much.

Anthony Garot
1954
Anthony Garot  
jatm:

how I can get the maximum volume per order before the common message "not enough money" being shown.


There are plenty of calculators available online.

Just Google the following:

online lot size calculator forex

I recommend that you re-read Fernando's response again. The quote that comes to mind:

“If you don't bet, you can't win. If you lose all your chips, you can't bet.” – Larry Hite

Fernando Carreiro
4766
Fernando Carreiro  
jatm:

I don´t know why you say that, you don´t know me and if you don´t know the answer you shouldn´t reply.  I supose this forum is to learn about the code, and I imagined if the program MT5 can show this mesagge it must be because there is some logical process behind. I suposed that this value could be get with  some function but if not then I will calculate it.  

But I would appreciate if anyone else  knows the answer, Thank you very much.

SYMBOL_VOLUME_MAX (see below) gives you the maximum allowed volume (lots) defined by the broker, but it does not tell you what the maximum volume should be based be on your Balance, Equity, Free Margin, Stop-Loss size, etc.

So, to place an order with just the right size of volume (lots), so as to control your risk, belongs in the realm of Risk and Money Management, and not in the realm of a an MQL function or parameter:

Here is an example of some code, but it is old MQL4 code and is need of some improvement, but serves the purpose of illustrating a point for this thread:

Forum on trading, automated trading systems and testing trading strategies

Need moneymanagement LOT size formula based on SL and Account Risk!

Fernando Carreiro, 2014.01.09 02:37

OK guys! This code was written by me and it is what I use in my own EA's. It is quite complex because, besides the Risk% and StopLoss, it also takes into account the Margin Risk% as well as correct the Lot Size based on Minimum, Maximum and Step Size. It also always uses the minimum value of Current Balance and Equity instead of just using one of them. I feel it is safer that way.

Please note, however, that it does not use the spread in the calculation, because I calculate that separately when calculating the StopLoss to be used. The below function, thus uses a relative stop loss size. In my EA's the argument dblStopLossPips is calculated beforehand depending on various factors such as the strategy, spread, ATR, etc.; so the final value passed on to the dblLotsRisk() function is already a final value in order to calculate the Lot size to be used.

// Function to Determine Tick Point Value in Account Currency

        double dblTickValue( string strSymbol )
        {
                return( MarketInfo( strSymbol, MODE_TICKVALUE ) );
        }
        

// Function to Determine Pip Point Value in Account Currency

        double dblPipValue( string strSymbol )
        {
                double dblCalcPipValue = dblTickValue( strSymbol );
                switch ( MarketInfo( strSymbol, MODE_DIGITS ) )
                {
                        case 3:
                        case 5:
                                dblCalcPipValue *= 10;
                                break;
                }
                
                return( dblCalcPipValue );
        }
        

// Calculate Lot Size based on Maximum Risk & Margin

        double dblLotsRisk( string strSymbol, double dblStopLossPips,
                double dblRiskMaxPercent, double dblMarginMaxPercent,
                double dblLotsMin, double dblLotsMax, double dblLotsStep )
        {
                double
                        dblValueAccount = MathMin( AccountEquity(), AccountBalance() )
                ,       dblValueRisk    = dblValueAccount * dblRiskMaxPercent / 100.0
                ,       dblValueMargin  = AccountFreeMargin() * dblMarginMaxPercent / 100.0
                ,       dblLossOrder    = dblStopLossPips * dblPipValue( strSymbol )
                ,       dblMarginOrder  = MarketInfo( strSymbol, MODE_MARGINREQUIRED )
                ,       dblCalcLotMin
                                = MathMax( dblLotsMin, MarketInfo( strSymbol, MODE_MINLOT ) )
                ,       dblCalcLotMax
                                = MathMin( dblLotsMax, MarketInfo( strSymbol, MODE_MAXLOT ) )
                ,       dblModeLotStep  = MarketInfo( strSymbol, MODE_LOTSTEP )
                ,       dblCalcLotStep  = MathCeil( dblLotsStep / dblModeLotStep ) * dblModeLotStep
                ,       dblCalcLotLoss
                                = MathFloor( dblValueRisk / dblLossOrder / dblCalcLotStep ) * dblCalcLotStep
                ,       dblCalcLotMargin
                                = MathFloor( dblValueMargin / dblMarginOrder / dblCalcLotStep ) * dblCalcLotStep
                ,       dblCalcLot = MathMin( dblCalcLotLoss, dblCalcLotMargin )
                ;
                
                if ( dblCalcLot < dblCalcLotMin ) dblCalcLot = dblCalcLotMin;
                if ( dblCalcLot > dblCalcLotMax ) dblCalcLot = dblCalcLotMax;

                return ( dblCalcLot );
        }

Forum on trading, automated trading systems and testing trading strategies

How to calculate lots using multiplier according to number of opened orders?

Fernando Carreiro, 2017.09.01 21:57

Don't use NormalizeDouble(). Here is some guidance (code is untested, just serves as example):

// Variables for Symbol Volume Conditions
double
   dblLotsMinimum = SymbolInfoDouble( _Symbol, SYMBOL_VOLUME_MIN  ),
   dblLotsMaximum = SymbolInfoDouble( _Symbol, SYMBOL_VOLUME_MAX  ),
   dblLotsStep    = SymbolInfoDouble( _Symbol, SYMBOL_VOLUME_STEP );
   
// Variables for Geometric Progression
double
   dblGeoRatio = 2.8,
   dblGeoInit  = dblLotsMinimum;
   
// Calculate Next Geometric Element
double
   dblGeoNext  = dblGeoInit * pow( dblGeoRatio, intOrderCount + 1 );
   
// Adjust Volume for allowable conditions
double
   dblLotsNext = fmin( dblLotsMaximum,                                     // Prevent too greater volume
                   fmax( dblLotsMinimum,                                   // Prevent too smaller volume
                     round( dblGeoNext / dblLotsStep ) * dblLotsStep ) );  // Align to Step value
To add comments, please log in or register