def test_minutely_fetcher(self): self.responses.add( self.responses.GET, 'https://fake.urls.com/aapl_minute_csv_data.csv', body=AAPL_MINUTE_CSV_DATA, content_type='text/csv', ) sim_params = factory.create_simulation_parameters( start=pd.Timestamp("2006-01-03", tz='UTC'), end=pd.Timestamp("2006-01-10", tz='UTC'), emission_rate="minute", data_frequency="minute" ) test_algo = TradingAlgorithm( script=""" from gateway.api import fetch_csv, record, sid def initialize(context): fetch_csv('https://fake.urls.com/aapl_minute_csv_data.csv') def handle_data(context, data): record(aapl_signal=data.current(sid(24), "signal")) """, sim_params=sim_params, data_frequency="minute", env=self.env) # manually setting data portal and getting generator because we need # the minutely emission packets here. TradingAlgorithm.run() only # returns daily packets. test_algo.data_portal = FetcherDataPortal(self.env, self.trading_calendar) gen = test_algo.get_generator() perf_packets = list(gen) signal = [result["minute_perf"]["recorded_vars"]["aapl_signal"] for result in perf_packets if "minute_perf" in result] self.assertEqual(6 * 390, len(signal)) # csv data is: # symbol,date,signal # aapl,1/4/06 5:31AM, 1 # aapl,1/4/06 11:30AM, 2 # aapl,1/5/06 5:31AM, 1 # aapl,1/5/06 11:30AM, 3 # aapl,1/9/06 5:31AM, 1 # aapl,1/9/06 11:30AM, 4 for dates 1/3 to 1/10 # 2 signals per day, only last signal is taken. So we expect # 390 bars of signal NaN on 1/3 # 390 bars of signal 2 on 1/4 # 390 bars of signal 3 on 1/5 # 390 bars of signal 3 on 1/6 (forward filled) # 390 bars of signal 4 on 1/9 # 390 bars of signal 4 on 1/9 (forward filled) np.testing.assert_array_equal([np.NaN] * 390, signal[0:390]) np.testing.assert_array_equal([2] * 390, signal[390:780]) np.testing.assert_array_equal([3] * 780, signal[780:1560]) np.testing.assert_array_equal([4] * 780, signal[1560:])
def test_bts_simulation_dt(self): code = """ def initialize(context): pass """ algo = TradingAlgorithm(script=code, sim_params=self.sim_params, env=self.env) algo.perf_tracker = PerformanceTracker( sim_params=self.sim_params, trading_calendar=self.trading_calendar, asset_finder=self.asset_finder, ) dt = pd.Timestamp("2016-08-04 9:13:14", tz='US/Eastern') algo_simulator = AlgorithmSimulator( algo, self.sim_params, self.data_portal, BeforeTradingStartsOnlyClock(dt), algo._create_benchmark_source(), NoRestrictions(), None ) # run through the algo's simulation list(algo_simulator.transform()) # since the clock only ever emitted a single before_trading_start # event, we can check that the simulation_dt was properly set self.assertEqual(dt, algo_simulator.simulation_dt)
def run_algo(self, code, sim_params=None, data_frequency="daily"): if sim_params is None: sim_params = self.sim_params test_algo = TradingAlgorithm( script=code, sim_params=sim_params, env=self.env, data_frequency=data_frequency ) results = test_algo.run(FetcherDataPortal(self.env, self.trading_calendar)) return results
def main(): with open(api.__file__.rstrip('c') + 'i', 'w') as stub: # Imports so that Asset et al can be resolved. # "from MOD import *" will re-export the imports from the stub, so # explicitly importing. stub.write( dedent("""\ import collections from gateway.assets import Asset, Equity, Future from gateway.assets.futures import FutureChain from gateway.finance.asset_restrictions import Restrictions from gateway.finance.cancel_policy import CancelPolicy from gateway.pipeline import Pipeline from gateway.protocol import Order from gateway.utils.events import EventRule from gateway.utils.security_list import SecurityList """)) # Sort to generate consistent stub file: for api_func in sorted(TradingAlgorithm.all_api_methods(), key=attrgetter('__name__')): sig = inspect._signature_bound_method(inspect.signature(api_func)) indent = ' ' * 4 stub.write( dedent('''\ def {func_name}{func_sig}: """'''.format(func_name=api_func.__name__, func_sig=sig))) stub.write( dedent('{indent}{func_doc}'.format( func_doc=api_func.__doc__ or '\n', # handle None docstring indent=indent, ))) stub.write('{indent}"""\n\n'.format(indent=indent))
index_col='time', parse_dates=['time']) df.rename(columns={'time': 'date'}, inplace=True) df['test'] = [i for i in range(len(df.index))] log.info(df.columns) log.info(df.head(5)) log.info(df.index) data[s] = df panel = pd.Panel(data) cn_cal = SHSZExchangeCalendar() panel.major_axis = panel.major_axis.tz_localize(cn_cal.tz) algo_obj = TradingAlgorithm(initialize=initialize, handle_data=handle_data, trading_calendar=cn_cal, data_frequency='minute', clock_file="data/clock.csv") log.info('run algorithm') perf_manual = algo_obj.run(panel) if False: print(perf_manual) if False: import pickle file = "e:\\perf.p" log.info("write performance to pickle : %s" % file) pickle.dump(perf_manual, open(file, 'wb')) log.info("total sn %d, start at %s, end at %s" %
panel = pd.read_hdf(panel_file) log.info("read panale from hdf5 file {} done".format(panel_file)) datetime_index = panel.major_axis set_global_panel(panel) global stocks stocks = list(panel.items) log.info("total {} stocks in panel, shape {}".format( len(stocks), panel.shape)) log.info("setup stocks list : " + str(stocks)) cn_cal = SHSZExchangeCalendar() panel.major_axis = panel.major_axis.tz_localize(cn_cal.tz) algo_obj = TradingAlgorithm(initialize=initialize, handle_data=handle_data, trading_calendar=cn_cal, data_frequency='minute', clock_dt_index=datetime_index) # clock_file = "data/clock.csv") log.info('run algorithm') perf_manual = algo_obj.run(panel) if False: print(perf_manual) if False: import pickle file = "e:\\perf.p" log.info("write performance to pickle : %s" % file) pickle.dump(perf_manual, open(file, 'wb'))
def get_results(self, algo_code): algo = TradingAlgorithm(script=algo_code, env=self.env, sim_params=self.sim_params) return algo.run(self.data_portal)