SmartQuant Discussion

Automated Quantitative Strategy Development, SmartQuant Product Discussion and Technical Support Forums
It is currently Mon Nov 30, 2020 4:59 pm

All times are UTC + 3 hours




Post new topic Reply to topic  [ 11 posts ] 
Author Message
PostPosted: Tue Dec 04, 2007 10:05 pm 
Offline

Joined: Wed Apr 27, 2005 4:41 pm
Posts: 609
Location: Helsinki, Finland
Item has already been added. Key in dictionary: '2935' Key being added: '2935'.

Any ideas why? I get the error when doing order.Send();

I haven't seen this before, so I believe it's some kind of one-timer, perhaps, but I still wondering why?

Thanks,
Eelofi


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 05, 2007 1:52 am 
Offline

Joined: Tue Oct 23, 2007 11:47 pm
Posts: 154
I sometimes get that error too, not really sure what causes it. Which data feed and which brokerage are you trying to use?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 05, 2007 11:06 am 
Offline

Joined: Tue Aug 05, 2003 3:43 pm
Posts: 6817
Also, it would be very helpful if you post more debugging info on this error (stack trace).

Thanks,
Anton


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 08, 2008 10:09 pm 
Offline

Joined: Fri Apr 04, 2008 6:11 am
Posts: 138
I'm getting the same error. It appears to happen when an order is sent twice.

Here's the stack trace:
Code:
System.ArgumentException: Item has already been added. Key in dictionary: '080408145048 364738'  Key being added: '080408145048 364738'
   at System.Collections.Hashtable.Insert(Object key, Object nvalue, Boolean add)
   at System.Collections.Hashtable.Add(Object key, Object value)
   at SmartQuant.Execution.OrderList.Add(IOrder order)
   at SmartQuant.Execution.InstrumentOrderListTable.Add(SingleOrder order)
   at SmartQuant.Execution.OrderListTable.Add(SingleOrder order)
   at SmartQuant.Execution.OrderManager.AB0DkN6mARtGTR3n3VoBeVTqQrC7fjj(SingleOrder )
   at SmartQuant.Execution.SingleOrder.Send()
   at OpenQuant.API.Order.Send()
   at MyStrategy.SendOrderOnce(Order order) in c:\Program Files\SmartQuant Ltd\OpenQuant 2\Projects\TrendingMarketStrategy\code.cs:line 771
   at MyStrategy.ApplyBuyStrategy(Double price) in c:\Program Files\SmartQuant Ltd\OpenQuant 2\Projects\TrendingMarketStrategy\code.cs:line 652
   at MyStrategy.ApplyEntryStrategy(Double price) in c:\Program Files\SmartQuant Ltd\OpenQuant 2\Projects\TrendingMarketStrategy\code.cs:line 530
   at MyStrategy.OnUpdate(Double price) in c:\Program Files\SmartQuant Ltd\OpenQuant 2\Projects\TrendingMarketStrategy\code.cs:line 509
   at MyStrategy.OnBar(Bar bar) in c:\Program Files\SmartQuant Ltd\OpenQuant 2\Projects\TrendingMarketStrategy\code.cs:line 246
   at OpenQuant.Trading.StrategyRunner.OnNewBar(Instrument instrument, Bar bar)
   at OpenQuant.Trading.StrategyRunner.SetNewBar(Instrument instrument, Bar bar)



At first I thought it was a bug on my part, but then I threw in some quick test code and discovered that orders aren't handled the way I expected they would be.

I wrote a utility method to guarantee that no order is sent more than once. All orders in my strategy are sent through this method:
Code:
    private void SendOrderOnce(Order order)
    {
        if (order != null && order.IsPendingNew)
        {
            Console.WriteLine("Attempting to send " + order);

            order.Send();

            if (order.IsPendingNew)
                Console.WriteLine("***ERROR: ORDER STILL PENDING NEW AFTER SEND!!!");
        }
    }


When I run the strategy, I get the following output (sorry about the generic order print out, I can't figure out how to get a unique order id before the order has made it to the broker):
Code:
Attempting to send OpenQuant.API.Order
***ERROR: ORDER STILL PENDING NEW AFTER SEND!!!
Attempting to send OpenQuant.API.Order
(Then error raised with above stack trace in dialog window)


My question is, how can an order still be in the PendingNew state after Send has been called? I thought it was supposed to move to the New state immediately. If that's not the case, what check can you use to see if an order's send() method has already been called without having to track it yourself?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 09, 2008 10:51 am 
Offline
Site Admin

Joined: Thu Jul 17, 2003 10:39 am
Posts: 1478
Hi,

in accordance with FIX order state change matrix, 'New' status is assigned to order when it is accepted by broker, not when order is sent.
Usually, a lot of time is needed to get response from broker, so during this time the order has PendingNew status.
You should use some flags in your strategy in order to do not send the orders twice or you can track order status using OnOrderStatusChanged, OnOrderRejected, etc callbacks.

Regards,
Alex

_________________
SmartQuant Development Team


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 09, 2008 11:56 pm 
Offline

Joined: Fri Apr 04, 2008 6:11 am
Posts: 138
The FIX order state change matrix is great and all, but I've seen desire for this behavior mentioned multiple times on the forums.

It seems like every trader creates their own "flagging" system for preventing this error. Why not just provide one that we can all use out of the box? Adding an Order.Sent property would be a four line fix for you guys internally and would still leave the order flow states in accordance with the FIX protocol.

Code:
// ... added to Order class
protected bool _sent = false;

public bool Sent
{
   get { return _sent; }
}

// ... added to Order.Send() method
_sent = true;



Just four little lines and we no longer have to worry about flagging our own orders.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 18, 2008 11:36 am 
Offline

Joined: Tue May 03, 2005 9:36 pm
Posts: 136
Location: Sydney
I am getting this error.

When I run my strategy on its own it runs without any errors. However when I add this strategy to a solution that contains other strategies it causes the error to be thrown in one of the other strategies in that solution.

Any suggestions on dealing with this?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 18, 2008 1:01 pm 
Offline
Site Admin

Joined: Thu Jul 17, 2003 10:39 am
Posts: 1478
Hi,

Could you give us more information about the situation - what market data and execution providers you use, what orders your strategy sends, etc?
You can send this info directly to me: alexei.kurov@smartquant.com

Regards,
Alex

_________________
SmartQuant Development Team


Top
 Profile  
 
PostPosted: Thu Nov 03, 2011 11:13 am 
Offline

Joined: Sun Oct 30, 2011 12:12 am
Posts: 220
I had this error in my strategy yesterday and solved it by debugging step-by-step in VS2010.
Here's my input to help other persons solve their own problem.

Basically, the exception says it all: it will be thrown when a strategy sends twice the same order.
I guess there is an internal logic in OQ which compares any new incoming order (= when order.Send() is called) to all existing instances in Orders (the OrderList) by computing and comparing a hashcode.

My situation: a portion of my code creates 2 different orders (1 buy & 1 sell) and sends them sequentially.
Code:
_buyOrder = SellStopOrder(...);
_sellOrder = BuyStopOrder(...);
...
_buyOrder.Send();
_sellOrder.Send();

These are truly 2 different orders and object instances so I was surprised to see the exception message.

It turns out my OnPositionOpen() logic contains the following code:
Code:
_buyOrder.Qty = newQty;
_buyOrder.Send();


Understanding the following helped me solve this issue:
1- _buyOrder.Send() should not be used to update an existing order. Use _buyOrder.Replace() instead.
2- OQ's execution logic of the above code is: create a buy order, send it, fire OnPositionOpen() which modifies _buyOrder.Qty and attempts to Send() it again (that's where the exception is thrown!), get back to my initial code and send _sellOrder
3- I guess the hashcode will not differenciate between a given order and the same order with Qty modified, which is fine

Hope it helps.


Top
 Profile  
 
PostPosted: Thu Nov 03, 2011 9:30 pm 
Offline

Joined: Tue Aug 05, 2003 3:43 pm
Posts: 6817
By the way, please note that

Order.Qty = qty;

doesn't actually change Order.Qty property until OQ gets a replace confirmation report from a broker (or from the execution simulator).


Top
 Profile  
 
PostPosted: Fri Nov 04, 2011 2:32 am 
Offline

Joined: Sun Oct 30, 2011 12:12 am
Posts: 220
Very good to know, thanks Anton.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 11 posts ] 

All times are UTC + 3 hours


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group