Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
how to invoke zipline strategy from another python file

Dear all,

in zipline exampe strategy, the algorithm always be trigger through following code:

if __name__ == '__main__':  
    algo = TradingAlgorithm(initialize=initialize, handle_data=handle_data, capital_base=10000)  
    data = load_from_yahoo(stocks=algo.stocks, indexes={}, start=algo.startDate, end=algo.endDate)  
    results = algo.run(data)  

How I could let the stratrgy to be running that trigger by another python file?
appreciate any suggestion, thanks a lot.

1 response

Not really all examples run the strategy on their own, 'buyapple' and 'dual_moving_average' just implements 'initialize' and 'handle_data' so you're going to run those using the run_algo.py utility.

About your question, you'd make the variables that you need in the other module available for importing, that means moving 'algo' outside the if block. In the other module you'll import 'algo' from the strategy file and call run() on it.
Python import statement follows a few rules to localize files to import according to paths in sys.paths, if you have your other module and the strategy in two different folders you may have to adjust the set of import paths.