Download MetaTrader 5

On randomly large values of required margin.

To add comments, please log in or register
Create interesting Forum topics concerning the automated trading!
Gustavo Pinho
16
Gustavo Pinho 2013.07.09 16:28 

Greetings,

 I was trying to program an EA that moves 22% of my  free margin for every trade it opens. Only one position is open at a time and the only positions opened are those from the EA.

The code I used for finding out the size of the trade was this one:

int AdjustLot(){
   double sizePosition =AccountFreeMargin()*0.22; //slice of free margin used in every new trade
   double step = MarketInfo(Symbol(),MODE_LOTSTEP); //smaller increment possible in a position size
   double lotSize =MarketInfo(Symbol(),MODE_MARGINREQUIRED); //how much do i need for each lot
   double lots = MathFloor(sizePosition/(lotSize*step))*step; //number of lots
   Print("Adjusting trade size for ",lots," lots!");
   return(lots);
 }

 And the this function gets called right before I open a new poisiton. For example:

//A old position is close right before here
while(true){//keep trying to send the new order until accepted!
    lots=AdjustLots();
    RefreshRates();
    if(OrderSend(Symbol(),OP_BUY,lots,Ask,3,Ask-1000*Point,Ask+1000*Point,"sending order",magicN,0,Green)>=0){
        break; //Stop trying when the order is accepted!
    }else{
        erro=GetLastError();
        Print("Error ",erro,".");
    }
}//end of while

 The EA only closes a short position if it will open a long one and vice-versa.

 The problem is: Most of the times the locked margin is  just what i expected (22%). But there are times that the locked margin is almost the double of what I expected. Any ideas of why it happens? My bet is there is something wrong with the timing of the communication with the server, but I don't know what it is.

Alain Verleyen
Moderator
30752
Alain Verleyen 2013.07.09 16:38  
GustavoPinho:

Greetings,

 I was trying to program an EA that moves 22% of my  free margin for every trade it opens. Only one position is open at a time and the only positions opened are those from the EA.

The code I used for finding out the size of the trade was this one:

 And the this function gets called right before I open a new poisiton. For example:

 The problem is: Most of the times the locked margin is  just what i expected (22%). But there are times that the locked margin is almost the double of what I expected. Any ideas of why it happens? My bet is there is something wrong with the timing of the communication with the server, but I don't know what it is.

This forum is about MT5, please post your question about MT4/mql4 on mql4.com forum. Thanks.
MQL4: automated trading forum
  • www.mql5.com
MQL4: automated trading forum
Gustavo Pinho
16
Gustavo Pinho 2013.07.09 22:01  
Oh, sorry. I'll fix that
To add comments, please log in or register