|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# ------------------------------------------------------------------------------------------------- |
| 3 | +# Copyright (C) 2015-2024 Nautech Systems Pty Ltd. All rights reserved. |
| 4 | +# https://nautechsystems.io |
| 5 | +# |
| 6 | +# Licensed under the GNU Lesser General Public License Version 3.0 (the "License"); |
| 7 | +# You may not use this file except in compliance with the License. |
| 8 | +# You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +# ------------------------------------------------------------------------------------------------- |
| 16 | + |
| 17 | +import asyncio |
| 18 | +from decimal import Decimal |
| 19 | + |
| 20 | +from nautilus_trader.adapters.bybit.common.constants import BYBIT_ALL_PRODUCTS |
| 21 | +from nautilus_trader.adapters.bybit.common.enums import BybitProductType |
| 22 | +from nautilus_trader.adapters.bybit.config import BybitDataClientConfig |
| 23 | +from nautilus_trader.adapters.bybit.factories import BybitLiveDataClientFactory |
| 24 | +from nautilus_trader.adapters.bybit.factories import get_bybit_http_client |
| 25 | +from nautilus_trader.adapters.bybit.factories import get_bybit_instrument_provider |
| 26 | +from nautilus_trader.adapters.sandbox.config import SandboxExecutionClientConfig |
| 27 | +from nautilus_trader.adapters.sandbox.execution import SandboxExecutionClient |
| 28 | +from nautilus_trader.adapters.sandbox.factory import SandboxLiveExecClientFactory |
| 29 | +from nautilus_trader.common.component import LiveClock |
| 30 | +from nautilus_trader.config import InstrumentProviderConfig |
| 31 | +from nautilus_trader.config import LoggingConfig |
| 32 | +from nautilus_trader.config import TradingNodeConfig |
| 33 | +from nautilus_trader.examples.strategies.volatility_market_maker import VolatilityMarketMaker |
| 34 | +from nautilus_trader.examples.strategies.volatility_market_maker import VolatilityMarketMakerConfig |
| 35 | +from nautilus_trader.live.node import TradingNode |
| 36 | +from nautilus_trader.model.data import BarType |
| 37 | +from nautilus_trader.model.identifiers import InstrumentId |
| 38 | +from nautilus_trader.model.identifiers import TraderId |
| 39 | + |
| 40 | + |
| 41 | +# fmt: on |
| 42 | + |
| 43 | + |
| 44 | +# *** THIS IS A TEST STRATEGY WITH NO ALPHA ADVANTAGE WHATSOEVER. *** |
| 45 | +# *** IT IS NOT INTENDED TO BE USED TO TRADE LIVE WITH REAL MONEY. *** |
| 46 | + |
| 47 | + |
| 48 | +async def main(): |
| 49 | + """ |
| 50 | + Show how to run a strategy in a sandbox for the Bybit venue. |
| 51 | + """ |
| 52 | + # Connect to Bybit client early to load all instruments |
| 53 | + clock = LiveClock() |
| 54 | + client = get_bybit_http_client(clock=clock) |
| 55 | + |
| 56 | + product_types = BYBIT_ALL_PRODUCTS |
| 57 | + instrument_provider_config = InstrumentProviderConfig(load_all=True) |
| 58 | + provider = get_bybit_instrument_provider( |
| 59 | + client=client, |
| 60 | + clock=clock, |
| 61 | + product_types=product_types, |
| 62 | + config=instrument_provider_config, |
| 63 | + ) |
| 64 | + await provider.load_all_async() |
| 65 | + |
| 66 | + instruments = provider.list_all() |
| 67 | + |
| 68 | + # Need to manually set instruments for sandbox exec client |
| 69 | + SandboxExecutionClient.INSTRUMENTS = instruments |
| 70 | + |
| 71 | + # Set up the execution clients (required per venue) |
| 72 | + venues = {str(instrument.venue) for instrument in instruments} |
| 73 | + |
| 74 | + exec_clients = {} |
| 75 | + for venue in venues: |
| 76 | + exec_clients[venue] = SandboxExecutionClientConfig( |
| 77 | + venue=venue, |
| 78 | + starting_balances=["10_000 USDT", "10 ETH"], |
| 79 | + instrument_provider=instrument_provider_config, |
| 80 | + account_type="MARGIN", |
| 81 | + oms_type="NETTING", |
| 82 | + ) |
| 83 | + |
| 84 | + # Configure the trading node |
| 85 | + config_node = TradingNodeConfig( |
| 86 | + trader_id=TraderId("TESTER-001"), |
| 87 | + logging=LoggingConfig( |
| 88 | + log_level="INFO", |
| 89 | + # log_level_file="DEBUG", |
| 90 | + # log_file_format="json", |
| 91 | + log_colors=True, |
| 92 | + use_pyo3=True, |
| 93 | + ), |
| 94 | + data_clients={ |
| 95 | + "BYBIT": BybitDataClientConfig( |
| 96 | + api_key=None, # 'BYBIT_API_KEY' env var |
| 97 | + api_secret=None, # 'BYBIT_API_SECRET' env var |
| 98 | + instrument_provider=instrument_provider_config, |
| 99 | + product_types=[BybitProductType.LINEAR], |
| 100 | + testnet=False, # If client uses the testnet |
| 101 | + ), |
| 102 | + }, |
| 103 | + exec_clients=exec_clients, |
| 104 | + timeout_connection=30.0, |
| 105 | + timeout_reconciliation=10.0, |
| 106 | + timeout_portfolio=10.0, |
| 107 | + timeout_disconnection=10.0, |
| 108 | + timeout_post_stop=5.0, |
| 109 | + ) |
| 110 | + |
| 111 | + # Instantiate the node with a configuration |
| 112 | + node = TradingNode(config=config_node) |
| 113 | + |
| 114 | + # Configure your strategy |
| 115 | + strat_config = VolatilityMarketMakerConfig( |
| 116 | + instrument_id=InstrumentId.from_str("ETHUSDT-LINEAR.BYBIT"), |
| 117 | + external_order_claims=[InstrumentId.from_str("ETHUSDT-LINEAR.BYBIT")], |
| 118 | + bar_type=BarType.from_str("ETHUSDT-LINEAR.BYBIT-1-MINUTE-LAST-EXTERNAL"), |
| 119 | + atr_period=20, |
| 120 | + atr_multiple=6.0, |
| 121 | + trade_size=Decimal("0.010"), |
| 122 | + # manage_gtd_expiry=True, |
| 123 | + ) |
| 124 | + # Instantiate your strategy |
| 125 | + strategy = VolatilityMarketMaker(config=strat_config) |
| 126 | + |
| 127 | + # Add your strategies and modules |
| 128 | + node.trader.add_strategy(strategy) |
| 129 | + |
| 130 | + # Register client factories with the node |
| 131 | + for data_client in config_node.data_clients: |
| 132 | + node.add_data_client_factory(data_client, BybitLiveDataClientFactory) |
| 133 | + |
| 134 | + for exec_client in config_node.exec_clients: |
| 135 | + node.add_exec_client_factory(exec_client, SandboxLiveExecClientFactory) |
| 136 | + |
| 137 | + node.build() |
| 138 | + |
| 139 | + try: |
| 140 | + await node.run_async() |
| 141 | + finally: |
| 142 | + await node.stop_async() |
| 143 | + await asyncio.sleep(1) |
| 144 | + node.dispose() |
| 145 | + |
| 146 | + |
| 147 | +# Stop and dispose of the node with SIGINT/CTRL+C |
| 148 | +if __name__ == "__main__": |
| 149 | + asyncio.run(main()) |
0 commit comments