def test_run_trade_ticks_through_aggregator_results_in_expected_bars(self):
        # Arrange
        bar_store = ObjectStorer()
        handler = bar_store.store
        instrument = ETHUSDT_BINANCE
        bar_spec = BarSpecification(1000, BarAggregation.VOLUME,
                                    PriceType.LAST)
        bar_type = BarType(instrument.id, bar_spec)
        aggregator = VolumeBarAggregator(
            instrument,
            bar_type,
            handler,
            Logger(TestClock()),
        )

        wrangler = TradeTickDataWrangler(instrument=ETHUSDT_BINANCE)
        provider = TestDataProvider()
        ticks = wrangler.process(
            provider.read_csv_ticks("binance-ethusdt-trades.csv")[:10000])

        # Act
        for tick in ticks:
            aggregator.handle_trade_tick(tick)

        # Assert
        last_bar = bar_store.get_store()[-1]
        assert len(bar_store.get_store()) == 26
        assert last_bar.open == Price.from_str("425.17")
        assert last_bar.high == Price.from_str("425.24")
        assert last_bar.low == Price.from_str("424.69")
        assert last_bar.close == Price.from_str("425.14")
        assert last_bar.volume == Quantity.from_int(1000)
示例#2
0
    def setup(self):
        # Fixture Setup
        config = BacktestEngineConfig(
            bypass_logging=True,
            run_analysis=False,
        )
        self.engine = BacktestEngine(config=config)

        self.venue = Venue("BINANCE")
        self.ethusdt = TestInstrumentProvider.ethusdt_binance()

        # Setup data
        wrangler = TradeTickDataWrangler(instrument=self.ethusdt)
        provider = TestDataProvider()
        ticks = wrangler.process(
            provider.read_csv_ticks("binance-ethusdt-trades.csv"))
        self.engine.add_instrument(self.ethusdt)
        self.engine.add_ticks(ticks)

        self.engine.add_venue(
            venue=self.venue,
            oms_type=OMSType.NETTING,
            account_type=AccountType.MARGIN,
            base_currency=None,  # Multi-currency account
            starting_balances=[Money(1_000_000, USDT)],
        )
示例#3
0
    def test_add_trade_ticks_adds_to_engine(self, capsys):
        # Arrange
        engine = BacktestEngine()
        engine.add_instrument(ETHUSDT_BINANCE)

        wrangler = TradeTickDataWrangler(ETHUSDT_BINANCE)
        provider = TestDataProvider()
        ticks = wrangler.process(
            provider.read_csv_ticks("binance-ethusdt-trades.csv"))

        # Act
        engine.add_ticks(ticks)

        # Assert
        log = "".join(capsys.readouterr())
        assert "Added 69,806 ETHUSDT.BINANCE TradeTick elements." in log
    def test_process(self):
        # Arrange
        instrument = TestInstrumentProvider.btcusdt_binance()
        wrangler = TradeTickDataWrangler(instrument=instrument)
        path = os.path.join(PACKAGE_ROOT, "data", "tardis_trades.csv")
        data = TardisTradeDataLoader.load(path)

        # Act
        ticks = wrangler.process(data)

        # Assert
        assert len(ticks) == 9999
        assert ticks[0].price == Price.from_str("9682.00")
        assert ticks[0].size == Quantity.from_str("0.132000")
        assert ticks[0].aggressor_side == AggressorSide.BUY
        assert ticks[0].trade_id == TradeId("42377944")
        assert ticks[0].ts_event == 1582329602418379008
        assert ticks[0].ts_init == 1582329602418379008
    def test_process(self):
        # Arrange
        ethusdt = TestInstrumentProvider.ethusdt_binance()
        wrangler = TradeTickDataWrangler(instrument=ethusdt)
        provider = TestDataProvider()

        # Act
        ticks = wrangler.process(
            provider.read_csv_ticks("binance-ethusdt-trades.csv")[:100])

        # Assert
        assert len(ticks) == 100
        assert ticks[0].price == Price.from_str("423.760")
        assert ticks[0].size == Quantity.from_str("2.67900")
        assert ticks[0].aggressor_side == AggressorSide.SELL
        assert ticks[0].trade_id == TradeId("148568980")
        assert ticks[0].ts_event == 1597399200223000064
        assert ticks[0].ts_init == 1597399200223000064
示例#6
0
    config = BacktestEngineConfig(
        trader_id="BACKTESTER-001",
        exec_engine={"allow_cash_positions":
                     True},  # Retain original behaviour for now
    )
    # Build the backtest engine
    engine = BacktestEngine(config=config)

    BINANCE = Venue("BINANCE")
    instrument_id = InstrumentId(symbol=Symbol("ETHUSDT"), venue=BINANCE)
    ETHUSDT_BINANCE = TestInstrumentProvider.ethusdt_binance()

    # Setup data
    provider = TestDataProvider()
    wrangler = TradeTickDataWrangler(instrument=ETHUSDT_BINANCE)
    ticks = wrangler.process(
        provider.read_csv_ticks("binance-ethusdt-trades.csv"))
    engine.add_instrument(ETHUSDT_BINANCE)
    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,
    )

    # Add an exchange (multiple exchanges possible)
    # Add starting balances for single-currency or multi-currency accounts
    engine.add_venue(
        venue=BINANCE,