Skip to content

Commit

Permalink
ENH: Adds a new api_method called get_environment so that users may
Browse files Browse the repository at this point in the history
check if their algorithm is running in zipline or on Quantopian.
  • Loading branch information
llllllllll committed Aug 14, 2014
1 parent 4420e37 commit 7563124
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
8 changes: 8 additions & 0 deletions tests/test_algorithm.py
Expand Up @@ -53,6 +53,7 @@
SetMaxOrderCountAlgorithm,
SetMaxOrderSizeAlgorithm,
api_algo,
api_get_environment_algo,
api_symbol_algo,
call_all_order_methods,
call_order_in_init,
Expand Down Expand Up @@ -407,6 +408,13 @@ def test_api_calls_string(self):
algo = TradingAlgorithm(script=api_algo)
algo.run(self.df)

def test_api_get_environment(self):
environment = 'zipline'
algo = TradingAlgorithm(script=api_get_environment_algo,
environment=environment)
algo.run(self.df)
self.assertEqual(algo.environment, environment)

def test_api_symbol(self):
algo = TradingAlgorithm(script=api_symbol_algo)
algo.run(self.df)
Expand Down
8 changes: 8 additions & 0 deletions zipline/algorithm.py
Expand Up @@ -126,6 +126,8 @@ def __init__(self, *args, **kwargs):
How much capital to start with.
instant_fill : bool <default: False>
Whether to fill orders immediately or on next bar.
environment : str <default: 'zipline'>
The environment that this algorithm is running in.
"""
self.datetime = None

Expand All @@ -139,6 +141,8 @@ def __init__(self, *args, **kwargs):
self._recorded_vars = {}
self.namespace = kwargs.get('namespace', {})

self._environment = kwargs.pop('environment', 'zipline')

self.logger = None

self.benchmark_return_source = None
Expand Down Expand Up @@ -470,6 +474,10 @@ def add_transform(self, transform_class, tag, *args, **kwargs):
'args': args,
'kwargs': kwargs}

@api_method
def get_environment(self):
return self._environment

@api_method
def record(self, *args, **kwargs):
"""
Expand Down
10 changes: 10 additions & 0 deletions zipline/test_algorithms.py
Expand Up @@ -939,6 +939,16 @@ def handle_data(context, data):
record(incr=context.incr)
"""

api_get_environment_algo = """
from zipline.api import get_environment, order, symbol
def initialize(context):
context.environment = get_environment()
handle_data = lambda context, data: order(symbol(0), 1)
"""

api_symbol_algo = """
from zipline.api import (order,
symbol)
Expand Down

0 comments on commit 7563124

Please sign in to comment.