<s> from collections import namedtuple <EOL> import datetime <EOL> from datetime import timedelta <EOL> from textwrap import dedent <EOL> from unittest import TestCase , skip <EOL> import logbook <EOL> from logbook import TestHandler , WARNING <EOL> from mock import MagicMock <EOL> from nose_parameterized import parameterized <EOL> from six import iteritems , itervalues <EOL> from six . moves import range <EOL> from testfixtures import TempDirectory <EOL> import numpy as np <EOL> import pandas as pd <EOL> import pytz <EOL> from toolz import merge <EOL> from zipline import TradingAlgorithm <EOL> from zipline . api import FixedSlippage <EOL> from zipline . assets import Equity , Future <EOL> from zipline . assets . synthetic import ( <EOL> make_jagged_equity_info , <EOL> make_simple_equity_info , <EOL> ) <EOL> from zipline . data . data_portal import DataPortal <EOL> from zipline . data . minute_bars import ( <EOL> BcolzMinuteBarReader , <EOL> BcolzMinuteBarWriter , <EOL> US_EQUITIES_MINUTES_PER_DAY , <EOL> ) <EOL> from zipline . data . us_equity_pricing import ( <EOL> BcolzDailyBarReader , <EOL> BcolzDailyBarWriter , <EOL> ) <EOL> from zipline . errors import ( <EOL> OrderDuringInitialize , <EOL> RegisterTradingControlPostInit , <EOL> TradingControlViolation , <EOL> AccountControlViolation , <EOL> SymbolNotFound , <EOL> RootSymbolNotFound , <EOL> UnsupportedDatetimeFormat , <EOL> CannotOrderDelistedAsset , <EOL> SetCancelPolicyPostInit , <EOL> UnsupportedCancelPolicy , <EOL> OrderInBeforeTradingStart ) <EOL> from zipline . api import ( <EOL> order , <EOL> order_value , <EOL> order_percent , <EOL> order_target , <EOL> order_target_value , <EOL> order_target_percent <EOL> ) <EOL> from zipline . finance . commission import PerShare <EOL> from zipline . finance . execution import LimitOrder <EOL> from zipline . finance . order import ORDER_STATUS <EOL> from zipline . finance . trading import TradingEnvironment , SimulationParameters <EOL> from zipline . sources import DataPanelSource <EOL> from zipline . testing import ( <EOL> FakeDataPortal , <EOL> create_daily_df_for_asset , <EOL> create_data_portal , <EOL> create_data_portal_from_trade_history , <EOL> create_minute_df_for_asset , <EOL> empty_trading_env , <EOL> make_test_handler , <EOL> make_trade_data_for_asset_info , <EOL> parameter_space , <EOL> str_to_seconds , <EOL> tmp_trading_env , <EOL> to_utc , <EOL> trades_by_sid_to_dfs , <EOL> ) <EOL> from zipline . testing . fixtures import ( <EOL> WithDataPortal , <EOL> WithLogger , <EOL> WithSimParams , <EOL> WithTradingEnvironment , <EOL> WithTmpDir , <EOL> ZiplineTestCase , <EOL> ) <EOL> from zipline . test_algorithms import ( <EOL> access_account_in_init , <EOL> access_portfolio_in_init , <EOL> AmbitiousStopLimitAlgorithm , <EOL> EmptyPositionsAlgorithm , <EOL> InvalidOrderAlgorithm , <EOL> RecordAlgorithm , <EOL> FutureFlipAlgo , <EOL> TestOrderAlgorithm , <EOL> TestOrderPercentAlgorithm , <EOL> TestOrderStyleForwardingAlgorithm , <EOL> TestOrderValueAlgorithm , <EOL> TestRegisterTransformAlgorithm , <EOL> TestTargetAlgorithm , <EOL> TestTargetPercentAlgorithm , <EOL> TestTargetValueAlgorithm , <EOL> SetLongOnlyAlgorithm , <EOL> SetAssetDateBoundsAlgorithm , <EOL> SetMaxPositionSizeAlgorithm , <EOL> SetMaxOrderCountAlgorithm , <EOL> SetMaxOrderSizeAlgorithm , <EOL> SetDoNotOrderListAlgorithm , <EOL> SetMaxLeverageAlgorithm , <EOL> api_algo , <EOL> api_get_environment_algo , <EOL> api_symbol_algo , <EOL> call_all_order_methods , <EOL> call_order_in_init , <EOL> handle_data_api , <EOL> handle_data_noop , <EOL> initialize_api , <EOL> initialize_noop , <EOL> noop_algo , <EOL> record_float_magic , <EOL> record_variables , <EOL> call_with_kwargs , <EOL> call_without_kwargs , <EOL> call_with_bad_kwargs_current , <EOL> call_with_bad_kwargs_history , <EOL> bad_type_history_assets , <EOL> bad_type_history_fields , <EOL> bad_type_history_bar_count , <EOL> bad_type_history_frequency , <EOL> bad_type_history_assets_kwarg_list , <EOL> bad_type_current_assets , <EOL> bad_type_current_fields , <EOL> bad_type_can_trade_assets , <EOL> bad_type_is_stale_assets , <EOL> bad_type_history_assets_kwarg , <EOL> bad_type_history_fields_kwarg , <EOL> bad_type_history_bar_count_kwarg , <EOL> bad_type_history_frequency_kwarg , <EOL> bad_type_current_assets_kwarg , <EOL> bad_type_current_fields_kwarg , <EOL> call_with_bad_kwargs_get_open_orders , <EOL> call_with_good_kwargs_get_open_orders , <EOL> call_with_no_kwargs_get_open_orders , <EOL> empty_positions , <EOL> set_benchmark_algo , <EOL> no_handle_data , <EOL> ) <EOL> from zipline . utils . api_support import ZiplineAPI , set_algo_instance <EOL> from zipline . utils . context_tricks import CallbackManager <EOL> from zipline . utils . control_flow import nullctx <EOL> import zipline . utils . events <EOL> from zipline . utils . events import DateRuleFactory , TimeRuleFactory , Always <EOL> import zipline . utils . factory as factory <EOL> from zipline . utils . tradingcalendar import trading_day , trading_days <EOL> _multiprocess_can_split_ = False <EOL> class TestRecordAlgorithm ( WithSimParams , WithDataPortal , ZiplineTestCase ) : <EOL> ASSET_FINDER_EQUITY_SIDS = <NUM_LIT> , <EOL> def test_record_incr ( self ) : <EOL> algo = RecordAlgorithm ( sim_params = self . sim_params , env = self . env ) <EOL> output = algo . run ( self . data_portal ) <EOL> np . testing . assert_array_equal ( output [ '<STR_LIT>' ] . values , <EOL> range ( <NUM_LIT:1> , len ( output ) + <NUM_LIT:1> ) ) <EOL> np . testing . assert_array_equal ( output [ '<STR_LIT:name>' ] . values , <EOL> range ( <NUM_LIT:1> , len ( output ) + <NUM_LIT:1> ) ) <EOL> np . testing . assert_array_equal ( output [ '<STR_LIT>' ] . values , <EOL> [ <NUM_LIT:2> ] * len ( output ) ) <EOL> np . testing . assert_array_equal ( output [ '<STR_LIT>' ] . values , <EOL> range ( <NUM_LIT:1> , len ( output ) + <NUM_LIT:1> ) ) <EOL> class TestMiscellaneousAPI ( WithLogger , <EOL> WithSimParams , <EOL> WithDataPortal , <EOL> ZiplineTestCase ) : <EOL> SIM_PARAMS_DATA_FREQUENCY = '<STR_LIT>' <EOL> sids = <NUM_LIT:1> , <NUM_LIT:2> <EOL> @ classmethod <EOL> def make_equity_info ( cls ) : <EOL> return pd . concat ( ( <EOL> make_simple_equity_info ( cls . sids , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> pd . DataFrame . from_dict ( <EOL> { <NUM_LIT:3> : { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } , <EOL> <NUM_LIT:4> : { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } } , <EOL> orient = '<STR_LIT:index>' , <EOL> ) , <EOL> ) ) <EOL> @ classmethod <EOL> def make_futures_info ( cls ) : <EOL> return pd . DataFrame . from_dict ( <EOL> { <EOL> <NUM_LIT:5> : { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : pd . Timestamp ( '<STR_LIT>' , tz = '<STR_LIT>' ) , <EOL> '<STR_LIT>' : pd . Timestamp ( '<STR_LIT>' , tz = '<STR_LIT>' ) , <EOL> '<STR_LIT>' : pd . Timestamp ( '<STR_LIT>' , tz = '<STR_LIT>' ) } , <EOL> <NUM_LIT:6> : { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : pd . Timestamp ( '<STR_LIT>' , tz = '<STR_LIT>' ) , <EOL> '<STR_LIT>' : pd . Timestamp ( '<STR_LIT>' , tz = '<STR_LIT>' ) , <EOL> '<STR_LIT>' : pd . Timestamp ( '<STR_LIT>' , tz = '<STR_LIT>' ) } , <EOL> <NUM_LIT:7> : { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : pd . Timestamp ( '<STR_LIT>' , tz = '<STR_LIT>' ) , <EOL> '<STR_LIT>' : pd . Timestamp ( '<STR_LIT>' , tz = '<STR_LIT>' ) , <EOL> '<STR_LIT>' : pd . Timestamp ( '<STR_LIT>' , tz = '<STR_LIT>' ) } , <EOL> <NUM_LIT:8> : { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : pd . Timestamp ( '<STR_LIT>' , tz = '<STR_LIT>' ) , <EOL> '<STR_LIT>' : pd . Timestamp ( '<STR_LIT>' , tz = '<STR_LIT>' ) , <EOL> '<STR_LIT>' : pd . Timestamp ( '<STR_LIT>' , tz = '<STR_LIT>' ) } <EOL> } , <EOL> orient = '<STR_LIT:index>' , <EOL> ) <EOL> def test_cancel_policy_outside_init ( self ) : <EOL> code = """<STR_LIT>""" <EOL> algo = TradingAlgorithm ( script = code , <EOL> sim_params = self . sim_params , <EOL> env = self . env ) <EOL> with self . assertRaises ( SetCancelPolicyPostInit ) : <EOL> algo . run ( self . data_portal ) <EOL> def test_cancel_policy_invalid_param ( self ) : <EOL> code = """<STR_LIT>""" <EOL> algo = TradingAlgorithm ( script = code , <EOL> sim_params = self . sim_params , <EOL> env = self . env ) <EOL> with self . assertRaises ( UnsupportedCancelPolicy ) : <EOL> algo . run ( self . data_portal ) <EOL> def test_zipline_api_resolves_dynamically ( self ) : <EOL> algo = TradingAlgorithm ( <EOL> initialize = lambda context : None , <EOL> handle_data = lambda context , data : None , <EOL> sim_params = self . sim_params , <EOL> ) <EOL> for method in algo . all_api_methods ( ) : <EOL> name = method . __name__ <EOL> sentinel = object ( ) <EOL> def fake_method ( * args , ** kwargs ) : <EOL> return sentinel <EOL> setattr ( algo , name , fake_method ) <EOL> with ZiplineAPI ( algo ) : <EOL> self . assertIs ( sentinel , getattr ( zipline . api , name ) ( ) ) <EOL> def test_sid_datetime ( self ) : <EOL> algo_text = """<STR_LIT>""" <EOL> algo = TradingAlgorithm ( script = algo_text , <EOL> sim_params = self . sim_params , <EOL> env = self . env ) <EOL> algo . namespace [ '<STR_LIT>' ] = self . assertEqual <EOL> algo . run ( self . data_portal ) <EOL> def test_get_environment ( self ) : <EOL> expected_env = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:start>' : pd . Timestamp ( '<STR_LIT>' , tz = '<STR_LIT>' ) , <EOL> '<STR_LIT:end>' : pd . Timestamp ( '<STR_LIT>' , tz = '<STR_LIT>' ) , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> def initialize ( algo ) : <EOL> self . assertEqual ( '<STR_LIT>' , algo . get_environment ( ) ) <EOL> self . assertEqual ( expected_env , algo . get_environment ( '<STR_LIT:*>' ) ) <EOL> def handle_data ( algo , data ) : <EOL> pass <EOL> algo = TradingAlgorithm ( initialize = initialize , <EOL> handle_data = handle_data , <EOL> sim_params = self . sim_params , <EOL> env = self . env ) <EOL> algo . run ( self . data_portal ) <EOL> def test_get_open_orders ( self ) : <EOL> def initialize ( algo ) : <EOL> algo . minute = <NUM_LIT:0> <EOL> def handle_data ( algo , data ) : <EOL> if algo . minute == <NUM_LIT:0> : <EOL> algo . order ( algo . sid ( <NUM_LIT:1> ) , <NUM_LIT:1> ) <EOL> algo . order ( algo . sid ( <NUM_LIT:2> ) , <NUM_LIT:1> , style = LimitOrder ( <NUM_LIT> ) ) <EOL> algo . order ( algo . sid ( <NUM_LIT:2> ) , <NUM_LIT:1> , style = LimitOrder ( <NUM_LIT> ) ) <EOL> algo . order ( algo . sid ( <NUM_LIT:2> ) , <NUM_LIT:1> , style = LimitOrder ( <NUM_LIT> ) ) <EOL> all_orders = algo . get_open_orders ( ) <EOL> self . assertEqual ( list ( all_orders . keys ( ) ) , [ <NUM_LIT:1> , <NUM_LIT:2> ] ) <EOL> self . assertEqual ( all_orders [ <NUM_LIT:1> ] , algo . get_open_orders ( <NUM_LIT:1> ) ) <EOL> self . assertEqual ( len ( all_orders [ <NUM_LIT:1> ] ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( all_orders [ <NUM_LIT:2> ] , algo . get_open_orders ( <NUM_LIT:2> ) ) <EOL> self . assertEqual ( len ( all_orders [ <NUM_LIT:2> ] ) , <NUM_LIT:3> ) <EOL> if algo . minute == <NUM_LIT:1> : <EOL> all_orders = algo . get_open_orders ( ) <EOL> self . assertEqual ( list ( all_orders . keys ( ) ) , [ <NUM_LIT:2> ] ) <EOL> self . assertEqual ( [ ] , algo . get_open_orders ( <NUM_LIT:1> ) ) <EOL> orders_2 = algo . get_open_orders ( <NUM_LIT:2> ) <EOL> self . assertEqual ( all_orders [ <NUM_LIT:2> ] , orders_2 ) <EOL> self . assertEqual ( len ( all_orders [ <NUM_LIT:2> ] ) , <NUM_LIT:3> ) <EOL> for order_ in orders_2 : <EOL> algo . cancel_order ( order_ ) <EOL> all_orders = algo . get_open_orders ( ) <EOL> self . assertEqual ( all_orders , { } ) <EOL> algo . minute += <NUM_LIT:1> <EOL> algo = TradingAlgorithm ( initialize = initialize , <EOL> handle_data = handle_data , <EOL> sim_params = self . sim_params , <EOL> env = self . env ) <EOL> algo . run ( self . data_portal ) <EOL> def test_schedule_function ( self ) : <EOL> date_rules = DateRuleFactory <EOL> time_rules = TimeRuleFactory <EOL> us_eastern = pytz . timezone ( '<STR_LIT>' ) <EOL> def incrementer ( algo , data ) : <EOL> algo . func_called += <NUM_LIT:1> <EOL> curdt = algo . get_datetime ( ) . tz_convert ( pytz . utc ) <EOL> self . assertEqual ( <EOL> curdt , <EOL> us_eastern . localize ( <EOL> datetime . datetime . combine ( <EOL> curdt . date ( ) , <EOL> datetime . time ( <NUM_LIT:9> , <NUM_LIT> ) <EOL> ) , <EOL> ) , <EOL> ) <EOL> def initialize ( algo ) : <EOL> algo . func_called = <NUM_LIT:0> <EOL> algo . days = <NUM_LIT:1> <EOL> algo . date = None <EOL> algo . schedule_function ( <EOL> func = incrementer , <EOL> date_rule = date_rules . every_day ( ) , <EOL> time_rule = time_rules . market_open ( ) , <EOL> ) <EOL> def handle_data ( algo , data ) : <EOL> if not algo . date : <EOL> algo . date = algo . get_datetime ( ) . date ( ) <EOL> if algo . date < algo . get_datetime ( ) . date ( ) : <EOL> algo . days += <NUM_LIT:1> <EOL> algo . date = algo . get_datetime ( ) . date ( ) <EOL> algo = TradingAlgorithm ( <EOL> initialize = initialize , <EOL> handle_data = handle_data , <EOL> sim_params = self . sim_params , <EOL> env = self . env , <EOL> ) <EOL> algo . run ( self . data_portal ) <EOL> self . assertEqual ( algo . func_called , algo . days ) <EOL> def test_event_context ( self ) : <EOL> expected_data = [ ] <EOL> collected_data_pre = [ ] <EOL> collected_data_post = [ ] <EOL> function_stack = [ ] <EOL> def pre ( data ) : <EOL> function_stack . append ( pre ) <EOL> collected_data_pre . append ( data ) <EOL> def post ( data ) : <EOL> function_stack . append ( post ) <EOL> collected_data_post . append ( data ) <EOL> def initialize ( context ) : <EOL> context . add_event ( Always ( ) , f ) <EOL> context . add_event ( Always ( ) , g ) <EOL> def handle_data ( context , data ) : <EOL> function_stack . append ( handle_data ) <EOL> expected_data . append ( data ) <EOL> def f ( context , data ) : <EOL> function_stack . append ( f ) <EOL> def g ( context , data ) : <EOL> function_stack . append ( g ) <EOL> algo = TradingAlgorithm ( <EOL> initialize = initialize , <EOL> handle_data = handle_data , <EOL> sim_params = self . sim_params , <EOL> create_event_context = CallbackManager ( pre , post ) , <EOL> env = self . env , <EOL> ) <EOL> algo . run ( self . data_portal ) <EOL> self . assertEqual ( len ( expected_data ) , <NUM_LIT> ) <EOL> self . assertEqual ( collected_data_pre , expected_data ) <EOL> self . assertEqual ( collected_data_post , expected_data ) <EOL> self . assertEqual ( <EOL> len ( function_stack ) , <EOL> <NUM_LIT> * <NUM_LIT:5> , <EOL> '<STR_LIT>' % <EOL> len ( function_stack ) , <EOL> ) <EOL> expected_functions = [ pre , handle_data , f , g , post ] * <NUM_LIT> <EOL> for n , ( f , g ) in enumerate ( zip ( function_stack , expected_functions ) ) : <EOL> self . assertEqual ( <EOL> f , <EOL> g , <EOL> '<STR_LIT>' <EOL> % ( n , g . __name__ , f . __name__ ) , <EOL> ) <EOL> @ parameterized . expand ( [ <EOL> ( '<STR_LIT>' , ) , <EOL> ( '<STR_LIT>' ) , <EOL> ] ) <EOL> def test_schedule_function_rule_creation ( self , mode ) : <EOL> def nop ( * args , ** kwargs ) : <EOL> return None <EOL> self . sim_params . data_frequency = mode <EOL> algo = TradingAlgorithm ( <EOL> initialize = nop , <EOL> handle_data = nop , <EOL> sim_params = self . sim_params , <EOL> env = self . env , <EOL> ) <EOL> algo . schedule_function ( nop , time_rule = zipline . utils . events . Never ( ) ) <EOL> event_rule = algo . event_manager . _events [ <NUM_LIT:1> ] . rule <EOL> self . assertIsInstance ( event_rule , zipline . utils . events . OncePerDay ) <EOL> inner_rule = event_rule . rule <EOL> self . assertIsInstance ( inner_rule , zipline . utils . events . ComposedRule ) <EOL> first = inner_rule . first <EOL> second = inner_rule . second <EOL> composer = inner_rule . composer <EOL> self . assertIsInstance ( first , zipline . utils . events . Always ) <EOL> if mode == '<STR_LIT>' : <EOL> self . assertIsInstance ( second , zipline . utils . events . Always ) <EOL> else : <EOL> self . assertIsInstance ( second , zipline . utils . events . Never ) <EOL> self . assertIs ( composer , zipline . utils . events . ComposedRule . lazy_and ) <EOL> def test_asset_lookup ( self ) : <EOL> algo = TradingAlgorithm ( env = self . env ) <EOL> algo . sim_params . period_end = pd . Timestamp ( '<STR_LIT>' , tz = '<STR_LIT>' ) <EOL> with self . assertRaises ( SymbolNotFound ) : <EOL> algo . symbol ( '<STR_LIT>' ) <EOL> with self . assertRaises ( SymbolNotFound ) : <EOL> algo . symbols ( '<STR_LIT>' ) <EOL> algo . sim_params . period_end = pd . Timestamp ( '<STR_LIT>' , tz = '<STR_LIT>' ) <EOL> list_result = algo . symbols ( '<STR_LIT>' ) <EOL> self . assertEqual ( <NUM_LIT:3> , list_result [ <NUM_LIT:0> ] ) <EOL> algo . sim_params . period_end = pd . Timestamp ( '<STR_LIT>' , tz = '<STR_LIT>' ) <EOL> self . assertEqual ( <NUM_LIT:3> , algo . symbol ( '<STR_LIT>' ) ) <EOL> algo . sim_params . period_end = pd . Timestamp ( '<STR_LIT>' , tz = '<STR_LIT>' ) <EOL> self . assertEqual ( <NUM_LIT:4> , algo . symbol ( '<STR_LIT>' ) ) <EOL> algo . sim_params . period_end = pd . Timestamp ( '<STR_LIT>' , tz = '<STR_LIT>' ) <EOL> self . assertEqual ( <NUM_LIT:4> , algo . symbol ( '<STR_LIT>' ) ) <EOL> list_result = algo . symbols ( '<STR_LIT>' ) <EOL> self . assertEqual ( <NUM_LIT:4> , list_result [ <NUM_LIT:0> ] ) <EOL> self . assertIsInstance ( algo . sid ( <NUM_LIT:3> ) , Equity ) <EOL> self . assertIsInstance ( algo . sid ( <NUM_LIT:4> ) , Equity ) <EOL> with self . assertRaises ( TypeError ) : <EOL> algo . symbol ( <NUM_LIT:1> ) <EOL> with self . assertRaises ( TypeError ) : <EOL> algo . symbol ( ( <NUM_LIT:1> , ) ) <EOL> with self . assertRaises ( TypeError ) : <EOL> algo . symbol ( { <NUM_LIT:1> } ) <EOL> with self . assertRaises ( TypeError ) : <EOL> algo . symbol ( [ <NUM_LIT:1> ] ) <EOL> with self . assertRaises ( TypeError ) : <EOL> algo . symbol ( { '<STR_LIT:foo>' : '<STR_LIT:bar>' } ) <EOL> def test_future_symbol ( self ) : <EOL> """<STR_LIT>""" <EOL> algo = TradingAlgorithm ( env = self . env ) <EOL> algo . datetime = pd . Timestamp ( '<STR_LIT>' , tz = '<STR_LIT>' ) <EOL> cl = algo . future_symbol ( '<STR_LIT>' ) <EOL> self . assertEqual ( cl . sid , <NUM_LIT:5> ) <EOL> self . assertEqual ( cl . symbol , '<STR_LIT>' ) <EOL> self . assertEqual ( cl . root_symbol , '<STR_LIT>' ) <EOL> self . assertEqual ( cl . start_date , pd . Timestamp ( '<STR_LIT>' , tz = '<STR_LIT>' ) ) <EOL> self . assertEqual ( cl . notice_date , pd . Timestamp ( '<STR_LIT>' , tz = '<STR_LIT>' ) ) <EOL> self . assertEqual ( cl . expiration_date , <EOL> pd . Timestamp ( '<STR_LIT>' , tz = '<STR_LIT>' ) ) <EOL> with self . assertRaises ( SymbolNotFound ) : <EOL> algo . future_symbol ( '<STR_LIT>' ) <EOL> with self . assertRaises ( SymbolNotFound ) : <EOL> algo . future_symbol ( '<STR_LIT>' ) <EOL> with self . assertRaises ( SymbolNotFound ) : <EOL> algo . future_symbol ( '<STR_LIT>' ) <EOL> with self . assertRaises ( TypeError ) : <EOL> algo . future_symbol ( <NUM_LIT:1> ) <EOL> with self . assertRaises ( TypeError ) : <EOL> algo . future_symbol ( ( <NUM_LIT:1> , ) ) <EOL> with self . assertRaises ( TypeError ) : <EOL> algo . future_symbol ( { <NUM_LIT:1> } ) <EOL> with self . assertRaises ( TypeError ) : <EOL> algo . future_symbol ( [ <NUM_LIT:1> ] ) <EOL> with self . assertRaises ( TypeError ) : <EOL> algo . future_symbol ( { '<STR_LIT:foo>' : '<STR_LIT:bar>' } ) <EOL> def test_future_chain ( self ) : <EOL> """<STR_LIT>""" <EOL> algo = TradingAlgorithm ( env = self . env ) <EOL> algo . datetime = pd . Timestamp ( '<STR_LIT>' , tz = '<STR_LIT>' ) <EOL> cl = algo . future_chain ( '<STR_LIT>' ) <EOL> self . assertEqual ( cl . root_symbol , '<STR_LIT>' ) <EOL> self . assertEqual ( cl . as_of_date , algo . datetime ) <EOL> as_of_date = pd . Timestamp ( '<STR_LIT>' , tz = '<STR_LIT>' ) <EOL> cl = algo . future_chain ( '<STR_LIT>' , as_of_date = as_of_date ) <EOL> self . assertEqual ( cl . root_symbol , '<STR_LIT>' ) <EOL> self . assertEqual ( cl . as_of_date , as_of_date ) <EOL> cl = algo . future_chain ( '<STR_LIT>' , as_of_date = '<STR_LIT>' ) <EOL> self . assertEqual ( cl . root_symbol , '<STR_LIT>' ) <EOL> self . assertEqual ( cl . as_of_date , as_of_date ) <EOL> cl = algo . future_chain ( '<STR_LIT>' ) <EOL> self . assertEqual ( cl . root_symbol , '<STR_LIT>' ) <EOL> cl = algo . future_chain ( '<STR_LIT>' ) <EOL> self . assertEqual ( cl . root_symbol , '<STR_LIT>' ) <EOL> with self . assertRaises ( RootSymbolNotFound ) : <EOL> algo . future_chain ( '<STR_LIT>' ) <EOL> with self . assertRaises ( RootSymbolNotFound ) : <EOL> algo . future_chain ( '<STR_LIT>' ) <EOL> with self . assertRaises ( UnsupportedDatetimeFormat ) : <EOL> algo . future_chain ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> with self . assertRaises ( UnsupportedDatetimeFormat ) : <EOL> algo . future_chain ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> with self . assertRaises ( TypeError ) : <EOL> algo . future_chain ( <NUM_LIT:1> ) <EOL> with self . assertRaises ( TypeError ) : <EOL> algo . future_chain ( ( <NUM_LIT:1> , ) ) <EOL> with self . assertRaises ( TypeError ) : <EOL> algo . future_chain ( { <NUM_LIT:1> } ) <EOL> with self . assertRaises ( TypeError ) : <EOL> algo . future_chain ( [ <NUM_LIT:1> ] ) <EOL> with self . assertRaises ( TypeError ) : <EOL> algo . future_chain ( { '<STR_LIT:foo>' : '<STR_LIT:bar>' } ) <EOL> def test_set_symbol_lookup_date ( self ) : <EOL> """<STR_LIT>""" <EOL> dates = pd . date_range ( '<STR_LIT>' , freq = '<STR_LIT>' , periods = <NUM_LIT:2> , tz = '<STR_LIT>' ) <EOL> metadata = pd . DataFrame . from_records ( <EOL> [ <EOL> { <EOL> '<STR_LIT>' : i + <NUM_LIT:3> , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : date . value , <EOL> '<STR_LIT>' : ( date + timedelta ( days = <NUM_LIT:1> ) ) . value , <EOL> } <EOL> for i , date in enumerate ( dates ) <EOL> ] <EOL> ) <EOL> with tmp_trading_env ( equities = metadata ) as env : <EOL> algo = TradingAlgorithm ( env = env ) <EOL> algo . sim_params . period_end = pd . Timestamp ( '<STR_LIT>' , tz = '<STR_LIT>' ) <EOL> result = algo . symbol ( '<STR_LIT>' ) <EOL> self . assertEqual ( result . symbol , '<STR_LIT>' ) <EOL> for i , date in enumerate ( dates ) : <EOL> algo . set_symbol_lookup_date ( date ) <EOL> result = algo . symbol ( '<STR_LIT>' ) <EOL> self . assertEqual ( result . symbol , '<STR_LIT>' ) <EOL> self . assertEqual ( result . sid , i + <NUM_LIT:3> ) <EOL> with self . assertRaises ( UnsupportedDatetimeFormat ) : <EOL> algo . set_symbol_lookup_date ( '<STR_LIT>' ) <EOL> class TestTransformAlgorithm ( WithLogger , <EOL> WithDataPortal , <EOL> WithSimParams , <EOL> ZiplineTestCase ) : <EOL> START_DATE = pd . Timestamp ( '<STR_LIT>' , tz = '<STR_LIT>' ) <EOL> END_DATE = pd . Timestamp ( '<STR_LIT>' , tz = '<STR_LIT>' ) <EOL> sids = ASSET_FINDER_EQUITY_SIDS = [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT> ] <EOL> @ classmethod <EOL> def make_futures_info ( cls ) : <EOL> return pd . DataFrame . from_dict ( { <NUM_LIT:3> : { '<STR_LIT>' : <NUM_LIT:10> } } , '<STR_LIT:index>' ) <EOL> @ classmethod <EOL> def make_daily_bar_data ( cls ) : <EOL> return trades_by_sid_to_dfs ( <EOL> { <EOL> sid : factory . create_trade_history ( <EOL> sid , <EOL> [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT:100> , <NUM_LIT:100> , <NUM_LIT:100> , <NUM_LIT> ] , <EOL> timedelta ( days = <NUM_LIT:1> ) , <EOL> cls . sim_params , <EOL> cls . env <EOL> ) for sid in cls . sids <EOL> } , <EOL> index = cls . sim_params . trading_days , <EOL> ) <EOL> @ classmethod <EOL> def init_class_fixtures ( cls ) : <EOL> super ( TestTransformAlgorithm , cls ) . init_class_fixtures ( ) <EOL> cls . futures_env = cls . enter_class_context ( <EOL> tmp_trading_env ( futures = cls . make_futures_info ( ) ) , <EOL> ) <EOL> def test_invalid_order_parameters ( self ) : <EOL> algo = InvalidOrderAlgorithm ( <EOL> sids = [ <NUM_LIT> ] , <EOL> sim_params = self . sim_params , <EOL> env = self . env , <EOL> ) <EOL> algo . run ( self . data_portal ) <EOL> @ parameterized . expand ( [ <EOL> ( order , <NUM_LIT:1> ) , <EOL> ( order_value , <NUM_LIT:1000> ) , <EOL> ( order_target , <NUM_LIT:1> ) , <EOL> ( order_target_value , <NUM_LIT:1000> ) , <EOL> ( order_percent , <NUM_LIT:1> ) , <EOL> ( order_target_percent , <NUM_LIT:1> ) , <EOL> ] ) <EOL> def test_cannot_order_in_before_trading_start ( self , order_method , amount ) : <EOL> algotext = """<STR_LIT>""" . format ( order_func = order_method . __name__ , arg = amount ) <EOL> algo = TradingAlgorithm ( script = algotext , sim_params = self . sim_params , <EOL> data_frequency = '<STR_LIT>' , env = self . env ) <EOL> with self . assertRaises ( OrderInBeforeTradingStart ) : <EOL> algo . run ( self . data_portal ) <EOL> def test_run_twice ( self ) : <EOL> algo1 = TestRegisterTransformAlgorithm ( <EOL> sim_params = self . sim_params , <EOL> sids = [ <NUM_LIT:0> , <NUM_LIT:1> ] <EOL> ) <EOL> res1 = algo1 . run ( self . data_portal ) <EOL> algo2 = TestRegisterTransformAlgorithm ( <EOL> sim_params = self . sim_params , <EOL> sids = [ <NUM_LIT:0> , <NUM_LIT:1> ] <EOL> ) <EOL> res2 = algo2 . run ( self . data_portal ) <EOL> res1 = res1 . fillna ( method = '<STR_LIT>' ) <EOL> res2 = res2 . fillna ( method = '<STR_LIT>' ) <EOL> np . testing . assert_array_equal ( res1 , res2 ) <EOL> def test_data_frequency_setting ( self ) : <EOL> self . sim_params . data_frequency = '<STR_LIT>' <EOL> sim_params = factory . create_simulation_parameters ( <EOL> num_days = <NUM_LIT:4> , env = self . env , data_frequency = '<STR_LIT>' ) <EOL> algo = TestRegisterTransformAlgorithm ( <EOL> sim_params = sim_params , <EOL> env = self . env , <EOL> ) <EOL> self . assertEqual ( algo . sim_params . data_frequency , '<STR_LIT>' ) <EOL> sim_params = factory . create_simulation_parameters ( <EOL> num_days = <NUM_LIT:4> , env = self . env , data_frequency = '<STR_LIT>' ) <EOL> algo = TestRegisterTransformAlgorithm ( <EOL> sim_params = sim_params , <EOL> env = self . env , <EOL> ) <EOL> self . assertEqual ( algo . sim_params . data_frequency , '<STR_LIT>' ) <EOL> @ parameterized . expand ( [ <EOL> ( TestOrderAlgorithm , ) , <EOL> ( TestOrderValueAlgorithm , ) , <EOL> ( TestTargetAlgorithm , ) , <EOL> ( TestOrderPercentAlgorithm , ) , <EOL> ( TestTargetPercentAlgorithm , ) , <EOL> ( TestTargetValueAlgorithm , ) , <EOL> ] ) <EOL> def test_order_methods ( self , algo_class ) : <EOL> algo = algo_class ( <EOL> sim_params = self . sim_params , <EOL> env = self . env , <EOL> ) <EOL> asset_to_test = algo . sid ( <NUM_LIT:0> ) <EOL> self . assertIsInstance ( asset_to_test , Equity ) <EOL> algo . run ( self . data_portal ) <EOL> @ parameterized . expand ( [ <EOL> ( TestOrderAlgorithm , ) , <EOL> ( TestOrderValueAlgorithm , ) , <EOL> ( TestTargetAlgorithm , ) , <EOL> ( TestOrderPercentAlgorithm , ) , <EOL> ( TestTargetValueAlgorithm , ) , <EOL> ] ) <EOL> def test_order_methods_for_future ( self , algo_class ) : <EOL> algo = algo_class ( <EOL> sim_params = self . sim_params , <EOL> env = self . env , <EOL> ) <EOL> asset_to_test = algo . sid ( <NUM_LIT:3> ) <EOL> self . assertIsInstance ( asset_to_test , Future ) <EOL> algo . run ( self . data_portal ) <EOL> @ parameterized . expand ( [ <EOL> ( "<STR_LIT>" , ) , <EOL> ( "<STR_LIT>" , ) , <EOL> ( "<STR_LIT>" , ) , <EOL> ( "<STR_LIT>" , ) , <EOL> ( "<STR_LIT>" , ) , <EOL> ( "<STR_LIT>" , ) , <EOL> ] ) <EOL> def test_order_method_style_forwarding ( self , order_style ) : <EOL> algo = TestOrderStyleForwardingAlgorithm ( <EOL> sim_params = self . sim_params , <EOL> method_name = order_style , <EOL> env = self . env <EOL> ) <EOL> algo . run ( self . data_portal ) <EOL> def test_order_on_each_day_of_asset_lifetime ( self ) : <EOL> algo_code = dedent ( """<STR_LIT>""" ) <EOL> asset133 = self . env . asset_finder . retrieve_asset ( <NUM_LIT> ) <EOL> sim_params = SimulationParameters ( <EOL> period_start = asset133 . start_date , <EOL> period_end = asset133 . end_date , <EOL> data_frequency = "<STR_LIT>" <EOL> ) <EOL> algo = TradingAlgorithm ( <EOL> script = algo_code , <EOL> sim_params = sim_params , <EOL> env = self . env <EOL> ) <EOL> results = algo . run ( FakeDataPortal ( self . env ) ) <EOL> for orders_for_day in results . orders : <EOL> self . assertEqual ( <NUM_LIT:1> , len ( orders_for_day ) ) <EOL> self . assertEqual ( orders_for_day [ <NUM_LIT:0> ] [ "<STR_LIT:status>" ] , ORDER_STATUS . FILLED ) <EOL> for txns_for_day in results . transactions : <EOL> self . assertEqual ( <NUM_LIT:1> , len ( txns_for_day ) ) <EOL> self . assertEqual ( <NUM_LIT:1> , txns_for_day [ <NUM_LIT:0> ] [ "<STR_LIT>" ] ) <EOL> @ parameterized . expand ( [ <EOL> ( TestOrderAlgorithm , ) , <EOL> ( TestOrderValueAlgorithm , ) , <EOL> ( TestTargetAlgorithm , ) , <EOL> ( TestOrderPercentAlgorithm , ) <EOL> ] ) <EOL> def test_minute_data ( self , algo_class ) : <EOL> period_start = pd . Timestamp ( '<STR_LIT>' , tz = '<STR_LIT>' ) <EOL> period_end = pd . Timestamp ( '<STR_LIT>' , tz = '<STR_LIT>' ) <EOL> equities = pd . DataFrame ( [ { <EOL> '<STR_LIT>' : period_start , <EOL> '<STR_LIT>' : period_end + timedelta ( days = <NUM_LIT:1> ) <EOL> } ] * <NUM_LIT:2> ) <EOL> with TempDirectory ( ) as tempdir , tmp_trading_env ( equities = equities ) as env : <EOL> sim_params = SimulationParameters ( <EOL> period_start = period_start , <EOL> period_end = period_end , <EOL> capital_base = float ( "<STR_LIT>" ) , <EOL> data_frequency = '<STR_LIT>' , <EOL> env = env <EOL> ) <EOL> data_portal = create_data_portal ( <EOL> env , <EOL> tempdir , <EOL> sim_params , <EOL> equities . index , <EOL> ) <EOL> algo = algo_class ( sim_params = sim_params , env = env ) <EOL> algo . run ( data_portal ) <EOL> class TestPositions ( WithLogger , <EOL> WithDataPortal , <EOL> WithSimParams , <EOL> ZiplineTestCase ) : <EOL> START_DATE = pd . Timestamp ( '<STR_LIT>' , tz = '<STR_LIT>' ) <EOL> END_DATE = pd . Timestamp ( '<STR_LIT>' , tz = '<STR_LIT>' ) <EOL> sids = ASSET_FINDER_EQUITY_SIDS = [ <NUM_LIT:1> , <NUM_LIT> ] <EOL> def test_empty_portfolio ( self ) : <EOL> algo = EmptyPositionsAlgorithm ( self . sids , <EOL> sim_params = self . sim_params , <EOL> env = self . env ) <EOL> daily_stats = algo . run ( self . data_portal ) <EOL> expected_position_count = [ <EOL> <NUM_LIT:0> , <EOL> <NUM_LIT:2> , <EOL> <NUM_LIT:0> , <EOL> <NUM_LIT:0> , <EOL> ] <EOL> for i , expected in enumerate ( expected_position_count ) : <EOL> self . assertEqual ( daily_stats . ix [ i ] [ '<STR_LIT>' ] , <EOL> expected ) <EOL> def test_noop_orders ( self ) : <EOL> algo = AmbitiousStopLimitAlgorithm ( sid = <NUM_LIT:1> , <EOL> sim_params = self . sim_params , <EOL> env = self . env ) <EOL> daily_stats = algo . run ( self . data_portal ) <EOL> empty_positions = daily_stats . positions . map ( lambda x : len ( x ) == <NUM_LIT:0> ) <EOL> self . assertTrue ( empty_positions . all ( ) ) <EOL> class TestBeforeTradingStart ( WithDataPortal , <EOL> WithSimParams , <EOL> ZiplineTestCase ) : <EOL> START_DATE = pd . Timestamp ( '<STR_LIT>' , tz = '<STR_LIT>' ) <EOL> END_DATE = pd . Timestamp ( '<STR_LIT>' , tz = '<STR_LIT>' ) <EOL> SIM_PARAMS_CAPITAL_BASE = <NUM_LIT> <EOL> SIM_PARAMS_DATA_FREQUENCY = '<STR_LIT>' <EOL> BCOLZ_DAILY_BAR_LOOKBACK_DAYS = BCOLZ_MINUTE_BAR_LOOKBACK_DAYS = <NUM_LIT:1> <EOL> data_start = ASSET_FINDER_EQUITY_START_DATE = pd . Timestamp ( <EOL> '<STR_LIT>' , <EOL> tz = '<STR_LIT>' , <EOL> ) <EOL> SPLIT_ASSET_SID = <NUM_LIT:3> <EOL> ASSET_FINDER_EQUITY_SIDS = <NUM_LIT:1> , <NUM_LIT:2> , SPLIT_ASSET_SID <EOL> @ classmethod <EOL> def make_minute_bar_data ( cls ) : <EOL> asset_minutes = cls . env . minutes_for_days_in_range ( <EOL> cls . data_start , <EOL> cls . END_DATE , <EOL> ) <EOL> minutes_count = len ( asset_minutes ) <EOL> minutes_arr = np . arange ( minutes_count ) + <NUM_LIT:1> <EOL> split_data = pd . DataFrame ( <EOL> { <EOL> '<STR_LIT>' : minutes_arr + <NUM_LIT:1> , <EOL> '<STR_LIT>' : minutes_arr + <NUM_LIT:2> , <EOL> '<STR_LIT>' : minutes_arr - <NUM_LIT:1> , <EOL> '<STR_LIT>' : minutes_arr , <EOL> '<STR_LIT>' : <NUM_LIT:100> * minutes_arr , <EOL> } , <EOL> index = asset_minutes , <EOL> ) <EOL> split_data . iloc [ <NUM_LIT> : ] = split_data . iloc [ <NUM_LIT> : ] / <NUM_LIT> <EOL> return merge ( <EOL> { <EOL> sid : create_minute_df_for_asset ( <EOL> cls . env , <EOL> cls . data_start , <EOL> cls . sim_params . period_end , <EOL> ) <EOL> for sid in ( <NUM_LIT:1> , <NUM_LIT> ) <EOL> } , <EOL> { <EOL> <NUM_LIT:2> : create_minute_df_for_asset ( <EOL> cls . env , <EOL> cls . data_start , <EOL> cls . sim_params . period_end , <EOL> <NUM_LIT:50> , <EOL> ) , <EOL> cls . SPLIT_ASSET_SID : split_data , <EOL> } , <EOL> ) <EOL> @ classmethod <EOL> def make_splits_data ( cls ) : <EOL> return pd . DataFrame . from_records ( [ <EOL> { <EOL> '<STR_LIT>' : str_to_seconds ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : <NUM_LIT:0.5> , <EOL> '<STR_LIT>' : cls . SPLIT_ASSET_SID , <EOL> } <EOL> ] ) <EOL> @ classmethod <EOL> def make_daily_bar_data ( cls ) : <EOL> for sid in cls . ASSET_FINDER_EQUITY_SIDS : <EOL> yield sid , create_daily_df_for_asset ( <EOL> cls . env , <EOL> cls . data_start , <EOL> cls . sim_params . period_end , <EOL> ) <EOL> def test_data_in_bts_minute ( self ) : <EOL> algo_code = dedent ( """<STR_LIT>""" ) <EOL> algo = TradingAlgorithm ( <EOL> script = algo_code , <EOL> sim_params = self . sim_params , <EOL> env = self . env <EOL> ) <EOL> results = algo . run ( self . data_portal ) <EOL> self . assertEqual ( <NUM_LIT> , results . iloc [ <NUM_LIT:0> ] . the_price1 ) <EOL> self . assertEqual ( <NUM_LIT> , results . iloc [ <NUM_LIT:0> ] . the_high1 ) <EOL> self . assertEqual ( <NUM_LIT> , results . iloc [ <NUM_LIT:0> ] . the_price2 ) <EOL> self . assertTrue ( np . isnan ( results . iloc [ <NUM_LIT:0> ] . the_high2 ) ) <EOL> np . testing . assert_array_equal ( <EOL> range ( <NUM_LIT> , <NUM_LIT> ) , algo . history_values [ <NUM_LIT:0> ] [ "<STR_LIT>" ] [ <NUM_LIT:1> ] <EOL> ) <EOL> np . testing . assert_array_equal ( <EOL> range ( <NUM_LIT> , <NUM_LIT> ) , algo . history_values [ <NUM_LIT:0> ] [ "<STR_LIT>" ] [ <NUM_LIT:1> ] <EOL> ) <EOL> np . testing . assert_array_equal ( <EOL> [ <NUM_LIT> ] * <NUM_LIT> , algo . history_values [ <NUM_LIT:0> ] [ "<STR_LIT>" ] [ <NUM_LIT:2> ] [ <NUM_LIT:0> : <NUM_LIT> ] <EOL> ) <EOL> np . testing . assert_array_equal ( <EOL> [ <NUM_LIT> ] * <NUM_LIT> , algo . history_values [ <NUM_LIT:0> ] [ "<STR_LIT>" ] [ <NUM_LIT:2> ] [ <NUM_LIT:20> : ] <EOL> ) <EOL> np . testing . assert_array_equal ( <EOL> np . full ( <NUM_LIT> , np . nan ) , algo . history_values [ <NUM_LIT:0> ] [ "<STR_LIT>" ] [ <NUM_LIT:2> ] [ <NUM_LIT:0> : <NUM_LIT> ] <EOL> ) <EOL> self . assertEqual ( <NUM_LIT> , algo . history_values [ <NUM_LIT:0> ] [ "<STR_LIT>" ] [ <NUM_LIT:2> ] [ <NUM_LIT> ] ) <EOL> np . testing . assert_array_equal ( <EOL> np . full ( <NUM_LIT> , np . nan ) , algo . history_values [ <NUM_LIT:0> ] [ "<STR_LIT>" ] [ <NUM_LIT:2> ] [ <NUM_LIT:20> : ] <EOL> ) <EOL> def test_data_in_bts_daily ( self ) : <EOL> algo_code = dedent ( """<STR_LIT>""" ) <EOL> algo = TradingAlgorithm ( <EOL> script = algo_code , <EOL> sim_params = self . sim_params , <EOL> env = self . env <EOL> ) <EOL> results = algo . run ( self . data_portal ) <EOL> self . assertEqual ( <NUM_LIT> , results . the_high1 [ <NUM_LIT:0> ] ) <EOL> self . assertEqual ( <NUM_LIT> , results . the_price1 [ <NUM_LIT:0> ] ) <EOL> self . assertTrue ( np . isnan ( results . the_high2 [ <NUM_LIT:0> ] ) ) <EOL> self . assertTrue ( <NUM_LIT> , results . the_price2 [ <NUM_LIT:0> ] ) <EOL> self . assertEqual ( <NUM_LIT> , algo . history_values [ <NUM_LIT:0> ] [ "<STR_LIT>" ] [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] ) <EOL> self . assertEqual ( <NUM_LIT> , algo . history_values [ <NUM_LIT:0> ] [ "<STR_LIT>" ] [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] ) <EOL> self . assertTrue ( np . isnan ( algo . history_values [ <NUM_LIT:0> ] [ "<STR_LIT>" ] [ <NUM_LIT:2> ] [ <NUM_LIT:0> ] ) ) <EOL> self . assertEqual ( <NUM_LIT> , algo . history_values [ <NUM_LIT:0> ] [ "<STR_LIT>" ] [ <NUM_LIT:2> ] [ <NUM_LIT:0> ] ) <EOL> def test_portfolio_bts ( self ) : <EOL> algo_code = dedent ( """<STR_LIT>""" ) <EOL> algo = TradingAlgorithm ( <EOL> script = algo_code , <EOL> data_frequency = "<STR_LIT>" , <EOL> sim_params = self . sim_params , <EOL> env = self . env <EOL> ) <EOL> results = algo . run ( self . data_portal ) <EOL> self . assertEqual ( results . pos_value . iloc [ <NUM_LIT:0> ] , <NUM_LIT:0> ) <EOL> self . assertEqual ( results . pos_value . iloc [ <NUM_LIT:1> ] , <NUM_LIT> ) <EOL> def test_account_bts ( self ) : <EOL> algo_code = dedent ( """<STR_LIT>""" ) <EOL> algo = TradingAlgorithm ( <EOL> script = algo_code , <EOL> data_frequency = "<STR_LIT>" , <EOL> sim_params = self . sim_params , <EOL> env = self . env <EOL> ) <EOL> results = algo . run ( self . data_portal ) <EOL> self . assertEqual ( results . port_value . iloc [ <NUM_LIT:0> ] , <NUM_LIT> ) <EOL> self . assertAlmostEqual ( results . port_value . iloc [ <NUM_LIT:1> ] , <EOL> <NUM_LIT> + <NUM_LIT> - <NUM_LIT> - <NUM_LIT:1> ) <EOL> def test_portfolio_bts_with_overnight_split ( self ) : <EOL> algo_code = dedent ( """<STR_LIT>""" ) <EOL> algo = TradingAlgorithm ( <EOL> script = algo_code , <EOL> data_frequency = "<STR_LIT>" , <EOL> sim_params = self . sim_params , <EOL> env = self . env <EOL> ) <EOL> results = algo . run ( self . data_portal ) <EOL> self . assertEqual ( results . pos_value . iloc [ <NUM_LIT:0> ] , <NUM_LIT:0> ) <EOL> self . assertEqual ( results . pos_value . iloc [ <NUM_LIT:1> ] , <NUM_LIT> ) <EOL> self . assertEqual ( results . pos_amount . iloc [ <NUM_LIT:0> ] , <NUM_LIT:0> ) <EOL> self . assertEqual ( results . pos_amount . iloc [ <NUM_LIT:1> ] , <NUM_LIT:2> ) <EOL> self . assertEqual ( results . last_sale_price . iloc [ <NUM_LIT:0> ] , <NUM_LIT:0> ) <EOL> self . assertEqual ( results . last_sale_price . iloc [ <NUM_LIT:1> ] , <NUM_LIT> ) <EOL> def test_account_bts_with_overnight_split ( self ) : <EOL> algo_code = dedent ( """<STR_LIT>""" ) <EOL> algo = TradingAlgorithm ( <EOL> script = algo_code , <EOL> data_frequency = "<STR_LIT>" , <EOL> sim_params = self . sim_params , <EOL> env = self . env <EOL> ) <EOL> results = algo . run ( self . data_portal ) <EOL> self . assertEqual ( results . port_value . iloc [ <NUM_LIT:0> ] , <NUM_LIT> ) <EOL> self . assertAlmostEqual ( results . port_value . iloc [ <NUM_LIT:1> ] , <EOL> <NUM_LIT> + <NUM_LIT> - <NUM_LIT> - <NUM_LIT:1> ) <EOL> class TestAlgoScript ( WithLogger , <EOL> WithDataPortal , <EOL> WithSimParams , <EOL> ZiplineTestCase ) : <EOL> START_DATE = pd . Timestamp ( '<STR_LIT>' , tz = '<STR_LIT>' ) <EOL> END_DATE = pd . Timestamp ( '<STR_LIT>' , tz = '<STR_LIT>' ) <EOL> BCOLZ_DAILY_BAR_LOOKBACK_DAYS = <NUM_LIT:5> <EOL> ARG_TYPE_TEST_CASES = ( <EOL> ( '<STR_LIT>' , ( bad_type_history_assets , '<STR_LIT>' , True ) ) , <EOL> ( '<STR_LIT>' , ( bad_type_history_fields , '<STR_LIT:str>' , True ) ) , <EOL> ( '<STR_LIT>' , ( bad_type_history_bar_count , '<STR_LIT:int>' , False ) ) , <EOL> ( '<STR_LIT>' , ( bad_type_history_frequency , '<STR_LIT:str>' , False ) ) , <EOL> ( '<STR_LIT>' , ( bad_type_current_assets , '<STR_LIT>' , True ) ) , <EOL> ( '<STR_LIT>' , ( bad_type_current_fields , '<STR_LIT:str>' , True ) ) , <EOL> ( '<STR_LIT>' , ( bad_type_is_stale_assets , '<STR_LIT>' , True ) ) , <EOL> ( '<STR_LIT>' , ( bad_type_can_trade_assets , '<STR_LIT>' , True ) ) , <EOL> ( '<STR_LIT>' , <EOL> ( bad_type_history_assets_kwarg , '<STR_LIT>' , True ) ) , <EOL> ( '<STR_LIT>' , <EOL> ( bad_type_history_assets_kwarg_list , '<STR_LIT>' , True ) ) , <EOL> ( '<STR_LIT>' , <EOL> ( bad_type_history_fields_kwarg , '<STR_LIT:str>' , True ) ) , <EOL> ( '<STR_LIT>' , <EOL> ( bad_type_history_bar_count_kwarg , '<STR_LIT:int>' , False ) ) , <EOL> ( '<STR_LIT>' , <EOL> ( bad_type_history_frequency_kwarg , '<STR_LIT:str>' , False ) ) , <EOL> ( '<STR_LIT>' , <EOL> ( bad_type_current_assets_kwarg , '<STR_LIT>' , True ) ) , <EOL> ( '<STR_LIT>' , <EOL> ( bad_type_current_fields_kwarg , '<STR_LIT:str>' , True ) ) , <EOL> ) <EOL> sids = <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:3> , <NUM_LIT> <EOL> @ classmethod <EOL> def make_equity_info ( cls ) : <EOL> data = make_simple_equity_info ( <EOL> cls . sids , <EOL> cls . START_DATE , <EOL> cls . END_DATE , <EOL> ) <EOL> data . loc [ <NUM_LIT:3> , '<STR_LIT>' ] = '<STR_LIT>' <EOL> return data <EOL> @ classmethod <EOL> def make_daily_bar_data ( cls ) : <EOL> days = len ( cls . env . days_in_range ( cls . START_DATE , cls . END_DATE ) ) <EOL> return trades_by_sid_to_dfs ( <EOL> { <EOL> <NUM_LIT:0> : factory . create_trade_history ( <EOL> <NUM_LIT:0> , <EOL> [ <NUM_LIT> ] * days , <EOL> [ <NUM_LIT:100> ] * days , <EOL> timedelta ( days = <NUM_LIT:1> ) , <EOL> cls . sim_params , <EOL> cls . env ) , <EOL> <NUM_LIT:3> : factory . create_trade_history ( <EOL> <NUM_LIT:3> , <EOL> [ <NUM_LIT> ] * days , <EOL> [ <NUM_LIT:100> ] * days , <EOL> timedelta ( days = <NUM_LIT:1> ) , <EOL> cls . sim_params , <EOL> cls . env ) <EOL> } , <EOL> index = cls . sim_params . trading_days , <EOL> ) <EOL> def test_noop ( self ) : <EOL> algo = TradingAlgorithm ( initialize = initialize_noop , <EOL> handle_data = handle_data_noop ) <EOL> algo . run ( self . data_portal ) <EOL> def test_noop_string ( self ) : <EOL> algo = TradingAlgorithm ( script = noop_algo ) <EOL> algo . run ( self . data_portal ) <EOL> def test_no_handle_data ( self ) : <EOL> algo = TradingAlgorithm ( script = no_handle_data ) <EOL> algo . run ( self . data_portal ) <EOL> def test_api_calls ( self ) : <EOL> algo = TradingAlgorithm ( initialize = initialize_api , <EOL> handle_data = handle_data_api , <EOL> env = self . env ) <EOL> algo . run ( self . data_portal ) <EOL> def test_api_calls_string ( self ) : <EOL> algo = TradingAlgorithm ( script = api_algo , env = self . env ) <EOL> algo . run ( self . data_portal ) <EOL> def test_api_get_environment ( self ) : <EOL> platform = '<STR_LIT>' <EOL> algo = TradingAlgorithm ( script = api_get_environment_algo , <EOL> platform = platform ) <EOL> algo . run ( self . data_portal ) <EOL> self . assertEqual ( algo . environment , platform ) <EOL> def test_api_symbol ( self ) : <EOL> algo = TradingAlgorithm ( script = api_symbol_algo , <EOL> env = self . env , <EOL> sim_params = self . sim_params ) <EOL> algo . run ( self . data_portal ) <EOL> def test_fixed_slippage ( self ) : <EOL> test_algo = TradingAlgorithm ( <EOL> script = """<STR_LIT>""" , <EOL> sim_params = self . sim_params , <EOL> env = self . env , <EOL> ) <EOL> results = test_algo . run ( self . data_portal ) <EOL> all_txns = [ val for sublist in results [ "<STR_LIT>" ] . tolist ( ) <EOL> for val in sublist ] <EOL> self . assertEqual ( len ( all_txns ) , <NUM_LIT:1> ) <EOL> txn = all_txns [ <NUM_LIT:0> ] <EOL> self . assertEqual ( <NUM_LIT> , txn [ "<STR_LIT>" ] ) <EOL> expected_spread = <NUM_LIT> <EOL> expected_commish = <NUM_LIT> <EOL> expected_price = test_algo . recorded_vars [ "<STR_LIT>" ] - expected_spread - expected_commish <EOL> self . assertEqual ( expected_price , txn [ '<STR_LIT>' ] ) <EOL> @ parameterized . expand ( <EOL> [ <EOL> ( '<STR_LIT>' , <NUM_LIT:0> , ) , <EOL> ( '<STR_LIT>' , <NUM_LIT:1> , ) , <EOL> ( '<STR_LIT>' , <NUM_LIT:2> , ) , <EOL> ] <EOL> ) <EOL> def test_volshare_slippage ( self , name , minimum_commission ) : <EOL> tempdir = TempDirectory ( ) <EOL> try : <EOL> if name == "<STR_LIT>" : <EOL> commission_line = "<STR_LIT>" <EOL> else : <EOL> commission_line = "<STR_LIT>" "<STR_LIT>" . format ( minimum_commission ) <EOL> test_algo = TradingAlgorithm ( <EOL> script = """<STR_LIT>""" . format ( commission_line ) , <EOL> sim_params = self . sim_params , <EOL> env = self . env , <EOL> ) <EOL> set_algo_instance ( test_algo ) <EOL> trades = factory . create_daily_trade_source ( <EOL> [ <NUM_LIT:0> ] , self . sim_params , self . env ) <EOL> data_portal = create_data_portal_from_trade_history ( <EOL> self . env , tempdir , self . sim_params , { <NUM_LIT:0> : trades } ) <EOL> results = test_algo . run ( data_portal ) <EOL> all_txns = [ <EOL> val for sublist in results [ "<STR_LIT>" ] . tolist ( ) <EOL> for val in sublist ] <EOL> self . assertEqual ( len ( all_txns ) , <NUM_LIT> ) <EOL> first_txn = all_txns [ <NUM_LIT:0> ] <EOL> if minimum_commission == <NUM_LIT:0> : <EOL> commish = first_txn [ "<STR_LIT>" ] * <NUM_LIT> <EOL> self . assertEqual ( commish , first_txn [ "<STR_LIT>" ] ) <EOL> else : <EOL> self . assertEqual ( minimum_commission , first_txn [ "<STR_LIT>" ] ) <EOL> finally : <EOL> tempdir . cleanup ( ) <EOL> def test_algo_record_vars ( self ) : <EOL> test_algo = TradingAlgorithm ( <EOL> script = record_variables , <EOL> sim_params = self . sim_params , <EOL> env = self . env , <EOL> ) <EOL> results = test_algo . run ( self . data_portal ) <EOL> for i in range ( <NUM_LIT:1> , <NUM_LIT> ) : <EOL> self . assertEqual ( results . iloc [ i - <NUM_LIT:1> ] [ "<STR_LIT>" ] , i ) <EOL> def test_algo_record_allow_mock ( self ) : <EOL> """<STR_LIT>""" <EOL> test_algo = TradingAlgorithm ( <EOL> script = record_variables , <EOL> sim_params = self . sim_params , <EOL> ) <EOL> set_algo_instance ( test_algo ) <EOL> test_algo . record ( foo = MagicMock ( ) ) <EOL> def test_algo_record_nan ( self ) : <EOL> test_algo = TradingAlgorithm ( <EOL> script = record_float_magic % '<STR_LIT>' , <EOL> sim_params = self . sim_params , <EOL> env = self . env , <EOL> ) <EOL> results = test_algo . run ( self . data_portal ) <EOL> for i in range ( <NUM_LIT:1> , <NUM_LIT> ) : <EOL> self . assertTrue ( np . isnan ( results . iloc [ i - <NUM_LIT:1> ] [ "<STR_LIT:data>" ] ) ) <EOL> def test_order_methods ( self ) : <EOL> """<STR_LIT>""" <EOL> test_algo = TradingAlgorithm ( <EOL> script = call_all_order_methods , <EOL> sim_params = self . sim_params , <EOL> env = self . env , <EOL> ) <EOL> test_algo . run ( self . data_portal ) <EOL> def test_order_dead_asset ( self ) : <EOL> params = SimulationParameters ( <EOL> period_start = pd . Timestamp ( "<STR_LIT>" , tz = '<STR_LIT>' ) , <EOL> period_end = pd . Timestamp ( "<STR_LIT>" , tz = '<STR_LIT>' ) , <EOL> env = self . env <EOL> ) <EOL> test_algo = TradingAlgorithm ( <EOL> script = """<STR_LIT>""" , <EOL> sim_params = params , <EOL> env = self . env <EOL> ) <EOL> test_algo . run ( self . data_portal ) <EOL> for order_str in [ "<STR_LIT>" , "<STR_LIT>" ] : <EOL> test_algo = TradingAlgorithm ( <EOL> script = """<STR_LIT>""" . format ( order_str ) , <EOL> sim_params = params , <EOL> env = self . env <EOL> ) <EOL> with self . assertRaises ( CannotOrderDelistedAsset ) : <EOL> test_algo . run ( self . data_portal ) <EOL> def test_order_in_init ( self ) : <EOL> """<STR_LIT>""" <EOL> with self . assertRaises ( OrderDuringInitialize ) : <EOL> test_algo = TradingAlgorithm ( <EOL> script = call_order_in_init , <EOL> sim_params = self . sim_params , <EOL> env = self . env , <EOL> ) <EOL> test_algo . run ( self . data_portal ) <EOL> def test_portfolio_in_init ( self ) : <EOL> """<STR_LIT>""" <EOL> test_algo = TradingAlgorithm ( <EOL> script = access_portfolio_in_init , <EOL> sim_params = self . sim_params , <EOL> env = self . env , <EOL> ) <EOL> test_algo . run ( self . data_portal ) <EOL> def test_account_in_init ( self ) : <EOL> """<STR_LIT>""" <EOL> test_algo = TradingAlgorithm ( <EOL> script = access_account_in_init , <EOL> sim_params = self . sim_params , <EOL> env = self . env , <EOL> ) <EOL> test_algo . run ( self . data_portal ) <EOL> def test_without_kwargs ( self ) : <EOL> """<STR_LIT>""" <EOL> test_algo = TradingAlgorithm ( <EOL> script = call_without_kwargs , <EOL> sim_params = self . sim_params , <EOL> env = self . env , <EOL> ) <EOL> test_algo . run ( self . data_portal ) <EOL> def test_good_kwargs ( self ) : <EOL> """<STR_LIT>""" <EOL> test_algo = TradingAlgorithm ( <EOL> script = call_with_kwargs , <EOL> sim_params = self . sim_params , <EOL> env = self . env , <EOL> ) <EOL> test_algo . run ( self . data_portal ) <EOL> @ parameterized . expand ( [ ( '<STR_LIT>' , call_with_bad_kwargs_history ) , <EOL> ( '<STR_LIT>' , call_with_bad_kwargs_current ) ] ) <EOL> def test_bad_kwargs ( self , name , algo_text ) : <EOL> """<STR_LIT>""" <EOL> with self . assertRaises ( TypeError ) as cm : <EOL> test_algo = TradingAlgorithm ( <EOL> script = algo_text , <EOL> sim_params = self . sim_params , <EOL> env = self . env , <EOL> ) <EOL> test_algo . run ( self . data_portal ) <EOL> self . assertEqual ( "<STR_LIT>" <EOL> % name , cm . exception . args [ <NUM_LIT:0> ] ) <EOL> @ parameterized . expand ( ARG_TYPE_TEST_CASES ) <EOL> def test_arg_types ( self , name , inputs ) : <EOL> keyword = name . split ( '<STR_LIT>' ) [ <NUM_LIT:1> ] <EOL> with self . assertRaises ( TypeError ) as cm : <EOL> algo = TradingAlgorithm ( <EOL> script = inputs [ <NUM_LIT:0> ] , <EOL> sim_params = self . sim_params , <EOL> env = self . env <EOL> ) <EOL> algo . run ( self . data_portal ) <EOL> expected = "<STR_LIT>" % ( <EOL> keyword , <EOL> '<STR_LIT>' if inputs [ <NUM_LIT:2> ] else '<STR_LIT>' , <EOL> inputs [ <NUM_LIT:1> ] <EOL> ) <EOL> self . assertEqual ( expected , cm . exception . args [ <NUM_LIT:0> ] ) <EOL> def test_empty_asset_list_to_history ( self ) : <EOL> algo = TradingAlgorithm ( <EOL> script = dedent ( """<STR_LIT>""" ) , <EOL> sim_params = self . sim_params , <EOL> env = self . env <EOL> ) <EOL> algo . run ( self . data_portal ) <EOL> @ parameterized . expand ( <EOL> [ ( '<STR_LIT>' , call_with_bad_kwargs_get_open_orders ) , <EOL> ( '<STR_LIT>' , call_with_good_kwargs_get_open_orders ) , <EOL> ( '<STR_LIT>' , call_with_no_kwargs_get_open_orders ) ] <EOL> ) <EOL> def test_get_open_orders_kwargs ( self , name , script ) : <EOL> algo = TradingAlgorithm ( <EOL> script = script , <EOL> sim_params = self . sim_params , <EOL> env = self . env <EOL> ) <EOL> if name == '<STR_LIT>' : <EOL> with self . assertRaises ( TypeError ) as cm : <EOL> algo . run ( self . data_portal ) <EOL> self . assertEqual ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , cm . exception . args [ <NUM_LIT:0> ] ) <EOL> else : <EOL> algo . run ( self . data_portal ) <EOL> def test_empty_positions ( self ) : <EOL> """<STR_LIT>""" <EOL> algo = TradingAlgorithm ( <EOL> script = empty_positions , <EOL> sim_params = self . sim_params , <EOL> env = self . env <EOL> ) <EOL> results = algo . run ( self . data_portal ) <EOL> num_positions = results . num_positions <EOL> amounts = results . amounts <EOL> self . assertTrue ( all ( num_positions == <NUM_LIT:0> ) ) <EOL> self . assertTrue ( all ( amounts == <NUM_LIT:0> ) ) <EOL> @ parameterized . expand ( [ <EOL> ( '<STR_LIT>' , noop_algo ) , <EOL> ( '<STR_LIT>' , set_benchmark_algo ) ] <EOL> ) <EOL> def test_zero_trading_days ( self , name , algocode ) : <EOL> """<STR_LIT>""" <EOL> sim_params = factory . create_simulation_parameters ( <EOL> start = pd . Timestamp ( '<STR_LIT>' , tz = '<STR_LIT>' ) , <EOL> end = pd . Timestamp ( '<STR_LIT>' , tz = '<STR_LIT>' ) <EOL> ) <EOL> algo = TradingAlgorithm ( <EOL> script = algocode , <EOL> sim_params = sim_params , <EOL> env = self . env <EOL> ) <EOL> algo . run ( self . data_portal ) <EOL> class TestGetDatetime ( WithLogger , <EOL> WithSimParams , <EOL> WithDataPortal , <EOL> ZiplineTestCase ) : <EOL> SIM_PARAMS_DATA_FREQUENCY = '<STR_LIT>' <EOL> START_DATE = to_utc ( '<STR_LIT>' ) <EOL> END_DATE = to_utc ( '<STR_LIT>' ) <EOL> ASSET_FINDER_EQUITY_SIDS = <NUM_LIT:0> , <NUM_LIT:1> <EOL> @ parameterized . expand ( <EOL> [ <EOL> ( '<STR_LIT:default>' , None , ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , ) , <EOL> ] <EOL> ) <EOL> def test_get_datetime ( self , name , tz ) : <EOL> algo = dedent ( <EOL> """<STR_LIT>""" . format ( tz = repr ( tz ) ) <EOL> ) <EOL> algo = TradingAlgorithm ( <EOL> script = algo , <EOL> sim_params = self . sim_params , <EOL> env = self . env , <EOL> ) <EOL> algo . run ( self . data_portal ) <EOL> self . assertFalse ( algo . first_bar ) <EOL> class TestTradingControls ( WithSimParams , WithDataPortal , ZiplineTestCase ) : <EOL> START_DATE = pd . Timestamp ( '<STR_LIT>' , tz = '<STR_LIT>' ) <EOL> END_DATE = pd . Timestamp ( '<STR_LIT>' , tz = '<STR_LIT>' ) <EOL> sid = <NUM_LIT> <EOL> sids = ASSET_FINDER_EQUITY_SIDS = <NUM_LIT> , <NUM_LIT> <EOL> @ classmethod <EOL> def init_class_fixtures ( cls ) : <EOL> super ( TestTradingControls , cls ) . init_class_fixtures ( ) <EOL> cls . asset = cls . asset_finder . retrieve_asset ( cls . sid ) <EOL> cls . another_asset = cls . asset_finder . retrieve_asset ( <NUM_LIT> ) <EOL> def _check_algo ( self , <EOL> algo , <EOL> handle_data , <EOL> expected_order_count , <EOL> expected_exc ) : <EOL> algo . _handle_data = handle_data <EOL> with self . assertRaises ( expected_exc ) if expected_exc else nullctx ( ) : <EOL> algo . run ( self . data_portal ) <EOL> self . assertEqual ( algo . order_count , expected_order_count ) <EOL> def check_algo_succeeds ( self , algo , handle_data , order_count = <NUM_LIT:4> ) : <EOL> self . _check_algo ( algo , handle_data , order_count , None ) <EOL> def check_algo_fails ( self , algo , handle_data , order_count ) : <EOL> self . _check_algo ( algo , <EOL> handle_data , <EOL> order_count , <EOL> TradingControlViolation ) <EOL> def test_set_max_position_size ( self ) : <EOL> def handle_data ( algo , data ) : <EOL> algo . order ( algo . sid ( self . sid ) , <NUM_LIT:1> ) <EOL> algo . order_count += <NUM_LIT:1> <EOL> algo = SetMaxPositionSizeAlgorithm ( asset = self . asset , <EOL> max_shares = <NUM_LIT:10> , <EOL> max_notional = <NUM_LIT> , <EOL> sim_params = self . sim_params , <EOL> env = self . env ) <EOL> self . check_algo_succeeds ( algo , handle_data ) <EOL> def handle_data ( algo , data ) : <EOL> algo . order ( algo . sid ( self . sid ) , <NUM_LIT:3> ) <EOL> algo . order_count += <NUM_LIT:1> <EOL> algo = SetMaxPositionSizeAlgorithm ( asset = self . asset , <EOL> max_shares = <NUM_LIT:10> , <EOL> max_notional = <NUM_LIT> , <EOL> sim_params = self . sim_params , <EOL> env = self . env ) <EOL> self . check_algo_fails ( algo , handle_data , <NUM_LIT:3> ) <EOL> def handle_data ( algo , data ) : <EOL> algo . order ( algo . sid ( self . sid ) , <NUM_LIT:3> ) <EOL> algo . order_count += <NUM_LIT:1> <EOL> algo = SetMaxPositionSizeAlgorithm ( asset = self . asset , <EOL> max_shares = <NUM_LIT:10> , <EOL> max_notional = <NUM_LIT> , <EOL> sim_params = self . sim_params , <EOL> env = self . env ) <EOL> self . check_algo_fails ( algo , handle_data , <NUM_LIT:2> ) <EOL> def handle_data ( algo , data ) : <EOL> algo . order ( algo . sid ( self . sid ) , <NUM_LIT> ) <EOL> algo . order_count += <NUM_LIT:1> <EOL> algo = SetMaxPositionSizeAlgorithm ( asset = self . another_asset , <EOL> max_shares = <NUM_LIT:10> , <EOL> max_notional = <NUM_LIT> , <EOL> sim_params = self . sim_params , <EOL> env = self . env ) <EOL> self . check_algo_succeeds ( algo , handle_data ) <EOL> def handle_data ( algo , data ) : <EOL> algo . order ( algo . sid ( self . sid ) , <NUM_LIT> ) <EOL> algo . order_count += <NUM_LIT:1> <EOL> algo = SetMaxPositionSizeAlgorithm ( max_shares = <NUM_LIT:10> , max_notional = <NUM_LIT> , <EOL> sim_params = self . sim_params , <EOL> env = self . env ) <EOL> self . check_algo_fails ( algo , handle_data , <NUM_LIT:0> ) <EOL> def test_set_do_not_order_list ( self ) : <EOL> algo = SetDoNotOrderListAlgorithm ( <EOL> sid = self . sid , <EOL> restricted_list = [ self . sid ] , <EOL> sim_params = self . sim_params , <EOL> env = self . env , <EOL> ) <EOL> def handle_data ( algo , data ) : <EOL> algo . order ( algo . sid ( self . sid ) , <NUM_LIT:100> ) <EOL> algo . order_count += <NUM_LIT:1> <EOL> self . check_algo_fails ( algo , handle_data , <NUM_LIT:0> ) <EOL> algo = SetDoNotOrderListAlgorithm ( <EOL> sid = self . sid , <EOL> restricted_list = [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] , <EOL> sim_params = self . sim_params , <EOL> env = self . env , <EOL> ) <EOL> def handle_data ( algo , data ) : <EOL> algo . order ( algo . sid ( self . sid ) , <NUM_LIT:100> ) <EOL> algo . order_count += <NUM_LIT:1> <EOL> self . check_algo_succeeds ( algo , handle_data ) <EOL> def test_set_max_order_size ( self ) : <EOL> def handle_data ( algo , data ) : <EOL> algo . order ( algo . sid ( self . sid ) , <NUM_LIT:1> ) <EOL> algo . order_count += <NUM_LIT:1> <EOL> algo = SetMaxOrderSizeAlgorithm ( asset = self . asset , <EOL> max_shares = <NUM_LIT:10> , <EOL> max_notional = <NUM_LIT> , <EOL> sim_params = self . sim_params , <EOL> env = self . env ) <EOL> self . check_algo_succeeds ( algo , handle_data ) <EOL> def handle_data ( algo , data ) : <EOL> algo . order ( algo . sid ( self . sid ) , algo . order_count + <NUM_LIT:1> ) <EOL> algo . order_count += <NUM_LIT:1> <EOL> algo = SetMaxOrderSizeAlgorithm ( asset = self . asset , <EOL> max_shares = <NUM_LIT:3> , <EOL> max_notional = <NUM_LIT> , <EOL> sim_params = self . sim_params , <EOL> env = self . env ) <EOL> self . check_algo_fails ( algo , handle_data , <NUM_LIT:3> ) <EOL> def handle_data ( algo , data ) : <EOL> algo . order ( algo . sid ( self . sid ) , algo . order_count + <NUM_LIT:1> ) <EOL> algo . order_count += <NUM_LIT:1> <EOL> algo = SetMaxOrderSizeAlgorithm ( asset = self . asset , <EOL> max_shares = <NUM_LIT:10> , <EOL> max_notional = <NUM_LIT> , <EOL> sim_params = self . sim_params , <EOL> env = self . env ) <EOL> self . check_algo_fails ( algo , handle_data , <NUM_LIT:3> ) <EOL> def handle_data ( algo , data ) : <EOL> algo . order ( algo . sid ( self . sid ) , <NUM_LIT> ) <EOL> algo . order_count += <NUM_LIT:1> <EOL> algo = SetMaxOrderSizeAlgorithm ( asset = self . another_asset , <EOL> max_shares = <NUM_LIT:1> , <EOL> max_notional = <NUM_LIT:1.0> , <EOL> sim_params = self . sim_params , <EOL> env = self . env ) <EOL> self . check_algo_succeeds ( algo , handle_data ) <EOL> def handle_data ( algo , data ) : <EOL> algo . order ( algo . sid ( self . sid ) , <NUM_LIT> ) <EOL> algo . order_count += <NUM_LIT:1> <EOL> algo = SetMaxOrderSizeAlgorithm ( max_shares = <NUM_LIT:1> , <EOL> max_notional = <NUM_LIT:1.0> , <EOL> sim_params = self . sim_params , <EOL> env = self . env ) <EOL> self . check_algo_fails ( algo , handle_data , <NUM_LIT:0> ) <EOL> def test_set_max_order_count ( self ) : <EOL> start = pd . Timestamp ( '<STR_LIT>' , tz = '<STR_LIT>' ) <EOL> metadata = pd . DataFrame . from_dict ( <EOL> { <EOL> <NUM_LIT:1> : { <EOL> '<STR_LIT>' : start , <EOL> '<STR_LIT>' : start + timedelta ( days = <NUM_LIT:6> ) <EOL> } , <EOL> } , <EOL> orient = '<STR_LIT:index>' , <EOL> ) <EOL> with TempDirectory ( ) as tempdir , tmp_trading_env ( equities = metadata ) as env : <EOL> sim_params = factory . create_simulation_parameters ( <EOL> start = start , <EOL> num_days = <NUM_LIT:4> , <EOL> env = env , <EOL> data_frequency = '<STR_LIT>' , <EOL> ) <EOL> data_portal = create_data_portal ( <EOL> env , <EOL> tempdir , <EOL> sim_params , <EOL> [ <NUM_LIT:1> ] <EOL> ) <EOL> def handle_data ( algo , data ) : <EOL> for i in range ( <NUM_LIT:5> ) : <EOL> algo . order ( algo . sid ( <NUM_LIT:1> ) , <NUM_LIT:1> ) <EOL> algo . order_count += <NUM_LIT:1> <EOL> algo = SetMaxOrderCountAlgorithm ( <NUM_LIT:3> , sim_params = sim_params , <EOL> env = env ) <EOL> with self . assertRaises ( TradingControlViolation ) : <EOL> algo . _handle_data = handle_data <EOL> algo . run ( data_portal ) <EOL> self . assertEqual ( algo . order_count , <NUM_LIT:3> ) <EOL> def handle_data2 ( algo , data ) : <EOL> if algo . minute_count == <NUM_LIT:0> or algo . minute_count == <NUM_LIT:100> : <EOL> for i in range ( <NUM_LIT:5> ) : <EOL> algo . order ( algo . sid ( <NUM_LIT:1> ) , <NUM_LIT:1> ) <EOL> algo . order_count += <NUM_LIT:1> <EOL> algo . minute_count += <NUM_LIT:1> <EOL> algo = SetMaxOrderCountAlgorithm ( <NUM_LIT:9> , sim_params = sim_params , <EOL> env = env ) <EOL> with self . assertRaises ( TradingControlViolation ) : <EOL> algo . _handle_data = handle_data2 <EOL> algo . run ( data_portal ) <EOL> self . assertEqual ( algo . order_count , <NUM_LIT:9> ) <EOL> def handle_data3 ( algo , data ) : <EOL> if ( algo . minute_count % <NUM_LIT> ) == <NUM_LIT:0> : <EOL> for i in range ( <NUM_LIT:5> ) : <EOL> algo . order ( algo . sid ( <NUM_LIT:1> ) , <NUM_LIT:1> ) <EOL> algo . order_count += <NUM_LIT:1> <EOL> algo . minute_count += <NUM_LIT:1> <EOL> algo = SetMaxOrderCountAlgorithm ( <NUM_LIT:5> , sim_params = sim_params , <EOL> env = env ) <EOL> algo . _handle_data = handle_data3 <EOL> algo . run ( data_portal ) <EOL> def test_long_only ( self ) : <EOL> def handle_data ( algo , data ) : <EOL> algo . order ( algo . sid ( self . sid ) , - <NUM_LIT:1> ) <EOL> algo . order_count += <NUM_LIT:1> <EOL> algo = SetLongOnlyAlgorithm ( sim_params = self . sim_params , env = self . env ) <EOL> self . check_algo_fails ( algo , handle_data , <NUM_LIT:0> ) <EOL> def handle_data ( algo , data ) : <EOL> if ( algo . order_count % <NUM_LIT:2> ) == <NUM_LIT:0> : <EOL> algo . order ( algo . sid ( self . sid ) , <NUM_LIT:1> ) <EOL> else : <EOL> algo . order ( algo . sid ( self . sid ) , - <NUM_LIT:1> ) <EOL> algo . order_count += <NUM_LIT:1> <EOL> algo = SetLongOnlyAlgorithm ( sim_params = self . sim_params , env = self . env ) <EOL> self . check_algo_succeeds ( algo , handle_data ) <EOL> def handle_data ( algo , data ) : <EOL> amounts = [ <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , - <NUM_LIT:3> ] <EOL> algo . order ( algo . sid ( self . sid ) , amounts [ algo . order_count ] ) <EOL> algo . order_count += <NUM_LIT:1> <EOL> algo = SetLongOnlyAlgorithm ( sim_params = self . sim_params , env = self . env ) <EOL> self . check_algo_succeeds ( algo , handle_data ) <EOL> def handle_data ( algo , data ) : <EOL> amounts = [ <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , - <NUM_LIT:4> ] <EOL> algo . order ( algo . sid ( self . sid ) , amounts [ algo . order_count ] ) <EOL> algo . order_count += <NUM_LIT:1> <EOL> algo = SetLongOnlyAlgorithm ( sim_params = self . sim_params , env = self . env ) <EOL> self . check_algo_fails ( algo , handle_data , <NUM_LIT:3> ) <EOL> def test_register_post_init ( self ) : <EOL> def initialize ( algo ) : <EOL> algo . initialized = True <EOL> def handle_data ( algo , data ) : <EOL> with self . assertRaises ( RegisterTradingControlPostInit ) : <EOL> algo . set_max_position_size ( self . sid , <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> with self . assertRaises ( RegisterTradingControlPostInit ) : <EOL> algo . set_max_order_size ( self . sid , <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> with self . assertRaises ( RegisterTradingControlPostInit ) : <EOL> algo . set_max_order_count ( <NUM_LIT:1> ) <EOL> with self . assertRaises ( RegisterTradingControlPostInit ) : <EOL> algo . set_long_only ( ) <EOL> algo = TradingAlgorithm ( initialize = initialize , <EOL> handle_data = handle_data , <EOL> sim_params = self . sim_params , <EOL> env = self . env ) <EOL> algo . run ( self . data_portal ) <EOL> def test_asset_date_bounds ( self ) : <EOL> metadata = pd . DataFrame ( [ { <EOL> '<STR_LIT>' : self . sim_params . period_start , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } ] ) <EOL> with TempDirectory ( ) as tempdir , tmp_trading_env ( equities = metadata ) as env : <EOL> algo = SetAssetDateBoundsAlgorithm ( <EOL> sim_params = self . sim_params , <EOL> env = env , <EOL> ) <EOL> data_portal = create_data_portal ( <EOL> env , <EOL> tempdir , <EOL> self . sim_params , <EOL> [ <NUM_LIT:0> ] <EOL> ) <EOL> algo . run ( data_portal ) <EOL> metadata = pd . DataFrame ( [ { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } ] ) <EOL> with TempDirectory ( ) as tempdir , tmp_trading_env ( equities = metadata ) as env : <EOL> data_portal = create_data_portal ( <EOL> env , <EOL> tempdir , <EOL> self . sim_params , <EOL> [ <NUM_LIT:0> ] <EOL> ) <EOL> algo = SetAssetDateBoundsAlgorithm ( <EOL> sim_params = self . sim_params , <EOL> env = env , <EOL> ) <EOL> with self . assertRaises ( TradingControlViolation ) : <EOL> algo . run ( data_portal ) <EOL> metadata = pd . DataFrame ( [ { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } ] ) <EOL> with TempDirectory ( ) as tempdir , tmp_trading_env ( equities = metadata ) as env : <EOL> data_portal = create_data_portal ( <EOL> env , <EOL> tempdir , <EOL> self . sim_params , <EOL> [ <NUM_LIT:0> ] <EOL> ) <EOL> algo = SetAssetDateBoundsAlgorithm ( <EOL> sim_params = self . sim_params , <EOL> env = env , <EOL> ) <EOL> with self . assertRaises ( TradingControlViolation ) : <EOL> algo . run ( data_portal ) <EOL> class TestAccountControls ( WithDataPortal , WithSimParams , ZiplineTestCase ) : <EOL> START_DATE = pd . Timestamp ( '<STR_LIT>' , tz = '<STR_LIT>' ) <EOL> END_DATE = pd . Timestamp ( '<STR_LIT>' , tz = '<STR_LIT>' ) <EOL> sidint , = ASSET_FINDER_EQUITY_SIDS = ( <NUM_LIT> , ) <EOL> @ classmethod <EOL> def make_daily_bar_data ( cls ) : <EOL> return trades_by_sid_to_dfs ( <EOL> { <EOL> cls . sidint : factory . create_trade_history ( <EOL> cls . sidint , <EOL> [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT:100> , <NUM_LIT:100> , <NUM_LIT:100> , <NUM_LIT> ] , <EOL> timedelta ( days = <NUM_LIT:1> ) , <EOL> cls . sim_params , <EOL> cls . env , <EOL> ) , <EOL> } , <EOL> index = cls . sim_params . trading_days , <EOL> ) <EOL> def _check_algo ( self , <EOL> algo , <EOL> handle_data , <EOL> expected_exc ) : <EOL> algo . _handle_data = handle_data <EOL> with self . assertRaises ( expected_exc ) if expected_exc else nullctx ( ) : <EOL> algo . run ( self . data_portal ) <EOL> def check_algo_succeeds ( self , algo , handle_data ) : <EOL> self . _check_algo ( algo , handle_data , None ) <EOL> def check_algo_fails ( self , algo , handle_data ) : <EOL> self . _check_algo ( algo , <EOL> handle_data , <EOL> AccountControlViolation ) <EOL> def test_set_max_leverage ( self ) : <EOL> def handle_data ( algo , data ) : <EOL> algo . order ( algo . sid ( self . sidint ) , <NUM_LIT:1> ) <EOL> algo = SetMaxLeverageAlgorithm ( <NUM_LIT:0> , sim_params = self . sim_params , <EOL> env = self . env ) <EOL> self . check_algo_fails ( algo , handle_data ) <EOL> def handle_data ( algo , data ) : <EOL> algo . order ( algo . sid ( self . sidint ) , <NUM_LIT:1> ) <EOL> algo = SetMaxLeverageAlgorithm ( <NUM_LIT:1> , sim_params = self . sim_params , <EOL> env = self . env ) <EOL> self . check_algo_succeeds ( algo , handle_data ) <EOL> class TestFutureFlip ( WithSimParams , WithDataPortal , ZiplineTestCase ) : <EOL> START_DATE = pd . Timestamp ( '<STR_LIT>' , tz = '<STR_LIT>' ) <EOL> END_DATE = pd . Timestamp ( '<STR_LIT>' , tz = '<STR_LIT>' ) <EOL> sid , = ASSET_FINDER_EQUITY_SIDS = ( <NUM_LIT:1> , ) <EOL> @ classmethod <EOL> def make_daily_bar_data ( cls ) : <EOL> return trades_by_sid_to_dfs ( <EOL> { <EOL> cls . sid : factory . create_trade_history ( <EOL> cls . sid , <EOL> [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:4> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] , <EOL> timedelta ( days = <NUM_LIT:1> ) , <EOL> cls . sim_params , <EOL> cls . env <EOL> ) , <EOL> } , <EOL> index = cls . sim_params . trading_days , <EOL> ) <EOL> @ skip <EOL> def test_flip_algo ( self ) : <EOL> metadata = { <NUM_LIT:1> : { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : self . sim_params . trading_days [ <NUM_LIT:0> ] , <EOL> '<STR_LIT>' : self . env . next_trading_day ( <EOL> self . sim_params . trading_days [ - <NUM_LIT:1> ] ) , <EOL> '<STR_LIT>' : <NUM_LIT:5> } } <EOL> self . env . write_data ( futures_data = metadata ) <EOL> algo = FutureFlipAlgo ( sid = <NUM_LIT:1> , amount = <NUM_LIT:1> , env = self . env , <EOL> commission = PerShare ( <NUM_LIT:0> ) , <EOL> order_count = <NUM_LIT:0> , <EOL> sim_params = self . sim_params ) <EOL> results = algo . run ( self . data_portal ) <EOL> expected_positions = [ <NUM_LIT:0> , <NUM_LIT:1> , - <NUM_LIT:1> ] <EOL> self . check_algo_positions ( results , expected_positions ) <EOL> expected_pnl = [ <NUM_LIT:0> , <NUM_LIT:5> , - <NUM_LIT:10> ] <EOL> self . check_algo_pnl ( results , expected_pnl ) <EOL> def check_algo_pnl ( self , results , expected_pnl ) : <EOL> np . testing . assert_array_almost_equal ( results . pnl , expected_pnl ) <EOL> def check_algo_positions ( self , results , expected_positions ) : <EOL> for i , amount in enumerate ( results . positions ) : <EOL> if amount : <EOL> actual_position = amount [ <NUM_LIT:0> ] [ '<STR_LIT>' ] <EOL> else : <EOL> actual_position = <NUM_LIT:0> <EOL> self . assertEqual ( <EOL> actual_position , expected_positions [ i ] , <EOL> "<STR_LIT>" . <EOL> format ( i , actual_position , expected_positions [ i ] ) ) <EOL> class TestTradingAlgorithm ( ZiplineTestCase ) : <EOL> def test_analyze_called ( self ) : <EOL> self . perf_ref = None <EOL> def initialize ( context ) : <EOL> pass <EOL> def handle_data ( context , data ) : <EOL> pass <EOL> def analyze ( context , perf ) : <EOL> self . perf_ref = perf <EOL> algo = TradingAlgorithm ( <EOL> initialize = initialize , <EOL> handle_data = handle_data , <EOL> analyze = analyze , <EOL> ) <EOL> with empty_trading_env ( ) as env : <EOL> data_portal = FakeDataPortal ( env ) <EOL> results = algo . run ( data_portal ) <EOL> self . assertIs ( results , self . perf_ref ) <EOL> class TestOrderCancelation ( WithDataPortal , <EOL> WithSimParams , <EOL> ZiplineTestCase ) : <EOL> START_DATE = pd . Timestamp ( '<STR_LIT>' , tz = '<STR_LIT>' ) <EOL> END_DATE = pd . Timestamp ( '<STR_LIT>' , tz = '<STR_LIT>' ) <EOL> ASSET_FINDER_EQUITY_SIDS = ( <NUM_LIT:1> , ) <EOL> ASSET_FINDER_EQUITY_SYMBOLS = ( '<STR_LIT>' , ) <EOL> code = dedent ( <EOL> """<STR_LIT>""" , <EOL> ) <EOL> @ classmethod <EOL> def make_minute_bar_data ( cls ) : <EOL> asset_minutes = cls . env . minutes_for_days_in_range ( <EOL> cls . sim_params . period_start , <EOL> cls . sim_params . period_end , <EOL> ) <EOL> minutes_count = len ( asset_minutes ) <EOL> minutes_arr = np . arange ( <NUM_LIT:1> , <NUM_LIT:1> + minutes_count ) <EOL> return { <EOL> <NUM_LIT:1> : pd . DataFrame ( <EOL> { <EOL> '<STR_LIT>' : minutes_arr + <NUM_LIT:1> , <EOL> '<STR_LIT>' : minutes_arr + <NUM_LIT:2> , <EOL> '<STR_LIT>' : minutes_arr - <NUM_LIT:1> , <EOL> '<STR_LIT>' : minutes_arr , <EOL> '<STR_LIT>' : np . full ( minutes_count , <NUM_LIT:1> ) , <EOL> } , <EOL> index = asset_minutes , <EOL> ) , <EOL> } <EOL> @ classmethod <EOL> def make_daily_bar_data ( cls ) : <EOL> yield <NUM_LIT:1> , pd . DataFrame ( <EOL> { <EOL> '<STR_LIT>' : np . full ( <NUM_LIT:3> , <NUM_LIT:1> ) , <EOL> '<STR_LIT>' : np . full ( <NUM_LIT:3> , <NUM_LIT:1> ) , <EOL> '<STR_LIT>' : np . full ( <NUM_LIT:3> , <NUM_LIT:1> ) , <EOL> '<STR_LIT>' : np . full ( <NUM_LIT:3> , <NUM_LIT:1> ) , <EOL> '<STR_LIT>' : np . full ( <NUM_LIT:3> , <NUM_LIT:1> ) , <EOL> } , <EOL> index = cls . sim_params . trading_days , <EOL> ) <EOL> def prep_algo ( self , cancelation_string , data_frequency = "<STR_LIT>" , <EOL> amount = <NUM_LIT:1000> ) : <EOL> code = self . code . format ( cancelation_string , amount ) <EOL> algo = TradingAlgorithm ( <EOL> script = code , <EOL> env = self . env , <EOL> sim_params = SimulationParameters ( <EOL> period_start = self . sim_params . period_start , <EOL> period_end = self . sim_params . period_end , <EOL> env = self . env , <EOL> data_frequency = data_frequency <EOL> ) <EOL> ) <EOL> return algo <EOL> @ parameterized . expand ( [ <EOL> ( <NUM_LIT:1> , ) , ( - <NUM_LIT:1> , ) , <EOL> ] ) <EOL> def test_eod_order_cancel_minute ( self , direction ) : <EOL> algo = self . prep_algo ( <EOL> "<STR_LIT>" , <EOL> amount = np . copysign ( <NUM_LIT:1000> , direction ) , <EOL> ) <EOL> log_catcher = TestHandler ( ) <EOL> with log_catcher : <EOL> results = algo . run ( self . data_portal ) <EOL> for daily_positions in results . positions : <EOL> self . assertEqual ( <NUM_LIT:1> , len ( daily_positions ) ) <EOL> self . assertEqual ( <EOL> np . copysign ( <NUM_LIT> , direction ) , <EOL> daily_positions [ <NUM_LIT:0> ] [ "<STR_LIT>" ] , <EOL> ) <EOL> self . assertEqual ( <NUM_LIT:1> , results . positions [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] [ "<STR_LIT>" ] . sid ) <EOL> np . testing . assert_array_equal ( [ <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> ] , <EOL> list ( map ( len , results . orders ) ) ) <EOL> np . testing . assert_array_equal ( [ <NUM_LIT> , <NUM_LIT:0> , <NUM_LIT:0> ] , <EOL> list ( map ( len , results . transactions ) ) ) <EOL> the_order = results . orders [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] <EOL> self . assertEqual ( ORDER_STATUS . CANCELLED , the_order [ "<STR_LIT:status>" ] ) <EOL> self . assertEqual ( np . copysign ( <NUM_LIT> , direction ) , the_order [ "<STR_LIT>" ] ) <EOL> warnings = [ record for record in log_catcher . records if <EOL> record . level == WARNING ] <EOL> self . assertEqual ( <NUM_LIT:1> , len ( warnings ) ) <EOL> if direction == <NUM_LIT:1> : <EOL> self . assertEqual ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> str ( warnings [ <NUM_LIT:0> ] . message ) <EOL> ) <EOL> elif direction == - <NUM_LIT:1> : <EOL> self . assertEqual ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> str ( warnings [ <NUM_LIT:0> ] . message ) <EOL> ) <EOL> def test_default_cancelation_policy ( self ) : <EOL> algo = self . prep_algo ( "<STR_LIT>" ) <EOL> log_catcher = TestHandler ( ) <EOL> with log_catcher : <EOL> results = algo . run ( self . data_portal ) <EOL> np . testing . assert_array_equal ( [ <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> ] , <EOL> list ( map ( len , results . orders ) ) ) <EOL> np . testing . assert_array_equal ( [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] , <EOL> list ( map ( len , results . transactions ) ) ) <EOL> self . assertFalse ( log_catcher . has_warnings ) <EOL> def test_eod_order_cancel_daily ( self ) : <EOL> algo = self . prep_algo ( <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" <EOL> ) <EOL> log_catcher = TestHandler ( ) <EOL> with log_catcher : <EOL> results = algo . run ( self . data_portal ) <EOL> np . testing . assert_array_equal ( [ <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> ] , <EOL> list ( map ( len , results . orders ) ) ) <EOL> np . testing . assert_array_equal ( [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:1> ] , <EOL> list ( map ( len , results . transactions ) ) ) <EOL> self . assertFalse ( log_catcher . has_warnings ) <EOL> @ skip ( "<STR_LIT>" ) <EOL> class TestRemoveData ( TestCase ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> self . env = env = TradingEnvironment ( ) <EOL> start_date = pd . Timestamp ( '<STR_LIT>' , tz = '<STR_LIT>' ) <EOL> start_ix = env . trading_days . get_loc ( start_date ) <EOL> days = env . trading_days <EOL> metadata = { <EOL> <NUM_LIT:0> : { <EOL> '<STR_LIT>' : '<STR_LIT:X>' , <EOL> '<STR_LIT>' : env . trading_days [ start_ix + <NUM_LIT:2> ] , <EOL> '<STR_LIT>' : env . trading_days [ start_ix + <NUM_LIT:5> ] , <EOL> '<STR_LIT>' : env . trading_days [ start_ix + <NUM_LIT:6> ] , <EOL> } , <EOL> <NUM_LIT:1> : { <EOL> '<STR_LIT>' : '<STR_LIT:Y>' , <EOL> '<STR_LIT>' : env . trading_days [ start_ix + <NUM_LIT:4> ] , <EOL> '<STR_LIT>' : env . trading_days [ start_ix + <NUM_LIT:7> ] , <EOL> '<STR_LIT>' : env . trading_days [ start_ix + <NUM_LIT:8> ] , <EOL> } <EOL> } <EOL> env . write_data ( futures_data = metadata ) <EOL> assetX , assetY = env . asset_finder . retrieve_all ( [ <NUM_LIT:0> , <NUM_LIT:1> ] ) <EOL> index_x = days [ days . slice_indexer ( assetX . start_date , assetX . end_date ) ] <EOL> data_x = pd . DataFrame ( [ [ <NUM_LIT:1> , <NUM_LIT:100> ] , [ <NUM_LIT:2> , <NUM_LIT:100> ] , [ <NUM_LIT:3> , <NUM_LIT:100> ] , [ <NUM_LIT:4> , <NUM_LIT:100> ] , <EOL> [ <NUM_LIT:5> , <NUM_LIT:100> ] ] , <EOL> index = index_x , columns = [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> index_y = days [ days . slice_indexer ( assetY . start_date , assetY . end_date ) ] <EOL> data_y = pd . DataFrame ( [ [ <NUM_LIT:6> , <NUM_LIT:100> ] , [ <NUM_LIT:7> , <NUM_LIT:100> ] , [ <NUM_LIT:8> , <NUM_LIT:100> ] , [ <NUM_LIT:9> , <NUM_LIT:100> ] , <EOL> [ <NUM_LIT:10> , <NUM_LIT:100> ] ] , <EOL> index = index_y , columns = [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> self . trade_data = pd . Panel ( { <NUM_LIT:0> : data_x , <NUM_LIT:1> : data_y } ) <EOL> self . live_asset_counts = [ ] <EOL> assets = env . asset_finder . retrieve_all ( [ <NUM_LIT:0> , <NUM_LIT:1> ] ) <EOL> for day in self . trade_data . major_axis : <EOL> count = <NUM_LIT:0> <EOL> for asset in assets : <EOL> if asset . start_date <= day <= asset . end_date : <EOL> count += <NUM_LIT:1> <EOL> self . live_asset_counts . append ( count ) <EOL> def test_remove_data ( self ) : <EOL> source = DataPanelSource ( self . trade_data ) <EOL> def initialize ( context ) : <EOL> context . data_lengths = [ ] <EOL> def handle_data ( context , data ) : <EOL> context . data_lengths . append ( len ( data ) ) <EOL> algo = TradingAlgorithm ( <EOL> initialize = initialize , <EOL> handle_data = handle_data , <EOL> env = self . env , <EOL> ) <EOL> algo . run ( source ) <EOL> self . assertEqual ( algo . data_lengths , self . live_asset_counts ) <EOL> class TestEquityAutoClose ( WithTmpDir , ZiplineTestCase ) : <EOL> """<STR_LIT>""" <EOL> @ classmethod <EOL> def init_class_fixtures ( cls ) : <EOL> super ( TestEquityAutoClose , cls ) . init_class_fixtures ( ) <EOL> start_date = pd . Timestamp ( '<STR_LIT>' , tz = '<STR_LIT>' ) <EOL> start_date_loc = trading_days . get_loc ( start_date ) <EOL> test_duration = <NUM_LIT:7> <EOL> cls . test_days = trading_days [ <EOL> start_date_loc : start_date_loc + test_duration <EOL> ] <EOL> cls . first_asset_expiration = cls . test_days [ <NUM_LIT:2> ] <EOL> def make_data ( self , auto_close_delta , frequency , <EOL> capital_base = float ( "<STR_LIT>" ) ) : <EOL> asset_info = make_jagged_equity_info ( <EOL> num_assets = <NUM_LIT:3> , <EOL> start_date = self . test_days [ <NUM_LIT:0> ] , <EOL> first_end = self . first_asset_expiration , <EOL> frequency = trading_day , <EOL> periods_between_ends = <NUM_LIT:2> , <EOL> auto_close_delta = auto_close_delta , <EOL> ) <EOL> sids = asset_info . index <EOL> env = self . enter_instance_context ( tmp_trading_env ( equities = asset_info ) ) <EOL> market_opens = env . open_and_closes . market_open . loc [ self . test_days ] <EOL> market_closes = env . open_and_closes . market_close . loc [ self . test_days ] <EOL> if frequency == '<STR_LIT>' : <EOL> dates = self . test_days <EOL> trade_data_by_sid = make_trade_data_for_asset_info ( <EOL> dates = dates , <EOL> asset_info = asset_info , <EOL> price_start = <NUM_LIT:10> , <EOL> price_step_by_sid = <NUM_LIT:10> , <EOL> price_step_by_date = <NUM_LIT:1> , <EOL> volume_start = <NUM_LIT:100> , <EOL> volume_step_by_sid = <NUM_LIT:100> , <EOL> volume_step_by_date = <NUM_LIT:10> , <EOL> frequency = frequency <EOL> ) <EOL> path = self . tmpdir . getpath ( "<STR_LIT>" ) <EOL> BcolzDailyBarWriter ( path , dates ) . write ( <EOL> iteritems ( trade_data_by_sid ) , <EOL> ) <EOL> data_portal = DataPortal ( <EOL> env , <EOL> equity_daily_reader = BcolzDailyBarReader ( path ) <EOL> ) <EOL> elif frequency == '<STR_LIT>' : <EOL> dates = env . minutes_for_days_in_range ( <EOL> self . test_days [ <NUM_LIT:0> ] , <EOL> self . test_days [ - <NUM_LIT:1> ] , <EOL> ) <EOL> writer = BcolzMinuteBarWriter ( <EOL> self . test_days [ <NUM_LIT:0> ] , <EOL> self . tmpdir . path , <EOL> market_opens , <EOL> market_closes , <EOL> US_EQUITIES_MINUTES_PER_DAY <EOL> ) <EOL> trade_data_by_sid = make_trade_data_for_asset_info ( <EOL> writer = writer , <EOL> dates = dates , <EOL> asset_info = asset_info , <EOL> price_start = <NUM_LIT:10> , <EOL> price_step_by_sid = <NUM_LIT:10> , <EOL> price_step_by_date = <NUM_LIT:1> , <EOL> volume_start = <NUM_LIT:100> , <EOL> volume_step_by_sid = <NUM_LIT:100> , <EOL> volume_step_by_date = <NUM_LIT:10> , <EOL> frequency = frequency <EOL> ) <EOL> data_portal = DataPortal ( <EOL> env , <EOL> equity_minute_reader = BcolzMinuteBarReader ( self . tmpdir . path ) <EOL> ) <EOL> else : <EOL> self . fail ( "<STR_LIT>" % frequency ) <EOL> assets = env . asset_finder . retrieve_all ( sids ) <EOL> sim_params = factory . create_simulation_parameters ( <EOL> start = self . test_days [ <NUM_LIT:0> ] , <EOL> end = self . test_days [ - <NUM_LIT:1> ] , <EOL> data_frequency = frequency , <EOL> emission_rate = frequency , <EOL> env = env , <EOL> capital_base = capital_base , <EOL> ) <EOL> if frequency == '<STR_LIT>' : <EOL> final_prices = { <EOL> asset . sid : trade_data_by_sid [ asset . sid ] . <EOL> loc [ asset . end_date ] . close <EOL> for asset in assets <EOL> } <EOL> else : <EOL> final_prices = { <EOL> asset . sid : trade_data_by_sid [ asset . sid ] . loc [ <EOL> env . get_open_and_close ( asset . end_date ) [ <NUM_LIT:1> ] <EOL> ] . close <EOL> for asset in assets <EOL> } <EOL> TestData = namedtuple ( <EOL> '<STR_LIT>' , <EOL> [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> ] , <EOL> ) <EOL> return TestData ( <EOL> asset_info = asset_info , <EOL> assets = assets , <EOL> env = env , <EOL> data_portal = data_portal , <EOL> final_prices = final_prices , <EOL> trade_data_by_sid = trade_data_by_sid , <EOL> sim_params = sim_params <EOL> ) <EOL> def prices_on_tick ( self , trades_by_sid , row ) : <EOL> return [ trades . iloc [ row ] . close <EOL> for trades in itervalues ( trades_by_sid ) ] <EOL> def default_initialize ( self ) : <EOL> """<STR_LIT>""" <EOL> def initialize ( context ) : <EOL> context . ordered = False <EOL> context . set_commission ( PerShare ( <NUM_LIT:0> , <NUM_LIT:0> ) ) <EOL> context . set_slippage ( FixedSlippage ( spread = <NUM_LIT:0> ) ) <EOL> context . num_positions = [ ] <EOL> context . cash = [ ] <EOL> return initialize <EOL> def default_handle_data ( self , assets , order_size ) : <EOL> """<STR_LIT>""" <EOL> def handle_data ( context , data ) : <EOL> if not context . ordered : <EOL> for asset in assets : <EOL> context . order ( asset , order_size ) <EOL> context . ordered = True <EOL> context . cash . append ( context . portfolio . cash ) <EOL> context . num_positions . append ( len ( context . portfolio . positions ) ) <EOL> return handle_data <EOL> @ parameter_space ( <EOL> order_size = [ <NUM_LIT:10> , - <NUM_LIT:10> ] , <EOL> capital_base = [ <NUM_LIT:0> , <NUM_LIT> ] , <EOL> auto_close_lag = [ <NUM_LIT:1> , <NUM_LIT:2> ] , <EOL> ) <EOL> def test_daily_delisted_equities ( self , <EOL> order_size , <EOL> capital_base , <EOL> auto_close_lag ) : <EOL> """<STR_LIT>""" <EOL> auto_close_delta = trading_day * auto_close_lag <EOL> resources = self . make_data ( auto_close_delta , '<STR_LIT>' , capital_base ) <EOL> assets = resources . assets <EOL> sids = [ asset . sid for asset in assets ] <EOL> final_prices = resources . final_prices <EOL> initial_fill_prices = self . prices_on_tick ( resources . trade_data_by_sid , <NUM_LIT:1> ) <EOL> cost_basis = sum ( initial_fill_prices ) * order_size <EOL> fp0 = final_prices [ <NUM_LIT:0> ] <EOL> fp1 = final_prices [ <NUM_LIT:1> ] <EOL> algo = TradingAlgorithm ( <EOL> initialize = self . default_initialize ( ) , <EOL> handle_data = self . default_handle_data ( assets , order_size ) , <EOL> env = resources . env , <EOL> sim_params = resources . sim_params <EOL> ) <EOL> output = algo . run ( resources . data_portal ) <EOL> initial_cash = capital_base <EOL> after_fills = initial_cash - cost_basis <EOL> after_first_auto_close = after_fills + fp0 * ( order_size ) <EOL> after_second_auto_close = after_first_auto_close + fp1 * ( order_size ) <EOL> if auto_close_lag == <NUM_LIT:1> : <EOL> expected_cash = [ <EOL> initial_cash , <EOL> after_fills , <EOL> after_fills , <EOL> after_first_auto_close , <EOL> after_first_auto_close , <EOL> after_second_auto_close , <EOL> after_second_auto_close , <EOL> ] <EOL> expected_num_positions = [ <NUM_LIT:0> , <NUM_LIT:3> , <NUM_LIT:3> , <NUM_LIT:2> , <NUM_LIT:2> , <NUM_LIT:1> , <NUM_LIT:1> ] <EOL> elif auto_close_lag == <NUM_LIT:2> : <EOL> expected_cash = [ <EOL> initial_cash , <EOL> after_fills , <EOL> after_fills , <EOL> after_fills , <EOL> after_first_auto_close , <EOL> after_first_auto_close , <EOL> after_second_auto_close , <EOL> ] <EOL> expected_num_positions = [ <NUM_LIT:0> , <NUM_LIT:3> , <NUM_LIT:3> , <NUM_LIT:3> , <NUM_LIT:2> , <NUM_LIT:2> , <NUM_LIT:1> ] <EOL> else : <EOL> self . fail ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> ) <EOL> self . assertEqual ( algo . cash , expected_cash ) <EOL> self . assertEqual ( expected_cash , list ( output [ '<STR_LIT>' ] ) ) <EOL> self . assertEqual ( algo . num_positions , expected_num_positions ) <EOL> if order_size > <NUM_LIT:0> : <EOL> self . assertEqual ( <EOL> expected_num_positions , <EOL> list ( output [ '<STR_LIT>' ] ) , <EOL> ) <EOL> self . assertEqual ( <EOL> [ <NUM_LIT:0> ] * len ( self . test_days ) , <EOL> list ( output [ '<STR_LIT>' ] ) , <EOL> ) <EOL> else : <EOL> self . assertEqual ( <EOL> expected_num_positions , <EOL> list ( output [ '<STR_LIT>' ] ) , <EOL> ) <EOL> self . assertEqual ( <EOL> [ <NUM_LIT:0> ] * len ( self . test_days ) , <EOL> list ( output [ '<STR_LIT>' ] ) , <EOL> ) <EOL> transactions = output [ '<STR_LIT>' ] <EOL> initial_fills = transactions . iloc [ <NUM_LIT:1> ] <EOL> self . assertEqual ( len ( initial_fills ) , len ( assets ) ) <EOL> for sid , txn in zip ( sids , initial_fills ) : <EOL> self . assertDictContainsSubset ( <EOL> { <EOL> '<STR_LIT>' : order_size , <EOL> '<STR_LIT>' : <NUM_LIT:0.0> , <EOL> '<STR_LIT>' : self . test_days [ <NUM_LIT:1> ] , <EOL> '<STR_LIT>' : initial_fill_prices [ sid ] , <EOL> '<STR_LIT>' : sid , <EOL> } , <EOL> txn , <EOL> ) <EOL> self . assertIsInstance ( txn [ '<STR_LIT>' ] , str ) <EOL> def transactions_for_date ( date ) : <EOL> return transactions . iloc [ self . test_days . get_loc ( date ) ] <EOL> ( first_auto_close_transaction , ) = transactions_for_date ( <EOL> assets [ <NUM_LIT:0> ] . auto_close_date <EOL> ) <EOL> self . assertEqual ( <EOL> first_auto_close_transaction , <EOL> { <EOL> '<STR_LIT>' : - order_size , <EOL> '<STR_LIT>' : <NUM_LIT:0.0> , <EOL> '<STR_LIT>' : assets [ <NUM_LIT:0> ] . auto_close_date , <EOL> '<STR_LIT>' : fp0 , <EOL> '<STR_LIT>' : sids [ <NUM_LIT:0> ] , <EOL> '<STR_LIT>' : None , <EOL> } , <EOL> ) <EOL> ( second_auto_close_transaction , ) = transactions_for_date ( <EOL> assets [ <NUM_LIT:1> ] . auto_close_date <EOL> ) <EOL> self . assertEqual ( <EOL> second_auto_close_transaction , <EOL> { <EOL> '<STR_LIT>' : - order_size , <EOL> '<STR_LIT>' : <NUM_LIT:0.0> , <EOL> '<STR_LIT>' : assets [ <NUM_LIT:1> ] . auto_close_date , <EOL> '<STR_LIT>' : fp1 , <EOL> '<STR_LIT>' : sids [ <NUM_LIT:1> ] , <EOL> '<STR_LIT>' : None , <EOL> } , <EOL> ) <EOL> def test_cancel_open_orders ( self ) : <EOL> """<STR_LIT>""" <EOL> auto_close_delta = trading_day <EOL> resources = self . make_data ( auto_close_delta , '<STR_LIT>' ) <EOL> env = resources . env <EOL> assets = resources . assets <EOL> first_asset_end_date = assets [ <NUM_LIT:0> ] . end_date <EOL> first_asset_auto_close_date = assets [ <NUM_LIT:0> ] . auto_close_date <EOL> def initialize ( context ) : <EOL> pass <EOL> def handle_data ( context , data ) : <EOL> assert ( <EOL> context . portfolio . cash == context . portfolio . starting_cash <EOL> ) <EOL> now = context . get_datetime ( ) <EOL> if now == first_asset_end_date : <EOL> assert len ( context . get_open_orders ( ) ) == <NUM_LIT:0> <EOL> context . order ( context . sid ( <NUM_LIT:0> ) , <NUM_LIT:10> ) <EOL> assert len ( context . get_open_orders ( ) ) == <NUM_LIT:1> <EOL> elif now == first_asset_auto_close_date : <EOL> assert len ( context . get_open_orders ( ) ) == <NUM_LIT:0> <EOL> algo = TradingAlgorithm ( <EOL> initialize = initialize , <EOL> handle_data = handle_data , <EOL> env = env , <EOL> sim_params = resources . sim_params <EOL> ) <EOL> results = algo . run ( resources . data_portal ) <EOL> orders = results [ '<STR_LIT>' ] <EOL> def orders_for_date ( date ) : <EOL> return orders . iloc [ self . test_days . get_loc ( date ) ] <EOL> original_open_orders = orders_for_date ( first_asset_end_date ) <EOL> assert len ( original_open_orders ) == <NUM_LIT:1> <EOL> self . assertDictContainsSubset ( <EOL> { <EOL> '<STR_LIT>' : <NUM_LIT:10> , <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : first_asset_end_date , <EOL> '<STR_LIT>' : first_asset_end_date , <EOL> '<STR_LIT>' : assets [ <NUM_LIT:0> ] , <EOL> '<STR_LIT:status>' : ORDER_STATUS . OPEN , <EOL> '<STR_LIT>' : <NUM_LIT:0> , <EOL> } , <EOL> original_open_orders [ <NUM_LIT:0> ] , <EOL> ) <EOL> orders_after_auto_close = orders_for_date ( first_asset_auto_close_date ) <EOL> assert len ( orders_after_auto_close ) == <NUM_LIT:1> <EOL> self . assertDictContainsSubset ( <EOL> { <EOL> '<STR_LIT>' : <NUM_LIT:10> , <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : first_asset_end_date , <EOL> '<STR_LIT>' : first_asset_auto_close_date , <EOL> '<STR_LIT>' : assets [ <NUM_LIT:0> ] , <EOL> '<STR_LIT:status>' : ORDER_STATUS . CANCELLED , <EOL> '<STR_LIT>' : <NUM_LIT:0> , <EOL> } , <EOL> orders_after_auto_close [ <NUM_LIT:0> ] , <EOL> ) <EOL> def test_minutely_delisted_equities ( self ) : <EOL> resources = self . make_data ( trading_day , '<STR_LIT>' ) <EOL> env = resources . env <EOL> assets = resources . assets <EOL> sids = [ a . sid for a in assets ] <EOL> final_prices = resources . final_prices <EOL> backtest_minutes = resources . trade_data_by_sid [ <NUM_LIT:0> ] . index . tolist ( ) <EOL> order_size = <NUM_LIT:10> <EOL> capital_base = <NUM_LIT> <EOL> algo = TradingAlgorithm ( <EOL> initialize = self . default_initialize ( ) , <EOL> handle_data = self . default_handle_data ( assets , order_size ) , <EOL> env = env , <EOL> sim_params = resources . sim_params , <EOL> data_frequency = '<STR_LIT>' , <EOL> capital_base = capital_base , <EOL> ) <EOL> output = algo . run ( resources . data_portal ) <EOL> initial_fill_prices = self . prices_on_tick ( resources . trade_data_by_sid , <NUM_LIT:1> ) <EOL> cost_basis = sum ( initial_fill_prices ) * order_size <EOL> fp0 = final_prices [ <NUM_LIT:0> ] <EOL> fp1 = final_prices [ <NUM_LIT:1> ] <EOL> initial_cash = capital_base <EOL> after_fills = initial_cash - cost_basis <EOL> after_first_auto_close = after_fills + fp0 * ( order_size ) <EOL> after_second_auto_close = after_first_auto_close + fp1 * ( order_size ) <EOL> expected_cash = [ initial_cash ] <EOL> expected_position_counts = [ <NUM_LIT:0> ] <EOL> expected_cash . extend ( [ after_fills ] * ( <NUM_LIT> + <NUM_LIT> + <NUM_LIT> ) ) <EOL> expected_position_counts . extend ( [ <NUM_LIT:3> ] * ( <NUM_LIT> + <NUM_LIT> + <NUM_LIT> ) ) <EOL> expected_cash . extend ( [ after_first_auto_close ] * ( <NUM_LIT> + <NUM_LIT> ) ) <EOL> expected_position_counts . extend ( [ <NUM_LIT:2> ] * ( <NUM_LIT> + <NUM_LIT> ) ) <EOL> expected_cash . extend ( [ after_second_auto_close ] * ( <NUM_LIT> + <NUM_LIT> ) ) <EOL> expected_position_counts . extend ( [ <NUM_LIT:1> ] * ( <NUM_LIT> + <NUM_LIT> ) ) <EOL> self . assertEqual ( algo . cash , expected_cash ) <EOL> self . assertEqual ( <EOL> list ( output [ '<STR_LIT>' ] ) , <EOL> [ <EOL> after_fills , <EOL> after_fills , <EOL> after_fills , <EOL> after_first_auto_close , <EOL> after_first_auto_close , <EOL> after_second_auto_close , <EOL> after_second_auto_close , <EOL> ] , <EOL> ) <EOL> self . assertEqual ( algo . num_positions , expected_position_counts ) <EOL> self . assertEqual ( <EOL> list ( output [ '<STR_LIT>' ] ) , <EOL> [ <NUM_LIT:3> , <NUM_LIT:3> , <NUM_LIT:3> , <NUM_LIT:2> , <NUM_LIT:2> , <NUM_LIT:1> , <NUM_LIT:1> ] , <EOL> ) <EOL> transactions = output [ '<STR_LIT>' ] <EOL> initial_fills = transactions . iloc [ <NUM_LIT:0> ] <EOL> self . assertEqual ( len ( initial_fills ) , len ( assets ) ) <EOL> for sid , txn in zip ( sids , initial_fills ) : <EOL> self . assertDictContainsSubset ( <EOL> { <EOL> '<STR_LIT>' : order_size , <EOL> '<STR_LIT>' : <NUM_LIT:0.0> , <EOL> '<STR_LIT>' : backtest_minutes [ <NUM_LIT:1> ] , <EOL> '<STR_LIT>' : initial_fill_prices [ sid ] , <EOL> '<STR_LIT>' : sid , <EOL> } , <EOL> txn , <EOL> ) <EOL> self . assertIsInstance ( txn [ '<STR_LIT>' ] , str ) <EOL> def transactions_for_date ( date ) : <EOL> return transactions . iloc [ self . test_days . get_loc ( date ) ] <EOL> ( first_auto_close_transaction , ) = transactions_for_date ( <EOL> assets [ <NUM_LIT:0> ] . auto_close_date <EOL> ) <EOL> self . assertEqual ( <EOL> first_auto_close_transaction , <EOL> { <EOL> '<STR_LIT>' : - order_size , <EOL> '<STR_LIT>' : <NUM_LIT:0.0> , <EOL> '<STR_LIT>' : assets [ <NUM_LIT:0> ] . auto_close_date , <EOL> '<STR_LIT>' : fp0 , <EOL> '<STR_LIT>' : sids [ <NUM_LIT:0> ] , <EOL> '<STR_LIT>' : None , <EOL> } , <EOL> ) <EOL> ( second_auto_close_transaction , ) = transactions_for_date ( <EOL> assets [ <NUM_LIT:1> ] . auto_close_date <EOL> ) <EOL> self . assertEqual ( <EOL> second_auto_close_transaction , <EOL> { <EOL> '<STR_LIT>' : - order_size , <EOL> '<STR_LIT>' : <NUM_LIT:0.0> , <EOL> '<STR_LIT>' : assets [ <NUM_LIT:1> ] . auto_close_date , <EOL> '<STR_LIT>' : fp1 , <EOL> '<STR_LIT>' : sids [ <NUM_LIT:1> ] , <EOL> '<STR_LIT>' : None , <EOL> } , <EOL> ) <EOL> class TestOrderAfterDelist ( WithTradingEnvironment , ZiplineTestCase ) : <EOL> start = pd . Timestamp ( '<STR_LIT>' , tz = '<STR_LIT>' ) <EOL> day_1 = pd . Timestamp ( '<STR_LIT>' , tz = '<STR_LIT>' ) <EOL> day_4 = pd . Timestamp ( '<STR_LIT>' , tz = '<STR_LIT>' ) <EOL> end = pd . Timestamp ( '<STR_LIT>' , tz = '<STR_LIT>' ) <EOL> @ classmethod <EOL> def make_equity_info ( cls ) : <EOL> return pd . DataFrame . from_dict ( <EOL> { <EOL> <NUM_LIT:1> : { <EOL> '<STR_LIT>' : cls . start , <EOL> '<STR_LIT>' : cls . day_1 , <EOL> '<STR_LIT>' : cls . day_4 , <EOL> '<STR_LIT>' : "<STR_LIT>" <EOL> } , <EOL> } , <EOL> orient = '<STR_LIT:index>' , <EOL> ) <EOL> @ classmethod <EOL> def init_class_fixtures ( cls ) : <EOL> super ( TestOrderAfterDelist , cls ) . init_class_fixtures ( ) <EOL> cls . data_portal = FakeDataPortal ( cls . env ) <EOL> def test_order_in_quiet_period ( self ) : <EOL> algo_code = dedent ( """<STR_LIT>""" ) <EOL> algo = TradingAlgorithm ( <EOL> script = algo_code , <EOL> env = self . env , <EOL> sim_params = SimulationParameters ( <EOL> period_start = pd . Timestamp ( "<STR_LIT>" , tz = '<STR_LIT>' ) , <EOL> period_end = pd . Timestamp ( "<STR_LIT>" , tz = '<STR_LIT>' ) , <EOL> env = self . env , <EOL> data_frequency = "<STR_LIT>" <EOL> ) <EOL> ) <EOL> with make_test_handler ( self ) as log_catcher : <EOL> algo . run ( self . data_portal ) <EOL> warnings = [ r for r in log_catcher . records <EOL> if r . level == logbook . WARNING ] <EOL> self . assertEqual ( <NUM_LIT:6> * <NUM_LIT> , len ( warnings ) ) <EOL> for w in warnings : <EOL> self . assertEqual ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> w . message ) </s>
