You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
Yes, - I guess so! Thank you, TheXpert !
I'll give it a try!
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.
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!