The algorithm I'm toying with presently involves unloading a stock that was purchased after n days if a specific criteria hasn't been met. I'm having a tough time deciphering through the object model how to determine when an equity was actually purchased.
I'm guessing it's something in the TransActions.GetOrders(x => x.Symbol = theSymbol) or something, but I'm not seeing how to actually get the date the original buy order was completed.
Suggestions? Thanks in advance.
Alexandre Catarino
We need to specify the nature of the orders we are trying to get from the transaction records. If we want the time a stock was purchased, we also need to select orders by their symbol and status:
var filledSpyOrders = Transactions .GetOrders(x=> x.Symbol == "SPY" && x.Status == OrderStatus.Filled);
Then we need to check whether any order felt in this criteria and after that get the time the stock was purchased:
if (filledSpyOrders.Count() > 0) { // Only the last one under my criteria var orderTime = filledSpyOrders.Last().Time; Log(Time + ": " + orderTime); }
Nicolas Ferrari
Hello Alexander, can you give an example of the FuncFilter with python? is a list comprehension? I cannot filter the GetOrders in the right way.
Â
Thanks!
Nicolás
Jack Simonson
Hi Nicolas,
You can use this code to get the same results in Python:
filledSpyOrders = [x for x in self.Transactions.GetOrders(None) if (x.Symbol.Value == 'SPY') and (x.Status == OrderStatus.Filled)] if len(filledSpyOrders) > 0: self.Log(f'{self.Time}: {filledSpyOrders[0].Time}')
This will retrieve all orders using self.Transactions.GetOrders(None) and then applies the boolean filtering using a list comprehension.
Sachinahj
Is there a way to pass the filter into the GetOrders function like mentioned here:
https://www.quantconnect.com/lean/documentation/topic27159.html
I was thinking something like this:
self.Transactions.GetOrders(lambda o: o.Status == OrderStatus.Filled)
but it fails with:
TypeError : No method matches given arguments for GetOrders (Open Stacktrace)
Are there any performance benfits if calling GetOrders with or without a filter function?Â
Derek Melchin
Hi Sachinahj,
Passing a function to the GetOrders method could have some performance benefits, but this currently only works when using C#. With Python, we can use the filtering method Jack published above.
A GitHub Issue has been created to implement this functionality in the python API. Its progression can be tracked here.
Best,
Derek Melchin
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.
TedVZ
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!