Back

[Feature] OrderTickets and Updating Orders

Recently we deployed a new version of the LEAN engine. In this new version there are load of new goodies. Here I showcase an updated version of the canonical QCU algorithm, How Do I Use Limit Orders. We've spent some time to make this easier, so I hope you guys like it!

In the new deploy, the standard order functions (MarketOrder, LimitOrder, ect...) will return you an OrderTicket object. The OrderTicket is your reference to your order. You can use it to update, cancel, and even check the status of the order.OrderTicket limitOrderTicket = LimitOrder("SPY", 100, 205);
In order to perform updates, you can call the Update method on the OrderTicket. The Update method takes an UpdateOrderFields object which defines what properties of the order should be updated. We can easily change the limit price using the following line of code: limitOrderTicket.Update(new UpdateOrderFields{LimitPrice = 207.50};
This will submit a request to update the order in our system.
Likewise, you can easily cancel your order using the Cancel method:limitOrderTicket.Cancel();
Check out the attached algorithm which shows updating of the limit price. Check out the UpdateOrderFields class for the other properties that can be updated.
Update Backtest






The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.



Hi Olivier; the generic answer to that question is in the API tab on the left side of the IDE. We actually have more examples of python than C# now! =) The Order Ticket example link is below.

https://github.com/QuantConnect/Lean/blob/master/Algorithm.Python/OrderTicketDemoAlgorithm.py
1

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


From the link above:

# submit another limit order to sell 10 shares at .1% above the bar's close
newTicket = self.LimitOrder(self.spy, -10, close * d.Decimal(1.001))
self.__openLimitOrders.append(newTicket)

 

What if you want to close the position? How do you programmatically obtain the current number of shares and what would that look like in place of "10"?

More specifically, I'd like to set stop and limit for closing positions as soon as limit orders to open them have filled, and this would need correcting

 

def OnOrderEvent(self, OrderEvent):
if OrderEvent.Status == OrderStatus.PartiallyFilled:
self.Log(' partial {}'.format(OrderEvent.Symbol))
elif OrderEvent.Status == 3 and OrderEvent.FillQuantity > 0:
self.Log(' filled {} {}'.format(OrderEvent.FillQuantity, OrderEvent.Symbol))
self.LimitOrder( OrderEvent.Symbol, -self.Securities[OrderEvent.Symbol].Quantity, self.Securities[OrderEvent.Symbol].Price * decimal.Decimal(1.02), tag='')
self.StopMarketOrder(OrderEvent.Symbol, -self.Securities[OrderEvent.Symbol].Quantity, self.Securities[OrderEvent.Symbol].Price * decimal.Decimal(.99), tag='')

0

-self.Portfolio[OrderEvent.Symbol].Quantity for the current number of shares. This is in OnOrderEvent()

 

self.LimitOrder(OrderEvent.Symbol, -self.Portfolio[OrderEvent.Symbol].Quantity, self.Securities[OrderEvent.Symbol].Price * decimal.Decimal(1.02), tag='')
0

check also the docu for canceling specific orders:

https://www.quantconnect.com/docs/algorithm-reference/trading-and-orders# Creating an Order:
limitOrderTicket = self.LimitOrder("SPY", 100, 205)

# Updating an Order:
updateOrderFields = UpdateOrderFields()
updateOrderFields.LimitPrice = decimal.Decimal(207.50)
limitOrderTicket.Update(updateOrderFields)

# Cancel an Order:
limitOrderTicket.Cancel()

# Cancel all open orders from SPY
cancelledOrders = self.Transactions.CancelOpenOrders("SPY")

# Cancel order #10
cancelledOrder = self.Transactions.CancelOrder(10)

# Get open orders
openOrders = self.Transactions.GetOpenOrders()

# Get open orders from SPY
openOrders = self.Transactions.GetOpenOrders("SPY")

# Get open order #10
openOrder = self.Transactions.GetOrderById(10)

# Get all orders
orders = self.Transactions.GetOrders()

# Get order ticket #10
orderTicket = self.Transactions.GetOrderTicket(10)

# Get all orders tickets
openOrderTickets = self.Transactions.GetOrderTickets()
0

Update Backtest





0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Loading...

This discussion is closed