Download MetaTrader 5

How to calculate Lot size based on account balance??

To add comments, please log in or register
zerodblone
9
zerodblone  

Hello all


As per the subject header, I would like to include some code in my EA which implements a position size as a percentage of account balance, using the fixed SL that I input for the asset as well as the maximum risk per trade = 6% of account balance.

E.g.  Account balance = $10k , my manually input fixed SL = 1200 pips , MAx risk = 6% therefore EA will open 0.5 Lot where P/L of the asset = $1/pip

Assuming a profitable trade occurs taking the account balance to $15k, then on the next trade the EA should generate an order of 0.75 lots as SL remains fixed at 1200 pips.

The code for my EA is as follows...(if you are able to help with the query above, could you please let me know where your code should appear in my code below?

Thanks and please forgive my naivety - new to MQL but very keen to learn!


extern int MagicNumber=10001;
extern double Lots =1;
extern double StopLoss=1200;
extern double TakeProfit=0;
extern int TrailingStop=0;
extern int Slippage=3;
//+------------------------------------------------------------------+
//    expert start function
//+------------------------------------------------------------------+
int start()
{
  double MyPoint=Point;
  if(Digits==3 || Digits==5) MyPoint=Point*10;
 
  double TheStopLoss=0;
  double TheTakeProfit=0;
  if( TotalOrdersCount()==0 )
  {
     int result=0;
     if((Bid>iMA(NULL,0,16,0,MODE_EMA,PRICE_CLOSE,0))&&(iMA(NULL,0,8,20,MODE_SMA,PRICE_CLOSE,0)>iMA(NULL,0,16,0,MODE_SMA,PRICE_CLOSE,0))) // Here is your open buy rule
     {
        result=OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,0,0,"EA Generator www.ForexEAdvisor.com",MagicNumber,0,Blue);
        if(result>0)
        {
         TheStopLoss=0;
         TheTakeProfit=0;
         if(TakeProfit>0) TheTakeProfit=Ask+TakeProfit*MyPoint;
         if(StopLoss>0) TheStopLoss=Ask-StopLoss*MyPoint;
         OrderSelect(result,SELECT_BY_TICKET);
         OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(TheStopLoss,Digits),NormalizeDouble(TheTakeProfit,Digits),0,Green);
        }
        return(0);
     }
     if((Close[0]<iMA(NULL,0,16,0,MODE_SMA,PRICE_CLOSE,0))&&(iMA(NULL,0,8,0,MODE_SMA,PRICE_CLOSE,0)<iMA(NULL,0,16,0,MODE_SMA,PRICE_CLOSE,0))) // Here is your open Sell rule
     {
        result=OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,0,0,"EA Generator www.ForexEAdvisor.com",MagicNumber,0,Red);
        if(result>0)
        {
         TheStopLoss=0;
         TheTakeProfit=0;
         if(TakeProfit>0) TheTakeProfit=Bid-TakeProfit*MyPoint;
         if(StopLoss>0) TheStopLoss=Bid+StopLoss*MyPoint;
         OrderSelect(result,SELECT_BY_TICKET);
         OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(TheStopLoss,Digits),NormalizeDouble(TheTakeProfit,Digits),0,Green);
        }
        return(0);
     }
  }
 
  for(int cnt=0;cnt<OrdersTotal();cnt++)
     {
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
      if(OrderType()<=OP_SELL &&  
         OrderSymbol()==Symbol() &&
         OrderMagicNumber()==MagicNumber
         ) 
        {
         if(OrderType()==OP_BUY) 
           {
              if((Close[0]<iMA(NULL,0,16,0,MODE_SMA,PRICE_CLOSE,0))&&(iMA(NULL,0,8,0,MODE_SMA,PRICE_CLOSE,0)<iMA(NULL,0,16,0,MODE_SMA,PRICE_CLOSE,0))) //here is your close buy rule
              {
                   OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),Slippage,Red);
              }
            if(TrailingStop>0) 
              {                
               if(Bid-OrderOpenPrice()>MyPoint*TrailingStop)
                 {
                  if(OrderStopLoss()<Bid-MyPoint*TrailingStop)
                    {
                     OrderModify(OrderTicket(),OrderOpenPrice(),Bid-TrailingStop*MyPoint,OrderTakeProfit(),0,Green);
                     return(0);
                    }
                 }
              }
           }
         else
           {
                if((Close[0]>iMA(NULL,0,16,0,MODE_SMA,PRICE_CLOSE,0))&&(iMA(NULL,0,8,0,MODE_SMA,PRICE_CLOSE,0)>iMA(NULL,0,16,0,MODE_SMA,PRICE_CLOSE,0))) // here is your close sell rule
                {
                   OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),Slippage,Red);
                }
            if(TrailingStop>0) 
              {                
               if((OrderOpenPrice()-Ask)>(MyPoint*TrailingStop))
                 {
                  if((OrderStopLoss()>(Ask+MyPoint*TrailingStop)) || (OrderStopLoss()==0))
                    {
                     OrderModify(OrderTicket(),OrderOpenPrice(),Ask+MyPoint*TrailingStop,OrderTakeProfit(),0,Red);
                     return(0);
                    }
                 }
              }
           }
        }
     }
   return(0);
}

int TotalOrdersCount()
{
  int result=0;
  for(int i=0;i<OrdersTotal();i++)
  {
     OrderSelect(i,SELECT_BY_POS ,MODE_TRADES);
     if (OrderMagicNumber()==MagicNumber) result++;

   }
  return (result);
}

whroeder1
16102
whroeder1  
  1. Don't paste code
    Play video
    Please edit your post.
    For large amounts of code, attach it

    • 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/100 = RISK = OrderLots * (|OrderOpenPrice - OrderStopLoss| * DeltaPerLot + CommissionPerLot) (Note OOP-OSL includes the SPREAD, and DeltaPerLot is usually around $10/pip but it takes account of the exchange rates of the pair vs. your account currency.)
    • 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
To add comments, please log in or register