Back

How to convert Python list to Quantconnect List

I'm trying to return a "list" of Symbol objects from the 'CoarseSelectionFunction'. However, it seems the function wants a very specific list. Namely a 'System.Collections.Generic.1' object. How can I easily convert a standard Python list to this object?

The brute force method I have found is this:

quantconnect_list = List[Symbol]()
for symbol in python_list:
    quantconnect_list.Add(symbol)

That isn't very elegant. Am I missing something?

Update Backtest








Looking at the Python algos included in Lean, specifically, CoarseFineFundamentalComboAlgorithm.py, it doesn't look there's a better solution. Although I'd add len(python_list) to the constructor of the CLR list, as you already know how big your Python list is, and it's more efficient to preallocate the clr_list, so that when you loop through the python_list, whilst copying to the clr_list, you're not going to suffer a resize, recopy et al.

clr_list = List[Symbol](len(python_list)) # preallocate target list
for symbol in python_list:
clr_list.Add(symbol)


 

0

Young Zhang 

Thanks for the response. That helps. 

I'm beginning to bump up against the fact that, under the covers, QauntConnect is C#. Python, especially coupled with pandas and numpy, is quite elegant and succinct and forgiving. Having to convert from the C# objects to the pandas objects and then back again sort of cancels that out. Using the C# objects alone seems no better than programming in C# and adding 'self' everywhere without the benefit of all the built in pandas/numpy methods. There are a few methods which use native python/pandas objects (eg the 'History' method returns a dataframe) but that seems to not be the norm. 

1

The solution is to add more Python compatible marshalling functions like:

PyObject History(PyObject tickers, int periods, Resolution? resolution = null) {
//
}

where PyObject (the C# representation of a Python object) is then converted over to a C# equivalent. Of course, that's going to time and effort to add to Lean.

0

Agree Dan, Young -- working on it as fast as possible! =)

You seem to know how things work Young; would love your help if you're open to it.

0

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.


Update Backtest





0

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.


Loading...

This discussion is closed