def test_run_quote_ticks_through_aggregator_results_in_expected_bars(self): # Arrange bar_store = ObjectStorer() handler = bar_store.store instrument = AUDUSD_SIM bar_spec = BarSpecification(1000, BarAggregation.VOLUME, PriceType.MID) bar_type = BarType(instrument.id, bar_spec) aggregator = VolumeBarAggregator( instrument, bar_type, handler, Logger(TestClock()), ) # Setup data wrangler = QuoteTickDataWrangler(instrument) provider = TestDataProvider() ticks = wrangler.process( data=provider.read_csv_ticks("truefx-audusd-ticks.csv")[:10000], default_volume=1, ) # Act for tick in ticks: aggregator.handle_quote_tick(tick) # Assert last_bar = bar_store.get_store()[-1] assert len(bar_store.get_store()) == 10 assert last_bar.open == Price.from_str("0.670635") assert last_bar.high == Price.from_str("0.670705") assert last_bar.low == Price.from_str("0.670370") assert last_bar.close == Price.from_str("0.670655") assert last_bar.volume == Quantity.from_int(1000)
def setup(self): # Fixture Setup config = BacktestEngineConfig( bypass_logging=True, run_analysis=False, ) self.engine = BacktestEngine(config=config) self.venue = Venue("SIM") self.audusd = TestInstrumentProvider.default_fx_ccy("AUD/USD") # Setup data wrangler = QuoteTickDataWrangler(self.audusd) provider = TestDataProvider() ticks = wrangler.process( provider.read_csv_ticks("truefx-audusd-ticks.csv")) self.engine.add_instrument(self.audusd) self.engine.add_ticks(ticks) interest_rate_data = provider.read_csv("short-term-interest.csv") fx_rollover_interest = FXRolloverInterestModule( rate_data=interest_rate_data) self.engine.add_venue( venue=Venue("SIM"), oms_type=OMSType.HEDGING, account_type=AccountType.MARGIN, base_currency=AUD, starting_balances=[Money(1_000_000, AUD)], modules=[fx_rollover_interest], )
def parser(data): if data is None: return data.loc[:, "timestamp"] = pd.to_datetime(data["timestamp"]) instrument = TestInstrumentProvider.default_fx_ccy("AUD/USD") wrangler = QuoteTickDataWrangler(instrument) ticks = wrangler.process(data.set_index("timestamp")) yield from ticks
def test_add_quote_ticks_adds_to_engine(self, capsys): # Arrange engine = BacktestEngine() # Setup data engine.add_instrument(AUDUSD_SIM) wrangler = QuoteTickDataWrangler(AUDUSD_SIM) provider = TestDataProvider() ticks = wrangler.process( provider.read_csv_ticks("truefx-audusd-ticks.csv")) # Act engine.add_ticks(ticks) # Assert log = "".join(capsys.readouterr()) assert "Added 100,000 AUD/USD.SIM QuoteTick elements." in log
def test_pre_process_with_tick_data(self): # Arrange instrument = TestInstrumentProvider.btcusdt_binance() wrangler = QuoteTickDataWrangler(instrument=instrument) path = os.path.join(PACKAGE_ROOT, "data", "tardis_quotes.csv") data = TardisQuoteDataLoader.load(path) # Act ticks = wrangler.process( data, ts_init_delta=1_000_501, ) # Assert assert len(ticks) == 9999 assert ticks[0].bid == Price.from_str("9681.92") assert ticks[0].ask == Price.from_str("9682.00") assert ticks[0].bid_size == Quantity.from_str("0.670000") assert ticks[0].ask_size == Quantity.from_str("0.840000") assert ticks[0].ts_event == 1582329603502091776 assert ticks[0].ts_init == 1582329603503092277
def test_process_tick_data(self): # Arrange usdjpy = TestInstrumentProvider.default_fx_ccy("USD/JPY") wrangler = QuoteTickDataWrangler(instrument=usdjpy) provider = TestDataProvider() # Act ticks = wrangler.process( data=provider.read_csv_ticks("truefx-usdjpy-ticks.csv"), default_volume=1000000, ) # Assert assert len(ticks) == 1000 assert ticks[0].instrument_id == usdjpy.id assert ticks[0].bid == Price.from_str("86.655") assert ticks[0].ask == Price.from_str("86.728") assert ticks[0].bid_size == Quantity.from_int(1000000) assert ticks[0].ask_size == Quantity.from_int(1000000) assert ticks[0].ts_event == 1357077600295000064 assert ticks[0].ts_event == 1357077600295000064
def test_given_list_of_ticks_aggregates_tick_bars(self): # Arrange instrument = TestInstrumentProvider.default_fx_ccy("USD/JPY") wrangler = QuoteTickDataWrangler(instrument) provider = TestDataProvider() ticks = wrangler.process( provider.read_csv_ticks("truefx-usdjpy-ticks.csv")) bar_store = ObjectStorer() handler = bar_store.store instrument_id = TestStubs.usdjpy_id() bar_spec = BarSpecification(3, BarAggregation.TICK, PriceType.MID) bar_type = BarType(instrument_id, bar_spec) clock = TestClock() logger = Logger(clock) builder = BulkTickBarBuilder(instrument, bar_type, logger, handler) # Act builder.receive(ticks) # Assert assert len(bar_store.get_store()[0]) == 333
if __name__ == "__main__": # Configure backtest engine config = BacktestEngineConfig( trader_id="BACKTESTER-001", ) # Build the backtest engine engine = BacktestEngine(config=config) # Setup trading instruments SIM = Venue("SIM") AUDUSD_SIM = TestInstrumentProvider.default_fx_ccy("AUD/USD", SIM) # Setup data provider = TestDataProvider() wrangler = QuoteTickDataWrangler(instrument=AUDUSD_SIM) ticks = wrangler.process(provider.read_csv_ticks("truefx-audusd-ticks.csv")) engine.add_instrument(AUDUSD_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") fx_rollover_interest = FXRolloverInterestModule(rate_data=interest_rate_data)