EA Calculates lots per Money Risk Percentage

 
Greetings,

I'm writing an EA which will trade according to user's risk percentage.

Let's say it opens an Long position during daylight and Short position during night, a simple/stupid EA.
EA's input value is the risk percentage, let's say 15 for 15% of available money.
Whenever opens a position it has to calculate the amount of lots according to the 15% for the money.

The following is the algorithm that I'm using for calculating the lots:

account's currency=USD
total money=ACCOUNT_EQUITY //1000
risk=percentage * total money // 0.15*1000
if (account's currency==symbol's margin currency)
{
  //example USDJPY
  lots= (risk*leverage) / contract size //(150*100)/100000=0.66
}
else if (account's currency==symbol's profit currency)
{
  //example EURUSD
  lots= ( (risk*leverage) / contract size ) / price //((150*100)/100000)/1.13=0.58
  //price is the ask/bid of EURUSD
}
else //not related to account's currency
{
  //example EURGBP
  lots= (risk*price*leverage) / contract size //(150*0.75*100)/100000=0.11
  //price is the ask/bid value of EURUSD
  //EURUSD = EUR-margin currency of EURGBP
  //         USD-account currency
}

if (lots>MAX_LOTS)
  lots=MAX_LOTS
else if (lots>MIN_LOTS)
  lots=MIN_LOTS
I suppose this approach should work on all symbols, forex, indeces futures etc...
But on metals, indeces and some futures it reaches the maximum amount of lots and it cannot open position because of insufficient amount of money.

What are your opinions?
 
You have to use AccountFreeMarginCheck() and not calculate your self from leverage. For example on GOLD, leverage is usually not the same as for Forex.
 
Thank you for your quick response!

Alain Verleyen:
You have to use AccountFreeMarginCheck() and not calculate your self from leverage. For example on GOLD, leverage is usually not the same as for Forex.

Correct me if I'm wrong.
A Broker gives a constant leverage for all it's symbols.
That's why we use AccountInfoInteger(ACCOUNT_LEVERAGE) an account function and not a symbol function.
But the contract size and volume step differs from symbol to symbol, so we use symbol functions SymbolInfoXXX(symbol, YYY) to get access to their values.

ex.symbols

As for AccountFreeMarginCheck()  its 3rd argument is the lots.
Basically it tells me the remaining money if I had opened a position with the given lots.
On my problem, lots is the requested number and not the given.

When I calculate the lots I use a fraction of the available money, so I should never reach that point of not enough money.
For example, I have $1000, I want to risk 15% of the money, that makes it $150. So I need to find the correct amount of lots that are equivalent to $150.

Thanks!
 
aknetmma:
Thank you for your quick response!


Correct me if I'm wrong.

A Broker gives a constant leverage for all it's symbols.

No, you are wrong, it's what I said previously. Don't ask me why, but it's a fact that the leverage symbols is not constant for all symbols. Of course, that could depends of the broker, I didn't check all, but I saw several where it's not the case.
...

As for AccountFreeMarginCheck()  its 3rd argument is the lots.
Basically it tells me the remaining money if I had opened a position with the given lots.
On my problem, lots is the requested number and not the given.

When I calculate the lots I use a fraction of the available money, so I should never reach that point of not enough money.
For example, I have $1000, I want to risk 15% of the money, that makes it $150. So I need to find the correct amount of lots that are equivalent to $150.

Thanks!
AccountFreeMarginCheck() gives your the margin use for X lots. It's exactly what you need. You know your current margin, you know the margin after you would have open X lots on a given symbol. That gives you the margin for X lots, then it's just a cross-multiplication to have your lot size.
Cross-multiplication - Wikipedia, the free encyclopedia
Cross-multiplication - Wikipedia, the free encyclopedia
  • en.wikipedia.org
Given an equation like: (where and are not zero), one can cross-multiply to get: In practice, the method of cross-multiplying means that we multiply the numerator of each (or one) side by the denominator of the other side, effectively crossing the terms over. The mathematical justification for the method is from the following longer...
 
//FUNCTION TO CALCULATE LOT SIZE
  double CalculateLotSize(int SL_Ticks,string symbol,double riskp)
  {
  double final_lot=0;
  //calculation with SL
  if(SL_Ticks>0)
  {
  double tick_value=MarketInfo(symbol,MODE_TICKVALUE);
  double sl_value=tick_value*SL_Ticks;
  double amount_to_risk=(AccountBalance()/100)*riskp;
  final_lot=amount_to_risk/sl_value;
  }
  //calculation without SL
  if(SL_Ticks==0)
  {
  double one_lot_margin=SymbolInfoDouble(symbol,SYMBOL_MARGIN_INITIAL);
  double amount_to_risk=(AccountBalance()/100)*riskp;
  final_lot=amount_to_risk/one_lot_margin;
  }
  //find lot digits for the asset  
    double minlot=MarketInfo(symbol,MODE_MINLOT);
    int digitos=0;
    double transfer=minlot;
    while(transfer<1)
    {
    digitos++;
    transfer=transfer*10;
    }
    final_lot=NormalizeDouble(final_lot,digitos);
  //find lot digits for the asset ends here

  
  return(final_lot);
  }
//FUNCTION TO CALCULATE LOT SIZE ENDS HERE 

 SL_Ticks -> Stop Loss In points , if you want to calculate without the stop loss in mind send SL_Ticks as 0
 symbol -> the symbol
 riskp    -> the risk percentage 

 
Lorentzos Roussos:

 SL_Ticks -> Stop Loss In points , if you want to calculate without the stop loss in mind send SL_Ticks as 0
 symbol -> the symbol
 riskp    -> the risk percentage 

SYMBOL_MARGIN_INITIAL is not for Forex.

 
You are right . 
Thanks Alain 
 
Thanks Alain!
 
What about the Margin Rates?
Should I take account the Margin Initial on lot calculation?
 
There is an issue on lots.

The method for calculating the lots that is used is according to the previous comments:

Lots = FreeMargin(100) * Risk(0.2) * LotStep(0.01) / FreeMargin(100) - CheckMargin(80)
The difference of FreeMargin and CheckMargin gives the value in dollars of 0.01 lot(input lot for CheckMargin function was 0.01).

Trade
Note: The picture is taken just a second after the positions opened.

The question is why some symbols such as silver behave differently from others.
For the same amount of risk percentage(2%) the proportion of lots and profit is vast.
Reason: