Correct calculation of the lot from the % of the deposit

 

Hi all,

Roger, one of the programmers, recently opened my eyes to the fact, that I've written my function of calculating lots by %.


my function is as follows:


double GetSizeLot() //Функция возвращает значение лотов,
{ //если включен ММ то значение лотов,
double lots,MD,RM,MinLots,LotSize; int lotsdigit; //вычисляется путем:Свободные средства*Risk
LotSize = MarketInfo(Symbol(), MODE_LOTSIZE); //поделить на стоимость одного лота
MD = NormalizeDouble(MarketInfo(Symbol(), MODE_LOTSTEP), 2); //А также вычисляет колличество знаков
if(MD==0.01) lotsdigit=2; //присваивает значения максимума лотов
if(MD==0.1) lotsdigit=1;
if(MM>1) {lots=NormalizeDouble((AccountFreeMargin()*MM/LotSize),lotsdigit);Lot=lots;}
else lots=Lot;
MinLots=NormalizeDouble(MarketInfo(Symbol(),MODE_MINLOT),2);
if(lots < MinLots) lots = MinLots;
if(lots > MaxLot) lots = MaxLot;
return(NormalizeDouble(lots,2));
}



I should use leverage and current price,


I tried to check it with Expert Advisors, but they all had the same function,


where is the truth?


I would like to ask you for the correct function to calculate the lot in %!

 
Usually the problem is that there is confusion between the deposit load and the amount of risk per position. It is advisable to check both. What counts in the above code is the load, or rather the lot size based on the load.
 
This one is much better, there's no shoulder calculation, it's a different way of doing it. I like it very much.

double LotSize( int type, double LotRisk, int SL  )
{   //    int znakov_lot=0;
        double  lot_min         = MarketInfo( Symbol(), MODE_MINLOT  ); 
        double  lot_max         = MarketInfo( Symbol(), MODE_MAXLOT  ); 
        double  lot_step        = MarketInfo( Symbol(), MODE_LOTSTEP ); 
        double  lotcost         = MarketInfo( Symbol(), MODE_TICKVALUE );       
                
        double  lot             = 0.0;
        double  dollarsPerPip   = 0.0;
        
        lot = AccountBalance()*LotRisk/100.0;
        dollarsPerPip = lot/SL;
     //   if (lot_min<2) {znakov_lot=0;  if (lot_min<0.2) {znakov_lot=1;  if (lot_min<0.02) {znakov_lot=3;  if (lot_min<0.002) {znakov_lot=4; }}}}      
        lot = NormalizeDouble( dollarsPerPip/lotcost, 2 );      
        
        lot = NormalizeDouble( lot / lot_step, 0 ) * lot_step;
        
        if ( lot < lot_min ) lot = lot_min;
        if ( lot > lot_max ) lot = lot_max;
        
        if ( AccountFreeMarginCheck( Symbol(), type, lot ) < 10 || GetLastError() == 134 ) 
        { 
                Alert ( "Impossible to open position with lot = ", DoubleToStr( lot, 2 ), ". Not enough money." );
                return(-1);
        }

        return( lot );
}
The calculation of the lot on the % of the deposit for a given distance of pips. i.e. "to drain 20% of the deposit in 10 pips = you need ?lot" is the function of this question and counts
 
wenay:
This one is much better, there is no leverage calculation here. I like very much the calculation of the lot for the % of the deposit for a given distance of pips. i.e. "to drain 20% of the deposit in 10 pips = you need ?lot" this function asks and calculates

In my opinion, leverage should be present in lot calculations.

1) If the leverage is not taken into account, it means that the TS is not capable of generating large profits.

2) If the deposit is already big enough, the leverage is reduced (in my brokerage company automatically), so the error "Wrong volume" appears.

It's a leverage, it's not a rubber one.

P.S. this is my IMHO

 
rensbit:

> No leverage calculation

In my opinion, leverage should be included in the lot calculation.

1) If the leverage is not included, it means that the TS is not capable of generating large profits.

2) If the deposit is already big enough, DT decreases the leverage (in my brokerage company automatically), so there will be an error "Wrong volume".


P.S. it's my IMHO


Instead of leverage, look at the cost of a tick. If you have 1k1 leverage - a tick is not expensive (say there is 1 cent), if 1k 500 - expensive (5 quid). I.e. the system works well with any leverage, here is another way of calculation, it seems to me more optimal and correct.

your brokerage company will automatically switch to different leverage for you.

 
Vladon:
... you still have to use the shoulder ...
If Risk is set as a percentage, then:
lot = AccountEquity()*0.01*Risk / MarketInfo(Symbol(), MODE_MARGINREQUIRED);
 

Roger was very helpful on this matter, for which he deserves special thanks to!!!!


I've reworked it slightly, so that the account currency can be determined by myself


int Percent=10;
string Valdepo=AccountCurrency();

int start()
{
double Lot,MinLots,MaxLots;
int lotdig;
if(MarketInfo(Symbol(),MODE_LOTSTEP)==0.01)lotdig=2; else lotdig=1;
if(Valdepo=="USD")
   {
   if(StringSubstr(Symbol(),0,3)=="USD")Lot=NormalizeDouble(AccountFreeMargin()*Percent*AccountLeverage()/100/MarketInfo(Symbol(),MODE_LOTSIZE),lotdig);
   else if(StringSubstr(Symbol(),3,3)=="USD")Lot=NormalizeDouble(AccountFreeMargin()*Percent*AccountLeverage()/Ask/100/MarketInfo(Symbol(),MODE_LOTSIZE),lotdig);
   else 
      {
      double pr=MarketInfo(StringSubstr(Symbol(),0,3)+"USD",MODE_ASK);
      if(pr!=0)Lot=NormalizeDouble(AccountFreeMargin()*Percent*AccountLeverage()/pr/100/MarketInfo(Symbol(),MODE_LOTSIZE),lotdig);
      else Lot=NormalizeDouble(AccountFreeMargin()*Percent*AccountLeverage()/100/MarketInfo(Symbol(),MODE_LOTSIZE),lotdig);
      }
   }
if(Valdepo=="EUR")
   {
   if(StringSubstr(Symbol(),0,3)=="EUR")Lot=NormalizeDouble(AccountFreeMargin()*Percent*AccountLeverage()/100/MarketInfo(Symbol(),MODE_LOTSIZE),lotdig);
   else
      {
      pr=MarketInfo("EUR"+StringSubstr(Symbol(),0,3),MODE_BID);
      if(pr!=0)Lot=NormalizeDouble(AccountFreeMargin()*Percent*AccountLeverage()*pr/100/MarketInfo(Symbol(),MODE_LOTSIZE),lotdig);
      else Lot=NormalizeDouble(AccountFreeMargin()*Percent*AccountLeverage()/100/MarketInfo(Symbol(),MODE_LOTSIZE),lotdig);
      }
   }
MinLots=MarketInfo(Symbol(),MODE_MINLOT);
MaxLots=MarketInfo(Symbol(),MODE_MAXLOT);
if(Lot < MinLots) Lot = MinLots;
if(Lot > MaxLots) Lot = MaxLots;
return(0);
}
 

Are there no other account currencies? ;-)

And the risks are not taken into account? This option also counts only on the deposit load, and what will be the risks (in % of the deposit) at drawdown - is ignored. As if when you open, you always guess the direction from the first moment and to the last.

 

useless function, with errors besides...

if(Lot < MinLots) Lot = MinLots;

what is this? not enough money still open ?

if we have 100 quid, 100 leverage and 100% risk, i.e. we open wide, what do we get ?

if(StringSubstr(Symbol(),0,3)=="USD")
Lot=NormalizeDouble(AccountFreeMargin()*Percent*AccountLeverage()/100/MarketInfo(Symbol(),MODE_LOTSIZE),lotdig);

lot = 100 * 100 * 100 / 100 / 100 = 100

I have not mixed up anything ? for 100 quid it is unlikely that a lot of 100 can open on a pair ...

 
keekkenen:

If we have 100 quid, 100 leverage and 100% risk, i.e. we open full, what do we get?

lot = 100 * 100 * 100 / 100 / 100 = 100

i did not confuse anything ? for 100 quid it is unlikely that a lot of 100 can open on a pair ...



Of course, mixed up, the real one is 100*100*100/100/100000=0.1
 
ah, sorry, that was me looking at gold... so no trading in gold now using your formula ?