-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
205 lines (164 loc) · 6.21 KB
/
Copy pathmain.py
File metadata and controls
205 lines (164 loc) · 6.21 KB
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
"""
Unit8 — main entry point.
This is how everything connects:
1. Load config (checklist + credentials)
2. Create broker connection
3. Create tools
4. Build checklist from config
5. Loop: get data → evaluate checklist → execute if GO → monitor positions
"""
import json
import sys
import time
import logging
import os
from datetime import datetime
# Make imports work regardless of where main.py is run from
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from broker.mock_broker import MockBroker
from broker.mt5_broker import MT5Broker
from broker.ctrader_broker import CTraderBroker
from broker.bridge_broker import BridgeBroker
from core.checklist import Checklist
from core.risk import RiskManager
from core.execution import ExecutionEngine
from tools import (
SwingDetector, Ichimoku, Pattern123,
TrendlineBreak, DivergenceDetector,
SupportResistance, SpreadFilter,
)
# --- Logging setup ---
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s [%(name)s] %(levelname)s: %(message)s",
)
logger = logging.getLogger("unit8")
def load_checklist_config(path: str = "config/checklist_example.json") -> dict:
"""Load strategy checklist from JSON."""
with open(path, "r") as f:
return json.load(f)
def build_broker(config: dict):
"""
Create the broker specified in config["broker"]["type"].
Supported types: "mock", "mt5", "ctrader", "bridge".
The whole broker section is passed to the broker's constructor,
so broker-specific keys (data_dir, account, ...) live there too.
"""
broker_cfg = config.get("broker", {})
broker_type = broker_cfg.get("type", "mock").lower()
broker_map = {
"mock": MockBroker,
"mt5": MT5Broker,
"ctrader": CTraderBroker,
"bridge": BridgeBroker,
}
cls = broker_map.get(broker_type)
if cls is None:
raise ValueError(
f"Unknown broker type: '{broker_type}'. "
f"Supported: {list(broker_map.keys())}"
)
logger.info(f"Using broker: {broker_type}")
return cls(broker_cfg)
def build_tools(checks: list) -> dict:
"""
Create tool instances from checklist config.
Each tool gets its own config from the checklist.
"""
tool_map = {
"swing_detector": SwingDetector,
"ichimoku": Ichimoku,
"pattern_123": Pattern123,
"trendline_break": TrendlineBreak,
"divergence": DivergenceDetector,
"support_resistance": SupportResistance,
"spread_filter": SpreadFilter,
}
tools = {}
for check in checks:
name = check["tool"]
cls = tool_map.get(name)
if cls:
tools[name] = cls(config=check.get("config", {}))
else:
logger.warning(f"Unknown tool: {name}")
return tools
def is_trading_time(schedule: dict) -> bool:
"""Check if we're within allowed trading hours and days."""
now = datetime.now()
weekdays = schedule.get("allowed_weekdays", [0, 1, 2, 3, 4])
if now.weekday() not in weekdays:
return False
hours = schedule.get("trading_hours", [8, 20])
if not (hours[0] <= now.hour < hours[1]):
return False
return True
def main():
# --- 1. Load config (optional path as first CLI arg) ---
config_path = sys.argv[1] if len(sys.argv) > 1 else "config/checklist_example.json"
config = load_checklist_config(config_path)
logger.info(f"Loaded strategy: {config['name']}")
# --- 2. Connect broker ---
broker = build_broker(config)
if not broker.connect():
logger.error("Failed to connect to broker")
return
# --- 3. Build tools ---
tools = build_tools(config["checks"])
logger.info(f"Tools loaded: {list(tools.keys())}")
# --- 4. Create checklist + risk + execution ---
checklist = Checklist(config, tools)
risk = RiskManager(config.get("risk", {}))
engine = ExecutionEngine(broker, risk)
# Set initial balance for daily tracking
account = broker.get_account_info()
if account:
risk.set_initial_balance(account.balance)
logger.info(f"Initial balance: {account.balance}")
# --- 5. Main loop ---
schedule = config.get("schedule", {})
interval = schedule.get("interval_seconds", 30)
symbols = config.get("symbols", [])
timeframe = config.get("timeframe", "M15")
logger.info(f"Starting loop | {len(symbols)} symbols | {timeframe} | every {interval}s")
try:
while True:
if not is_trading_time(schedule):
logger.debug("Outside trading hours")
time.sleep(60)
continue
for symbol in symbols:
try:
# Check for open positions first
positions = broker.get_positions(symbol)
if positions:
engine.check_risk_free(symbol)
continue
# Get data
df = broker.get_ohlcv(symbol, timeframe)
if df is None or df.empty:
continue
# Inject live tick data into context (needed by spread_filter)
context = {}
tick = broker.get_tick(symbol)
sym_info = broker.get_symbol_info(symbol)
if tick and sym_info:
context["tick"] = {
"bid": tick.bid,
"ask": tick.ask,
"point": sym_info.point,
}
# Evaluate checklist
decision = checklist.evaluate(df, symbol, initial_context=context)
if decision.go:
logger.info(f"\n{decision.summary()}")
engine.execute(decision)
except Exception as e:
logger.error(f"Error processing {symbol}: {e}", exc_info=True)
time.sleep(interval)
except KeyboardInterrupt:
logger.info("Shutting down...")
finally:
broker.disconnect()
if __name__ == "__main__":
main()