Lot size calculation is wrong

 

Hello

I uses below function for calculate lot size in MQL5 for use simple OrderSend (symbol EURUSD_i and Alpari broker).

double dblLotsRisk( double dbStopLoss, double dbRiskRatio ) {
   double
      dbLotsMinimum  = SymbolInfoDouble( _Symbol, SYMBOL_VOLUME_MIN       ),
      dbLotsMaximum  = SymbolInfoDouble( _Symbol, SYMBOL_VOLUME_MAX       ),
      dbLotsStep     = SymbolInfoDouble( _Symbol, SYMBOL_VOLUME_STEP      ),
      dbTickSize     = SymbolInfoDouble( _Symbol, SYMBOL_TRADE_TICK_SIZE  ),
      dbTickValue    = SymbolInfoDouble( _Symbol, SYMBOL_TRADE_TICK_VALUE ),
      dbValueAccount = fmin( fmin( 
                       AccountInfoDouble( ACCOUNT_EQUITY      )  , 
                       AccountInfoDouble( ACCOUNT_BALANCE     ) ),
                       AccountInfoDouble( ACCOUNT_MARGIN_FREE ) ),
      dbValueRisk    = dbValueAccount * dbRiskRatio,
      dbLossOrder    = dbStopLoss * dbTickValue / dbTickSize,
      dbCalcLot      = fmin(  dbLotsMaximum,                  // Prevent too greater volume
                       fmax(  dbLotsMinimum,                  // Prevent too smaller volume
                       round( dbValueRisk / dbLossOrder       // Calculate stop risk
                       / dbLotsStep ) * dbLotsStep ) );       // Align to step value

   return ( dbCalcLot );
}
   double lot = dblLotsRisk(sl, 0.01); // return 0.01

my balance is 1000$.

When I input the function with certain stoppless and 1% risk, I exepted margin be 10$.

But margin become fixed value: 2.16$

Why? can anyone help me?

 
yadafarin:

Hello

I uses below function for calculate lot size in MQL5 for use simple OrderSend (symbol EURUSD_i and Alpari broker).

my balance is 1000$.

When I input the function with certain stoppless and 1% risk, I exepted margin be 10$.

But margin become fixed value: 2.16$

Why? can anyone help me?

Margin has nothing to do with risk per trade 
Margin is how much USD the account requires to have to open the trade.
Risk percent per trade is how much the account will lose once the trade reaches stop loss
 

There are so many solution to calculate the lot size: https://www.mql5.com/en/search#!keyword=calculation%20lot%20size.

Learn to search before you start programming. There is practically nothing that has not already been programmed for MT4/MT5!

 
Michalis Phylactou #:
Margin has nothing to do with risk per trade 
Margin is how much USD the account requires to have to open the trade.
Risk percent per trade is how much the account will lose once the trade reaches stop loss
To calculate the margin, we need to calculate the lot/volume, which is entered as an input in the OrderSend function.  To calculate the lot/volume, we also need risk percentage and stopless.  How do you say they have nothing to do with each other?
 
Carl Schreiber #:

There is practically nothing that has not already been programmed for MT4/MT5!

I don't know, I think we are missing some things, I'm bringing them :-)

 

Hi

Margin and risk per trade are interconnected in trading, but they represent different aspects of risk management. Margin determines the size of the position a trader can take based on available capital and leverage,

double volume = PositionGetDouble(POSITION_VOLUME);

double margin_required = SymbolInfoDouble(symbol, SYMBOL_MARGIN_INITIAL) * volume

while risk per trade determines the percentage of capital a trader is willing to risk on each individual trade.

double riskOfTrade   = volume*dbLossOrder,

So when you calculate the lot from risk – you don’t calculate margin. Required margin s not the risk directly.

Have a nice weekend

 
Conor Mcnamara #:

I don't know, I think we are missing some things, I'm bringing them :-)

Surprise me :)

 
Marzena Maria Szmit #:

Hi

Margin and risk per trade are interconnected in trading, but they represent different aspects of risk management. Margin determines the size of the position a trader can take based on available capital and leverage,

while risk per trade determines the percentage of capital a trader is willing to risk on each individual trade.

So when you calculate the lot from risk – you don’t calculate margin. Required margin s not the risk directly.

Have a nice weekend

Thank you for the answer. I understood the logic. In order for the margin to be a fixed number like $10 for all symbols, how should I calculate the lot/volume?
 

Risk depends on your initial stop loss, lot size, and the value of the symbol. It does not depend on margin or leverage. No SL means you have infinite risk (on leveraged symbols). Never risk more than a small percentage of your trading funds, certainly less than 2% per trade, 6% account total.

  1. You place the stop where it needs to be — where the reason for the trade is no longer valid. E.g. trading a support bounce, the stop goes below the support. Then you compute your lot size.

  2. AccountBalance * percent/100 = RISK = OrderLots * (|OrderOpenPrice - OrderStopLoss| * DeltaPerLot + CommissionPerLot) (Note OOP-OSL includes the spread, and DeltaPerLot is usually around $10/PIP, but it takes account of the exchange rates of the pair vs. your account currency.)

  3. Do NOT use TickValue by itself - DeltaPerLot and verify that MODE_TICKVALUE is returning a value in your deposit currency, as promised by the documentation, or whether it is returning a value in the instrument's base currency.
              MODE_TICKVALUE is not reliable on non-fx instruments with many brokers - MQL4 programming forum (2017)
              Is there an universal solution for Tick value? - Currency Pairs - General - MQL5 programming forum (2018)
              Lot value calculation off by a factor of 100 - MQL5 programming forum (2019)

  4. You must normalize lots properly and check against min and max.

  5. You must also check Free Margin to avoid stop out

  6. For MT5, see 'Money Fixed Risk' - MQL5 Code Base (2017)

Most pairs are worth about $10 per PIP. A $5 risk with a (very small) 5 PIP SL is $5/$10/5 or 0.1 Lots maximum.

 
yadafarin #:
Thank you for the answer. I understood the logic. In order for the margin to be a fixed number like $10 for all symbols, how should I calculate the lot/volume?

Hi

I suppose if you want to set the lot size based on margin only and set the lot size to manage the chosen part of your margin – you need to get this value of required margin per 1 lot:

SymbolInfoDouble(symbol, SYMBOL_MARGIN_INITIAL)

And calculate the chosen value using simple proportion. For example - if you need 1000$ margin to open 1 lot – and you want to use only 10$ margin –you need to open 1/100 part of 1000$ - and that means that you can open 1/100 part from 1 lot = 0.01.

You can try if this is working for you.

Best Regards

Reason: