Download MetaTrader 5

Dynamic Lots for EA

To add comments, please log in or register
Aleksei Skokov
557
Aleksei Skokov  

Hello Ladies and Gents,

 

Was wondering if anyone can assist with something very simple, but I am unable to actually make it work, found multiple lines of code to do what I need but none worked as my MQL skills = 0.

Would appreciate if someone could help me insert a code allowing to change Lot size based on equity, with predetermined equity, something like, equity = 1000 > lot = 0.1 equity = 1500 > lot = 0.15, equity = 500 > lot = 0.05, etc.

File with sample code attached, I am very curious how to get this particular EA with dynamic lot sizing, everything I have tried before failed miserably due to lack of skill and understanding. 

 

P.S. This is mostly for me to understand how to make dynamic lots within simple EAs. Please do not refer to search or google, I have tried numerous attempts to implement something, but I guess I need an example with a simple, clean code as this one.

 

Thank you in advance! 

Siti Latifah
11047
Siti Latifah  

try this fuction


double LOT()
{

   double ilotsi;
   double ilot_max =MarketInfo(Symbol(),MODE_MAXLOT);
   double ilot_min =MarketInfo(Symbol(),MODE_MINLOT);
   double lotstep=MarketInfo(Symbol(),MODE_LOTSTEP);
   
   double digits;
   if(lotstep==0.01) digits=2; else digits=1;
   
   double margin=MarketInfo(Symbol(),MODE_MARGINREQUIRED);
   double  myAccount=AccountEquity(); 

   double iRisk=(Risk-1)/100;

   
   if (DynamicLots) {
      ilotsi=NormalizeDouble(((myAccount*iRisk)/(margin)),digits); 
      } else { ilotsi=Lots; }
   
   if(ilotsi>=ilot_max) { ilotsi=ilot_max; }
   if(ilotsi<=ilot_min) { ilotsi=ilot_min; }
   
   return(ilotsi);
   
}
Aleksei Skokov
557
Aleksei Skokov  
Siti Latifah:

try this fuction


Hi Siti Latifah,

Thank you for the information and help, will try it out!

Yesterday I spent several hours trying to figure something out, was able to use the following code by adding it too:  

double Lot;
double Lots()                                            
   {
   Lot=NormalizeDouble(AccountEquity()*Risk/100/1000,2);  
   double Min_Lot = MarketInfo(Symbol(), MODE_MINLOT);  
   if (Lot == 0 ) Lot = Min_Lot;                        
   return(Lot);
   }

Removed input double Lots =0.01; in the beginning, so I no longer have to determine lot sizes, added extern double  Risk = 10; instead of it.

And in 'Ordersend' functions Added () After Lots: OrderSend(Symbol(),OP_SELL,Lots(),Bid,3,0,0,"Scal SELL",MAGICMA,0,Red);

I am not sure which option is better, by the looks of it, yours is more flexible, so I will try to implement it. Thought of leaving my findings here too, in case if some doofus like myself had the same issue, but couldn't find a solution.

 

Thank you once again for your assistance Siti! 

Siti Latifah
11047
Siti Latifah  
Aleksei Skokov:

Hi Siti Latifah,

Thank you for the information and help, will try it out!

Yesterday I spent several hours trying to figure something out, was able to use the following code by adding it too:  

Removed input double Lots =0.01; in the beginning, so I no longer have to determine lot sizes, added extern double  Risk = 10; instead of it.

And in 'Ordersend' functions Added () After Lots: OrderSend(Symbol(),OP_SELL,Lots(),Bid,3,0,0,"Scal SELL",MAGICMA,0,Red);

I am not sure which option is better, by the looks of it, yours is more flexible, so I will try to implement it. Thought of leaving my findings here too, in case if some doofus like myself had the same issue, but couldn't find a solution.

 

Thank you once again for your assistance Siti! 

little correction your code

if (Lot<=Min_lot).....  do not if (Lot==0) .....

double Lot;
double Lots()                                            
   {
   Lot=NormalizeDouble(AccountEquity()*Risk/100/1000,2);  
   double Min_Lot = MarketInfo(Symbol(), MODE_MINLOT);  
   if (Lot <= Min_lot ) Lot = Min_Lot;                        
   return(Lot);
   }
itriad
162
itriad  
Here is a simple method to change the lot size based on the value of the account. 
void OnTick()
  {

//--- lot size calculation
  double lot=0.01;

        if(AccountEquity()<200){lot =0.12;}
   else if(AccountEquity()<300){lot =0.10;}
   else if(AccountEquity()<400){lot =0.09;}
   else if(AccountEquity()<500){lot =0.08;}
   else if(AccountEquity()<600){lot =0.07;}
   else if(AccountEquity()<700){lot =0.06;}
   else if(AccountEquity()<800){lot =0.05;}
   else if(AccountEquity()<900){lot =0.03;}
   else if(AccountEquity()<1000){lot=0.02;}
   else                          {lot=lot;}

  }
Marco vd Heijden
Moderator
5349
Marco vd Heijden  
double lots=AccountBalance()/10000;//100000 for 0.1 scalable depending on account balance 
To add comments, please log in or register