Increase position using Netting MT5 account

 

Hi there!


My EA needs to work with the increase position lots approach.


I'm quite aware that in a netting account, which is my case, I need to close the opened position first and, only after that, I can send an order in the same direction with higher volume.


During the operation of my EA I got my increased order refused, even when I put a Sleep() of 20000 ms (or 20 seconds) command between the closing position and increasing position.


The message attached in this post had been sent.


My question is: is there a clever wait to guarantee that the close order position has been done and just after that send another order with high volume?


Thanks in advance.

Files:
 

Hi, Enrique.


Thanks for replying.


Could you clarify a bit more how the OnTradeTransaction funciton would help closing a position and then opening a position in the same direction with increased volume?

 
GustavoFonseca92:

Hi there!


My EA needs to work with the increase position lots approach.


I'm quite aware that in a netting account, which is my case, I need to close the opened position first and, only after that, I can send an order in the same direction with higher volume.


During the operation of my EA I got my increased order refused, even when I put a Sleep() of 20000 ms (or 20 seconds) command between the closing position and increasing position.


The message attached in this post had been sent.


My question is: is there a clever wait to guarantee that the close order position has been done and just after that send another order with high volume?


Thanks in advance.

It doesn't make sense to close if what you want is to increase (add to) a position.

Please show your code if you need coding help.

 
Alain Verleyen:

It doesn't make sense to close if what you want is to increase (add to) a position.

Please show your code if you need coding help.

Hi, Alain.


As long as my account is the netting type I cannot increase a position, I'm afraid. I guess it's only possible to take partial exits or to close the whole position.

I've already tried adding lots to my current position (e.g.: if I've bought 1 lot, try to buy + 15 lots) and I got my order rejected.


Then, my next approach was closing my current position and, after that, create another position with higher volume.


Here`s the piece of my code:

// For a short position:

   SellStock(myStock.Bid(),positionCurrent,true); // selling (closing) the current short position

            Sleep(20000); // wait for 20 seconds

            BuyStockIncrease(price,positionLotsIncreased); // buy with higher volume


// Functions:

void SellStock(double price, double volume, bool flagPartial)

{   

   request.action = TRADE_ACTION_DEAL;

   request.type = ORDER_TYPE_SELL;

   request.symbol = _Symbol;

   request.volume = volume;

   request.type_filling = ORDER_FILLING_FOK;

   request.price = price;

   request.sl = 0.0;

   request.tp = 0.0;

   request.deviation = 50;

   TimeCurrent(timePosition);   

   OrderSend(request,result);

   if(result.retcode == TRADE_RETCODE_DONE || result.retcode == TRADE_RETCODE_PLACED)

     {

         Print("Trade placed");

     }

   else

     {

       Print("Trade not placed. Error code: ", result.retcode);

     }

}


void BuyStockIncrease(double price, double volume)

{  

   request.action = TRADE_ACTION_DEAL;

   request.type = ORDER_TYPE_BUY;

   request.symbol = _Symbol;

   request.volume = volume;

   request.type_filling = ORDER_FILLING_FOK;

   request.price = price;

   stopLossPrice = positionPrice;

   request.sl = stopLossPrice;

   request.tp = 0.0;

   request.deviation = 50;

   TimeCurrent(timePosition);

   

   OrderSend(request,result);

   if(result.retcode == TRADE_RETCODE_DONE || result.retcode == TRADE_RETCODE_PLACED)

     {

         Print("Trade placed");

     }

   else

     {

       Print("Trade not placed. Error code: ", result.retcode);

     }  

}


Tks!

 
GustavoFonseca92:

My question is: is there a clever wait to guarantee that the close order position has been done and just after that send another order with high volume?

#include <MT4Orders.mqh> // https://www.mql5.com/en/code/16006

input TICKET_TYPE PositionTicket = 0;
input double ReverseLots = 20;
input int Deviation = 100;

void OnStart()
{
  if (OrderSelect(PositionTicket, SELECT_BY_TICKET) && (OrderType() <= OP_SELL))
    if (OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), Deviation)) // Close with waiting.    
      OrderSend(OrderSymbol(), 1 - OrderType(), ReverseLots,
                SymbolInfoDouble(OrderSymbol(), OrderType() ? SYMBOL_ASK : SYMBOL_BID), Deviation, 0, 0);
}
 
fxsaber:
Thanks, fxsaber. I really appreciate your help!