Aurix Exchange

Add Money Managenent code

After extern parameters :

Code:

extern double Lots=0.1;//|-----------------------lots size
extern bool RiskMM=false;//|---------------------risk management
extern double RiskPercent=1;//|------------------risk percentage
extern double MinLots=0.01;//|-------------------minlots
extern double MaxLots=100;//|--------------------maxlots

After start(){ :

Code:

if(RiskMM)CalculateMM();

At the end of the code :

Code:

void CalculateMM()
{
double MinLots=MarketInfo(Symbol(),MODE_MINLOT);
double MaxLots=MarketInfo(Symbol(),MODE_MAXLOT);
Lots=AccountFreeMargin()/100000*RiskPercent;
Lots=MathMin(MaxLots,MathMax(MinLots,Lots));
if(MinLots<0.1)Lots=NormalizeDouble(Lots,2);
else
{
if(MinLots<1)Lots=NormalizeDouble(Lots,1);
else Lots=NormalizeDouble(Lots,0);
}
if(Lots<MinLots)Lots=MinLots;
if(Lots>MaxLots)Lots=MaxLots;
return(0);
}
 
dave_rimmer,
thanks for the code.

I have seen this lot-size calculation in many EAs, but I think it is
wrong ( or maybe I am wrong :) ).

What I need in my manual trading and in an EA is the risk in percent
of the balance ( or maybe free margin if desired ).

Now I need to calculate the Lot-size in relation to the Pips where I place the StopLoss.

Therefore the correct lot-size calculation can be coded like this:

double CalcLots(double risk, int sl)
{
int dig = 0;
double result = 0;
RefreshRates();
if(MarketInfo(Symbol(),MODE_LOTSTEP)==0.1) dig = 1;
if(MarketInfo(Symbol(),MODE_LOTSTEP)==0.01) dig = 2;
double money_to_loss = AccountBalance() * risk;
result = NormalizeDouble(money_to_loss/sl/10,dig);
return(result);
}

The function is called like this:

if(UseMM) LotSize = CalcLots(Risk,StopLossPips);

and these definitions have to be made:

extern bool UseMM = true;
extern double Risk = 0.01; // 0.01 = 1 percent
extern int StopLossPips = 0;

This is valid for USD pairs if the account is held in USD.
For other pairs we would need the Pipvalue but I don?t like it too much complicated.

What do you think of this ?
 
dave_rimmer,
thanks for the code.

I have seen this lot-size calculation in many EAs, but I think it is
wrong ( or maybe I am wrong :) ).

What I need in my manual trading and in an EA is the risk in percent
of the balance ( or maybe free margin if desired ).

Now I need to calculate the Lot-size in relation to the Pips where I place the StopLoss.

Therefore the correct lot-size calculation can be coded like this:

double CalcLots(double risk, int sl)
{
int dig = 0;
double result = 0;
RefreshRates();
if(MarketInfo(Symbol(),MODE_LOTSTEP)==0.1) dig = 1;
if(MarketInfo(Symbol(),MODE_LOTSTEP)==0.01) dig = 2;
double money_to_loss = AccountBalance() * risk;
result = NormalizeDouble(money_to_loss/sl/10,dig);
return(result);
}

The function is called like this:

if(UseMM) LotSize = CalcLots(Risk,StopLossPips);

and these definitions have to be made:

extern bool UseMM = true;
extern double Risk = 0.01; // 0.01 = 1 percent
extern int StopLossPips = 0;

This is valid for USD pairs if the account is held in USD.
For other pairs we would need the Pipvalue but I don?t like it too much complicated.

What do you think of this ?

yeh!!

good stuff
 
It doesn't increase and doesn't reduce the size of a prize. The prize remains to constants. Excuse for my English.
The original EA which you posted only uses a fixed lot-size.
It has no code for reduction or increase of lot-size.

I modified it now in a way that it calculates the lot-size for each order according to the account-balance ... before the order is sent.

Is this what you want ?
 

Attachments

  • SETMOCHA_v2.5(2)_MM.mq4
    18.8 KB · Views: 2

rgabor

Active member
Lot Size Calculator

Lot Size Calculator

Dear All,

Since few day I am searching the correct way to calculate lot size however I just cannot find it.

The problem is that no one calculate LotSize regarding the Leverage used on the account.

Most of the time I see the following formula:

Code:
extern double FixedLots = 0.0;   // if > 0.0 fixed lots used
extern double Risk = 0.03;       // per cent of free margin of a single offer
extern int StopLoss = 10;

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);
}

However, Imagine that your account have 500 Dollar, and you want to risk 3% of this, with an StopLoss of 10 Pips.

The calculated Lot Size given by this function would be around 1.89 which is to high for an account with Leverage of 1:300

So my question is, do anyone know how to correctly get the correct lot size by also considering account leverage?
 

maximo

Member
This is my money manement code. If you want to factor in leverage to the equation then replace the number 100 in Leverage=100 with AccountLeverage()

double Lots = 0.1; // if MM=False then uses fixed Lots
bool MoneyManagement = True;
double PercentRisk = 2.0; // risk 2% of equity


void MoneyManagement()
{
if (MoneyManagement)
{ // Fixed fractional risk management model
int RiskPips=70; // StopLoss or Average loss pips
double Leverage = 100 * 0.001; //AccountLeverage()=100
Lots = (AccountEquity() * PercentRisk / 100) / RiskPips * Leverage;
}
// Maximum-Minimum available, Normalize Lots
double maxlot = MarketInfo(Symbol(), MODE_MAXLOT);
double minlot = MarketInfo(Symbol(), MODE_MINLOT);
int Ndigits = MathAbs(MathLog(minlot) * 0.5);
Lots = NormalizeDouble(MathMax(MathMin(Lots, maxlot), minlot),Ndigits);
}


p.s.
I think factoring in leverage is like puting nitro in your fuel tank...
performance will improve while reducing life expectancy of the
engine or equity.
 

Aurix Exchange
Top