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
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; }
Michael Handschuh
Michael Handschuh
Adam Hedges
Jonathan Evans
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?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.Jonathan Evans
Michael Handschuh
if (ticket.Status.IsOpen()) { // open ticket }
Likewise, there's also one for closed:if (ticket.Status.IsClosed()) { // closed ticket }
Jonathan Evans
Michael Handschuh
Jonathan Evans
Jonathan Evans
Michael Handschuh
Jonathan Evans
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, JJMichael Handschuh
JayJayD
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
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.
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!