|
41 | 41 | from nautilus_trader.model import AccountType |
42 | 42 | from nautilus_trader.model import AggressorSide |
43 | 43 | from nautilus_trader.model import BarType |
| 44 | +from nautilus_trader.model import BettingInstrument |
| 45 | +from nautilus_trader.model import BookAction |
| 46 | +from nautilus_trader.model import BookOrder |
| 47 | +from nautilus_trader.model import BookType |
44 | 48 | from nautilus_trader.model import Currency |
45 | 49 | from nautilus_trader.model import ExecAlgorithmId |
| 50 | +from nautilus_trader.model import InstrumentId |
46 | 51 | from nautilus_trader.model import Money |
47 | 52 | from nautilus_trader.model import OmsType |
| 53 | +from nautilus_trader.model import OrderBookDelta |
| 54 | +from nautilus_trader.model import OrderBookDeltas |
48 | 55 | from nautilus_trader.model import OrderSide |
49 | 56 | from nautilus_trader.model import OrderStatus |
50 | 57 | from nautilus_trader.model import Price |
51 | 58 | from nautilus_trader.model import Quantity |
52 | 59 | from nautilus_trader.model import QuoteTick |
| 60 | +from nautilus_trader.model import Symbol |
53 | 61 | from nautilus_trader.model import TradeId |
54 | 62 | from nautilus_trader.model import TradeTick |
55 | 63 | from nautilus_trader.model import Venue |
|
87 | 95 | EMA_CROSS_TRAILING_STOP_STRATEGY = "strategies.acceptance:EMACrossTrailingStop" |
88 | 96 | EMA_CROSS_TRAILING_STOP_CONFIG = "strategies.acceptance:EMACrossTrailingStopConfig" |
89 | 97 |
|
| 98 | +ORDER_BOOK_IMBALANCE_STRATEGY = "strategies.acceptance:OrderBookImbalance" |
| 99 | +ORDER_BOOK_IMBALANCE_CONFIG = "strategies.acceptance:OrderBookImbalanceConfig" |
| 100 | + |
90 | 101 | EMA_CROSS_TRAILING_STOP_TAG = "ema-cross-trailing-stop" |
91 | 102 |
|
92 | 103 |
|
@@ -670,12 +681,119 @@ def test_run_ema_cross_with_tick_bar_spec(self): |
670 | 681 | assert result.total_orders > 0 |
671 | 682 |
|
672 | 683 |
|
673 | | -@pytest.mark.skip( |
674 | | - reason="v2 missing: Betfair adapter / data provider + OrderBookImbalance strategy", |
675 | | -) |
676 | 684 | class TestBacktestAcceptanceTestsOrderBookImbalance: |
| 685 | + def setup_method(self): |
| 686 | + self.engine = _engine() |
| 687 | + self.venue = Venue("BETFAIR") |
| 688 | + self.gbp = Currency.from_str("GBP") |
| 689 | + self.instrument = _betfair_betting_instrument(selection_id=19248890) |
| 690 | + |
| 691 | + self.engine.add_venue( |
| 692 | + venue=self.venue, |
| 693 | + oms_type=OmsType.NETTING, |
| 694 | + account_type=AccountType.BETTING, |
| 695 | + base_currency=self.gbp, |
| 696 | + starting_balances=[Money(100_000.0, self.gbp)], |
| 697 | + book_type=BookType.L2_MBP, |
| 698 | + ) |
| 699 | + self.engine.add_instrument(self.instrument) |
| 700 | + self.engine.add_data(_betfair_order_book_deltas(self.instrument)) |
| 701 | + |
| 702 | + def teardown_method(self): |
| 703 | + self.engine.dispose() |
| 704 | + |
677 | 705 | def test_run_order_book_imbalance(self): |
678 | | - pass |
| 706 | + self.engine.add_strategy_from_config( |
| 707 | + ImportableStrategyConfig( |
| 708 | + strategy_path=ORDER_BOOK_IMBALANCE_STRATEGY, |
| 709 | + config_path=ORDER_BOOK_IMBALANCE_CONFIG, |
| 710 | + config={ |
| 711 | + "instrument_id": str(self.instrument.id), |
| 712 | + "trade_size": "5.00", |
| 713 | + }, |
| 714 | + ), |
| 715 | + ) |
| 716 | + |
| 717 | + self.engine.run() |
| 718 | + result = self.engine.get_result() |
| 719 | + |
| 720 | + assert result.iterations == 1 |
| 721 | + assert result.total_orders == 2 |
| 722 | + assert result.total_positions == 1 |
| 723 | + assert result.summary["venues.total"] == "1" |
| 724 | + assert result.summary["orders.closed"] == "2" |
| 725 | + assert result.summary["positions.closed"] == "1" |
| 726 | + |
| 727 | + |
| 728 | +def _betfair_betting_instrument(selection_id: int) -> BettingInstrument: |
| 729 | + raw_symbol = Symbol(f"1-166811431-{selection_id}-None") |
| 730 | + gbp = Currency.from_str("GBP") |
| 731 | + return BettingInstrument( |
| 732 | + instrument_id=InstrumentId(raw_symbol, Venue("BETFAIR")), |
| 733 | + raw_symbol=raw_symbol, |
| 734 | + event_type_id=6423, |
| 735 | + event_type_name="American Football", |
| 736 | + competition_id=12282733, |
| 737 | + competition_name="NFL", |
| 738 | + event_id=29678534, |
| 739 | + event_name="NFL", |
| 740 | + event_country_code="GB", |
| 741 | + event_open_date=1644276600000000000, |
| 742 | + betting_type="ODDS", |
| 743 | + market_id="1-166811431", |
| 744 | + market_name="AFC Conference Winner", |
| 745 | + market_type="SPECIAL", |
| 746 | + market_start_time=1644276600000000000, |
| 747 | + selection_id=selection_id, |
| 748 | + selection_name="Kansas City Chiefs", |
| 749 | + selection_handicap=-9999999.0, |
| 750 | + currency=gbp, |
| 751 | + price_precision=2, |
| 752 | + size_precision=2, |
| 753 | + price_increment=Price.from_str("0.01"), |
| 754 | + size_increment=Quantity.from_str("0.01"), |
| 755 | + ts_event=0, |
| 756 | + ts_init=0, |
| 757 | + ) |
| 758 | + |
| 759 | + |
| 760 | +def _betfair_order_book_deltas(instrument: BettingInstrument) -> list[OrderBookDeltas]: |
| 761 | + ts = 1_600_000_000_000_000_000 |
| 762 | + return [ |
| 763 | + OrderBookDeltas( |
| 764 | + instrument_id=instrument.id, |
| 765 | + deltas=[ |
| 766 | + OrderBookDelta( |
| 767 | + instrument.id, |
| 768 | + BookAction.ADD, |
| 769 | + BookOrder( |
| 770 | + OrderSide.BUY, |
| 771 | + Price.from_decimal_dp(Decimal("1.99"), instrument.price_precision), |
| 772 | + Quantity.from_decimal_dp(Decimal("250.00"), instrument.size_precision), |
| 773 | + 1, |
| 774 | + ), |
| 775 | + 0, |
| 776 | + 1, |
| 777 | + ts, |
| 778 | + ts, |
| 779 | + ), |
| 780 | + OrderBookDelta( |
| 781 | + instrument.id, |
| 782 | + BookAction.ADD, |
| 783 | + BookOrder( |
| 784 | + OrderSide.SELL, |
| 785 | + Price.from_decimal_dp(Decimal("2.00"), instrument.price_precision), |
| 786 | + Quantity.from_decimal_dp(Decimal("10.00"), instrument.size_precision), |
| 787 | + 2, |
| 788 | + ), |
| 789 | + 0, |
| 790 | + 2, |
| 791 | + ts, |
| 792 | + ts, |
| 793 | + ), |
| 794 | + ], |
| 795 | + ), |
| 796 | + ] |
679 | 797 |
|
680 | 798 |
|
681 | 799 | @pytest.mark.skip(reason="v2 missing: Betfair adapter + MarketMaker example strategy") |
|
0 commit comments