Problem creating Autolot configuration

 

Hello friends,

 

I am developing a strategy, where i want to use an auto lot configuration, but not based on Stop loss or take profit, i want that the code uses a lot size that is a % of the total balance, for example:

 

 If i have10.000 USD balance in the account and i setup the percentage of balance to be used to 40%, that the EA calculates automatic a lot size configuration for 4.000 USD (40% of 10.000 balance) between the maximum orders and the minimum lot size allowed for this account configurated, so in this case if i have setup to open a maximum of 4 orders for EUR/USD and my minimum lot size is of 0.01 lots and if the lot price is 1600 USD, that the program uses the math:

 10.000 (balance) x 0.4 (% to use) / 4 (maximum orders) / (1600/100) (min lot price)  x 0.01 (minimum lot) = 0.625 with only 2 digits = 0.63 

 This way the EA would open a maximum of 4 orders of 0.63 lots for this pair at this moment, allways leaving a 60% of free margin 

 

Is it possible somebody can help me with this, or knows how would this to be a better way of configurating it?

Would this formula be correct?

Is it possible that the code calculates itself the lot price ?

How would it change if i'm using this on sp500 where minimum lot size is 1 lot = 25 USD?

Can somebody send me this code?

 

thanks in advance,

 

Daniel 

 
daniel1983:

Hello friends,

 

I am developing a strategy, where i want to use an auto lot configuration, but not based on Stop loss or take profit, i want that the code uses a lot size that is a % of the total balance, for example:

 

 If i have10.000 USD balance in the account and i setup the percentage of balance to be used to 40%, that the EA calculates automatic a lot size configuration for 4.000 USD (40% of 10.000 balance) between the maximum orders and the minimum lot size allowed for this account configurated, so in this case if i have setup to open a maximum of 4 orders for EUR/USD and my minimum lot size is of 0.01 lots and if the lot price is 1600 USD, that the program uses the math:

 10.000 (balance) x 0.4 (% to use) / 4 (maximum orders) / (1600/100) (min lot price)  x 0.01 (minimum lot) = 0.625 with only 2 digits = 0.63 

 This way the EA would open a maximum of 4 orders of 0.63 lots for this pair at this moment, allways leaving a 60% of free margin 

 

Is it possible somebody can help me with this, or knows how would this to be a better way of configurating it?

Would this formula be correct?

Is it possible that the code calculates itself the lot price ?

How would it change if i'm using this on sp500 where minimum lot size is 1 lot = 25 USD?

Can somebody send me this code?

 

thanks in advance,

 

Daniel 

Hello. If you want to have the risk on each trade was the same,  then you should consider the distance between the open price of the order and its stop. In General, the risk shall determine the size of the position. And in yours  formula the position  determines the risk. This is not correct.

I use this formula. Here the risk is constant and does not depend on the distance between the open and stop:

balance: this is the account balance.
dis: the distance between the price of position opening and stoploss in ticks.
pt: it is price of the tick  in USD for one lot.
risk: this risk positions or transactions in percent.
lotstep: it is the minimum step size changes position. In this case it is equal to 0.01.
size: this is  size of the position.

Formula where the risk is constant:

x = 100%*dis*pt*lotstep

y = risk*balance/x

y: this is the quantity of the lotstep and y rounded to an integer.
 
size = lotstep*y
 
this script calculates the size position and shows the risks.
//+------------------------------------------------------------------+
//|                                                         SIZE.mq4 |
//|                        Copyright 2014, MetaQuotes Software Corp. |
//|                                              https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2014, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
//#property strict
#property show_inputs
extern double Risk       = 5.0;           // The risk of the position as a percentage of the balance. 
extern color Clr_Line    = DarkGray;      // Color line. 
extern color Clr_Text    = DarkGray;      // Color text.
extern int Shift_X_axis  = 1087;          // The offset of the text horizontally in pixels.
extern int Shift_Y_axis  = 10;            // The offset of the text vertically in pixels.
extern int Density_lines = 20;            // The density of lines.
extern int Distance=150;                  // Adjusting the distance between characters.
string symb;
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//---
   string symbol=Symbol();
   double lotstep=MarketInfo(symbol,MODE_LOTSTEP);
   int digit=(int)MarketInfo(symbol,MODE_DIGITS);
   double tick=MarketInfo(symbol,MODE_TICKSIZE);
   double pt=MarketInfo(symbol,MODE_TICKVALUE);
   double ask=MarketInfo(symbol,MODE_ASK);
   double ar=0.25*(High[1]-Low[1]+High[2]-Low[2]+High[3]-Low[3]+High[4]-Low[4]);
   ObjectCreate("LINE",OBJ_TREND,0,2*Time[0]-Time[4],ask-ar,Time[0],ask);
   ObjectSet("LINE",OBJPROP_COLOR,Clr_Line);
   ObjectSet("LINE",OBJPROP_RAY,0);
   ObjectSet("LINE",OBJPROP_BACK,1);
   string text;
   string name[]={"SIZE OF POSITION","RISK FROM BALANCE","RISK IN CURRENCY","PRICE_1","PRICE_2","SIZE","PERCENTAGE","CURRENCY","UP","DOWN","LINE"};
   int i,y;
   int e=0;
   int d=0;
   double z[13],dis,x,size;

   for(i=0; i<10; i++)
     {
      if(i==5)
        {
         e=0;
         d=Distance;
        }
      ObjectCreate(name[i],OBJ_LABEL,0,0,0,0,0,0,0);
      ObjectSet(name[i],OBJPROP_XDISTANCE,Shift_X_axis+d);
      ObjectSet(name[i],OBJPROP_YDISTANCE,5+e*Density_lines);
      e++;
     }
//----

   z[8]=ObjectGet("LINE",OBJPROP_PRICE1);
   z[9]=ObjectGet("LINE",OBJPROP_PRICE2);
//----
   if(tick>0)dis=MathAbs(z[8]-z[9])/tick;
   x=100*dis*pt*lotstep;
   if(x>0)y=MathFloor(Risk*AccountBalance()/x);
   size=lotstep*y;
   z[5]=size;
   z[6]=Risk;
   if(Risk==0) size=lotstep;
   z[7]=dis*pt*size;
//----
   for(i=0; i<10; i++)
     {
      text=name[i];
      if(i>4 && i<8) text=DoubleToStr(z[i],2);
      if(i>7) text=DoubleToStr(z[i],digit);
      ObjectSetText(name[i],text,10,"Arial",Clr_Text);
     }

  }
//+------------------------------------------------------------------+
Reason: