mirror of
https://github.com/wassname/catalyst.git
synced 2026-09-10 11:50:32 +08:00
ENH: Adds futures trading and asset management logic to TradingAlgorithm and performance classes
This commit is contained in:
+62
-9
@@ -31,14 +31,15 @@ except:
|
||||
PYGMENTS = False
|
||||
|
||||
import zipline
|
||||
from zipline.errors import NoSourceError, PipelineDateError
|
||||
|
||||
DEFAULTS = {
|
||||
'start': '2012-01-01',
|
||||
'end': '2012-12-31',
|
||||
'data_frequency': 'daily',
|
||||
'capital_base': '10e6',
|
||||
'source': 'yahoo',
|
||||
'symbols': 'AAPL'
|
||||
'symbols': 'AAPL',
|
||||
'metadata_index': 'symbol',
|
||||
'source_time_column': 'Date',
|
||||
}
|
||||
|
||||
|
||||
@@ -99,9 +100,12 @@ def parse_args(argv, ipython_mode=False):
|
||||
parser.add_argument('--start', '-s')
|
||||
parser.add_argument('--end', '-e')
|
||||
parser.add_argument('--capital_base')
|
||||
parser.add_argument('--source', choices=('yahoo',))
|
||||
parser.add_argument('--source', '-d')
|
||||
parser.add_argument('--source_time_column', '-t')
|
||||
parser.add_argument('--symbols')
|
||||
parser.add_argument('--output', '-o')
|
||||
parser.add_argument('--metadata_path', '-m')
|
||||
parser.add_argument('--metadata_index', '-x')
|
||||
if ipython_mode:
|
||||
parser.add_argument('--local_namespace', action='store_true')
|
||||
|
||||
@@ -153,14 +157,59 @@ def run_pipeline(print_algo=True, **kwargs):
|
||||
pygments syntax coloring if pygments is found.
|
||||
|
||||
"""
|
||||
start = pd.Timestamp(kwargs['start'], tz='UTC')
|
||||
end = pd.Timestamp(kwargs['end'], tz='UTC')
|
||||
start = kwargs['start']
|
||||
end = kwargs['end']
|
||||
# Compare against None because strings/timestamps may have been given
|
||||
if start is not None:
|
||||
start = pd.Timestamp(start, tz='UTC')
|
||||
if end is not None:
|
||||
end = pd.Timestamp(end, tz='UTC')
|
||||
|
||||
# Fail out if only one bound is provided
|
||||
if ((start is None) or (end is None)) and (start != end):
|
||||
raise PipelineDateError(start=start, end=end)
|
||||
|
||||
# Check if start and end are provided, and if the sim_params need to read
|
||||
# a start and end from the DataSource
|
||||
if start is None:
|
||||
overwrite_sim_params = True
|
||||
else:
|
||||
overwrite_sim_params = False
|
||||
|
||||
symbols = kwargs['symbols'].split(',')
|
||||
asset_identifier = kwargs['metadata_index']
|
||||
|
||||
if kwargs['source'] == 'yahoo':
|
||||
# Pull asset metadata
|
||||
asset_metadata = kwargs.get('asset_metadata', None)
|
||||
asset_metadata_path = kwargs['metadata_path']
|
||||
# Read in a CSV file, if applicable
|
||||
if asset_metadata_path is not None:
|
||||
if os.path.isfile(asset_metadata_path):
|
||||
asset_metadata = pd.read_csv(asset_metadata_path,
|
||||
index_col=asset_identifier)
|
||||
|
||||
source_arg = kwargs['source']
|
||||
source_time_column = kwargs['source_time_column']
|
||||
|
||||
if source_arg is None:
|
||||
raise NoSourceError()
|
||||
|
||||
elif source_arg == 'yahoo':
|
||||
source = zipline.data.load_bars_from_yahoo(
|
||||
stocks=symbols, start=start, end=end)
|
||||
|
||||
elif os.path.isfile(source_arg):
|
||||
source = zipline.data.load_prices_from_csv(
|
||||
filepath=source_arg,
|
||||
identifier_col=source_time_column
|
||||
)
|
||||
|
||||
elif os.path.isdir(source_arg):
|
||||
source = zipline.data.load_prices_from_csv_folder(
|
||||
folderpath=source_arg,
|
||||
identifier_col=source_time_column
|
||||
)
|
||||
|
||||
else:
|
||||
raise NotImplementedError(
|
||||
'Source %s not implemented.' % kwargs['source'])
|
||||
@@ -188,9 +237,13 @@ def run_pipeline(print_algo=True, **kwargs):
|
||||
algo = zipline.TradingAlgorithm(script=algo_text,
|
||||
namespace=kwargs.get('namespace', {}),
|
||||
capital_base=float(kwargs['capital_base']),
|
||||
algo_filename=kwargs.get('algofile'))
|
||||
algo_filename=kwargs.get('algofile'),
|
||||
asset_metadata=asset_metadata,
|
||||
identifiers=symbols,
|
||||
start=start,
|
||||
end=end)
|
||||
|
||||
perf = algo.run(source)
|
||||
perf = algo.run(source, overwrite_sim_params=overwrite_sim_params)
|
||||
|
||||
output_fname = kwargs.get('output', None)
|
||||
if output_fname is not None:
|
||||
|
||||
Reference in New Issue
Block a user