Libraries: Maximum Percentage of Equity Risk

 

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

 
Please do not have the MQL4 version?
 
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. 

 

I was looking at your code in MQL5 for "Maximum Percentage of Equity Risk". 
I have some doubts regarding to how to use it. I have the mqh file in the include file and use #include <RPTrade.mqh> at the expert that I want to use. 
Can you elaborate more on how to use this code in a expert advisor please? I'm new to coding. I have made expert advisors, but I don't know how to call a class or function in another file.

I need to call the function GetLots (lotsize) right? And in my order request need to use "lotsize" value for the lots?

Thank you!

 
patagonia2015:

Hi Patagonia2015,

First you must add the 2 inputs to your EA:

bool 	UseMaximumPercentageRisk=true;
double 	MaximumPercentageRisk=25;

Then you must call the function GetLots() and set the lotsize you wish into the brackets, for instance call:

double  MyLotSize;
MyLotSize = GetLots (100);

And according to what you entered as MaximumPercentageRisk and your account equity, you will get the right size.

For instance, if MaximumPercentageRisk = 1; and your equity = 1000$, the function will return 0.01 lot.

Trade at low risk, markets are wild actually ;-)

 

Hi, Thanks for sharing this information.

I am a rookie but I would love to be able to have a way to be consistent with my lot size and I would like to know how can I use your code to do that?

Thanks in advance

 
Remi Passanello #:

Hi Patagonia2015,

First you must add the 2 inputs to your EA:

Then you must call the function GetLots() and set the lotsize you wish into the brackets, for instance call:

And according to what you entered as MaximumPercentageRisk and your account equity, you will get the right size.

For instance, if MaximumPercentageRisk = 1; and your equity = 1000$, the function will return 0.01 lot.

Trade at low risk, markets are wild actually ;-)

Sorry, you kind of confused me when u said "you must call the function GetLots() and set the lotsize you wish into the brackets"

I thought the whole point was for you to get the appropriate lots based on ur chosen risk and current account balance.

A little clarity would be much appreciated. Thanks
 
Muhammad Shamsuddeen Muhammad #:
Sorry, you kind of confused me when u said "you must call the function GetLots() and set the lotsize you wish into the brackets"

I thought the whole point was for you to get the appropriate lots based on ur chosen risk and current account balance.

A little clarity would be much appreciated. Thanks
I was thinking exactly of the same
Reason: