Download MetaTrader 5

Libraries: Maximum Percentage of Equity Risk

To add comments, please log in or register
Documentation is available in CHM and PDF. Download it in any convenient form!
Automated-Trading
Admin
99085
Automated-Trading 2014.08.01 10:35 

Maximum Percentage of Equity Risk:

This code allows you to set a maximum percentage of equity risk.

It checks if asked buy/sell lots are fitting the risk. If not, lots are automatically adjusted to fit the risk that was set.

Author: Remi PASSANELLO

Francis Dogbe
9501
Francis Dogbe 2014.08.16 11:13  
Please do not have the MQL4 version?
Remi Passanello
2622
Remi Passanello 2014.08.16 11:35  
Roszey:
Please do not have the MQL4 version?
bool UseMaximumPercentageatRisk = true;
double MaximumPercentageatRisk =1;
bool IsMicroAccount = true ; //for accounts allowing 0.01 lots
double GetLots(double lotsize_bn)
  {

   int tmpdecimal=1;
   if(IsMicroAccount)
     {
      tmpdecimal=2;
     }
   double old_lot=lotsize_bn;
   if((NormalizeDouble(AccountFreeMargin()*(MaximumPercentageatRisk/100)/1000.0,tmpdecimal)<lotsize_bn) && UseMaximumPercentageatRisk)
     {
      lotsize_bn=NormalizeDouble(AccountFreeMargin()*(MaximumPercentageatRisk/100)/1000.0,tmpdecimal);
      if(lotsize_bn<MarketInfo(Symbol(),MODE_MINLOT))
        {
         lotsize_bn=MarketInfo(Symbol(),MODE_MINLOT);
         Print("RPTrade-Lot adjusted from ",old_lot," to minimum size allowed by the server of ",lotsize_bn," but it DOES NOT comply with Maximum Risk condition. User interaction is required!");
        }
      else
        {
         Print("RPTrade-Lot adjusted from ",old_lot," to ",lotsize_bn," to comply with Maximum Risk condition. Each trade can risk only ",MaximumPercentageatRisk,"% of free margin.");
        }
     }
   return (lotsize_bn);
  }

Should be something like this ;-) 

R. 

To add comments, please log in or register