Correct calculation of the lot from the % of the deposit - page 3

 
7bec:

but surely it should be AccountBalance and not AccountFreeMargin here, then suddenly the expert is not working in one lot?

Did you think long before asking a four year old post a question? Do you really think he will answer you?
 
zoritch:

Did you think long and hard before asking a four-year-old post a question? Do you really think he will answer you?
What if you're lucky?
 
7bec:

but surely it should be AccountBalance here and not AccountFreeMargin, then suddenly the expert is not working in one lot?

You shouldn't use this function, I mean the whole function here that I liked in the summer of 2010. Because it can be printed in a better way
 
wenay:

You shouldn't use this function, I mean the whole function here that I liked in the summer of 2010. Because there are better ways to print


for example? From what I found on the net overnight, I liked this one the best.

Of course any code can be honed to infinity, but the best is the enemy of the good ;)

 

I'm not an expert in MQL4 programming, so I'm looking for some tips...

This is the third day I'm scratching my head over writing a script for lot calculation. My idea is the following: having a (conditional) deposit of $ 1000 the lot will be 0.05 (actually divided by 20000).

It's OK when you work with EURUSD, GBPUSD, AUDUSD, NZDUSD (i.e. buying in the deposit currency) .... I don't know how to get a lot for USDJPY (or USDRUR)... I don't know how to get lots in USDJPY... If I'm bored, what am I missing????, tell me where to look for something else.

 
gochu:

I'm not an expert in MQL4 programming, so I'm looking for some tips...

This is the third day I'm scratching my head over writing a script for lot calculation. My idea is the following: having a (conditional) deposit of $ 1000 the lot will be 0.05 (actually divided by 20000).

It's OK when you work with EURUSD, GBPUSD, AUDUSD, NZDUSD (i.e. buying in the deposit currency) .... How do I calculate the lot for CADJPY (or USDRUR)? 1000 quid turns (for today) into 113,000 yen or 38700 rubles, it won't be 5.65 and 0.19 lots in that case ... I'm working too hard, what am I missing???? advise where to go ... Leave your banter and trolling for later!


Look at the problem from a slightly different angle: to use a lot equivalent to a certain amount of money in the currency of the deposit.

Here's a script to calculate the lot size depending on the contract size. The contract size is set in the deposit currency.

Files:
 

Below is my way of calculating the lot for a trade (code before MQL4 update so no #property strict):

extern double DealLevel = 5.0; // процент свободной маржи для сделки
//---
double LotDeal = NormalizeDouble(TradedLotFunc(DealLevel),2);

//--- TradedLotFunc() - start ----- сама функция
double TradedLotFunc(double Deal_f)
{  
   double MinLot_f  =MarketInfo(Symbol(),MODE_MINLOT);
   double MaxLot_f  =MarketInfo(Symbol(),MODE_MAXLOT);
   double LotStep_f =MarketInfo(Symbol(),MODE_LOTSTEP);
   double LotSize_f =MarketInfo(Symbol(),MODE_LOTSIZE);
   int    Leverage_f=AccountLeverage();
   //---
   int    i, coef; 
   double HighBorder;
   //---
   if(Deal_f<=0.0)  return(MinLot_f);
   if(Deal_f>100.0) Deal_f=100.0;
   double TradedLot=(AccountFreeMargin()*Deal_f/100)*Leverage_f/LotSize_f;
   if(TradedLot<=MinLot_f) return(MinLot_f);
   if(LotStep_f==0.001 || LotStep_f==0.01 || LotStep_f==0.1 || LotStep_f==1.0)
      TradedLot=MathRound(TradedLot/LotStep_f)*LotStep_f;
   else
   {  if(((TradedLot-MinLot_f)/LotStep_f)<=5.0) coef=1;
      else coef=MathRound((TradedLot-MinLot_f)/LotStep_f)-3;
      for(i=0; i<=100; i++)
      {  HighBorder=MinLot_f+(LotStep_f*coef)+(LotStep_f*i);
         if(TradedLot> HighBorder) continue;
         if(TradedLot==HighBorder) break;
         if(TradedLot< HighBorder)
         {  if(MathAbs(HighBorder-TradedLot)<=MathAbs(TradedLot-(HighBorder-LotStep_f)))
            {  TradedLot=HighBorder; break; }
            else
            {  TradedLot=HighBorder-LotStep_f;  break; }
   }  }  }
   if(TradedLot>=MaxLot_f) return(MaxLot_f);
   return(TradedLot);
}
//--- TradedLotFunc() - end -----
 

does TradedLotFunc() always return more than zero?

If no, then the code turns into a bullshit if(Deal_f<=0.0) return(MinLot_f); - you are a member of a party? ...and machine gun firing again...

And the rest is even more funny if(TradedLot<=MinLot_f) return(MinLot_f) ; - Are you a party man? ...and the machine gun's gone off again...

---------------------

in general, if we assume the function works correctly, it's barely good enough to open the first and only order in a trading account...

Hardly because it does not take into account either existing open positions or the stop loss of the order to be opened, i.e. it is a schoolboy's version

And no sane person would use this goodness when trading on a real account...

 

Thank you for the tips, but I didn't quite formulate my question correctly.... I want to base my calculation on the amount of money lost/stolen. Obviously the collateral for euro.quid and pound.quid is different, but that's not exactly what I want to arrive at. Ideally I should have a function that would take parameters as input:

1- deposit currency (I will check on quid and euro),

2 - Deposit amount (1000)

3 - my trading currency pair (I would like to check CADJPY and USDCHF)

4 - my deposit drawdown percentage (let's assume 2%) and

5- a certain number of points (100 pips for 4-digit platforms and 1000 for five-digit).

Output parameter I want to get the lot size.

That is, (everything is clear for pairs with inverse quotes, EURUSD, GBPUSD, AUDUSD, NZDUSD, that is, if the quote currency is the currency of the deposit)... in order to

at a deposit of 1000 quid, I lose / win 2% (i.e. 20 quid) when the currency passes 100(4 digits)/1000 pips (5 digits), I have to use 0.02 lot.

(I want to repeat that I should use more money for trading GBPUSD than for EURUSD, or especially NZDUSD, but my question is not about money usage, but about nominal loss and/or profit ... please advise me ... I'm losing my head ... I've been working with this problem for less than a week). I got to the point that I had to calculate the value of loss in the currency quotes ... but how to transform it (the loss) in the desired me a lot of the deposit currency..... all here I stumble ... Thanks for the help!

 
gochu:

Thanks, for the tips, but I didn't quite formulate my question correctly.... I want to calculate a lot based on the amount of money lost/stolen. ...

Look at the function https://docs.mql4.com/ru/marketinformation/marketinfo with the query identifier MODE_TICKVALUE

Reason: