Back

How to import coarse and fine selection functions from a separate file into main?

I'm trying to find a method to import the universe selection functions from a different file using the classic algorithm framework. But my method generates an error. Here's the code for both main.py and universe_selection.py

# main.py

from universe_selection import *


class VerticalQuantumInterceptor(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2019, 7, 12)# Set Start Date
self.SetEndDate(2019, 7, 16)
self.SetCash(100000) # Set Strategy Cash

self.UniverseSettings.Resolution = Resolution.Daily

self.tickers = ["AAPL", "TSLA", "FB"]

self.AddUniverse(CoarseSelection, FineSelection)


def OnData(self, data):

for ticker in self.tickers:
self.Debug("TICKER: " + ticker)

######
# univserse_selection.py

def CoarseSelection(self, coarse):
filteredCoarse = [x.Symbol for x in coarse if x.Symbol.Value in self.tickers and x.HasFundamentalData]
return filteredCoarse

def FineSelection(self, fine):
return [x.Symbol for x in fine]

 

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.


Is universe_selection in another directory or alongside main? Have you named your file correctly? Because you didn't in your comment section. I encountered import issues like yours when I had a bunch of files to import from within a library folder. I couldn't import everything under the current file's scope unless I created a file called `__init__.py` in the library folder with the following contents:

from os.path import dirname, basename, isfile, join
import glob
modules = glob.glob(join(dirname(__file__), "*.py"))
__all__ = [ basename(f)[:-3] for f in modules if isfile(f) and not f.endswith('__init__.py')]

for i in __all__:
    exec("from {0}.{1} import {1}".format(basename(dirname(__file__)), i))

This script allows you to do imports like how you want to write it (from X import *). I don't know if this will work if there are no library directories you're using and both files are in the same directory though.

0

I appreciate the response. It's just a typo in this question. My file is named correctly and it sits at the same level as main.py. I've imported other custom modules using the `from x import *` so I know the file structure works correctly.

0

Hey Brian,

You can't access self.tickers from universe_selection.py since self.tickers is a field of the QCAlgorithm class.

Also since it looks like CoarseSelection and FineSelection are not defined within any class, we can drop self from the arguments.

Let's define tickers in universe_selection.py instead so we can access it.

# In universe_selection.py

tickers = ["AAPL", "TSLA", "FB"]

def CoarseSelection(coarse):
return [x.Symbol for x in coarse if x.Symbol.Value in tickers and x.HasFundamentalData]

def FineSelection(fine):
return [x.Symbol for x in fine]

 

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