Hello everybody, I'm very new to QuantConnect and very interested. I'm looking for a very specific thing I just can't find.
How do I recover every order ticket from the broker, WITHOUT ever opening one from the algo?
Thanks for any help.
Luca
QUANTCONNECT COMMUNITY
Hello everybody, I'm very new to QuantConnect and very interested. I'm looking for a very specific thing I just can't find.
How do I recover every order ticket from the broker, WITHOUT ever opening one from the algo?
Thanks for any help.
Luca
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.
Stefano Raggi
Hi Luca, I assume you're asking how to fetch existing open orders at startup.
In live trading, when an algorithm is deployed, Lean connects to the selected brokerage and automatically fetches all existing open orders and positions.
The orders (or order tickets) can be retrieved with these two methods:
Transactions.GetOpenOrderTickets() and Transactions.GetOpenOrders()
Keep in mind that these methods cannot be called in the Initialize method, since it is called before the connection to the brokerage, so they will have to be called later (e.g. in OnWarmupFinished or in OnData):
public override void OnWarmupFinished() { var tickets = Transactions.GetOpenOrderTickets().ToList(); var orders = Transactions.GetOpenOrders(); }
Luca Napo
Ciao Stefano, thank you very much,
I had used GetOpenOrderTickets() but I somehow assumed it wasnt possible to get pre existing tickets.
So thanks again, i'll get better as I learn. ;)
Luca Napo
Hello again, this is pretty much what I still don't understand:
I have 3 manually opened EURUSD executed market orders on the paper account. I run this little test and it keeps telling me 0 and 0. (And of course the loops don't run as the lists are empty).
But shouldnt it take 3 open tickets from my paper trading account and loop between them? I just don't understand how to do that.
namespace QuantConnect.Algorithm.CSharp { public class BasicTemplateAlgorithm : QCAlgorithm { private Symbol _spy = QuantConnect.Symbol.Create("SPY", SecurityType.Equity, Market.USA); public override void Initialize() { SetStartDate(2013, 10, 07); //Set Start Date SetEndDate(2013, 10, 11); //Set End Date SetCash(100000); //Set Strategy Cash AddEquity("SPY", Resolution.Minute); var eurusd = AddForex("EURUSD", Resolution.Minute); eurusd.SetDataNormalizationMode(DataNormalizationMode.Raw); } public override void OnData(Slice data) { //if (Securities.ContainsKey("EURUSD")) //{ var tickets = Transactions.GetOpenOrderTickets().ToList(); var orders = Transactions.GetOpenOrders(); Debug("tickets count " + tickets.Count); Debug("Orders count " + orders.Count); foreach (var ticket in tickets) { Debug("Order id " + ticket.OrderId + " at time " + ticket.Time); } foreach (var order in orders) { Debug("Order " + order.Status + " at price " + order.Price + " opened " + (order.Time - Time) + " ago."); } //} } } }
Jack Simonson
Hi Luca,
I looked into the code you posted and tested it a bit. One thing to note is that Market Orders get filled instantly and so they won't be classified as 'Open' by the time the code assigns all open orders to the variable orders. This is due to a difference between FXCM style Order Tickets and Stock style "averaged portfolio". We group and average all the holdings together, but FXCM/FX sites have separate mini portfolios for each trade. The code for the tickers should work properly, but I've attached the code I ran so you can view what worked for me. I instantiated a private Order Ticket list variable and then added order ticks in the main OnData function.
namespace QuantConnect.Algorithm.CSharp { public class BasicTemplateAlgorithm : QCAlgorithm { private Symbol _spy = QuantConnect.Symbol.Create("SPY", SecurityType.Equity, Market.USA); private List<OrderTicket> _tickets = new List<OrderTicket>(); public override void Initialize() { SetStartDate(2013, 10, 07); //Set Start Date SetEndDate(2013, 10, 08); //Set End Date SetCash(10000000); //Set Strategy Cash AddEquity("SPY", Resolution.Daily); var eurusd = AddForex("EURUSD", Resolution.Daily); } public override void OnData(Slice data) { var newTicket = MarketOrder("EURUSD", 10000); var newTicket2 = MarketOrder("EURUSD", -10000); var newTicket3 = MarketOrder("EURUSD", 50000); var orders = Transactions.GetOpenOrders(); _tickets.Add(newTicket); _tickets.Add(newTicket2); _tickets.Add(newTicket3); Log("tickets count " + _tickets.Count); Log("Orders count " + orders.Count); foreach (var ticket in _tickets) { Log("Order id " + ticket.OrderId + " at time " + ticket.Time); } foreach (var order in orders) { Log("Order " + order.Status + " at price " + order.Price + " opened " + (order.Time - Time) + " ago."); } } } }
Â
Luca Napo
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!