From eccaf8d53d523693aa4fec88a94a05c345eba7f8 Mon Sep 17 00:00:00 2001 From: twiecki Date: Wed, 5 Mar 2014 12:13:06 -0500 Subject: [PATCH] ENH: Add symbol api function A symbol() lookup feature was added to Quantopian. By adding the same API function to zipline we can make copy&pasting of a zipline algo to Quantopian easier. --- tests/test_algorithm.py | 5 +++++ zipline/api.py | 9 +++++++++ zipline/test_algorithms.py | 11 +++++++++++ 3 files changed, 25 insertions(+) diff --git a/tests/test_algorithm.py b/tests/test_algorithm.py index b603b1d5..d506fe1e 100644 --- a/tests/test_algorithm.py +++ b/tests/test_algorithm.py @@ -38,6 +38,7 @@ from zipline.test_algorithms import (TestRegisterTransformAlgorithm, handle_data_api, noop_algo, api_algo, + api_symbol_algo, call_all_order_methods, record_variables, record_float_magic @@ -294,6 +295,10 @@ class TestAlgoScript(TestCase): algo = TradingAlgorithm(script=api_algo) algo.run(self.df) + def test_api_symbol(self): + algo = TradingAlgorithm(script=api_symbol_algo) + algo.run(self.df) + def test_fixed_slippage(self): # verify order -> transaction -> portfolio position. # -------------- diff --git a/zipline/api.py b/zipline/api.py index bc012ab0..9586741e 100644 --- a/zipline/api.py +++ b/zipline/api.py @@ -29,6 +29,15 @@ from zipline.finance.slippage import ( batch_transform = zipline.transforms.BatchTransform + +def symbol(symbol_str, as_of_date=None): + """Default symbol lookup for any source that directly maps the + symbol to the identifier (e.g. yahoo finance). + + Keyword argument as_of_date is ignored. + """ + return symbol_str + __all__ = [ 'slippage', 'commission', diff --git a/zipline/test_algorithms.py b/zipline/test_algorithms.py index 3b74d70f..70b0c8ae 100644 --- a/zipline/test_algorithms.py +++ b/zipline/test_algorithms.py @@ -745,6 +745,17 @@ def handle_data(context, data): record(incr=context.incr) """ +api_symbol_algo = """ +from zipline.api import (order, + symbol) + +def initialize(context): + pass + +def handle_data(context, data): + order(symbol(0), 1) +""" + call_all_order_methods = """ from zipline.api import (order, order_value,