Closing Ticket errors

 

I am having a problem where I attempt to close all open orders. Sometimes it will error out saying that it was an invalid price and when I look at the price shown there are like 4 or 5 extra zeio's at the end of the price value. The errors are as follows:

2009.07.09 23:51:37 SimpleGrail6 USDCHFgfx,M1: invalid ticket for OrderClose function


2009.07.09 23:51:26 SimpleGrail6 EURUSDgfx,M1: unknown ticket 3377609 for OrderClose function

Code:

extern int        CloseAllAt     = 200;        // The max profit value for sum of all opern trades


  if (AccountProfit() >= CloseAllAt)
   {
      Print("Closing all open trades, profits are at: ", AccountProfit()); 
      for (int n = 0; n <= OrdersTotal(); n++)
         {
            // looping through all the trades to close them
            OrderSelect(n, SELECT_BY_POS, MODE_TRADES);
            
            // Refresh after each one is processed
            RefreshRates();
            
            // Set quick bid or ask value
            if (OrderType() == OP_BUY)
            {
               tempPrice = Bid;
            }
            else
            {
               tempPrice = Ask;
            }
            
            // Perform the close
            IsClosed = OrderClose(OrderTicket(), OrderLots(), tempPrice, gap, Orange);
            
            // If we failed to close, sleep then try again
            if (IsClosed == false)
               {
                  Sleep(10000);
                  // try again
                  IsClosed = OrderClose(OrderTicket(), OrderLots(), tempPrice, gap, Orange);
               }
               
            // Setting boolean for next order process
            IsClosed = false;
        }
   }         
 

First: revert the selection : for (n=OrdersTotal()-1;n>=0;n--)

Then: place evertything after OrderSelect within an If block: like this:

if (OrderSelect(n, SELECT_BY_POS, MODE_TRADES)==True) { rest of code goes here }

you have the OrderClose() errors cause OrderSelect() didn't select anything so gets invalid tickets from OrderTicket()

 

Hello LEHays

TheEconomist totally correct.

If I may, I'd just add that: Maybe this post helps with more supporting why's ' Select_By_Position'

hth

 
fbj:

Hello LEHays

TheEconomist totally correct.

If I may, I'd just add that: Maybe this post helps with more supporting why's ' Select_By_Position'

hth

Its amazing how often the old upward loop chestnut has been coming up in the last week.


CB

 
cloudbreaker wrote >>

Its amazing how often the old upward loop chestnut has been coming up in the last week.

CB

From what I understand, instead of the following:

Forward looping:

for (i = 0; i <= OrderTotal(); i++)

{

//Close my orders

}

I should use the following:

for (i = OrderTotal(); i >=0; i--)

{

// close my orders

}

The second for loop will actually start at the bottom of the order index and count up, which will leave all open orders with their previously existing index instead of them being bumped up to the earlier closed index.

Makes logic, thanks guys

 

Hello LEHayes

Please to alter below:

for (i = OrderTotal()-1; i >=0; i--)

1. OrdersTotal()

2. Also please to loop from total-1 .. 0

.

Maybe some more verbage? ;)

.

The Pool orders list is from 0..total-1, where total will change as list shrinks/expands... and is why must not 'grab' total and then within a loop construct do remove/add to list and expect total to still be valid [listentry] (*)

.

You want to go down from High-1 to Low because of what you have read at more supporting why's ' Select_By_Position'

.

"The second for loop will actually start at the bottom of the order index and count up"

no, will start at top and go down viz: i--. As above link says, if go up you be going up to total-1 but it not exist anymore(*) cuz list has been diminished by previous closes/deletes.

.

Effectively, EA is always presented with a sequentially numbered set of list entries without any empty gaps/items/entries. Not matter that close or delete say the middle entry. CT deals with list admin.

Is like maybe doubly linked list - remove any list item and the total list length shrinks by one and eg, the list head will still be [0] and list tail will be mapped to list item [OrdersTotal()-1]

okaaay, i waffle and maybe even confuse self - but u must go High-1 to Low regardless of how you decide to picture/think about the list organisation - which is not really our concern as long as the perceived warranty is that the list always remains a sequential ordering with no gaps in it and [0] is base and [total-1] is top.

.

Base is only constant, is unchanging, yes? Top on the other hand will always move about in sympathy to current list size.

.

Well, good fortunes... :)

 

you must use closeing in orderselect block so in your code must use

if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)

{

//close function

}

so in you code :

extern int CloseAllAt = 200; // The max profit value for sum of all opern trades


if (AccountProfit() >= CloseAllAt)
{
Print("Closing all open trades, profits are at: ", AccountProfit());
for (int n = 0; n <= OrdersTotal(); n++)
{
// looping through all the trades to close them
  if (OrderSelect(n, SELECT_BY_POS, MODE_TRADES)==true){//changed

// Refresh after each one is processed
RefreshRates();

// Set quick bid or ask value
if (OrderType() == OP_BUY)
{
tempPrice = Bid;
}
else
{
tempPrice = Ask;
}

// Perform the close
IsClosed = OrderClose(OrderTicket(), OrderLots(), tempPrice, gap, Orange);

// If we failed to close, sleep then try again
if (IsClosed == false)
{
Sleep(10000);
// try again
IsClosed = OrderClose(OrderTicket(), OrderLots(), tempPrice, gap, Orange);
}

// Setting boolean for next order process
IsClosed = false;
}

}

 

OK, I have set the new code and tested it. Now the errors are even more than before, almost constantly.

  // Checking the accounts profit level, if greater than value set by user, then we close all trades 
  if (AccountProfit() >= CloseAllAt)
   {
      Print("Closing all open trades, profits are at: ", AccountProfit()); 
      for (int n = OrdersTotal()-1; n >= 0; n--)
         {
            // looping through all the trades to close them
            OrderSelect(n, SELECT_BY_POS, MODE_TRADES);
            
            // Refresh after each one is processed
            RefreshRates();
            
            // Set quick bid or ask value
            if (OrderType() == OP_BUY)
            {
               tempPrice = Bid;
            }
            else
            {
               tempPrice = Ask;
            }
            
            // Perform the close
            IsClosed = OrderClose(OrderTicket(), OrderLots(), NormalizeDouble(tempPrice, Digits), gap, Orange);
            
            // If we failed to close, sleep then try again
            if (IsClosed == false)
               {
                  Sleep(10000);
                  // try again
                  IsClosed = OrderClose(OrderTicket(), OrderLots(), NormalizeDouble(tempPrice, Digits), gap, Orange);
               }
               
            // Setting boolean for next order process
            IsClosed = false;
        }
   }         

Sample of some of the errors:

2009.07.13 07:05:37 SimpleGrail7 EURUSDgfx,M1: unknown ticket 3489992 for OrderClose function
2009.07.13 07:05:28 SimpleGrail7 USDJPYgfx,M1: close #3489992 sell 0.10 USDJPYgfx at 92.310 sl: 92.607 at price 92.177
2009.07.13 07:05:27 SimpleGrail7 EURUSDgfx,M1: invalid price 1.39241000 for OrderClose function
2009.07.13 07:05:27 SimpleGrail7 EURUSDgfx,M1: invalid ticket for OrderClose function