how calcolate size of 2% equity

 
Hello to all
I'm writing an ea for mt4, I wanted to open an order with a size dimensioned to 2% risk relative to capital

I wrote the following code, when I go to calculate the size, gives me a size of 14 lots, using a demo account by EUR 5,000 and a stop of 50 pips.

Obviously it is wrong, you tell me how to calculate the correct size?

the wrong code is that of getSize () function

thank you

 

 

//+------------------------------------------------------------------+
//|                                                   TestSizeEA.mq4 |
//|                        Copyright 2015, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2015, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict


//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
   openBuy() ;
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
  
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
  
  }

void openBuy()
   {
   double stopLossInPips = 50;
   double takeProfitInPips = 75;

   int slippage = 3;
   int magicNumber = 1;

   double pairPipScale = 0.0001;
   if (Digits() == 2 || Digits() == 3) pairPipScale = 0.01;
  
   double stopLossInPipsScaled = stopLossInPips*pairPipScale;
   double size = getSize(stopLossInPips);

        double stopLossPrice = NormalizeDouble(Ask-stopLossInPipsScaled,Digits);

   double takeProfitInPipsScaled = takeProfitInPips*pairPipScale;
        double takeProfitPrice= NormalizeDouble(Ask+takeProfitInPipsScaled,Digits);

   Print("buy Ask: ",Ask," - stopLossPrice: ",stopLossPrice," - takeProfitPrice: ",takeProfitPrice);
  
        double orderTicket = OrderSend(NULL,OP_BUY,size,Ask,slippage,stopLossPrice,takeProfitPrice,"pos1",magicNumber,0,clrGreen);
        if(orderTicket<0)      
           {
                Print("OrderSend buy failed with error: ",GetLastError());

           }

   }
  
double getSize(double stopLossInPips) {
        double maxLossPercent = 2;//rischio per operazione impostato al 2 % dell'equity
        double pipValue = MarketInfo(Symbol(),MODE_TICKVALUE);
        if (Digits==3 || Digits==5) pipValue *= 10;
        double orderSize = NormalizeDouble(AccountEquity() * maxLossPercent / stopLossInPips * pipValue / 100, Digits);
        Print("orderSize ",orderSize);
        return orderSize;
}


 

 
  • 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
 

Asked again and again and again...

Please do some searches before posting

 

after read many many topic, i get this code:

 

double getSizeForse(double stopLossInPips)
   {
   double pipValue = MarketInfo(Symbol(), MODE_TICKVALUE);
   Print("pipValue  ",pipValue);
   //Print("getPipValue  ",getPipValue(""));
   double lots = AccountFreeMargin() * Risk / (stopLossInPips * pipValue);

   lots = lots/1000;

   double lotStep = MarketInfo(Symbol(), MODE_LOTSTEP);
   int digits = 0;
   if(lotStep <= 0.01)
      digits = 2;
   else if(lotStep <= 0.1)
      digits = 1;
   lots = NormalizeDouble(lots, digits);

      
   double stopInMoney = AccountEquity()/100*Risk;
   Print("stopInMoney ",stopInMoney);
   stopInMoney = NormalizeDouble(stopInMoney,2);
   Print("stopInMoney approssiamto ",stopInMoney);
   Print("Equity ",AccountEquity()," Risk ", Risk, "%, Risk ",stopInMoney,"€ StopInPips ", stopLossInPips, " Size ", lots);
   return (lots);
}

 this code work well if my account is in USD but this code is wrong if my account is in Euro.

Example

if i have a  AccountEquity = 10.000 $ and stopInPips = 50 and risk = 1%, this code give my a size of 2 minilots <--- CORRECT because i get stop of 100$

but 

if i have a  AccountEquity = 10.000 euro and stopInPips = 50 and risk = 1%, this code give my a size of 2.1 minilots <--- INCORRECT because i get stop of 89 euro, but i want a stop of 100 euro

 

I need help, please

 
andream1977:

after read many many topic, i get this code:

 

double getSizeForse(double stopLossInPips)
   {
   double pipValue = MarketInfo(Symbol(), MODE_TICKVALUE);
   Print("pipValue  ",pipValue);
   //Print("getPipValue  ",getPipValue(""));
   double lots = AccountFreeMargin() * Risk / (stopLossInPips * pipValue);

   lots = lots/1000;

   double lotStep = MarketInfo(Symbol(), MODE_LOTSTEP);
   int digits = 0;
   if(lotStep <= 0.01)
      digits = 2;
   else if(lotStep <= 0.1)
      digits = 1;
   lots = NormalizeDouble(lots, digits);

      
   double stopInMoney = AccountEquity()/100*Risk;
   Print("stopInMoney ",stopInMoney);
   stopInMoney = NormalizeDouble(stopInMoney,2);
   Print("stopInMoney approssiamto ",stopInMoney);
   Print("Equity ",AccountEquity()," Risk ", Risk, "%, Risk ",stopInMoney,"€ StopInPips ", stopLossInPips, " Size ", lots);
   return (lots);
}

 this code work well if my account is in USD but this code is wrong if my account is in Euro.

Example

if i have a  AccountEquity = 10.000 $ and stopInPips = 50 and risk = 1%, this code give my a size of 2 minilots <--- CORRECT because i get stop of 100$

but 

if i have a  AccountEquity = 10.000 euro and stopInPips = 50 and risk = 1%, this code give my a size of 2.1 minilots <--- INCORRECT because i get stop of 89 euro, but i want a stop of 100 euro

 

I need help, please

You are mixing pip and tick, that's not the same.

You are mixing free margin and equity, that's not the same. 

  lots = lots/1000;

Why are you dividing by 1000 ?

Anyway, despite all of that the right answer is 2.1 (or 0.21 standard lot).

No idea where your "89 euros" comes from. It could be 99€ as there is some rounding, but certainly not 89, if your stops is really 50 pips.
 

 
andream1977:

if i have a  AccountEquity = 10.000 $ and stopInPips = 50 and risk = 1%, this code give my a size of 2 minilots <--- CORRECT because i get stop of 100$

but 

if i have a  AccountEquity = 10.000 euro and stopInPips = 50 and risk = 1%, this code give my a size of 2.1 minilots <--- INCORRECT because i get stop of 89 euro, but i want a stop of 100 euro

  1. No you don't. If you trade pair X/Y and the account currency is Y, then you get exactly 10 Y per pip per lot. Otherwise exchange rates are involved. Your 89 euro is 100 USD, and your risk is correct . If you used 100 euro you would be risking 30% more!
  2. Go back to my post #1 and do it right.
 
whroeder1:
  1. No you don't. If you trade pair X/Y and the account currency is Y, then you get exactly 10 Y per pip per lot. Otherwise exchange rates are involved. Your 89 euro is 100 USD, and your risk is correct . If you used 100 euro you would be risking 30% more!
  2. Go back to my post #1 and do it right.

whroeder1 the account is 10.000 euro so 89 euro is not the 2 $ of the amount

i dont understand how to change my code