Download MetaTrader 5

Lot Size Calculation from Risk Percentage

To add comments, please log in or register
Shahzaib Idrees
85
Shahzaib Idrees 2016.03.10 05:11 

i am trying to make an EA for auto risk calculation but still not successful.

i am trying to calculate risk from Stoploss value in pip.

Example my stoploss is 10pip & i want to take the risk of 5% of my total Account balance. it should calculate from these two parameters...

Here is my code i tried but not worked for me.

//RiskCalculate
double riskcalculation(int Val_SL01)
{
      AccRiskAmount = MathAbs(MathRound((MathMin(AccountEquity(),AccountBalance())/100)*RiskPercentage));
      Symbol_RC = Symbol();
      BaseCurr = StringSubstr(Symbol_RC,0,3);
      QuoteCurr = StringSubstr(Symbol_RC,3,3);
      AcctCurr = AccountCurrency(); 
      _bid = MarketInfo(Symbol_RC,MODE_BID);
      _ask = MarketInfo(Symbol_RC,MODE_ASK);
      minlot = MarketInfo(Symbol_RC,MODE_MINLOT);
      USDExchange = MarketInfo("EURUSD",MODE_BID);    
      lotSize = MarketInfo(Symbol_RC,MODE_LOTSIZE);
      double ValPerPip;
      //Calc By Pair Curruncy value per pip
      if(Symbol()=="GDAXIm.lmx" && Symbol()=="XAUUSD.lmx"){ValPerPip=10;}
      else{    
         if(QuoteCurr==AcctCurr){ ValPerPip = lotSize*tickSize;}
         else if(BaseCurr==AcctCurr){ ValPerPip = (lotSize*tickSize)/_bid;}
         else{ValPerPip = (lotSize*tickSize*USDExchange)/_bid;}
      }
      double lotsizecalc = NormalizeDouble(AccRiskAmount/(ValPerPip*Val_SL01),1);
      if(lotsizecalc<minlot){lotsizecalc=minlot;}
   return(NormalizeDouble(lotsizecalc,1));
}

it would be really grateful if anyone have the code for the EA & help me out for the correct formula.Thanks

Vasyl Nosal
18635
Vasyl Nosal 2016.03.10 09:44  
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 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));
 }
whroeder1
14742
whroeder1 2016.03.10 11:24  
  1. Don't paste code
    Play video
    Please edit your post.
    For large amounts of code, attach it.

  2. Symbol_RC = Symbol();
    _bid = MarketInfo(Symbol_RC,MODE_BID);
    _ask = MarketInfo(Symbol_RC,MODE_ASK);
    Why use those function calls when working with current symbol. Just use the predefined Bid/Ask
    • You place the stop where it needs to be - where the reason for the trade is no longer valid. E.g. trading a support bounce the stop goes below the support.
    • Account Balance * percent = RISK = |OrderOpenPrice - OrderStopLoss| * OrderLots * DeltaPerlot (Note OOP-OSL includes the SPREAD)
    • Do NOT use TickValue by itself - DeltaPerlot
    • You must normalize lots properly and check against min and max.
    • You must also check FreeMargin to avoid stop out
  3.  return(NormalizeDouble(lotsizecalc,1));
    Do NOT use NormalizeDouble, EVER. For ANY Reason. It's a kludge, don't use it. It's use is always wrong
Shahzaib Idrees
85
Shahzaib Idrees 2016.03.10 12:40  
eevviill:

 

Thanks @eevvill: but i want to calculate from stoploss
Shahzaib Idrees
85
Shahzaib Idrees 2016.03.10 12:43  
WHRoeder:
  1. Why use those function calls when working with current symbol. Just use the predefined Bid/Ask
    • You place the stop where it needs to be - where the reason for the trade is no longer valid. E.g. trading a support bounce the stop goes below the support.
    • Account Balance * percent = RISK = |OrderOpenPrice - OrderStopLoss| * OrderLots * DeltaPerlot (Note OOP-OSL includes the SPREAD)
    • Do NOT use TickValue by itself - DeltaPerlot
    • You must normalize lots properly and check against min and max.
    • You must also check FreeMargin to avoid stop out
  2. Do NOT use NormalizeDouble, EVER. For ANY Reason. It's a kludge, don't use it. It's use is always wrong

Thanks WHRoeder: this stuff helped me alot let me try if can solve the issue to following these.... well really thanks for this :)

Vasyl Nosal
18635
Vasyl Nosal 2016.03.10 13:09  
shahzaibidrees:
Thanks @eevvill: but i want to calculate from stoploss
https://www.mql5.com/ru/code/viewcode/14296/79100/lot_risk_sl__2.mq4
whroeder1
14742
whroeder1 2016.03.10 13:16  
  1. Use the link button  Use the link button
  2. Violates
    Do NOT use TickValue by itself - DeltaPerlot
Vasyl Nosal
18635
Vasyl Nosal 2016.03.10 14:37  
WHRoeder:
  1. Use the link button 
  2. Violates
    Do NOT use TickValue by itself - DeltaPerlot

You are the worstest teacher(explanation) that I saw ever. So unfortenetly I can't use your advices.
Shahzaib Idrees
85
Shahzaib Idrees 2016.03.10 18:04  
eevviill:
https://www.mql5.com/ru/code/viewcode/14296/79100/lot_risk_sl__2.mq4
Thanks @eevvill for the code :)
whroeder1
14742
whroeder1 2016.03.10 20:51  
eevviill: You are the worstest teacher(explanation) that I saw ever. So unfortenetly I can't use your advices.
You won't read, study, learn, nor ask questions, so no one can teach you.
Vasyl Nosal
18635
Vasyl Nosal 2016.03.10 23:51  
shahzaibidrees:
Thanks @eevvill for the code :)
My pleasure.
12
To add comments, please log in or register