Trading Evolution

Markets are never wrong - opinions often are

How to create an order that automatically cancels in 5 min

When you write your automated system sometimes your signal is valid only for the next 5, 10 or 15 min. It is useful to create orders that auto expire if not filled after certain period of time so you don't have to write code that cancels them. All is needed is to set Time In Force (Tif) property to "GTD" and set GoodTillDate to the time in a future you want this order to expire if it is not filled. In the example below I used DateTime.Now.AddMinutes to add 10 minutes to current time.

var order = new IBApi.Order();

order.OrderId = _ibWrapper.NextOrderId++;
order.Action = "BUY";
order.OrderType = "LMT";
order.LmtPrice = price;
order.TotalQuantity = quantity;
order.Tif = "GTD";
order.GoodTillDate     = DateTime.Now.AddMinutes(10).ToString("yyyyMMdd HH:mm:ss ");

_ibWrapper.IBClient.placeOrder(order.OrderId, ..., order);