Tracking and Managing Orders

Tracking and managing orders is an important part of an algorithmic trading strategy. Intelligent order management encourages discipline and a deep understanding of your algorithm. Through the QuantConnect API you can get order fields, update their values and cancel pending orders. This can be useful for lowering trading costs and improving order fills.

When you place a trade you receive an OrderTicket for you to access the order. This allows you to safely (asynchronously) update and cancel the order while in live trading. In live trading you cannot assume order updates are processed successfully as the brokerage may have already filled the trade.

You can place several types of orders including:

Supported Order Types
Market Order MarketOrder("SPY", 100);
Limit Order var ticket = LimitOrder("SPY", 100, 100.10m);
Stop Market Order var ticket = StopMarketOrder("SPY", 100, 100.10m);
Stop Limit Order var ticket = StopLimitOrder("SPY", 100, 100.12m, 99.5m);
Market On Open Order var ticket = MarketOnOpen("SPY", 100);
Market On Close Order var ticket = MarketOnClose("SPY", 100);

 

Once you have an order ticket you can use it to get order fields:

var currentStopPrice = _ticket.Get(OrderField.StopPrice);

Or update the order fields (LimitPrice, StopPrice, Tag or Quantity):

_ticket.Update(new UpdateOrderFields
{
    LimitPrice = newLongLimit,
    Tag = "Update #" + (longOrder.UpdateRequests.Count + 1)
});

You can also cancel your order if required:

_ticket.Cancel();

In the video below we demonstrate putting it all together to create a moving take-profit order which gradually decreases its profit target as time passes.

[embedyt] https://www.youtube.com/watch?v=HykXfstdNW0[/embedyt]

Avatar

By: Jared Broad

Founder & CEO

05.04.2017