• Home
  • Forums
  • Trades
  • News
  • Calendar
  • Market
  • Brokers
  • Login
  • Join
  • User/Email: Password:
  • 10:59pm
Menu
  • Forums
  • Trades
  • News
  • Calendar
  • Market
  • Brokers
  • Login
  • Join
  • 10:59pm
Sister Sites
  • Metals Mine
  • Energy EXCH
  • Crypto Craft

Options

Bookmark Thread

First Page First Unread Last Page Last Post

Print Thread

Similar Threads

Scalping with quick SL measurement to calculate lot size? 27 replies

Help me calculate this lot size algorithm please! 0 replies

Calculate lot size function 0 replies

Lot size, contract size, max position ? 1 reply

Calculate the right lot size MQL4/MT4 3 replies

  • Platform Tech
  • /
  • Reply to Thread
  • Subscribe
  • 2
Attachments: How to calculate LOT size
Exit Attachments

How to calculate LOT size

  • Post #1
  • Quote
  • First Post: Jul 23, 2009 5:22am Jul 23, 2009 5:22am
  •  dipen
  • | Joined May 2009 | Status: MQL4 developer | 5 Posts
Guys,

Is there any method to calculate the LOT size in MQL4?

Any formula available to calculate LOT size.

Please let me know

Cheers
Dipen
  • Post #2
  • Quote
  • Jul 23, 2009 10:03am Jul 23, 2009 10:03am
  •  spekitox
  • | Joined Sep 2008 | Status: Lucky Man | 2,267 Posts
there was an EA somewhere here.. called Daily95pips... the following lotsize code is from that EA:

parameters:

Inserted Code
extern double FixedLots = 0.0;   // if > 0.0 fixed lots used
extern double Risk = 0.01;       // per cent of free margin of a single offer
extern int StopLoss = 30;
...
Inserted Code
double CalculateLotSize()
{
    if(FixedLots > 0.0)
        return (FixedLots);

    double pipValue = MarketInfo(Symbol(), MODE_TICKVALUE);
    double lots = AccountFreeMargin() * Risk / (StopLoss * pipValue);
    
    double lotStep = MarketInfo(Symbol(), MODE_LOTSTEP);
    int digits = 0;
    if(lotStep <= 0.01)
        digits = 2;
    else if(lotStep <= 0.1)
        digits = 1;
    lots = NormalizeDouble(lots, digits);
      
      double minLots = MarketInfo(Symbol(), MODE_MINLOT);
      if(lots < minLots)
          lots = minLots;
      
      double maxLots = MarketInfo(Symbol(), MODE_MAXLOT);
      if(lots > maxLots)
          lots = maxLots;
      
      return (lots);
}
forget about tomorrow, just steal away into the night
  • Post #3
  • Quote
  • Jul 24, 2009 5:40am Jul 24, 2009 5:40am
  •  dipen
  • | Joined May 2009 | Status: MQL4 developer | 5 Posts
Hey spekitox.

Thanks for your code . it will helps.
  • Post #4
  • Quote
  • Jan 8, 2014 8:09pm Jan 8, 2014 8:09pm
  •  JohnTR
  • | Joined Apr 2013 | Status: Member | 9 Posts
Quoting spekitox
Disliked
there was an EA somewhere here.. called Daily95pips... the following lotsize code is from that EA: parameters: extern double FixedLots = 0.0; // if > 0.0 fixed lots used extern double Risk = 0.01; // per cent of free margin of a single offer extern int StopLoss = 30; ... double CalculateLotSize() { if(FixedLots > 0.0) return (FixedLots); double pipValue = MarketInfo(Symbol(), MODE_TICKVALUE); double lots = AccountFreeMargin() * Risk / (StopLoss * pipValue); double lotStep = MarketInfo(Symbol(), MODE_LOTSTEP); int digits = 0; if(lotStep <= 0.01)...
Ignored
It doesn't work.
using this code a 2000 account risking 200 dollars and using 150 pip stop range, on EURGBP pipvalue of 1.64 gives a .81 result which is 8 mini lots, when it should be .08.
  • Post #5
  • Quote
  • Dec 25, 2019 9:22am Dec 25, 2019 9:22am
  •  SalmanM
  • | Additional Username | Joined Dec 2019 | 96 Posts
What is better formula for Lot precision:
1)
double LOT=4.44999;
lots=MathMin(MathMax((MathRound(LOT/MarketInfo(Symbol(),MODE_LOTSTEP))*MarketInfo(Symbol(),MODE_LOTSTEP)),MarketInfo(Symbol(),MODE_MINLOT)),MarketInfo(Symbol(),MODE_MAXLOT));

or

double LOT=4.44999;
lots=MathMin(MathMax((int(LOT/MarketInfo(Symbol(),MODE_LOTSTEP))*MarketInfo(Symbol(),MODE_LOTSTEP)),MarketInfo(Symbol(),MODE_MINLOT)),MarketInfo(Symbol(),MODE_MAXLOT));


for first ans is 4.45 --- for second ans is 4.44
  • Post #6
  • Quote
  • Edited at 2:01pm Dec 25, 2019 1:47pm | Edited at 2:01pm
  •  emmzett
  • Joined Apr 2008 | Status: Member | 436 Posts
Quoting SalmanM
Disliked
What is better formula for Lot precision: 1) double LOT=4.44999; lots=MathMin(MathMax((MathRound(LOT/MarketInfo(Symbol(),MODE_LOTSTEP))*MarketInfo(Symbol(),MODE_LOTSTEP)),MarketInfo(Symbol(),MODE_MINLOT)),MarketInfo(Symbol(),MODE_MAXLOT)); or double LOT=4.44999; lots=MathMin(MathMax((int(LOT/MarketInfo(Symbol(),MODE_LOTSTEP))*MarketInfo(Symbol(),MODE_LOTSTEP)),MarketInfo(Symbol(),MODE_MINLOT)),MarketInfo(Symbol(),MODE_MAXLOT)); for first ans is 4.45 --- for second ans is 4.44
Ignored
Looking at the general approach the first one is "better" because more exact. "Better" in quotes because both may return invalid results. Both approaches miss a final NormalizeDouble(). Also it's recommended to move the min/max evaluation out of the formula and apply it in your strategy logic. Otherwise sooner or later you will experience surprises. A better general function looks like this:

Inserted Code
/**
 * Round a lot size according to the specified symbol's lot step value (MODE_LOTSTEP).
 *
 * @param  double lots              - lot size
 * @param  string symbol [optional] - symbol (default: the current symbol)
 *
 * @return double - rounded lot size
 */
double NormalizeLots(double lots, string symbol = "") {
   if (!StringLen(symbol))
      symbol = Symbol();
   double lotstep = MarketInfo(symbol, MODE_LOTSTEP);
   return(NormalizeDouble(MathRound(lots/lotstep) * lotstep, 2));
}

After you got the result you may apply min/max, or wrap min/max in a second helper function with a different name. Full source here: NormalizeLots()
2
  • Post #7
  • Quote
  • Dec 25, 2019 3:16pm Dec 25, 2019 3:16pm
  •  SalmanM
  • | Additional Username | Joined Dec 2019 | 96 Posts
after some years I find the best lot accuracy approach:
Inserted Code
double Lots=9.765433;
double LotStep=MarketInfo(Symbol,MODE_LOTSTEP);
if(LotStep==0) LotStep=1;
int FFF=-MathLog10(LotStep);
Lots=int(StringToDouble(DoubleToString(Pairs_Unified_Lots/LotStep,FFF)))*LotStep;

in this way, it is valid for lot step with tenth (1.2)or hundredth(1.23) or 0th (1) decimal
  • Post #8
  • Quote
  • Dec 25, 2019 3:18pm Dec 25, 2019 3:18pm
  •  Burton
  • | Joined Jan 2008 | Status: Member | 215 Posts
Here are 2 indicators that will calculate lot size, from % Risk, and SL distance.
Attached Files
File Type: mq4 lot size calc.mq4   2 KB | 189 downloads
File Type: mq4 Lot_size_calc_v2.mq4   6 KB | 196 downloads
  • Post #9
  • Quote
  • Last Post: Dec 25, 2019 3:27pm Dec 25, 2019 3:27pm
  •  SalmanM
  • | Additional Username | Joined Dec 2019 | 96 Posts
Quoting emmzett
Disliked
{quote} Looking at the general approach the first one is "better" because more exact. "Better" in quotes because both may return invalid results. Both approaches miss a final NormalizeDouble(). Also it's recommended to move the min/max evaluation out of the formula and apply it in your strategy logic. Otherwise sooner or later you will experience surprises. A better general function looks like this: /** * Round a lot size according to the specified symbol's lot step value (MODE_LOTSTEP). * * @param double lots - lot size * @param string symbol [optional]...
Ignored
https://github.com/rosasurfer
you are really great programmer
1
  • Platform Tech
  • /
  • How to calculate LOT size
  • Reply to Thread
0 traders viewing now
Top of Page
  • Facebook
  • Twitter
About FF
  • Mission
  • Products
  • User Guide
  • Media Kit
  • Blog
  • Contact
FF Products
  • Forums
  • Trades
  • Calendar
  • News
  • Market
  • Brokers
  • Trade Explorer
FF Website
  • Homepage
  • Search
  • Members
  • Report a Bug
Follow FF
  • Facebook
  • Twitter

FF Sister Sites:

  • Metals Mine
  • Energy EXCH
  • Crypto Craft

Forex Factory® is a brand of Fair Economy, Inc.

Terms of Service / ©2021