Good afternoon,
My trading system places limit orders after market is closed. After the close next day I want to check which positions were executed. How do I loop through the currently open positions?
Entry is based on a ranking from a universe. The system does not liquidate stocks on _changes.RemovedSecurities. The system holds the position until stop loss or profit target is hit. I would like to loop through each stock to set stop and profit target price dependent on the fill price. Any suggestions? Many thanks!
Alexandre Catarino
In Lean/QuantConnect, we differ Order from OrderTicket. While Order is a struct for placing a new trade, an OrderTicket is a single reference to an order for the algorithm to maintain. As the order gets updated this ticket will also get updated. Here we find the fill price, fill quantity, status, etc.
If we want the orders that got filled (open positions), we can use the Transactions object:
// All filled orders var tickets = Transactions .GetOrderTickets(x => x.Status == OrderStatus.Filled);
Then we can loop through them to get the information we need (symbol, fill price and quantity) to issue a new order:
foreach (var ticket in tickets) { var symbol = ticket.Symbol; var fillQuantity = ticket.QuantityFilled; var fillPrice = ticket.AverageFillPrice; }
Alexandre Catarino
Let me add a suggestion: instead of looping throught the position, we can use the OnOrderEvent event handler:
public override void OnOrderEvent(OrderEvent orderEvent) { if (orderEvent.Status.IsFill()) { var symbol = orderEvent.Symbol; var fillQuantity = orderEvent.FillQuantity; var fillPrice = orderEvent.FillPrice; } }
and we can add a new stop and profit target as soon as the order is filled.
Carlos Mata
Many Thanks for your quick reply Alexandre!
Just to double check:
The looping example runs in on OnData() method, so with daily resolution, the stop and profit target orders would be placed after the day's close?
The second example OnOrderEvent can place the stop and profit target orders as soon as the order is filled (intraday)?
Alexandre Catarino
The daily bar only arrives after market close, so the new orders will be placed on market open. If there is a gap, these orders may never get filled.
When we subscribe to daily resolution data in backtest, "as soon as possible" does not mean intraday, since there is no data flowing to fill the order in a lower resolution.
Carlos Mata
Larry Smith
Alexandre Catarino, do you have any design notes that describe dataflow ?
Such as to place a trade, module X receives trade Order which then calls method Y that is responsible for submitting trade?
I found a PDF , however , it doesn't get to this level of depth & breadth of detail.
Carlos Mata
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!