Download MetaTrader 5

Coding Help Metatrader 4: Money Management / Lots in Accordance to Account Balance

To add comments, please log in or register
Daniel Cormier
123
Daniel Cormier  

Hello,

 This is my first post to the community and I need some help.

 I have built an EA which I will be running only on EUR/USD but possibly exploring other pairs in the future.

Based on the results, I am confident that for every 25 Euro in my account balance, I can trade 0.01 lot. Can anybody help me with the code to achieve this when trades open. EA is programmed to only take 1 trade at a time. Please note my SL is fixed, so I am only looking for coding of lots and not a set TP or SL.

Many Thanks & Best Regards,

Dan 

Alain Verleyen
Moderator
32028
Alain Verleyen  
keydcuk:

Hello,

 This is my first post to the community and I need some help.

 I have built an EA which I will be running only on EUR/USD but possibly exploring other pairs in the future.

Based on the results, I am confident that for every 25 Euro in my account balance, I can trade 0.01 lot. Can anybody help me with the code to achieve this when trades open. EA is programmed to only take 1 trade at a time. Please note my SL is fixed, so I am only looking for coding of lots and not a set TP or SL.

Many Thanks & Best Regards,

Dan 

What is so hard ?

lot=MathFloor(AccountBalance()/25)*0.01
Daniel Cormier
123
Daniel Cormier  
angevoyageur:

What is so hard ?

This is my first time using the language. Most of the EA has been made with a diagram based ea builder. Other than that, I have searched other functions and their code but couldn't find this. It seems everyone else's posts had such a complicated code for a simple function.

 

Thanks for your help! I will give this one a try :)

 

Best Regards,

 

Daniel 

Daniel Cormier
123
Daniel Cormier  

Still struggling as the compile error brings

 

'lot' - declaration without type :( 

Rodrigo Malacarne
Moderator
10105
Rodrigo Malacarne  
keydcuk:

Still struggling as the compile error brings

 

'lot' - declaration without type :( 

Try:

double lot = ...
Hongliang Liu
1259
Hongliang Liu  
keydcuk:

Hello,

 This is my first post to the community and I need some help.

 I have built an EA which I will be running only on EUR/USD but possibly exploring other pairs in the future.

Based on the results, I am confident that for every 25 Euro in my account balance, I can trade 0.01 lot. Can anybody help me with the code to achieve this when trades open. EA is programmed to only take 1 trade at a time. Please note my SL is fixed, so I am only looking for coding of lots and not a set TP or SL.

Many Thanks & Best Regards,

Dan 

i prefer to use equity instead of balance,in one of my ea,the count and lots are calculated below: 

extern double initlots            = 0.01;
double Lots                       = 0;

    

int tempint = AccountEquity()/100;
    Lots = initlots * tempint;
    if(Lots < initlots)
    {
        Lots = initlots;
    }

 

extern double minequityuserate= 0.015;//min:percent of equity to open order
extern double maxequityuserate= 0.21;//max:percent of equity to open order
int GetBestTotalCnt()
{
    int cnt = 0;
    double marginrequired = MarketInfo(Symbol(),MODE_MARGINREQUIRED);
    double equity = AccountEquity();
    
    cnt = MathFloor((equity*minequityuserate)/(marginrequired*Lots));
    
  
    if(cnt > MathFloor((equity*maxequityuserate)/(marginrequired*Lots)))
    {
        cnt = MathFloor((equity*maxequityuserate)/(marginrequired*Lots));
    }

    if(cnt < 2)
    {
        cnt = 2;
    }
    //...
    return (cnt);
}
To add comments, please log in or register