Mikołaj Gogola
Mikołaj Gogola

Reputation: 129

Invalid Ticket when closing orders

I'm trying to close all opened orders, they are all MarketOrders not PendingOrders and have SL and TP set to 0 so they shouldn't close themselves (they are still opened in Terminal). I am storing tickets in the array so my loop looks like:

for(int i = 0; i < trades.size(); ++i) OrderClose(tickets[i], lots[i], Bid, Slippage);

yet I'm still receiving "INVALID TICKET" error, can you tell me why? It doesn't happen always, its like some orders are closed and some throw Invalid Ticket. I didn't notice this behavior with only 1 order so I'm assuming it occurs only when there are more of them.

Code:

template < typename T >
struct vector {
  vector() {}
  vector(int arraySize) {
     if (arraySize < 0) { arraySize *= -1; }
     ArrayResize(m_data, arraySize);
  }
  vector(vector & rhs) { 
     if (size() < rhs.size()) { ArrayResize(m_data, rhs.size()); }
     for(uint i = 0; i < size(); ++i) { m_data[i] = rhs.m_data[i]; }
  }
  vector operator=(vector & rhs) {
     if (size() < rhs.size()) { ArrayResize(m_data, rhs.size()); }
     for(uint i = 0; i < size(); ++i) { m_data[i] = rhs.m_data[i]; }
     return this;
  }
  T operator[](uint index) { return m_data[index]; }
  void push_back( T value ) {

     ArrayResize(m_data, ArraySize(m_data) + 1); 
     m_data[ ArraySize(m_data) - 1 ] = value;
  }
  uint size() { return ArraySize(m_data); }
  void resize(uint newSize) { ArrayResize(m_data, newSize); }
  void erase() {
     ZeroMemory(m_data);
     ArrayResize(m_data, 0);
  }
  void assign(uint index, T value) {
     m_data[index] = value;
  }
private:
  T m_data[];
};

string Buy(double lots) {
  string alertString = "";
  int __ticket;
  if ( (__ticket = OrderSend (Symbol(), OP_BUY, lots, Ask, Slippage, 0, 0, NULL, m_magic)) != -1 )
  {
     m_buyTicket.push_back( __ticket );
     m_buyLots.push_back( lots );
     m_buyPrice.push_back( Ask );
     m_buyAccel.push_back( lots / Lots );
     m_buyPos.push_back( 0 );

     alertString = "Buy function call." +
            "\nAsk\t= " + (string)Round(Ask) +
            "\nBid\t= " + (string)Round(Bid) +
            "\nLots\t= " + (string)Round(lots) +
            "\nSpread\t= " + (string)m_spread +
            "\nID\t= " + (string)CountAll();
  }
  else { 
     int _error = GetLastError();
     alertString = "Error " + (string)_error + "\n" + TranslateError( _error );
  }
  return alertString;
}

string CloseAll() {
  string alertString = "CloseAll function call.";
  // Buy closing
  for (uint i = 0; i < m_buyPrice.size(); ++i)
  {
     if ( OrderClose ( m_buyTicket[i], m_buyLots[i], Bid, Slippage) )
     {
        alertString += "\nBuy " + (string)(i+1) + " closed with profit " +
                       (string)Shrink ( (Bid - m_buyPrice[i]) * m_buyAccel[i] );
     }
     else
     {
        int _error = GetLastError();
        alertString += "\nError " + (string)_error + "\n" + TranslateError( _error ) +
                       "\n(while closing Buy " + (string)(i+1) + ")";
     }
  }
  // Sell closing
  for (uint i = 0; i < m_sellPrice.size(); ++i)
  {
     if ( OrderClose ( m_sellTicket[i], m_sellLots[i], Ask, Slippage) )
     {
        alertString += "\nSell " + (string)(i+1) + " closed with profit " +
                       (string)Shrink ( (m_sellPrice[i] - Ask) * m_sellAccel[i] );
     }
     else
     {
        int _error = GetLastError();
        alertString += "\nError " + (string)_error + "\n" + TranslateError( _error ) +
                       "\n(while closing Sell " + (string)(i+1) + ")";
     }
  }
  return alertString;
}

Upvotes: 1

Views: 343

Answers (1)

Daniel Kniaz
Daniel Kniaz

Reputation: 4681

when you close some ticket successfully, you do not remove it from the list trades, right? And it seems you should. Use your struct to clear the elements if order close is successful. By the way, there's probably no need to reinvent the wheel, use CArrayObj as a container for your elements and delete them once order is closed.

Upvotes: 1

Related Questions