Loop from https://docs.mql4.com/trading/OrdersTotal - 1 to 0 increment -1
https://docs.mql4.com/trading/OrderSelect then check the order's https://docs.mql4.com/trading/OrderMagicNumber and https://docs.mql4.com/trading/OrderSymbol for a match to ensure it is from the correct EA, if it is store it's https://docs.mql4.com/trading/OrderOpenPrice and it's https://docs.mql4.com/trading/OrderType and https://docs.mql4.com/trading/OrderTicket
Then a nested loop similar to the above, and look for an order with matching OrderOpenPrice but not matching Type, if found closed both orders and exit, if not found check the next Order
You will have to check that you are not comparing the Order from the outside loop with itself in the inside loop. . . hence the reason for keeping track of it's Ticket no.
Loop from https://docs.mql4.com/trading/OrdersTotal - 1 to 0 increment -1
https://docs.mql4.com/trading/OrderSelect then check the order's https://docs.mql4.com/trading/OrderMagicNumber and https://docs.mql4.com/trading/OrderSymbol for a match to ensure it is from the correct EA, if it is store it's https://docs.mql4.com/trading/OrderOpenPrice and it's https://docs.mql4.com/trading/OrderType and https://docs.mql4.com/trading/OrderTicket
Then a nested loop similar to the above, and look for an order with matching OrderOpenPrice but not matching Type, if found closed both orders and exit, if not found check the next Order
You will have to check that you are not comparing the Order from the outside loop with itself in the inside loop. . . hence the reason for keeping track of it's Ticket no.
Thank you for your quick reply.
I have coded this but its not closing the orders.. can you please tell me where I went wrong?
void CloseSamePrice()
{
bool result;
nt _total=OrdersTotal(); // number of lots or trades ????
int _ordertype;// order type
if (_total==0) {return;} // if total==0
int _ticket; // ticket number
double _orderprice;
double _priceClose;// price to close orders;
for(int _i=_total-1;_i>=0;_i--)
{ //# for loop
if (OrderSelect(_i,SELECT_BY_POS))
{ //# if
_ticket=OrderTicket();
_orderprice=OrderOpenPrice();
if(OrderType() == OP_BUY)
{
int _total1=OrdersTotal(); // number of lots or trades ????
int _ticket1; // ticket number
double _priceClose1;// price to close orders;
for(int _ii=_total1-1;_ii>=0;_ii--)
{ //# for loop
if (OrderSelect(_i,SELECT_BY_POS))
{ //# if
_ticket1=OrderTicket();
if(OrderType()== OP_SELL && OrderOpenPrice()==_orderprice)
{
_priceClose=MarketInfo(OrderSymbol(),MODE_BID);
_priceClose1=MarketInfo(OrderSymbol(),MODE_ASK);
OrderClose(_ticket,OrderLots(),_priceClose,10,Red);
OrderClose(_ticket1,OrderLots(),_priceClose1,10,Red);
}
}
}
}
} // # if
}
return;
}
Please use this to post code . . . it makes it easier to read.
Please use this to post code . . . it makes it easier to read.
void CloseSamePrice() { bool result; int _total=OrdersTotal(); // number of lots or trades ???? int _ordertype;// order type if (_total==0) {return;} // if total==0 int _ticket; // ticket number double _orderprice; double _priceClose;// price to close orders; for(int _i=_total-1;_i>=0;_i--) { //# for loop if (OrderSelect(_i,SELECT_BY_POS)) { //# if _ticket=OrderTicket(); _orderprice=OrderOpenPrice(); if(OrderType() == OP_BUY) { int _total1=OrdersTotal(); // number of lots or trades ???? int _ticket1; // ticket number double _priceClose1;// price to close orders; for(int _ii=_total1-1;_ii>=0;_ii--) { //# for loop if (OrderSelect(_i,SELECT_BY_POS)) { //# if _ticket1=OrderTicket(); if(OrderType()== OP_SELL && OrderOpenPrice()==_orderprice) { _priceClose=MarketInfo(OrderSymbol(),MODE_BID); _priceClose1=MarketInfo(OrderSymbol(),MODE_ASK); OrderClose(_ticket,OrderLots(),_priceClose,10,Red); OrderClose(_ticket1,OrderLots(),_priceClose1,10,Red); } } } } } // # if } return; }
If the first order you come across is a Sell and the 2nd a Buy with the same OOP your code fails . . . get rid of this . . .
if(OrderType() == OP_BUY)
do this . . . .
if(_ticket1 == _ticket) continue;
If the first order you come across is a Sell and the 2nd a Buy with the same OOP your code fails . . . get rid of this . . .
do this . . . .
Thanks for your help. I have not done yet. I will try after some time.
for(int _ii=_total1-1;_ii>=0;_ii--) { //# for loop if (OrderSelect(_i,SELECT_BY_POS))
A) Selecting the wrong orders B) No check for duplicate ticket C) Don't need to indent twice.for(int _ii=_total1-1;_ii>=0;_ii--) if( OrderSelect(_ii,SELECT_BY_POS) // Only my orders w/ && OrderMagicNumber() == magic.number // my magic number && OrderSymbol() == Symbol() // and my pair. && OrderType() == OP_SELL && OrderOpenPrice() == _orderprice ){
_priceClose=MarketInfo(OrderSymbol(),MODE_BID);
Why use a function call when you could just use Bid- Since server calls take time, you must RefreshRates between them
_priceClose=MarketInfo(OrderSymbol(),MODE_BID); OrderClose(_ticket,OrderLots(),_priceClose,10,Red); _priceClose1=MarketInfo(OrderSymbol(),MODE_ASK); // or RefreshRates(); _priceClose1 = Ask; OrderClose(_ticket1,OrderLots(),_priceClose1,10,Red);
int _total=OrdersTotal();
This makes the EA incompatible with every other including itself on other charts and manual trading. ALWAYS use magic numbers.int _total=0; for(iPos = OrdersTotal()-1; iPos >= 0 ; iPos--) if ( OrderSelect(iPos, SELECT_BY_POS) // Only my orders w/ && OrderMagicNumber() == magic.number // my magic number && OrderSymbol() == Symbol() // and my pair. ){ _total++; }
-
A) Selecting the wrong orders B) No check for duplicate ticket C) Don't need to indent twice.
-
Why use a function call when you could just use Bid
- Since server calls take time, you must RefreshRates between them
-
This makes the EA incompatible with every other including itself on other charts and manual trading. ALWAYS use magic numbers.
Wow.. Thank you so much. I have incorporated everything and its working fine.. :) here is the code if anyone require in future.
void CloseSamePrice() { bool result; int _total=0; int _ordertype;// order type int _ticket; // ticket number double _orderprice; for(int iPos = OrdersTotal()-1; iPos >=0; iPos--) if ( OrderSelect(iPos, SELECT_BY_POS) // Only my orders w/ && OrderMagicNumber() == MAGICMA // my magic number && OrderSymbol() == Symbol() // and my pair. ) { _total++; } for(int _i=_total-1;_i>=0;_i--) if( OrderSelect(_i,SELECT_BY_POS) // Only my orders w/ && OrderMagicNumber() == MAGICMA // my magic number && OrderSymbol() == Symbol() // and my pair. && OrderType() == OP_BUY ) { _ticket=OrderTicket(); _orderprice=OrderOpenPrice(); int _total1 =0; for(int iPos1 = OrdersTotal()-1; iPos1 >=0; iPos1--) if ( OrderSelect(iPos1, SELECT_BY_POS) // Only my orders w/ && OrderMagicNumber() == MAGICMA // my magic number && OrderSymbol() == Symbol() // and my pair. ) { _total1++; } int _ticket1; // ticket number for(int _ii=_total1-1;_ii>=0;_ii--) if( OrderSelect(_ii,SELECT_BY_POS) // Only my orders w/ && OrderMagicNumber() == MAGICMA // my magic number && OrderSymbol() == Symbol() // and my pair. && OrderType() == OP_SELL && OrderOpenPrice() == _orderprice ) { _ticket1=OrderTicket(); // Comment("ticket ", _ticket, "\n", // "ticket1 ", _ticket1); OrderClose(_ticket,OrderLots(),Bid,10,Red); RefreshRates(); OrderClose(_ticket1,OrderLots(),Ask,10,Red); } } return; }
int _ticket1; // ticket number for(... { _ticket1=OrderTicket();Having to declare variables before use went out of style after C language. Declare them when first used.
for(... { int _ticket1=OrderTicket();
Доброго времени суток уважаемые форумчане!
Меня зовут Герман, мне 23 года, я являюсь трейдером компании "Инстафорекс"
Помогите в поиске нужного скрипта! Скрипт нужен для сетки отложенных ордеров.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hi
I am new to mql and I am trying to develop a trading system in which sometimes I will have both buy & sell orders at the same price. I wanted to close both buy & sell orders if they are at the same price.
for example: my system will have buy order at 1.4160 also a sell order at 1.4160 (close both)
I would highly appreciate if some one guide me how do I achieve this.
Thanks
Richo