Question for connoisseurs - page 13

 

Yes, - I guess so! Thank you, TheXpert !

I'll give it a try!

 
Dear experts, please help me to understand OrderCloseBy() function. I am totally confused. I have an Expert Advisor, which opens and closes positions by reversed conditions. That is, the conditions OPEN BUY and CLOSE SELL are the same, as well as OPEN SELL and CLOSE BUY, which means that right after exiting one position at the same price, a position opens in the opposite direction with the same size. So I decided to try and replace OrderClose() by opening a position in the opposite direction with double volume and then closing opposite orders with OrderCloseBy(). That is, the same algorithm should have been applied but it would have saved one spread per trade, but the tester has shown quite a different result. I cannot decide whether I have written the code incorrectly or have misunderstood the idea... I would be very grateful to you for clarification...
 
First of all, check with your brokerage company's technical support if this function is supported by the terminal. Not all brokerage houses allow the OrderCloseBy() function.
 
On the demo OrderCloseBy() is supported.
 

I'm not an expert. I have this note on this function in my "help" folder, in addition to everything "normal" (copied it here too - I don't remember which branch) -

And the OrderCloseBy function is used ONLY to close 2 orders simultaneously, at that one spread is saved.
We should walk through the terminal programmatically, memorize all open order numbers and their characteristics, select the main order number, match it with the desired (from available) counter order and paste that number.


// That is, as I understand it, with this function you can only close two counter positions, pre-selected.

That's all there is to it.

 

Yeah, well...

That's why it's called that.

 

Let me rephrase the question as to whether there is any economic difference in the following two possibilities of position reversal:

1) Close SELL with 1 lot of volume using OrderClose function and then open BUY with 1 lot of volume

2) While SELL is open in 1 lot, first open BUY in 2 lots and then close SELL with OrderCloseBy function, which will also leave an open BUY position in 1 lot.

According to the example in the workbook, one spread will remain in the 2) variant.

 
In short, the 1+1 spread will be compensated and the remaining 1 will be the normal trading spread.
 
https://book.mql4.com/ru/trading/orderclose - here is a description of the OrderCloseBy() function. Tried to do the same on the demo - it really does reduce the spread by a lot. But it still does not work on Expert Advisor... I am so confused...
 
Dmirtiy писал(а) >>
https://book.mql4.com/ru/trading/orderclose - here you can see how the OrderCloseBy() function works. I tried to do the same on demo - it really has smaller spread. But it still does not work on Expert Advisor... I am really confused...

You've been in two threads with this question. You're inquisitive.

There are no simple solutions. With the release of MQL5 this problem will completely disappear.

I can suggest a couple of procedures that I use in my EAs.

//Detect the current state of open orders:

void CheckOrders()
{
int i;
BuyOrder = false;
SellOrder = false;
BuyLots = 0.0;
SellLots = 0.0;

for ( i=0; i < OrdersTotal(); i++)
{
OrderSelect(i, SELECT_BY_POS);
if ( OrderSymbol() == Symbol())
{
if ( OrderType() == OP_BUY )
{
BuyOrder = true;
LastBuyLots = OrderLots();
BuyLots += LastBuyLots; // sum of open Buy positions
BuyTicket = OrderTicket();
}
if ( OrderType() == OP_SELL)
{
SellOrder = true;
LastSellLots = OrderLots();
SellLots += LastSellLots; // sum of open Sell positions
SellTicket = OrderTicket();
}
}
}
}

//Open (close) orders:

void TradeVariant( int variant )
{
switch( variant )
{
// if (Pattern < 0 && !BuyOrder && !SellOrder && !FridayLastHour) variant = 1;
// signal to sell, no order is open and it is not the last hour of Friday
// open a sell order
case 1:
OpenSellOrder(Lots);
break;

// if (Pattern < 0 && BuyOrder && !SellOrder && !FridayLastHour) variant = 2;
// signal to sell, there is an open Buy order, no Sell order
// it is not the last hour of Friday - we are turning over
case 2:
OpenSellOrder(Lots + BuyLots);
break;

// if (Pattern < 0 && BuyOrder && !SellOrder && FridayLastHour) variant = 3;
// signal to sell, there is an open Buy order, no Sell order
// it is the last hour of Friday - close the Buy order
case 3:
CloseBuyOrders();
break;

// if (Pattern > 0 && !SellOrder && !BuyOrder && !FridayLastHour) variant = 4;
// signal to buy, no order is open and it is not the last hour of Friday
// open Buy order
case 4:
OpenBuyOrder(Lots);
break;

// if (Pattern > 0 && SellOrder && !BuyOrder && !FridayLastHour) variant = 5;
// signal to buy, there is an open Sell order, no Buy order
// it is not the last hour of Friday - we are turning over
case 5:
OpenBuyOrder(Lots + SellLots);
break;

// if (Pattern > 0 && SellOrder && !BuyOrder && FridayLastHour) variant = 6;
// signal to buy, there is an open Sell order, no Buy order
// it is the last hour of Friday - close the Sell order
case 6:
CloseSellOrders();
break;

case 7:
// if (SellOrder && BuyOrder) variant = 7;
// have an open cross order
if (WaitBeforeTransaction(WaitSeconds) == 1) // pause between requests
{
if (LastBuyLots <= LastSellLots)
OrderCloseBy( BuyTicket, SellTicket);
else
OrderCloseBy( SellTicket, BuyTicket);
}
break

// in other variants do not do anything
default:
break;
}
}

I hope you will manage with the variables.

>> Good luck!