Lot calculation

 

Kind of taking a survey here.  

I've been reading around various area's on lot calculation.  I'm wondering, how do you calculate lots? Here are some examples from a thread I posted on another forum:

// a) Found this in a spreadsheet somewhere on what I've read so far:
Lot=(Risk_Percentage*Free_Margin)/(Dollars_Per_Pip_1_Standard_Lot*(StopLoss_Pips+Spread_Pips))
 
// b) Version IBFX has been handing out for years
Lot=(Risk_Percentage*Free_Margin)/(Lot_Size/Leverage);

// c) FerruFx @ FF
Lot=((Balance*Risk%)/SL)/Pipvalue;

// d) DenForex @ FF
Lot = ((risk parcentage in %)*(your balance))/ K*(price of open - price of close)
/*
K = -1 for sell
K = +1 for buy
*/

// e) LawKin @ FF
/*
2000 USD (deposit) * 100 (leverage size) = 200,000 USD - capital for trading in dollars
200 000 USD / 164 190 USD (100 000 GBP to be converted into dollars at the current rate of 1.6419) = 1.21 - the maximum possible number of lots.
1.21 * 10% = 0.12.
*/

 Note: I put this inside of source code tags for easier reading.  :)

 
/////////////////////////////////////////////////////////
double perc_lot_by_stop_f(int SL=0,double Lot_sl_risk=1)
{
if(SL<=0) {Alert("Need SL to be set!..min lot mode");return(MarketInfo(Symbol(),MODE_MINLOT));}

double lose_on_stop_lose=SL*(MarketInfo(Symbol(),MODE_TICKVALUE)/100);
double propotion=(AccountBalance()/100*Lot_sl_risk)/lose_on_stop_lose;
double Loto_=MarketInfo(Symbol(),MODE_MINLOT)*propotion;

if(Loto_<MarketInfo(Symbol(),MODE_MINLOT)) Loto_=MarketInfo(Symbol(),MODE_MINLOT);
if(Loto_>MarketInfo(Symbol(),MODE_MAXLOT)) Loto_=MarketInfo(Symbol(),MODE_MAXLOT);
if(MarketInfo(Symbol(),MODE_MARGINREQUIRED)*Loto_>AccountFreeMargin()) {Alert("Not enouth money to open order!..min lot mode");return(MarketInfo(Symbol(),MODE_MINLOT));}

return(Loto_);

}

////

////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 double GetLot_f()
 {
 double Free    =AccountFreeMargin();
 double One_Lot =MarketInfo(Symbol(),MODE_MARGINREQUIRED);
 double Step    =MarketInfo(Symbol(),MODE_LOTSTEP);
 double Loto     =MathFloor(Free*percent_of_deposit/100/One_Lot/Step)*Step;
 
 double Min_Lot =MarketInfo(Symbol(),MODE_MINLOT);
 double Max_Lot =MarketInfo(Symbol(),MODE_MAXLOT);
 if(Loto<Min_Lot) Loto=Min_Lot;
 if(Loto>Max_Lot) Loto=Max_Lot;
 
 return(NormalizeDouble(Loto,nor_lot));
 }

 

 

 

 
//+------------------------------------------------------------------+
//| Code below deals with lot size allocation for trades.
//+------------------------------------------------------------------+
double LotCalc()                                    
{
   // Get market info for symbol
   if(IsConnected())
   {
      double One_Lot = MarketInfo(Symbol(), MODE_MARGINREQUIRED);  // Get cost of 1 lot
      double Min_Lot = MarketInfo(Symbol(), MODE_MINLOT);          // Get minimum lot size
      double Step = MarketInfo(Symbol(), MODE_LOTSTEP);            // Get lot stepping size
      double Free = AccountFreeMargin();                           // Get my free margin level
      double Money, Giving;
   } else return(0.00);
   
   // If a lot size is specified, can I afford it?
   if(LotSize > 0)
   {  
      // Calc cost of lots wanted
      Money = LotSize * One_Lot;
      
      // If I can afford it, accept, otherwise calculate it based on free margin available
      if(Money <= Free)
         Giving = LotSize;
      else
         Giving = MathFloor(Free/One_Lot/Step) * Step;
   }
   else
   {
      // Calc lot size as a percentage of available margin
      if(Percentage > 100) Percentage = 100;       
      
      // If percentage is not set, take just one lot, otherwise calculate it
      if(Percentage <= 0)
         Giving = Min_Lot;
      else
         Giving = MathFloor(Free*(Percentage/100)/One_Lot/Step) * Step;
   }

   // If OptimizeTrade flag is set, increase or decrease lots size by OptimizeSize based on consecutive wins/losses
   if(OptimizeTrade)
   {
      if(EAWins - EALosses > 2) 
         Giving += ((EAWins - EALosses)-2) * OptimizeSize;
      if(EALosses - EAWins > 2)
         Giving -= ((EALosses - EAWins)-2) * OptimizeSize;
   }
   
   // Is the lot size selected is less than the minimum lot, set to minimum
   if(Giving < Min_Lot) Giving = Min_Lot;

   // Check for maximum lots size
   Giving = MathMin(MarketInfo(NULL, MODE_MAXLOT), Giving);
   
   // Final check to see if I can afford this...
   if(Giving * One_Lot > Free)
      return(0.00);    // Nope  :(
   else
      return(Giving);  // All good!
}

 
Claude Beaudoin:
Does this work for non FX securities like Gold, and Oil? that have 0.25 steps?