# How to calculate Lot size based on account balance??

9

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(result>0)
{
TheStopLoss=0;
TheTakeProfit=0;
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
{
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++)
{
if(OrderType()<=OP_SELL &&
OrderSymbol()==Symbol() &&
OrderMagicNumber()==MagicNumber
)
{
{
{
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)
{
{
{
return(0);
}
}
}
}
}
}
return(0);
}

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

}
return (result);
}

16102

1.  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