SmartQuant Discussion

Automated Quantitative Strategy Development, SmartQuant Product Discussion and Technical Support Forums
It is currently Mon Sep 24, 2018 7:16 am

All times are UTC + 3 hours




Post new topic Reply to topic  [ 4 posts ] 
Author Message
PostPosted: Mon Sep 22, 2008 11:55 am 
Offline

Joined: Wed Sep 26, 2007 10:03 pm
Posts: 100
Hi

I wanted to understand in which order the events are called when I send an order to IB.

I have following test code and output, see below.

What I don't understand is, why is OnPositionOpened() not fired after the first partial fill? It seems that OnPositionOpened() fires only when the order status changes to filled, but essentialy I already have a position established after the first partial fill.

I also observed that when I for example have 1000 contracts and I sell all contracts, OnPositionChanged() does not fire until all contracts have been sold. I would have expected, that OnPositionChanged() fires whenever a partial fill has been confirmed by IB.

Would appreciate any help...

Regards,
Boris

Code:
public class MyStrategy : Strategy
{
   Order buyMarketOrder, sellMarketOrder;
   int qty = 1000;
   
   public override void OnBar(Bar bar)
   {
      if(Bars.Count == 1)
      {
         buyMarketOrder = BuyOrder(qty);
         buyMarketOrder.Text = string.Concat("Buy market order for ", qty, " ", Instrument.Symbol);
         buyMarketOrder.Send();
      }   
   }

   public override void OnPositionOpened()
   {
      Console.WriteLine("OnPositionOpened");
      Console.WriteLine("\tClock.Now: {0}. buyMarketOrder.Status: {1}. Position.EntryDate: {2}. Position.EntryPrice: {3}. Position.EntryQty. {4}. Position.Qty: {5}. Position.QtyBought: {6}.", Clock.Now, buyMarketOrder.Status, Position.EntryDate, Position.EntryPrice, Position.EntryQty, Position.Qty, Position.QtyBought);
   }   
   
   public override void OnPositionChanged()
   {
      Console.WriteLine("OnPositionChanged");
      if(Position != null)
      {
         Console.WriteLine("\tClock.Now: {0}. buyMarketOrder.Status: {1}. Position.EntryDate: {2}. Position.EntryPrice: {3}. Position.EntryQty. {4}. Position.Qty: {5}. Position.QtyBought: {6}.", Clock.Now, buyMarketOrder.Status, Position.EntryDate, Position.EntryPrice, Position.EntryQty, Position.Qty, Position.QtyBought);
      }
   }

   public override void OnNewOrder(Order order)
   {
      Console.WriteLine("OnNewOrder");
      Console.WriteLine("\tClock.Now: {0}. order.DateTime: {1}. order.Side: {2}. order.Qty: {3}. order.Status: {4}.", Clock.Now, order.DateTime, order.Side, order.Qty, order.Status);   
   }

   public override void OnOrderStatusChanged(Order order)
   {
      Console.WriteLine("OnOrderStatusChanged");
      Console.WriteLine("\tClock.Now {0}. OrderStatus: {1}", Clock.Now, order.Status);
   }
   public override void OnOrderPartiallyFilled(Order order)
   {
      Console.WriteLine("OnOrderPartiallyFilled");
      Console.WriteLine("\tClock.Now: {0}. order.DateTime: {1}. order.LastQty: {2}. order.LeavesQty: {3}. order.CumQty: {4}. order.AvgPrice: {5}. order.LastPrice {6}. order.Status: {7}.", Clock.Now, order.DateTime, order.LastQty, order.LeavesQty, order.CumQty, order.AvgPrice, order.LastPrice, order.Status);      
   }
   
   public override void OnOrderFilled(Order order)
   {
      Console.WriteLine("OnOrderFilled");
      Console.WriteLine("\tClock.Now: {0}. order.DateTime: {1}. order.LastQty: {2}. order.LeavesQty: {3}. order.CumQty: {4}. order.AvgPrice: {5}. order.LastPrice {6}. order.Status: {7}.", Clock.Now, order.DateTime, order.LastQty, order.LeavesQty, order.CumQty, order.AvgPrice, order.LastPrice, order.Status);      
   }

   public override void OnOrderDone(Order order)
   {
      Console.WriteLine("OnOrderDone");
      Console.WriteLine("\tClock.Now: {0}. order.DateTime: {1}. order.Qty {2}. order.Status: {3}.", Clock.Now, order.DateTime, order.Qty, order.Status);
   }
}



The output is:


Quote:
OnNewOrder
Clock.Now: 22.09.2008 10:51:20. order.DateTime: 22.09.2008 10:51:20. order.Side: Buy. order.Qty: 1000. order.Status: PendingNew.
OnOrderStatusChanged
Clock.Now 22.09.2008 10:51:21. OrderStatus: New
OnOrderPartiallyFilled
Clock.Now: 22.09.2008 10:51:21. order.DateTime: 22.09.2008 10:51:20. order.LastQty: 327. order.LeavesQty: 673. order.CumQty: 327. order.AvgPrice: 3272. order.LastPrice 3272. order.Status: PartiallyFilled.
OnOrderStatusChanged
Clock.Now 22.09.2008 10:51:21. OrderStatus: PartiallyFilled
OnOrderPartiallyFilled
Clock.Now: 22.09.2008 10:51:23. order.DateTime: 22.09.2008 10:51:20. order.LastQty: 326. order.LeavesQty: 347. order.CumQty: 653. order.AvgPrice: 3272. order.LastPrice 3272. order.Status: PartiallyFilled.
OnOrderPartiallyFilled
Clock.Now: 22.09.2008 10:51:23. order.DateTime: 22.09.2008 10:51:20. order.LastQty: 100. order.LeavesQty: 247. order.CumQty: 753. order.AvgPrice: 3271.86719787517. order.LastPrice 3271. order.Status: PartiallyFilled.
OnOrderStatusChanged
Clock.Now 22.09.2008 10:51:24. OrderStatus: Filled
OnPositionOpened
Clock.Now: 22.09.2008 10:51:24. buyMarketOrder.Status: Filled. Position.EntryDate: 22.09.2008 10:51:24. Position.EntryPrice: 3271.9. Position.EntryQty. 1000. Position.Qty: 1000. Position.QtyBought: 1000.
OnPositionChanged
Clock.Now: 22.09.2008 10:51:24. buyMarketOrder.Status: Filled. Position.EntryDate: 22.09.2008 10:51:24. Position.EntryPrice: 3271.9. Position.EntryQty. 1000. Position.Qty: 1000. Position.QtyBought: 1000.
OnOrderFilled
Clock.Now: 22.09.2008 10:51:24. order.DateTime: 22.09.2008 10:51:20. order.LastQty: 247. order.LeavesQty: 0. order.CumQty: 1000. order.AvgPrice: 3271.9. order.LastPrice 3272. order.Status: Filled.
OnOrderDone
Clock.Now: 22.09.2008 10:51:24. order.DateTime: 22.09.2008 10:51:20. order.Qty 1000. order.Status: Filled.




Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 28, 2008 2:37 am 
Offline

Joined: Thu Jun 08, 2006 3:56 pm
Posts: 537
Location: BC Canada
Yes, the event chain seems a little unintuitive. Your posting is timely because I was about to embark on working with these OnPosition* events myself.

I can understand the OnPositionOpened behavior as a definition/naming issue if OnPositionOpened really means OnPositionFullyOpened. There seems to be an implicit attempt to treat Positions as fully opened or fully closed (ie, atomic) in these event handlers. Presumably the flip side of this policy is that a Position is not a position until all outstanding orders against the position are completed themselves.

It looks like orders are orders until they are done, at which time positions (consequences of the orders) become active. But you can't have Position consequences (OnPositionChanged, OnPositionOpened, etc) of orders until the orders are complete.

Having said all that, I have to admit that OnPositionChanged sounds a lot like it should fire whenever partial fills affect the position. Maybe Anton can explain the naming, behavior, and suggested coding practices associated with these events?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 19, 2009 9:56 pm 
Offline

Joined: Sat Jun 07, 2008 1:41 pm
Posts: 47
Location: Munich, Germany
This posting is some time ago, but now I reached the same point. Which event-handler can I use? Whenever my size changes (independently if partially filled or fully filled) I want to take a look and do some actions. Which event-handler can I use? OnOrderPartiallyFilled plus OnOrderFilled is not useful for me.

When I have a fill I place some new orders. So whenever my position changes I want to place orders. When I do it with OnOrderPartiallyFilled I lose control because of interaction between OnOrderFilled and OnOrderPartiallyFilled.

Is there any handler I can use? A handler which is active whenever there is a fill.


Top
 Profile  
 
PostPosted: Fri Jan 27, 2012 7:54 pm 
Offline

Joined: Sun Oct 30, 2011 12:12 am
Posts: 220
Basically everything is said in this post wrt partial fills & positions handling.
OQ considers that no position is opened after an order is partially filled. This is conceptually wrong.
Given that there appears to be no chance for a fix anytime soon, how have OQ live users dealt with that issue?


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 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