def test_pre_process_bar_data_with_random_seed(self): # Arrange usdjpy = TestInstrumentProvider.default_fx_ccy("USD/JPY") provider = TestDataProvider() bid_data = provider.read_csv_bars("fxcm-usdjpy-m1-bid-2013.csv")[:100] ask_data = provider.read_csv_bars("fxcm-usdjpy-m1-ask-2013.csv")[:100] wrangler = QuoteTickDataWrangler(instrument=usdjpy) # Act ticks = wrangler.process_bar_data( bid_data=bid_data, ask_data=ask_data, default_volume=1000000, random_seed=42, # <-- with random seed ) # Assert assert ticks[0].bid == Price.from_str("91.715") assert ticks[0].ask == Price.from_str("91.717") assert ticks[1].bid == Price.from_str("91.653") assert ticks[1].ask == Price.from_str("91.655") assert ticks[2].bid == Price.from_str("91.715") assert ticks[2].ask == Price.from_str("91.717") assert ticks[3].bid == Price.from_str("91.653") assert ticks[3].ask == Price.from_str("91.655")
def test_pre_process_bar_data_with_delta(self): # Arrange usdjpy = TestInstrumentProvider.default_fx_ccy("USD/JPY") provider = TestDataProvider() bid_data = provider.read_csv_bars("fxcm-usdjpy-m1-bid-2013.csv")[:100] ask_data = provider.read_csv_bars("fxcm-usdjpy-m1-ask-2013.csv")[:100] wrangler = QuoteTickDataWrangler(instrument=usdjpy) # Act ticks = wrangler.process_bar_data( bid_data=bid_data, ask_data=ask_data, default_volume=1000000, ts_init_delta=1_000_500, ) # Assert assert len(ticks) == 400 assert ticks[0].instrument_id == usdjpy.id assert ticks[0].bid == Price.from_str("91.715") assert ticks[0].ask == Price.from_str("91.717") assert ticks[0].bid_size == Quantity.from_int(1000000) assert ticks[0].ask_size == Quantity.from_int(1000000) assert ticks[0].ts_event == 1359676799700000000 assert ticks[0].ts_init == 1359676799701000500 # <-- delta diff
def setup(self): # Fixture Setup config = BacktestEngineConfig( bypass_logging=True, run_analysis=False, ) self.engine = BacktestEngine(config=config) self.venue = Venue("SIM") self.usdjpy = TestInstrumentProvider.default_fx_ccy("USD/JPY") # Setup data wrangler = QuoteTickDataWrangler(instrument=self.usdjpy) provider = TestDataProvider() ticks = wrangler.process_bar_data( bid_data=provider.read_csv_bars("fxcm-usdjpy-m1-bid-2013.csv"), ask_data=provider.read_csv_bars("fxcm-usdjpy-m1-ask-2013.csv"), ) self.engine.add_instrument(self.usdjpy) self.engine.add_ticks(ticks) interest_rate_data = pd.read_csv( os.path.join(PACKAGE_ROOT, "data", "short-term-interest.csv")) fx_rollover_interest = FXRolloverInterestModule( rate_data=interest_rate_data) self.engine.add_venue( venue=self.venue, oms_type=OMSType.HEDGING, account_type=AccountType.MARGIN, base_currency=USD, starting_balances=[Money(1_000_000, USD)], modules=[fx_rollover_interest], )
def setup(): # Arrange config = BacktestEngineConfig(bypass_logging=True) engine = BacktestEngine(config=config) # Setup data wrangler = QuoteTickDataWrangler(USDJPY_SIM) provider = TestDataProvider() ticks = wrangler.process_bar_data( bid_data=provider.read_csv_bars("fxcm-usdjpy-m1-bid-2013.csv"), ask_data=provider.read_csv_bars("fxcm-usdjpy-m1-ask-2013.csv"), ) engine.add_instrument(USDJPY_SIM) engine.add_ticks(ticks) engine.add_venue( venue=Venue("SIM"), venue_type=VenueType.BROKERAGE, oms_type=OMSType.HEDGING, account_type=AccountType.MARGIN, base_currency=USD, starting_balances=[Money(1_000_000, USD)], fill_model=FillModel(), ) strategies = [TradingStrategy()] start = datetime(2013, 1, 1, 22, 0, 0, 0, tzinfo=pytz.utc) end = datetime(2013, 8, 10, 0, 0, 0, 0, tzinfo=pytz.utc) return (engine, start, end, strategies), {}
def setup(self): # Fixture Setup config = BacktestEngineConfig( bypass_logging=True, run_analysis=False, ) self.engine = BacktestEngine(config=config) self.usdjpy = TestInstrumentProvider.default_fx_ccy("USD/JPY") # Setup data wrangler = QuoteTickDataWrangler(self.usdjpy) provider = TestDataProvider() ticks = wrangler.process_bar_data( bid_data=provider.read_csv_bars("fxcm-usdjpy-m1-bid-2013.csv"), ask_data=provider.read_csv_bars("fxcm-usdjpy-m1-ask-2013.csv"), ) self.engine.add_instrument(self.usdjpy) self.engine.add_ticks(ticks) self.engine.add_venue( venue=Venue("SIM"), venue_type=VenueType.BROKERAGE, oms_type=OMSType.HEDGING, account_type=AccountType.MARGIN, base_currency=USD, starting_balances=[Money(1_000_000, USD)], modules=[], )
def setup(self): # Fixture Setup config = BacktestEngineConfig( bypass_logging=False, run_analysis=False, cache_database=CacheDatabaseConfig(), # default redis cache_db_flush=False, ) self.engine = BacktestEngine(config=config) self.usdjpy = TestInstrumentProvider.default_fx_ccy("USD/JPY") wrangler = QuoteTickDataWrangler(self.usdjpy) provider = TestDataProvider() ticks = wrangler.process_bar_data( bid_data=provider.read_csv_bars("fxcm-usdjpy-m1-bid-2013.csv"), ask_data=provider.read_csv_bars("fxcm-usdjpy-m1-ask-2013.csv"), ) self.engine.add_instrument(self.usdjpy) self.engine.add_ticks(ticks) self.engine.add_venue( venue=Venue("SIM"), oms_type=OMSType.HEDGING, account_type=AccountType.MARGIN, base_currency=USD, starting_balances=[Money(1_000_000, USD)], modules=[], ) self.test_redis = redis.Redis(host="localhost", port=6379, db=0)
def setup(self): # Fixture Setup self.engine = BacktestEngine() self.usdjpy = TestInstrumentProvider.default_fx_ccy("USD/JPY") # Setup data wrangler = QuoteTickDataWrangler(self.usdjpy) provider = TestDataProvider() ticks = wrangler.process_bar_data( bid_data=provider.read_csv_bars( "fxcm-usdjpy-m1-bid-2013.csv")[:2000], ask_data=provider.read_csv_bars("fxcm-usdjpy-m1-ask-2013.csv") [:2000], ) self.engine.add_instrument(USDJPY_SIM) self.engine.add_ticks(ticks) self.engine.add_venue( venue=Venue("SIM"), oms_type=OMSType.HEDGING, account_type=AccountType.MARGIN, base_currency=USD, starting_balances=[Money(1_000_000, USD)], fill_model=FillModel(), )
def setup(): config = BacktestEngineConfig(bypass_logging=True) engine = BacktestEngine(config=config) # Setup data wrangler = QuoteTickDataWrangler(USDJPY_SIM) provider = TestDataProvider() ticks = wrangler.process_bar_data( bid_data=provider.read_csv_bars("fxcm-usdjpy-m1-bid-2013.csv"), ask_data=provider.read_csv_bars("fxcm-usdjpy-m1-ask-2013.csv"), ) engine.add_instrument(USDJPY_SIM) engine.add_ticks(ticks) engine.add_venue( venue=Venue("SIM"), oms_type=OMSType.HEDGING, account_type=AccountType.MARGIN, base_currency=USD, starting_balances=[Money(1_000_000, USD)], ) config = EMACrossConfig( instrument_id=str(USDJPY_SIM.id), bar_type=str(TestDataStubs.bartype_usdjpy_1min_bid()), trade_size=Decimal(1_000_000), fast_ema=10, slow_ema=20, ) strategy = EMACross(config=config) start = datetime(2013, 2, 1, 0, 0, 0, 0, tzinfo=pytz.utc) end = datetime(2013, 2, 10, 0, 0, 0, 0, tzinfo=pytz.utc) return (engine, start, end, strategy), {}
def setup(): config = BacktestEngineConfig(bypass_logging=True) engine = BacktestEngine(config=config) # Setup data wrangler = QuoteTickDataWrangler(USDJPY_SIM) provider = TestDataProvider() ticks = wrangler.process_bar_data( bid_data=provider.read_csv_bars("fxcm-usdjpy-m1-bid-2013.csv"), ask_data=provider.read_csv_bars("fxcm-usdjpy-m1-ask-2013.csv"), ) engine.add_instrument(USDJPY_SIM) engine.add_ticks(ticks) interest_rate_data = pd.read_csv( os.path.join(PACKAGE_ROOT, "data", "short-term-interest.csv")) fx_rollover_interest = FXRolloverInterestModule( rate_data=interest_rate_data) engine.add_venue( venue=Venue("SIM"), venue_type=VenueType.BROKERAGE, oms_type=OMSType.HEDGING, account_type=AccountType.MARGIN, base_currency=USD, starting_balances=[Money(1_000_000, USD)], modules=[fx_rollover_interest], ) config = EMACrossConfig( instrument_id=str(USDJPY_SIM.id), bar_type=str(TestStubs.bartype_usdjpy_1min_bid()), trade_size=Decimal(1_000_000), fast_ema=10, slow_ema=20, ) strategy = EMACross(config=config) start = datetime(2013, 2, 1, 0, 0, 0, 0, tzinfo=pytz.utc) end = datetime(2013, 3, 1, 0, 0, 0, 0, tzinfo=pytz.utc) return (engine, start, end, [strategy]), {}
def test_run_ema_cross_with_trade_ticks_from_bar_data(self): # Arrange wrangler = QuoteTickDataWrangler(instrument=self.btcusdt) provider = TestDataProvider() # Build ticks from bar data ticks = wrangler.process_bar_data( bid_data=provider.read_csv_bars( "ftx-btc-perp-20211231-20220201_1m.csv")[:10000], ask_data=provider.read_csv_bars( "ftx-btc-perp-20211231-20220201_1m.csv")[:10000], ) self.engine.add_ticks(ticks) config = EMACrossConfig( instrument_id=str(self.btcusdt.id), bar_type="BTCUSDT.BINANCE-1-MINUTE-BID-INTERNAL", trade_size=Decimal(0.001), fast_ema=10, slow_ema=20, ) strategy = EMACross(config=config) self.engine.add_strategy(strategy) # Act self.engine.run() # Assert assert strategy.fast_ema.count == 10000 assert self.engine.iteration == 40000 btc_ending_balance = self.engine.portfolio.account( self.venue).balance_total(BTC) usdt_ending_balance = self.engine.portfolio.account( self.venue).balance_total(USDT) assert btc_ending_balance == Money(9.57200000, BTC) assert usdt_ending_balance == Money(10017114.27716700, USDT)
trader_id="BACKTESTER-001", use_data_cache= True, # Pre-cache data for increased performance on repeated runs ) # Build the backtest engine engine = BacktestEngine(config=config) # Setup trading instruments SIM = Venue("SIM") GBPUSD_SIM = TestInstrumentProvider.default_fx_ccy("GBP/USD", SIM) # Setup data provider = TestDataProvider() wrangler = QuoteTickDataWrangler(GBPUSD_SIM) ticks = wrangler.process_bar_data( bid_data=provider.read_csv_bars("fxcm-gbpusd-m1-bid-2012.csv"), ask_data=provider.read_csv_bars("fxcm-gbpusd-m1-ask-2012.csv"), ) engine.add_instrument(GBPUSD_SIM) engine.add_ticks(ticks) # Create a fill model (optional) fill_model = FillModel( prob_fill_on_limit=0.2, prob_fill_on_stop=0.95, prob_slippage=0.5, random_seed=42, ) # Optional plug in module to simulate rollover interest, # the data is coming from packaged test data. interest_rate_data = provider.read_csv("short-term-interest.csv")