Lot Size Calculation from Risk Percentage

 

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

 
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 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));
 }
 
  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
 
eevviill:

 

Thanks @eevvill: but i want to calculate from stoploss
 
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 :)

 
shahzaibidrees:
Thanks @eevvill: but i want to calculate from stoploss
https://www.mql5.com/ru/code/viewcode/14296/79100/lot_risk_sl__2.mq4
 
  1. Use the link button  Use the link button
  2. Violates
    Do NOT use TickValue by itself - DeltaPerlot
 
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.
 
eevviill:
https://www.mql5.com/ru/code/viewcode/14296/79100/lot_risk_sl__2.mq4
Thanks @eevvill for the code :)
 
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.
 
shahzaibidrees:
Thanks @eevvill for the code :)
My pleasure.
Reason: