<s> from nose_parameterized import parameterized <EOL> import numpy as np <EOL> import pandas as pd <EOL> from toolz import merge <EOL> from zipline . _protocol import handle_non_market_minutes <EOL> from zipline . protocol import BarData <EOL> from zipline . testing import ( <EOL> MockDailyBarReader , <EOL> create_daily_df_for_asset , <EOL> create_minute_df_for_asset , <EOL> str_to_seconds , <EOL> ) <EOL> from zipline . testing . fixtures import ( <EOL> WithDataPortal , <EOL> ZiplineTestCase , <EOL> ) <EOL> OHLC = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> OHLCP = OHLC + [ "<STR_LIT>" ] <EOL> ALL_FIELDS = OHLCP + [ "<STR_LIT>" , "<STR_LIT>" ] <EOL> field_info = { <EOL> "<STR_LIT>" : <NUM_LIT:1> , <EOL> "<STR_LIT>" : <NUM_LIT:2> , <EOL> "<STR_LIT>" : - <NUM_LIT:1> , <EOL> "<STR_LIT>" : <NUM_LIT:0> <EOL> } <EOL> class WithBarDataChecks ( object ) : <EOL> def assert_same ( self , val1 , val2 ) : <EOL> try : <EOL> self . assertEqual ( val1 , val2 ) <EOL> except AssertionError : <EOL> if val1 is pd . NaT : <EOL> self . assertTrue ( val2 is pd . NaT ) <EOL> elif np . isnan ( val1 ) : <EOL> self . assertTrue ( np . isnan ( val2 ) ) <EOL> else : <EOL> raise <EOL> def check_internal_consistency ( self , bar_data ) : <EOL> df = bar_data . current ( [ self . ASSET1 , self . ASSET2 ] , ALL_FIELDS ) <EOL> asset1_multi_field = bar_data . current ( self . ASSET1 , ALL_FIELDS ) <EOL> asset2_multi_field = bar_data . current ( self . ASSET2 , ALL_FIELDS ) <EOL> for field in ALL_FIELDS : <EOL> asset1_value = bar_data . current ( self . ASSET1 , field ) <EOL> asset2_value = bar_data . current ( self . ASSET2 , field ) <EOL> multi_asset_series = bar_data . current ( <EOL> [ self . ASSET1 , self . ASSET2 ] , field <EOL> ) <EOL> self . assert_same ( multi_asset_series . loc [ self . ASSET1 ] , asset1_value ) <EOL> self . assert_same ( multi_asset_series . loc [ self . ASSET2 ] , asset2_value ) <EOL> self . assert_same ( df . loc [ self . ASSET1 ] [ field ] , asset1_value ) <EOL> self . assert_same ( df . loc [ self . ASSET2 ] [ field ] , asset2_value ) <EOL> self . assert_same ( asset1_multi_field [ field ] , asset1_value ) <EOL> self . assert_same ( asset2_multi_field [ field ] , asset2_value ) <EOL> for field in [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" ] : <EOL> with self . assertRaises ( AttributeError ) : <EOL> getattr ( bar_data , field ) <EOL> class TestMinuteBarData ( WithBarDataChecks , <EOL> WithDataPortal , <EOL> ZiplineTestCase ) : <EOL> START_DATE = pd . Timestamp ( '<STR_LIT>' , tz = '<STR_LIT>' ) <EOL> END_DATE = ASSET_FINDER_EQUITY_END_DATE = pd . Timestamp ( <EOL> '<STR_LIT>' , <EOL> tz = '<STR_LIT>' , <EOL> ) <EOL> ASSET_FINDER_EQUITY_SIDS = <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:5> <EOL> SPLIT_ASSET_SID = <NUM_LIT:3> <EOL> ILLIQUID_SPLIT_ASSET_SID = <NUM_LIT:4> <EOL> HILARIOUSLY_ILLIQUID_ASSET_SID = <NUM_LIT:5> <EOL> @ classmethod <EOL> def make_minute_bar_data ( cls ) : <EOL> return merge ( <EOL> { <EOL> sid : create_minute_df_for_asset ( <EOL> cls . env , <EOL> cls . bcolz_minute_bar_days [ <NUM_LIT:0> ] , <EOL> cls . bcolz_minute_bar_days [ - <NUM_LIT:1> ] , <EOL> ) <EOL> for sid in ( <NUM_LIT:1> , cls . SPLIT_ASSET_SID ) <EOL> } , <EOL> { <EOL> sid : create_minute_df_for_asset ( <EOL> cls . env , <EOL> cls . bcolz_minute_bar_days [ <NUM_LIT:0> ] , <EOL> cls . bcolz_minute_bar_days [ - <NUM_LIT:1> ] , <EOL> <NUM_LIT:10> , <EOL> ) <EOL> for sid in ( <NUM_LIT:2> , cls . ILLIQUID_SPLIT_ASSET_SID ) <EOL> } , <EOL> { <EOL> cls . HILARIOUSLY_ILLIQUID_ASSET_SID : create_minute_df_for_asset ( <EOL> cls . env , <EOL> cls . bcolz_minute_bar_days [ <NUM_LIT:0> ] , <EOL> cls . bcolz_minute_bar_days [ - <NUM_LIT:1> ] , <EOL> <NUM_LIT:50> , <EOL> ) <EOL> } , <EOL> ) <EOL> @ classmethod <EOL> def make_splits_data ( cls ) : <EOL> return pd . DataFrame ( [ <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> '<STR_LIT>' : str_to_seconds ( "<STR_LIT>" ) , <EOL> '<STR_LIT>' : <NUM_LIT:0.5> , <EOL> '<STR_LIT>' : cls . ILLIQUID_SPLIT_ASSET_SID , <EOL> } , <EOL> ] ) <EOL> @ classmethod <EOL> def init_class_fixtures ( cls ) : <EOL> super ( TestMinuteBarData , cls ) . init_class_fixtures ( ) <EOL> cls . ASSET1 = cls . asset_finder . retrieve_asset ( <NUM_LIT:1> ) <EOL> cls . ASSET2 = cls . asset_finder . retrieve_asset ( <NUM_LIT:2> ) <EOL> cls . SPLIT_ASSET = cls . asset_finder . retrieve_asset ( <EOL> cls . SPLIT_ASSET_SID , <EOL> ) <EOL> cls . ILLIQUID_SPLIT_ASSET = cls . asset_finder . retrieve_asset ( <EOL> cls . ILLIQUID_SPLIT_ASSET_SID , <EOL> ) <EOL> cls . HILARIOUSLY_ILLIQUID_ASSET = cls . asset_finder . retrieve_asset ( <EOL> cls . HILARIOUSLY_ILLIQUID_ASSET_SID , <EOL> ) <EOL> cls . ASSETS = [ cls . ASSET1 , cls . ASSET2 ] <EOL> def test_minute_before_assets_trading ( self ) : <EOL> minutes = self . env . market_minutes_for_day ( <EOL> self . env . previous_trading_day ( self . bcolz_minute_bar_days [ <NUM_LIT:0> ] ) <EOL> ) <EOL> for idx , minute in enumerate ( minutes ) : <EOL> bar_data = BarData ( self . data_portal , lambda : minute , "<STR_LIT>" ) <EOL> self . check_internal_consistency ( bar_data ) <EOL> self . assertFalse ( bar_data . can_trade ( self . ASSET1 ) ) <EOL> self . assertFalse ( bar_data . can_trade ( self . ASSET2 ) ) <EOL> self . assertFalse ( bar_data . is_stale ( self . ASSET1 ) ) <EOL> self . assertFalse ( bar_data . is_stale ( self . ASSET2 ) ) <EOL> for field in ALL_FIELDS : <EOL> for asset in self . ASSETS : <EOL> asset_value = bar_data . current ( asset , field ) <EOL> if field in OHLCP : <EOL> self . assertTrue ( np . isnan ( asset_value ) ) <EOL> elif field == "<STR_LIT>" : <EOL> self . assertEqual ( <NUM_LIT:0> , asset_value ) <EOL> elif field == "<STR_LIT>" : <EOL> self . assertTrue ( asset_value is pd . NaT ) <EOL> def test_regular_minute ( self ) : <EOL> minutes = self . env . market_minutes_for_day ( <EOL> self . bcolz_minute_bar_days [ <NUM_LIT:0> ] , <EOL> ) <EOL> for idx , minute in enumerate ( minutes ) : <EOL> bar_data = BarData ( self . data_portal , lambda : minute , "<STR_LIT>" ) <EOL> self . check_internal_consistency ( bar_data ) <EOL> asset2_has_data = ( ( ( idx + <NUM_LIT:1> ) % <NUM_LIT:10> ) == <NUM_LIT:0> ) <EOL> self . assertTrue ( bar_data . can_trade ( self . ASSET1 ) ) <EOL> self . assertFalse ( bar_data . is_stale ( self . ASSET1 ) ) <EOL> if idx < <NUM_LIT:9> : <EOL> self . assertFalse ( bar_data . can_trade ( self . ASSET2 ) ) <EOL> self . assertFalse ( bar_data . is_stale ( self . ASSET2 ) ) <EOL> else : <EOL> self . assertTrue ( bar_data . can_trade ( self . ASSET2 ) ) <EOL> if asset2_has_data : <EOL> self . assertFalse ( bar_data . is_stale ( self . ASSET2 ) ) <EOL> else : <EOL> self . assertTrue ( bar_data . is_stale ( self . ASSET2 ) ) <EOL> for field in ALL_FIELDS : <EOL> asset1_value = bar_data . current ( self . ASSET1 , field ) <EOL> asset2_value = bar_data . current ( self . ASSET2 , field ) <EOL> if idx == <NUM_LIT:0> and field == "<STR_LIT>" : <EOL> self . assertTrue ( np . isnan ( asset1_value ) ) <EOL> else : <EOL> if field in OHLC : <EOL> self . assertEqual ( <EOL> idx + <NUM_LIT:1> + field_info [ field ] , <EOL> asset1_value <EOL> ) <EOL> if asset2_has_data : <EOL> self . assertEqual ( <EOL> idx + <NUM_LIT:1> + field_info [ field ] , <EOL> asset2_value <EOL> ) <EOL> else : <EOL> self . assertTrue ( np . isnan ( asset2_value ) ) <EOL> elif field == "<STR_LIT>" : <EOL> self . assertEqual ( ( idx + <NUM_LIT:1> ) * <NUM_LIT:100> , asset1_value ) <EOL> if asset2_has_data : <EOL> self . assertEqual ( ( idx + <NUM_LIT:1> ) * <NUM_LIT:100> , asset2_value ) <EOL> else : <EOL> self . assertEqual ( <NUM_LIT:0> , asset2_value ) <EOL> elif field == "<STR_LIT>" : <EOL> self . assertEqual ( idx + <NUM_LIT:1> , asset1_value ) <EOL> if asset2_has_data : <EOL> self . assertEqual ( idx + <NUM_LIT:1> , asset2_value ) <EOL> elif idx < <NUM_LIT:9> : <EOL> self . assertTrue ( np . isnan ( asset2_value ) ) <EOL> else : <EOL> self . assertEqual ( ( idx // <NUM_LIT:10> ) * <NUM_LIT:10> , asset2_value ) <EOL> elif field == "<STR_LIT>" : <EOL> self . assertEqual ( minute , asset1_value ) <EOL> if idx < <NUM_LIT:9> : <EOL> self . assertTrue ( asset2_value is pd . NaT ) <EOL> elif asset2_has_data : <EOL> self . assertEqual ( minute , asset2_value ) <EOL> else : <EOL> last_traded_minute = minutes [ ( idx // <NUM_LIT:10> ) * <NUM_LIT:10> ] <EOL> self . assertEqual ( last_traded_minute - <NUM_LIT:1> , <EOL> asset2_value ) <EOL> def test_minute_of_last_day ( self ) : <EOL> minutes = self . env . market_minutes_for_day ( <EOL> self . bcolz_daily_bar_days [ - <NUM_LIT:1> ] , <EOL> ) <EOL> for idx , minute in enumerate ( minutes ) : <EOL> bar_data = BarData ( self . data_portal , lambda : minute , "<STR_LIT>" ) <EOL> self . assertTrue ( bar_data . can_trade ( self . ASSET1 ) ) <EOL> self . assertTrue ( bar_data . can_trade ( self . ASSET2 ) ) <EOL> def test_minute_after_assets_stopped ( self ) : <EOL> minutes = self . env . market_minutes_for_day ( <EOL> self . env . next_trading_day ( self . bcolz_minute_bar_days [ - <NUM_LIT:1> ] ) <EOL> ) <EOL> last_trading_minute = self . env . market_minutes_for_day ( self . bcolz_minute_bar_days [ - <NUM_LIT:1> ] ) [ - <NUM_LIT:1> ] <EOL> for idx , minute in enumerate ( minutes ) : <EOL> bar_data = BarData ( self . data_portal , lambda : minute , "<STR_LIT>" ) <EOL> self . assertFalse ( bar_data . can_trade ( self . ASSET1 ) ) <EOL> self . assertFalse ( bar_data . can_trade ( self . ASSET2 ) ) <EOL> self . assertFalse ( bar_data . is_stale ( self . ASSET1 ) ) <EOL> self . assertFalse ( bar_data . is_stale ( self . ASSET2 ) ) <EOL> self . check_internal_consistency ( bar_data ) <EOL> for field in ALL_FIELDS : <EOL> for asset in self . ASSETS : <EOL> asset_value = bar_data . current ( asset , field ) <EOL> if field in OHLCP : <EOL> self . assertTrue ( np . isnan ( asset_value ) ) <EOL> elif field == "<STR_LIT>" : <EOL> self . assertEqual ( <NUM_LIT:0> , asset_value ) <EOL> elif field == "<STR_LIT>" : <EOL> self . assertEqual ( last_trading_minute , asset_value ) <EOL> def test_spot_price_is_unadjusted ( self ) : <EOL> splits = self . adjustment_reader . get_adjustments_for_sid ( <EOL> "<STR_LIT>" , <EOL> self . SPLIT_ASSET . sid <EOL> ) <EOL> self . assertEqual ( <NUM_LIT:1> , len ( splits ) ) <EOL> split = splits [ <NUM_LIT:0> ] <EOL> self . assertEqual ( <EOL> split [ <NUM_LIT:0> ] , <EOL> pd . Timestamp ( "<STR_LIT>" , tz = '<STR_LIT>' ) <EOL> ) <EOL> minutes = self . env . minutes_for_days_in_range ( <EOL> start = self . bcolz_minute_bar_days [ <NUM_LIT:0> ] , <EOL> end = self . bcolz_minute_bar_days [ <NUM_LIT:1> ] , <EOL> ) <EOL> for idx , minute in enumerate ( minutes ) : <EOL> bar_data = BarData ( self . data_portal , lambda : minute , "<STR_LIT>" ) <EOL> self . assertEqual ( <EOL> idx + <NUM_LIT:1> , <EOL> bar_data . current ( self . SPLIT_ASSET , "<STR_LIT>" ) <EOL> ) <EOL> def test_spot_price_is_adjusted_if_needed ( self ) : <EOL> day0_minutes = self . env . market_minutes_for_day ( <EOL> self . bcolz_minute_bar_days [ <NUM_LIT:0> ] , <EOL> ) <EOL> day1_minutes = self . env . market_minutes_for_day ( <EOL> self . bcolz_minute_bar_days [ <NUM_LIT:1> ] , <EOL> ) <EOL> for idx , minute in enumerate ( day0_minutes [ - <NUM_LIT:10> : - <NUM_LIT:1> ] ) : <EOL> bar_data = BarData ( self . data_portal , lambda : minute , "<STR_LIT>" ) <EOL> self . assertEqual ( <EOL> <NUM_LIT> , <EOL> bar_data . current ( self . ILLIQUID_SPLIT_ASSET , "<STR_LIT>" ) <EOL> ) <EOL> bar_data = BarData ( <EOL> self . data_portal , lambda : day0_minutes [ - <NUM_LIT:1> ] , "<STR_LIT>" <EOL> ) <EOL> self . assertEqual ( <EOL> <NUM_LIT> , <EOL> bar_data . current ( self . ILLIQUID_SPLIT_ASSET , "<STR_LIT>" ) <EOL> ) <EOL> for idx , minute in enumerate ( day1_minutes [ <NUM_LIT:0> : <NUM_LIT:9> ] ) : <EOL> bar_data = BarData ( self . data_portal , lambda : minute , "<STR_LIT>" ) <EOL> self . assertEqual ( <EOL> <NUM_LIT> , <EOL> bar_data . current ( self . ILLIQUID_SPLIT_ASSET , "<STR_LIT>" ) <EOL> ) <EOL> def test_spot_price_at_midnight ( self ) : <EOL> day = self . bcolz_minute_bar_days [ <NUM_LIT:1> ] <EOL> eight_fortyfive_am_eastern = pd . Timestamp ( "<STR_LIT>" . format ( <EOL> day . year , day . month , day . day ) , <EOL> tz = '<STR_LIT>' <EOL> ) <EOL> bar_data = BarData ( self . data_portal , lambda : day , "<STR_LIT>" ) <EOL> bar_data2 = BarData ( self . data_portal , <EOL> lambda : eight_fortyfive_am_eastern , <EOL> "<STR_LIT>" ) <EOL> with handle_non_market_minutes ( bar_data ) , handle_non_market_minutes ( bar_data2 ) : <EOL> for bd in [ bar_data , bar_data2 ] : <EOL> for field in [ "<STR_LIT>" , "<STR_LIT>" ] : <EOL> self . assertEqual ( <EOL> <NUM_LIT> , <EOL> bd . current ( self . ASSET1 , field ) <EOL> ) <EOL> self . assertEqual ( <EOL> <NUM_LIT> , <EOL> bd . current ( self . HILARIOUSLY_ILLIQUID_ASSET , "<STR_LIT>" ) <EOL> ) <EOL> self . assertTrue ( <EOL> np . isnan ( bd . current ( self . HILARIOUSLY_ILLIQUID_ASSET , <EOL> "<STR_LIT>" ) ) <EOL> ) <EOL> self . assertEqual ( <EOL> <NUM_LIT:0> , <EOL> bd . current ( self . HILARIOUSLY_ILLIQUID_ASSET , "<STR_LIT>" ) <EOL> ) <EOL> def test_can_trade_at_midnight ( self ) : <EOL> the_day_after = self . env . next_trading_day ( <EOL> self . bcolz_minute_bar_days [ - <NUM_LIT:1> ] , <EOL> ) <EOL> bar_data = BarData ( self . data_portal , lambda : the_day_after , "<STR_LIT>" ) <EOL> for asset in [ self . ASSET1 , self . HILARIOUSLY_ILLIQUID_ASSET ] : <EOL> self . assertFalse ( bar_data . can_trade ( asset ) ) <EOL> with handle_non_market_minutes ( bar_data ) : <EOL> self . assertFalse ( bar_data . can_trade ( asset ) ) <EOL> bar_data2 = BarData ( <EOL> self . data_portal , <EOL> lambda : self . bcolz_minute_bar_days [ <NUM_LIT:1> ] , <EOL> "<STR_LIT>" , <EOL> ) <EOL> for asset in [ self . ASSET1 , self . HILARIOUSLY_ILLIQUID_ASSET ] : <EOL> self . assertTrue ( bar_data2 . can_trade ( asset ) ) <EOL> with handle_non_market_minutes ( bar_data2 ) : <EOL> self . assertTrue ( bar_data2 . can_trade ( asset ) ) <EOL> def test_is_stale_at_midnight ( self ) : <EOL> bar_data = BarData ( <EOL> self . data_portal , <EOL> lambda : self . bcolz_minute_bar_days [ <NUM_LIT:1> ] , <EOL> "<STR_LIT>" , <EOL> ) <EOL> with handle_non_market_minutes ( bar_data ) : <EOL> self . assertTrue ( bar_data . is_stale ( self . HILARIOUSLY_ILLIQUID_ASSET ) ) <EOL> def test_overnight_adjustments ( self ) : <EOL> splits = self . adjustment_reader . get_adjustments_for_sid ( <EOL> "<STR_LIT>" , <EOL> self . SPLIT_ASSET . sid <EOL> ) <EOL> self . assertEqual ( <NUM_LIT:1> , len ( splits ) ) <EOL> split = splits [ <NUM_LIT:0> ] <EOL> self . assertEqual ( <EOL> split [ <NUM_LIT:0> ] , <EOL> pd . Timestamp ( "<STR_LIT>" , tz = '<STR_LIT>' ) <EOL> ) <EOL> day = self . bcolz_daily_bar_days [ <NUM_LIT:1> ] <EOL> eight_fortyfive_am_eastern = pd . Timestamp ( "<STR_LIT>" . format ( <EOL> day . year , day . month , day . day ) , <EOL> tz = '<STR_LIT>' <EOL> ) <EOL> bar_data = BarData ( self . data_portal , <EOL> lambda : eight_fortyfive_am_eastern , <EOL> "<STR_LIT>" ) <EOL> expected = { <EOL> '<STR_LIT>' : <NUM_LIT> / <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> / <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> / <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> / <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> * <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> / <NUM_LIT> , <EOL> } <EOL> with handle_non_market_minutes ( bar_data ) : <EOL> for field in OHLCP + [ '<STR_LIT>' ] : <EOL> value = bar_data . current ( self . SPLIT_ASSET , field ) <EOL> self . assertEqual ( value , expected [ field ] ) <EOL> class TestDailyBarData ( WithBarDataChecks , <EOL> WithDataPortal , <EOL> ZiplineTestCase ) : <EOL> START_DATE = pd . Timestamp ( '<STR_LIT>' , tz = '<STR_LIT>' ) <EOL> END_DATE = ASSET_FINDER_EQUITY_END_DATE = pd . Timestamp ( <EOL> '<STR_LIT>' , <EOL> tz = '<STR_LIT>' , <EOL> ) <EOL> sids = ASSET_FINDER_EQUITY_SIDS = set ( range ( <NUM_LIT:1> , <NUM_LIT:9> ) ) <EOL> SPLIT_ASSET_SID = <NUM_LIT:3> <EOL> ILLIQUID_SPLIT_ASSET_SID = <NUM_LIT:4> <EOL> MERGER_ASSET_SID = <NUM_LIT:5> <EOL> ILLIQUID_MERGER_ASSET_SID = <NUM_LIT:6> <EOL> DIVIDEND_ASSET_SID = <NUM_LIT:7> <EOL> ILLIQUID_DIVIDEND_ASSET_SID = <NUM_LIT:8> <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> '<STR_LIT>' : str_to_seconds ( "<STR_LIT>" ) , <EOL> '<STR_LIT>' : <NUM_LIT:0.5> , <EOL> '<STR_LIT>' : cls . ILLIQUID_SPLIT_ASSET_SID , <EOL> } , <EOL> ] ) <EOL> @ classmethod <EOL> def make_mergers_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 . MERGER_ASSET_SID , <EOL> } , <EOL> { <EOL> '<STR_LIT>' : str_to_seconds ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : cls . ILLIQUID_MERGER_ASSET_SID , <EOL> } <EOL> ] ) <EOL> @ classmethod <EOL> def make_dividends_data ( cls ) : <EOL> return pd . DataFrame . from_records ( [ <EOL> { <EOL> '<STR_LIT>' : <EOL> pd . Timestamp ( '<STR_LIT>' , tz = '<STR_LIT>' ) . to_datetime64 ( ) , <EOL> '<STR_LIT>' : <EOL> pd . Timestamp ( '<STR_LIT>' , tz = '<STR_LIT>' ) . to_datetime64 ( ) , <EOL> '<STR_LIT>' : <EOL> pd . Timestamp ( '<STR_LIT>' , tz = '<STR_LIT>' ) . to_datetime64 ( ) , <EOL> '<STR_LIT>' : <EOL> pd . Timestamp ( '<STR_LIT>' , tz = '<STR_LIT>' ) . to_datetime64 ( ) , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : cls . DIVIDEND_ASSET_SID , <EOL> } , <EOL> { <EOL> '<STR_LIT>' : <EOL> pd . Timestamp ( '<STR_LIT>' , tz = '<STR_LIT>' ) . to_datetime64 ( ) , <EOL> '<STR_LIT>' : <EOL> pd . Timestamp ( '<STR_LIT>' , tz = '<STR_LIT>' ) . to_datetime64 ( ) , <EOL> '<STR_LIT>' : <EOL> pd . Timestamp ( '<STR_LIT>' , tz = '<STR_LIT>' ) . to_datetime64 ( ) , <EOL> '<STR_LIT>' : <EOL> pd . Timestamp ( '<STR_LIT>' , tz = '<STR_LIT>' ) . to_datetime64 ( ) , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : cls . ILLIQUID_DIVIDEND_ASSET_SID , <EOL> } ] , <EOL> columns = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] <EOL> ) <EOL> @ classmethod <EOL> def make_adjustment_writer_daily_bar_reader ( cls ) : <EOL> return MockDailyBarReader ( ) <EOL> @ classmethod <EOL> def make_daily_bar_data ( cls ) : <EOL> for sid in cls . sids : <EOL> yield sid , create_daily_df_for_asset ( <EOL> cls . env , <EOL> cls . bcolz_daily_bar_days [ <NUM_LIT:0> ] , <EOL> cls . bcolz_daily_bar_days [ - <NUM_LIT:1> ] , <EOL> interval = <NUM_LIT:2> - sid % <NUM_LIT:2> <EOL> ) <EOL> @ classmethod <EOL> def init_class_fixtures ( cls ) : <EOL> super ( TestDailyBarData , cls ) . init_class_fixtures ( ) <EOL> cls . ASSET1 = cls . asset_finder . retrieve_asset ( <NUM_LIT:1> ) <EOL> cls . ASSET2 = cls . asset_finder . retrieve_asset ( <NUM_LIT:2> ) <EOL> cls . SPLIT_ASSET = cls . asset_finder . retrieve_asset ( <EOL> cls . SPLIT_ASSET_SID , <EOL> ) <EOL> cls . ILLIQUID_SPLIT_ASSET = cls . asset_finder . retrieve_asset ( <EOL> cls . ILLIQUID_SPLIT_ASSET_SID , <EOL> ) <EOL> cls . MERGER_ASSET = cls . asset_finder . retrieve_asset ( <EOL> cls . MERGER_ASSET_SID , <EOL> ) <EOL> cls . ILLIQUID_MERGER_ASSET = cls . asset_finder . retrieve_asset ( <EOL> cls . ILLIQUID_MERGER_ASSET_SID , <EOL> ) <EOL> cls . DIVIDEND_ASSET = cls . asset_finder . retrieve_asset ( <EOL> cls . DIVIDEND_ASSET_SID , <EOL> ) <EOL> cls . ILLIQUID_DIVIDEND_ASSET = cls . asset_finder . retrieve_asset ( <EOL> cls . ILLIQUID_DIVIDEND_ASSET_SID , <EOL> ) <EOL> cls . ASSETS = [ cls . ASSET1 , cls . ASSET2 ] <EOL> def test_day_before_assets_trading ( self ) : <EOL> day = self . env . previous_trading_day ( self . bcolz_daily_bar_days [ <NUM_LIT:0> ] ) <EOL> bar_data = BarData ( self . data_portal , lambda : day , "<STR_LIT>" ) <EOL> self . check_internal_consistency ( bar_data ) <EOL> self . assertFalse ( bar_data . can_trade ( self . ASSET1 ) ) <EOL> self . assertFalse ( bar_data . can_trade ( self . ASSET2 ) ) <EOL> self . assertFalse ( bar_data . is_stale ( self . ASSET1 ) ) <EOL> self . assertFalse ( bar_data . is_stale ( self . ASSET2 ) ) <EOL> for field in ALL_FIELDS : <EOL> for asset in self . ASSETS : <EOL> asset_value = bar_data . current ( asset , field ) <EOL> if field in OHLCP : <EOL> self . assertTrue ( np . isnan ( asset_value ) ) <EOL> elif field == "<STR_LIT>" : <EOL> self . assertEqual ( <NUM_LIT:0> , asset_value ) <EOL> elif field == "<STR_LIT>" : <EOL> self . assertTrue ( asset_value is pd . NaT ) <EOL> def test_semi_active_day ( self ) : <EOL> bar_data = BarData ( <EOL> self . data_portal , <EOL> lambda : self . bcolz_daily_bar_days [ <NUM_LIT:0> ] , <EOL> "<STR_LIT>" , <EOL> ) <EOL> self . check_internal_consistency ( bar_data ) <EOL> self . assertTrue ( bar_data . can_trade ( self . ASSET1 ) ) <EOL> self . assertFalse ( bar_data . can_trade ( self . ASSET2 ) ) <EOL> self . assertFalse ( bar_data . is_stale ( self . ASSET1 ) ) <EOL> self . assertFalse ( bar_data . is_stale ( self . ASSET2 ) ) <EOL> self . assertEqual ( <NUM_LIT:3> , bar_data . current ( self . ASSET1 , "<STR_LIT>" ) ) <EOL> self . assertEqual ( <NUM_LIT:4> , bar_data . current ( self . ASSET1 , "<STR_LIT>" ) ) <EOL> self . assertEqual ( <NUM_LIT:1> , bar_data . current ( self . ASSET1 , "<STR_LIT>" ) ) <EOL> self . assertEqual ( <NUM_LIT:2> , bar_data . current ( self . ASSET1 , "<STR_LIT>" ) ) <EOL> self . assertEqual ( <NUM_LIT:200> , bar_data . current ( self . ASSET1 , "<STR_LIT>" ) ) <EOL> self . assertEqual ( <NUM_LIT:2> , bar_data . current ( self . ASSET1 , "<STR_LIT>" ) ) <EOL> self . assertEqual ( self . bcolz_daily_bar_days [ <NUM_LIT:0> ] , <EOL> bar_data . current ( self . ASSET1 , "<STR_LIT>" ) ) <EOL> for field in OHLCP : <EOL> self . assertTrue ( np . isnan ( bar_data . current ( self . ASSET2 , field ) ) , <EOL> field ) <EOL> self . assertEqual ( <NUM_LIT:0> , bar_data . current ( self . ASSET2 , "<STR_LIT>" ) ) <EOL> self . assertTrue ( <EOL> bar_data . current ( self . ASSET2 , "<STR_LIT>" ) is pd . NaT <EOL> ) <EOL> def test_fully_active_day ( self ) : <EOL> bar_data = BarData ( <EOL> self . data_portal , <EOL> lambda : self . bcolz_daily_bar_days [ <NUM_LIT:1> ] , <EOL> "<STR_LIT>" , <EOL> ) <EOL> self . check_internal_consistency ( bar_data ) <EOL> for asset in self . ASSETS : <EOL> self . assertTrue ( bar_data . can_trade ( asset ) ) <EOL> self . assertFalse ( bar_data . is_stale ( asset ) ) <EOL> self . assertEqual ( <NUM_LIT:4> , bar_data . current ( asset , "<STR_LIT>" ) ) <EOL> self . assertEqual ( <NUM_LIT:5> , bar_data . current ( asset , "<STR_LIT>" ) ) <EOL> self . assertEqual ( <NUM_LIT:2> , bar_data . current ( asset , "<STR_LIT>" ) ) <EOL> self . assertEqual ( <NUM_LIT:3> , bar_data . current ( asset , "<STR_LIT>" ) ) <EOL> self . assertEqual ( <NUM_LIT> , bar_data . current ( asset , "<STR_LIT>" ) ) <EOL> self . assertEqual ( <NUM_LIT:3> , bar_data . current ( asset , "<STR_LIT>" ) ) <EOL> self . assertEqual ( <EOL> self . bcolz_daily_bar_days [ <NUM_LIT:1> ] , <EOL> bar_data . current ( asset , "<STR_LIT>" ) <EOL> ) <EOL> def test_last_active_day ( self ) : <EOL> bar_data = BarData ( <EOL> self . data_portal , <EOL> lambda : self . bcolz_daily_bar_days [ - <NUM_LIT:1> ] , <EOL> "<STR_LIT>" , <EOL> ) <EOL> self . check_internal_consistency ( bar_data ) <EOL> for asset in self . ASSETS : <EOL> self . assertTrue ( bar_data . can_trade ( asset ) ) <EOL> self . assertFalse ( bar_data . is_stale ( asset ) ) <EOL> self . assertEqual ( <NUM_LIT:6> , bar_data . current ( asset , "<STR_LIT>" ) ) <EOL> self . assertEqual ( <NUM_LIT:7> , bar_data . current ( asset , "<STR_LIT>" ) ) <EOL> self . assertEqual ( <NUM_LIT:4> , bar_data . current ( asset , "<STR_LIT>" ) ) <EOL> self . assertEqual ( <NUM_LIT:5> , bar_data . current ( asset , "<STR_LIT>" ) ) <EOL> self . assertEqual ( <NUM_LIT> , bar_data . current ( asset , "<STR_LIT>" ) ) <EOL> self . assertEqual ( <NUM_LIT:5> , bar_data . current ( asset , "<STR_LIT>" ) ) <EOL> def test_after_assets_dead ( self ) : <EOL> next_day = self . env . next_trading_day ( self . bcolz_daily_bar_days [ - <NUM_LIT:1> ] ) <EOL> bar_data = BarData ( self . data_portal , lambda : next_day , "<STR_LIT>" ) <EOL> self . check_internal_consistency ( bar_data ) <EOL> for asset in self . ASSETS : <EOL> self . assertFalse ( bar_data . can_trade ( asset ) ) <EOL> self . assertFalse ( bar_data . is_stale ( asset ) ) <EOL> for field in OHLCP : <EOL> self . assertTrue ( np . isnan ( bar_data . current ( asset , field ) ) ) <EOL> self . assertEqual ( <NUM_LIT:0> , bar_data . current ( asset , "<STR_LIT>" ) ) <EOL> last_traded_dt = bar_data . current ( asset , "<STR_LIT>" ) <EOL> if asset == self . ASSET1 : <EOL> self . assertEqual ( self . bcolz_daily_bar_days [ - <NUM_LIT:2> ] , last_traded_dt ) <EOL> else : <EOL> self . assertEqual ( self . bcolz_daily_bar_days [ <NUM_LIT:1> ] , last_traded_dt ) <EOL> @ parameterized . expand ( [ <EOL> ( "<STR_LIT>" , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:3> , <NUM_LIT> ) , <EOL> ( "<STR_LIT>" , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:3> , <NUM_LIT> ) , <EOL> ( "<STR_LIT>" , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:3> , <NUM_LIT> ) <EOL> ] ) <EOL> def test_spot_price_adjustments ( self , <EOL> adjustment_type , <EOL> liquid_day_0_price , <EOL> liquid_day_1_price , <EOL> illiquid_day_0_price , <EOL> illiquid_day_1_price_adjusted ) : <EOL> """<STR_LIT>""" <EOL> table_name = adjustment_type + '<STR_LIT:s>' <EOL> liquid_asset = getattr ( self , ( adjustment_type . upper ( ) + "<STR_LIT>" ) ) <EOL> illiquid_asset = getattr ( <EOL> self , <EOL> ( "<STR_LIT>" + adjustment_type . upper ( ) + "<STR_LIT>" ) <EOL> ) <EOL> adjustments = self . adjustment_reader . get_adjustments_for_sid ( <EOL> table_name , <EOL> liquid_asset . sid <EOL> ) <EOL> self . assertEqual ( <NUM_LIT:1> , len ( adjustments ) ) <EOL> adjustment = adjustments [ <NUM_LIT:0> ] <EOL> self . assertEqual ( <EOL> adjustment [ <NUM_LIT:0> ] , <EOL> pd . Timestamp ( "<STR_LIT>" , tz = '<STR_LIT>' ) <EOL> ) <EOL> bar_data = BarData ( <EOL> self . data_portal , <EOL> lambda : self . bcolz_daily_bar_days [ <NUM_LIT:0> ] , <EOL> "<STR_LIT>" , <EOL> ) <EOL> self . assertEqual ( <EOL> liquid_day_0_price , <EOL> bar_data . current ( liquid_asset , "<STR_LIT>" ) <EOL> ) <EOL> bar_data = BarData ( <EOL> self . data_portal , <EOL> lambda : self . bcolz_daily_bar_days [ <NUM_LIT:1> ] , <EOL> "<STR_LIT>" , <EOL> ) <EOL> self . assertEqual ( <EOL> liquid_day_1_price , <EOL> bar_data . current ( liquid_asset , "<STR_LIT>" ) <EOL> ) <EOL> bar_data = BarData ( <EOL> self . data_portal , <EOL> lambda : self . bcolz_daily_bar_days [ <NUM_LIT:1> ] , <EOL> "<STR_LIT>" , <EOL> ) <EOL> self . assertEqual ( <EOL> illiquid_day_0_price , bar_data . current ( illiquid_asset , "<STR_LIT>" ) <EOL> ) <EOL> bar_data = BarData ( <EOL> self . data_portal , <EOL> lambda : self . bcolz_daily_bar_days [ <NUM_LIT:2> ] , <EOL> "<STR_LIT>" , <EOL> ) <EOL> self . assertAlmostEqual ( <EOL> illiquid_day_1_price_adjusted , <EOL> bar_data . current ( illiquid_asset , "<STR_LIT>" ) <EOL> ) </s>
