ENH: Implement CLI.

Add a CLI that reads in an algorithm, loads data,
run the algorithm, and output performance metrics.

The examples are adapted to the new zipline API and
analyses are split into separate files.

Also add config files that run the example
algorithms with preset settings.
This commit is contained in:
twiecki
2014-05-07 15:34:36 -04:00
parent fde40192cf
commit f9fded97ac
12 changed files with 249 additions and 285 deletions
+17 -6
View File
@@ -174,19 +174,20 @@ class TradingAlgorithm(object):
self.algoscript = kwargs.pop('script', None)
self._initialize = None
self._analyze = None
if self.algoscript is not None:
self.ns = {}
exec_(self.algoscript, self.ns)
if 'initialize' not in self.ns:
raise ValueError('You must define an initialze function.')
self._initialize = self.ns.get('initialize', None)
if 'handle_data' not in self.ns:
raise ValueError('You must define a handle_data function.')
self._initialize = self.ns['initialize']
self._handle_data = self.ns['handle_data']
else:
self._handle_data = self.ns['handle_data']
# Optional analyze function, gets called after run
self._analyze = self.ns.get('analyze', None)
# If two functions are passed in assume initialize and
# handle_data are passed in.
elif kwargs.get('initialize', False) and kwargs.get('handle_data'):
if self.algoscript is not None:
raise ValueError('You can not set script and \
@@ -194,6 +195,7 @@ class TradingAlgorithm(object):
self._initialize = kwargs.pop('initialize')
self._handle_data = kwargs.pop('handle_data')
# If method not defined, NOOP
if self._initialize is None:
self._initialize = lambda x: None
@@ -216,6 +218,13 @@ class TradingAlgorithm(object):
self._handle_data(self, data)
def analyze(self, perf):
if self._analyze is None:
return
with ZiplineAPI(self):
self._analyze(self, perf)
def __repr__(self):
"""
N.B. this does not yet represent a string that can be used
@@ -420,6 +429,8 @@ class TradingAlgorithm(object):
# convert perf dict to pandas dataframe
daily_stats = self._create_daily_stats(perfs)
self.analyze(daily_stats)
return daily_stats
def _create_daily_stats(self, perfs):