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.
Adam Hedges
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.
Michael Handschuh
var ticket = MarketOrder("SPY", 100); if (ticket.Status == OrderStatus.Filled) { // grab the fill events var fills = ticket.OrderEvents.Where(orderEvent => orderEvent.Status.IsFill()).ToList(); // to compute average price we want a weighted average = SUM(n*p)/n var sumQuantityPrice = fills.Sum(fill => fill.FillQuantity * fill.FillPrice); var sumQuantity = fills.Sum(fill => fill.FillQuantity); var fillPrice = sumQuantityPrice / sumQuantity; }
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.
Michael Handschuh
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.
Michael Handschuh
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.
Adam Hedges
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.
Jonathan Evans
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.
Jonathan Evans
var longOrder = StopLimitOrder(Symbol, quantity, currentPrice * 1.02m, currentPrice); var stopLoss = StopLimitOrder(Symbol, -quantity, currentPrice * 0.99m, currentPrice); // elsewhere, update on new highs stopLoss.Update(new UpdateOrderFields { StopPrice = newPrice * 0.92m });
Basically, I'll need 2 orders (one with the upper target, one with the lower target), then update the lower target on new highs?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.
Michael Handschuh
// submit order to sell at +2% current price var profitTakingTicket = LimitOrder(Symbol, -10, currentPrice*1.02m);
If we'd like to submit a 1% stop loss:// submit order to execute when price drops below 1% var stopLossTicket = StopMarketOrder(Symbol, -10, currentPrice*.99m);
If we'd like to update our stop loss so that it 'trails' the current price:// update stop loss order on new highs if (currentPrice > previousHigh) stopLossTicket.Update(new UpdateOrderFields{StopPrice = currentPrice*.99m);
Let me know if you require further clarification with this, or if I've misunderstood your requirements. Also, I would recommend to use StopMarketOrder for stop loss instead of StopLimitOrder.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.
Jonathan Evans
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.
Michael Handschuh
if (ticket.Status.IsOpen()) { // open ticket }
Likewise, there's also one for closed:if (ticket.Status.IsClosed()) { // closed ticket }
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.
Jonathan Evans
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.
Michael Handschuh
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.
Jonathan Evans
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.
Jonathan Evans
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.
Michael Handschuh
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.
Jonathan Evans
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.
JayJayD
2013-10-06 20:00:00 Launching analysis for 3c8694c30fd6bc9a2bebbf2137903b1f with LEAN Engine v2.1.3.5 2013-10-07 09:31:00 10/07/2013 09:31:00 : Sell Market Order Id 1 of AIG was submitted. 2013-10-07 09:31:00 10/07/2013 09:31:00 : Buy Market Order Id 1 of AIG was filled at $48.265499725. 2013-10-07 09:31:00 10/07/2013 09:31:00 : Sell Market Order Id 2 of BAC was submitted. 2013-10-07 09:31:00 10/07/2013 09:31:00 : Buy Market Order Id 2 of BAC was filled at $13.70672050. 2013-10-07 09:31:00 10/07/2013 09:31:00 : Sell Market Order Id 3 of IBM was submitted. 2013-10-07 09:31:00 10/07/2013 09:31:00 : Buy Market Order Id 3 of IBM was filled at $174.310339515. 2013-10-07 09:31:00 10/07/2013 09:31:00 : Sell Market Order Id 4 of SPY was submitted. 2013-10-07 09:31:00 10/07/2013 09:31:00 : Buy Market Order Id 4 of SPY was filled at $161.795815272.
Like the direction changes if the order is filled or not. Question 2: Why do you said in the OnOrderEven help? For the unseasoned like me is very intimidating! Best, JJThe 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.
Michael Handschuh
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.
JayJayD
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.
Sdoof
That 's very useful, how would one do the same in Python though? It would be great to see a Python version of the attached example
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.
Jared Broad
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.
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.
Garyha
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='')
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.
Garyha
-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='')
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.
Michael Manus
check also the docu for canceling specific 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()
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.
Patrick Verhoeven
Can I simply say Price*Quantity<$10 then ignore?
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.
Louis Szeto
Hi Patrick
Please refers to this thread. Thanks!
Best
Louis
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.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!