Download MetaTrader 5

Calculating Lot size

To add comments, please log in or register
Do you want to deposit to your account? Do this in any convenient way!
Doctor Ro
Doctor Ro 2011.01.09 21:29 

Hi guys,

Im new here, i've read many articles about some estrategies and money management.

Well, i have studied some about lots calculation (exponential and others), then i just think in some strategy that could be calculate using some relation of Volume (ATR), risk per trade and tick value.

#extern variables

The TradeAtRisk variable is used by another function to calc SL price. This represent the quantity of money that i accepted to loss for each order.

extern	double	TradeAtRisk = 2.0; 
double GetLots(int TypeOperation) //TypeOperation [OP_BUY or OP_SELL]
     double CurrOscilation = iATR(Symbol(),NULL,20,0); //no special reason to use 20 periods.
     CurrOscilation = MathFloor(CurrOscilation * 100000);
     double TickPrice = MarketInfo(Symbol(),MODE_TICKVALUE);
     double LotSize = MathSqrt( (AccountBalance()*TradeAtRisk/100) / CurrencyOscilation ) * TickPrice;
     while ( AccountFreeMarginCheck(Symbol(),TypeOperation,LotSize) <= 0 || GetLastError() == ERR_NOT_ENOUGH_MONEY)
         LotSize = LotSize - (LotSize*0.01);
     return (NormalizeDouble(LotSize,2));

The result maybe like this:

Balance$ 10.000,00
ATR ("X" Periods)120
TickValue$ 0,98

Daniel Niero

whroeder1 2011.01.11 17:19  
  1. double TickPrice = MarketInfo(Symbol(),MODE_TICKVALUE);
    double  PointValuePerLot() { // Value in account currency of a Point of Symbol.
        /* In tester I had a sale: open=1.35883 close=1.35736 (0.00147)
         * gain$=97.32/6.62 lots/147 points=$0.10/point or $1.00/pip.
         * IBFX demo/mini       EURUSD TICKVALUE=0.1 MAXLOT=50 LOTSIZE=10,000
         * IBFX demo/standard   EURUSD TICKVALUE=1.0 MAXLOT=50 LOTSIZE=100,000
         *                                  $1.00/point or $10.00/pip.
         * CB: MODE_TICKSIZE will usually return the
         * same value as MODE_POINT (or Point for the current symbol), however, an
         * example of where to use MODE_TICKSIZE would be as part of a ratio with
         * MODE_TICKVALUE when performing money management calculations which need
         * to take account of the pair and the account currency. The reason I use
         * this ratio is that although TV and TS may constantly be returned as
         * something like 7.00 and 0.00001 respectively, I've seen this
         * (intermittently) change to 14.00 and 0.00002 respectively (just example
         * tick values to illustrate). */
        return(  MarketInfo(Symbol(), MODE_TICKVALUE)
               / MarketInfo(Symbol(), MODE_TICKSIZE) ); // Not Point.

  2. return (NormalizeDouble(LotSize,2));
    This assumes LotStep == 0.01.
        double  minLot  = MarketInfo(Symbol(), MODE_MINLOT),
                LotStep = MarketInfo(Symbol(), MODE_LOTSTEP),
                perLotPerPoint  = PointValuePerLot(),
                maxLossPerLot   = (risk+Slippage.Pips*pips2dbl) * perLotPerPoint,
                size = maxRisk / maxLossPerLot;     // Must still round to LotStep.
        /*---- Compute lot size based on account balance and MM mode*/}
        /* The broker doesn't care about the at.risk/account balance. They care
         * about margin. Margin used=lots used*marginPerLot and that must be less
         * than free margin available. Using the lesser of size vs
         * AccountFreeMargin / MODE_MARGINREQUIRED should have been sufficient, but
         * the tester was generating error 134 even when marginFree should have been
         * OK. So I also use AccountFreeMarginCheck < 0 which agrees with the
         * tester. Reported at
         * Second problem, after opening the new order, if free margin then drops to
         * zero we get a margin call. In the tester, the test stops with: "EA:
         * stopped because of Stop Out" So I make sure that the free margin
         * after is larger then the equity risk so I never get a margin call. */
        EA.status = "SL>AE";    // Assume size < minLot
        while (true){   // Adjust for broker, test for margin, combine with TEF...
            size = MathFloor(MathMax(0.,size)/LotStep)*LotStep;
   = size * maxLossPerLot;                 // Export for Comment
            if (size < minLot){     /*;*/                  return(0); }
            double  AFMC    = AccountFreeMarginCheck(Symbol(), op.code, size),
                    eRisk   = at.risk.equity + risk*size*perLotPerPoint;
            if (AFMC*0.99 <= eRisk){    size *= 0.95;   EA.status = "Free Margin";
                continue;   }   // Prevent margin call if new trade goes against us.

Doctor Ro
Doctor Ro 2011.01.12 20:20  

Hi Roeder,
i just saw your response. I will study more calmly later. Its very interesting, cause im having the exactly problem of STOP OUT as you said.

I will response later.

thanks a lot!

whroeder1 2011.01.13 00:46  
Stop out is a different matter. AFMC just tells you if you have enough margin to open the order. As the trades go against you AccountEquity and AccountFreeMargin drop.
at.risk.equity += Direction( OrderType() )
              * (OrderClosePrice()-OrderStopLoss())*perPoint;
Summed for all open orders.
deltatangoxray 2016.09.06 14:00  

If I know the account balance, the dollar amount I am willing to risk, and the stop loss in pips, how do I calculate the order size in lots? For example:

Account balance ($USD): 1,000.00

Amount of risk (%): 0.02

Total dollar risk ($USD): 1,000 * 0.02 = 20.00

Stop loss (pips): 50

Dollar risk per pip ($USD): 20.00 / 50 = 0.40

Order size (lots): ?

Thank you.

whroeder1 2016.09.06 14:50  
  1. deltatangoxray: Dollar risk per pip ($USD): 20.00 / 50 = 0.40
    Dollar risk per pip is $10/lot ($1/point on a 5 digit broker) for a pair ending with USD, on a USD currency account. Period. The Dollar risk total is 50 pips * $10/lot * lotsize. $20.00/500=0.04 lots.
  2. In code
    • 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.
    • Account Balance * percent/100 = RISK = OrderLots * (|OrderOpenPrice - OrderStopLoss| * DeltaPerlot + CommissionPerLot) (Note OOP-OSL includes the SPREAD)
    • Do NOT use TickValue by itself - DeltaPerlot
    • You must normalize lots properly and check against min and max.
    • You must also check FreeMargin to avoid stop out
  3. Use a GUI: Indicators: Money Manager Graphic Tool - traders' Forum - Page 5 'Money Manager Graphic Tool' indicator by 'takycard'
To add comments, please log in or register