I have code that loops over open tickets, and I want to close a particular position. All the examples I found show the use of setHoldings. How do I close a particular position?
// All filled orders
var tickets = Transactions
.GetOrderTickets(x => x.Status == OrderStatus.Filled);
//loop over order tickets
foreach (OrderTicket ticket in tickets) {
//what is the code to flatten the order here?
}
Gurumeher Sawhney
While OrderTickets can be looped over, specific positions cannot actually be changed via the method suggested above. Order tickers are meant to update, cancel or check the status of your specific order but once an order is placed they can't be used to close a position. QuantConnect aggregates the whole portfolio together so its an average of your past trades. I understand that in FX brokerages one can close specific matching orders. In order to flatten a position, Liquidate(Symbol) is a useful method. In this case, it might be worth maintaining a list of symbols for specific positions. Another useful method is self.Portfolio[Symbol].Quantity, which will return the current quantity held of the security. So instead of Liquidate(), orders can be made using this value.
Yechiel Michael Aaron
Give Thanks.
So, for example, if the position is Long 200 IBM, and wish to rebalance to 100 IBM, do I use:
newTicket = MarketOrder("IBM", -100, asynchronous: true);
?
Yechiel Michael Aaron
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!