-
Notifications
You must be signed in to change notification settings - Fork 11
/
quant.py
150 lines (122 loc) · 4.54 KB
/
quant.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import asyncio
import logging
import os
import signal
import uvloop
from dotenv import load_dotenv
from copilot import CopilotActor
from coral import DataSourceFactory
from core.models.datasource_type import DataSourceType
from executor import OrderExecutorActorFactory
from feed import FeedActorFactory
from infrastructure.config import ConfigService
from infrastructure.event_dispatcher.event_dispatcher import EventDispatcher
from infrastructure.logger import configure_logging
from infrastructure.shutdown import GracefulShutdown
from market import MarketActor
from ocean import OceanActor
from optimization import StrategyOptimizerFactory
from portfolio import Portfolio
from position import PositionActorFactory, PositionFactory
from position.size.fixed import PositionFixedSizeStrategy
from reef import ReefActor
from risk import RiskActorFactory
from service import (
EnvironmentSecretService,
LLMService,
SignalService,
TimeSeriesService,
WasmManager,
)
from sor import SmartRouter
from strategy import SignalActorFactory
from strategy.generator import StrategyGeneratorFactory
from system.backtest import BacktestSystem
from system.context import SystemContext
from system.trading import TradingSystem
asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
load_dotenv()
REGIME = os.getenv("REGIME")
LOG_DIR = os.getenv("LOG_DIR")
LOG_LEVEL = os.getenv("LOG_LEVEL", "INFO").upper()
WASM_FOLDER = os.getenv("WASM_FOLDER")
COPILOT_MODEL_PATH = os.getenv("COPILOT_MODEL_PATH")
OCEAN_EMB_PATH = os.getenv("OCEAN_EMB_PATH")
configure_logging(LOG_LEVEL)
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
graceful_shutdown = GracefulShutdown()
signal.signal(signal.SIGTERM, graceful_shutdown.exit)
async def main():
logging.info("Initializing...")
config_service = ConfigService()
config_service.load(config_path=f"config.{REGIME}.ini")
config = {
"bus": {"num_workers": os.cpu_count()},
"store": {"base_dir": LOG_DIR},
"copilot": {"model_path": COPILOT_MODEL_PATH},
"ocean": {"emb_file": OCEAN_EMB_PATH},
}
config_service.update(config)
default_datasource = DataSourceType.BYBIT
event_bus = EventDispatcher(config_service)
datasource = DataSourceFactory(EnvironmentSecretService())
datasource.register_rest_exchange(default_datasource)
datasource.register_ws_exchange(default_datasource)
wasm = WasmManager(WASM_FOLDER)
position_factory = PositionFactory(
config_service,
PositionFixedSizeStrategy(),
)
OceanActor(datasource, config_service).start()
ReefActor(datasource, config_service).start()
MarketActor(TimeSeriesService(wasm)).start()
CopilotActor(LLMService(config_service)).start()
Portfolio(config_service)
SmartRouter(datasource, config_service)
signal_actor_factory = SignalActorFactory(SignalService(wasm))
position_actor_factory = PositionActorFactory(position_factory, config_service)
risk_actor_factory = RiskActorFactory(config_service)
executor_actor_factory = OrderExecutorActorFactory()
feed_actor_factory = FeedActorFactory(datasource, config_service)
trend_context = SystemContext(
signal_actor_factory,
position_actor_factory,
risk_actor_factory,
executor_actor_factory,
feed_actor_factory,
StrategyGeneratorFactory(config_service),
StrategyOptimizerFactory(config_service),
datasource=default_datasource,
config_service=config_service,
)
backtest_system = BacktestSystem(trend_context)
trading_system = TradingSystem(
signal_actor_factory,
position_actor_factory,
risk_actor_factory,
executor_actor_factory,
feed_actor_factory,
config_service,
datasource=default_datasource,
)
backtest_system_task = asyncio.create_task(backtest_system.start())
trading_system_task = asyncio.create_task(trading_system.start())
shutdown_task = asyncio.create_task(graceful_shutdown.wait_for_exit_signal())
try:
logging.info("Started")
await asyncio.gather(
*[backtest_system_task, trading_system_task, shutdown_task]
)
finally:
logging.info("Closing...")
shutdown_task.cancel()
backtest_system_task.cancel()
trading_system_task.cancel()
trading_system.stop()
backtest_system.stop()
await event_bus.stop()
await event_bus.wait()
logging.info("Finished.")
with asyncio.Runner(loop_factory=uvloop.new_event_loop) as runner:
runner.run(main())