Risk As Percentage of AccountFreeMargin

 

I'm currently using the below function to determine lot sizes. I'd like to make it so that users can use a risk %. How can this be done?


double CalculateLots(double Lots)

{
double Margin;
Margin = AccountFreeMargin( ) ;
double Hasil;
double MLots ;
Hasil = Margin/1000;
MLots = MathFloor(Hasil);
//Seeing Max Lots of 50 for IBFX Mini
if(Lots==0)
{
return(MLots);
}
if(Lots!=0)
{
return(Lots);
}
}
 

Hi, Just call this function, its properly coded and takes care of all eventualities:

extern	double Lots = 0.0;//Fixed lotsize, 0.0: using AutoLot based on risk%
extern	double MaxLots = 50.0;//50
extern	int    RiskPercent = 15;//5



//---------- CalcLotsVolume ---------------------------
double CalcLotsVolume()
{
   double SendLots;//The actual Lotsize that will be sent with the order
   double MaxLot;//Broker's Maximum permitted amount of a lot.
   double MinLot;//Broker's Step for changing lots, e.g 0.01.
   double LotStep;//Broker's Minimum permitted amount of a lot.
   if (Lots > 0.0) SendLots = NormalizeLot(Lots);//The manual LotSize, bounded by broker's specs
   else {//Calculate AutoLot:
      MaxLot = MarketInfo(Symbol(), MODE_MAXLOT);
      MinLot = MarketInfo(Symbol(), MODE_MINLOT);
      LotStep = MarketInfo(Symbol(), MODE_LOTSTEP);
      SendLots = NormalizeDouble(MathFloor(AccountFreeMargin() * RiskPercent / 100.0 / (MarketInfo(Symbol(), MODE_MARGINREQUIRED) * LotStep)) * LotStep, 2);
      if (SendLots < MinLot) SendLots = MinLot;//Broker's absolute minimum Lot
      if (SendLots > MaxLot) SendLots = MaxLot;//Broker's absolute maximum Lot
   }
   if (SendLots > MaxLots) SendLots = MaxLots;//Never exceed our manualy set limit
   return (SendLots);
}
//-----------------------------------------------------
 
DayTrader:

Hi, Just call this function, its properly coded and takes care of all eventualities:


Uh.. You'll also need this function:

//---------- NormalizeLot -----------------------------
double NormalizeLot(double Lots)
{
   double NormalizedLot;//The final LotSize, bounded by broker's specs
   double InverseLotStep;
   double LotStep = MarketInfo(Symbol(), MODE_LOTSTEP);
   double MinLot  = MarketInfo(Symbol(), MODE_MINLOT);
   double MaxLot  = MarketInfo(Symbol(), MODE_MAXLOT);
   if (MinLot  == 0.0) MinLot = 0.1;//In case MarketInfo returns no info
   if (MaxLot  == 0.0) MaxLot = 5;  //In case MarketInfo returns no info
   if (LotStep == 0.0) InverseLotStep = 1 / MinLot;//In case MarketInfo returns no info
   else InverseLotStep = 1 / LotStep;   
   NormalizedLot = MathFloor(Lots * InverseLotStep) / InverseLotStep;   
   if (NormalizedLot < MinLot) NormalizedLot = MinLot;//Broker's absolute minimum Lot
   if (NormalizedLot > MaxLot) NormalizedLot = MaxLot;//Broker's absolute maximum Lot
   return (NormalizedLot);
}
//-----------------------------------------------------
 

Have I missed something?

Shouldn't leverage come into this calculation, so that 'cost of lot' is calculated between the proportion of free margin used and the final lot size?

-BB-

 
BarrowBoy wrote >>

Have I missed something?

Shouldn't leverage come into this calculation, so that 'cost of lot' is calculated between the proportion of free margin used and the final lot size?

-BB-

Also you need to consider stop loss size, for any given lot size, your risk percentage will change depending on whether you have a 5 or 50 pip stop loss.

Keith

 

Well... if you want to be absolutley correct, and end up wuth a true percentage...then Yes to both of you.

But... SL can be changed at any time (TS) and what do you do then...can't change the contract size!

Leverage... maybe I'll add that parameter some time, but for now this works well in my apps.

 
DayTrader wrote >>

Well... if you want to be absolutley correct, and end up wuth a true percentage...then Yes to both of you.

But... SL can be changed at any time (TS) and what do you do then...can't change the contract size!

Leverage... maybe I'll add that parameter some time, but for now this works well in my apps.

The idea of limiting lot size is to get a safe level of risk for your entry, you should never increase the size of your stop loss, only trail profits with it, so changing lot size is not an issue.

The bigger problem is dealing with dynamic stop losses which are set at time of placing the order for example stop losses set relative to a pivot point or fib level, in these case you need to determine SL before calculating Lot Size.

Keith

Reason: