problems with pending orders

 

Hello,

i've encountered a very strage situation while investigating the behavior of pending orders ( slippage, respond time, etc... ).

Sometimes, there is a pending order (with a valid ticket) which cannot be deleted by OrderDelete() nor closed by OrderClosed()

It seems like the order become a Market Order on the server but is stiil a pending order on client terminal...

some details:

i wrote an endless loop which lookes like:

{

// open a pending order at the current price + stoplevel (different symbol on each iteration)

// wait ( up to 10 sec ) while the order is pending ( meaning while OrderType() didnt changed to OP_BUY nor OP_SELL )

// if the order become a Market Order, Sleep 30 seconds and close it

// else - delete the pending order

// log results to file

}

The problem: Sometimes, there is a pending order (with a valid ticket) which cannot be deleted by OrderDelete() nor closed by OrderClosed()

OrderDelete(), returns with error code 3 - ERR_INVALID_TRADE_PARAMETERS

OrderClose(), returns with error code 4108 - ERR_INVALID_TICKET

Only after RE-LOGIN, i can see that the order become a Market Order which only now (after the RE-LOGIN ) can finally be closed.

Did anyone encountered this problem ?

Any ideas ? sulotions ?

Thanks

 
lekoby:

Hello,

i've encountered a very strage situation while investigating the behavior of pending orders ( slippage, respond time, etc... ).

Sometimes, there is a pending order (with a valid ticket) which cannot be deleted by OrderDelete() nor closed by OrderClosed()

It seems like the order become a Market Order on the server but is stiil a pending order on client terminal...

some details:

i wrote an endless loop which lookes like:


I suspect you are seeing this strange behaviour because of your endless loop . . . there have been various reports about incorrect Order information when the order info is queried immediately after placing a trade. It's almost as if a tick is needed to update the locally stored Order history.

If you could provide test code that reproduces this issue then it will be easier for other users and the Service Desk to investigate . . .
 

Hi, Thanks

Here is my test code:

the endless loop can be exited by setting -1 to TERMINAL GLOBAL VARIABLE "gv_Script_Flag"

#include <stderror.mqh>
#include <stdlib.mqh>

//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//---
double script_start_time = GetTickCount();

string Symbol_Name[6];
Symbol_Name[0] = "EURUSD";
Symbol_Name[1] = "EURJPY";
Symbol_Name[2] = "EURCAD";
Symbol_Name[3] = "USDCHF";
Symbol_Name[4] = "USDCAD";
Symbol_Name[5] = "GBPCHF";

int Num_Of_Symb_On_Market = 6;

GlobalVariableSet( "gv_Script_Flag", 1 );

for ( int symb_id = 0; GlobalVariableGet( "gv_Script_Flag" ) > 0 ; symb_id++ )
{
if ( symb_id == Num_Of_Symb_On_Market )
symb_id = 0;

int op_type;
for ( op_type = OP_BUYLIMIT ; op_type <= OP_SELLSTOP ; op_type++ )
{
double open_time;
open_time = GetTickCount();

double Ask_On_Open, Bid_On_Open, point, minimum_distance, digits;
RefreshRates();
string cur_symb = Symbol_Name[symb_id];
Ask_On_Open = MarketInfo( cur_symb, MODE_ASK );
Bid_On_Open = MarketInfo( cur_symb, MODE_BID );
point = MarketInfo( cur_symb, MODE_POINT );
minimum_distance = MarketInfo( cur_symb, MODE_STOPLEVEL );
digits = MarketInfo( cur_symb, MODE_DIGITS );

Ask_On_Open = NormalizeDouble( Ask_On_Open, (int)digits );
Bid_On_Open = NormalizeDouble( Bid_On_Open, (int)digits );

double requested_open_price;
if ( op_type == OP_BUYLIMIT )
requested_open_price = Ask_On_Open - minimum_distance * point;

else if ( op_type == OP_BUYSTOP )
requested_open_price = Ask_On_Open + minimum_distance * point;

else if ( op_type == OP_SELLLIMIT )
requested_open_price = Bid_On_Open + minimum_distance * point;

else
requested_open_price = Bid_On_Open - minimum_distance * point;

requested_open_price = NormalizeDouble( requested_open_price, (int)digits );

int open_err_code;
int ticket;

open_err_code = GetLastError(); // Clear Last Error
ticket = OrderSend( cur_symb, op_type, 0.1, requested_open_price, 0, 0, 0 );
open_err_code = GetLastError();

double applyed_open_price;
double open_slippage;

applyed_open_price = 0;
open_slippage = 0;
if ( open_err_code != ERR_NO_ERROR )
{
Comment( StringConcatenate( "OrderSend Failed. OP Type: ", op_type, " on ", cur_symb, ", Err code: ", open_err_code ) );
}
else
{
double pend_time = 0;
OrderSelect( ticket, SELECT_BY_TICKET );

while ( ( OrderType() == op_type ) && ( pend_time < 10 ) )
{
pend_time = ( GetTickCount() - open_time ) / 1000;

Comment( StringConcatenate( "OrderSend Succeeded. Pending for ", MathRound( pend_time * 10 ) / 10, " sec, \n",
"OP Type: ", op_type, " on ", cur_symb, "\n",
" Ask On Open: ", Ask_On_Open, "\n",
" Reque Price: ", requested_open_price, "\n",
" Bid On Open: ", Bid_On_Open, "\n",
" Min Distace: ", minimum_distance, "\n",
" Point: ", point, "\n",
" Ask: ", MarketInfo( cur_symb, MODE_ASK ), "\n",
" Bid: ", MarketInfo( cur_symb, MODE_ASK ) ) );

Sleep(100);
RefreshRates();
OrderSelect( ticket, SELECT_BY_TICKET );
}

while ( true )
{
if ( GlobalVariableGet( "gv_Script_Flag" ) <= 0 )
{
Comment( "Bye Bye" );
return(INIT_SUCCEEDED);
}

pend_time = ( GetTickCount() - open_time ) / 1000;
OrderSelect( ticket, SELECT_BY_TICKET );
if ( OrderType() != op_type )
{
applyed_open_price = OrderOpenPrice();
open_slippage = ( applyed_open_price - requested_open_price ) / point;
Comment( StringConcatenate( "Pending Order was opened after: ", MathRound( pend_time * 10 ) / 10, " sec, Open Slippage: ", open_slippage ));
break;
}

Comment( StringConcatenate( "Deleting Pending Order..." ) );

GetLastError(); // Clear Last Error
if ( OrderDelete( ticket ) == true )
{
Comment( StringConcatenate( "Pending order wad deleted successfully" ) );
break;
}
else
{
Alert( StringConcatenate( MathRound( pend_time * 10 ) / 10, " - Delete pending order failed. Ticket: ", ticket, ", Err: ", GetLastError(), ", Symbol: ", cur_symb ) );

RefreshRates();
double close_price;
if ( ( op_type == OP_BUYLIMIT ) || ( op_type == OP_BUYSTOP ) )
close_price = MarketInfo( cur_symb, MODE_BID );
else
close_price = MarketInfo( cur_symb, MODE_ASK );

close_price = NormalizeDouble( close_price, (int)digits );
if ( OrderClose( ticket, 0.1, close_price, 0 ) == true )
break;

Alert( StringConcatenate( MathRound( pend_time * 10 ) / 10, " - Closing pending order failed. Ticket: ", ticket, ", Err: ", GetLastError(), ", Symbol: ", cur_symb ) );
}

Sleep( 1000 );
}
}

Sleep( 5*1000 );

double requested_close_price = 0;
double applyed_close_price = 0;
double close_slippage = 0;

OrderSelect( ticket, SELECT_BY_TICKET );
if ( ( open_err_code == ERR_NO_ERROR ) && ( OrderType() != op_type ) )
{
if ( OrderType() == OP_BUY )
requested_close_price = MarketInfo( cur_symb, MODE_BID );
else
requested_close_price = MarketInfo( cur_symb, MODE_ASK );

requested_close_price = NormalizeDouble( requested_close_price, (int)digits );

Close_Order( ticket, cur_symb, OrderType() );

OrderSelect( ticket, SELECT_BY_TICKET );

applyed_close_price = OrderClosePrice();
close_slippage = ( applyed_close_price - requested_close_price ) / point;

Comment( StringConcatenate( "Closing is done, Close Slippage: ", close_slippage ));
}

Sleep( 5*1000 );
}
}
Comment( "Bye Bye" );

//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+


void Close_Order( int tkt, string symb, int op )
{

Comment( StringConcatenate( "Closing market order. ticket: ", tkt, ", Symbol: ", symb ) );

double digits = MarketInfo( symb, MODE_DIGITS );

double close_price;
do
{
if ( GlobalVariableGet( "gv_Script_Flag" ) <= 0 )
return;

Sleep( 100 );
RefreshRates();

if ( op == OP_BUY )
close_price = MarketInfo( symb, MODE_BID );
else
close_price = MarketInfo( symb, MODE_ASK );

close_price = NormalizeDouble( close_price, (int)digits );

}
while ( OrderClose( tkt, 0.1, close_price, 0 ) == false );
}
 
lekoby: Here is my test code:

Play video
Please edit your post.
For large amounts of code, attach it.
 
lekoby:

Hi, Thanks

Here is my test code:

the endless loop can be exited by setting -1 to TERMINAL GLOBAL VARIABLE "gv_Script_Flag"


Hello,

Please use the SRC button when you post code. Thank you.


This time, I edited it for you.

Reason: