generated from The-Swarm-Corporation/Multi-Agent-Template-App
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
queen_swarm.py
224 lines (181 loc) · 6.94 KB
/
queen_swarm.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
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import asyncio
from dataclasses import dataclass
from decimal import Decimal
from typing import Any, Dict
import ccxt
import pandas as pd
from loguru import logger
from swarms import Agent
# Configure logging
logger.add("swarm.log", rotation="500 MB", level="INFO")
@dataclass
class SharedMemory:
"""Shared memory system for all agents"""
market_data: Dict = None
trading_signals: Dict = None
risk_metrics: Dict = None
positions: Dict = None
def update(self, key: str, value: Any):
"""Update any attribute in shared memory"""
if hasattr(self, key):
setattr(self, key, value)
logger.info(f"Updated shared memory: {key}")
class MarketDataAgent(Agent):
def __init__(self, shared_memory: SharedMemory):
super().__init__(
agent_name="Market-Data-Agent",
system_prompt="You are a market data specialist. Monitor and analyze crypto market data.",
)
self.exchange = ccxt.kraken({"enableRateLimit": True})
self.shared_memory = shared_memory
async def run(self, symbol: str) -> None:
try:
data = await self.exchange.fetch_ohlcv(
symbol, "1h", limit=100
)
df = pd.DataFrame(
data,
columns=[
"timestamp",
"open",
"high",
"low",
"close",
"volume",
],
)
self.shared_memory.update("market_data", {symbol: df})
except Exception as e:
logger.error(f"Market data error: {e}")
return None
class SignalAgent(Agent):
def __init__(self, shared_memory: SharedMemory):
super().__init__(
agent_name="Signal-Agent",
system_prompt="You are a trading signal specialist. Generate trading signals based on market data.",
)
self.shared_memory = shared_memory
async def run(self, symbol: str) -> None:
try:
if not self.shared_memory.market_data:
return
df = self.shared_memory.market_data[symbol]
# Get AI analysis
analysis = await self.run(
f"Analyze price action for {symbol}: Current price {df['close'].iloc[-1]}, "
f"Volume: {df['volume'].iloc[-1]}"
)
signal = {
"symbol": symbol,
"action": analysis.get("recommendation", "HOLD"),
"confidence": analysis.get("confidence", 0),
"timestamp": pd.Timestamp.now(),
}
self.shared_memory.update(
"trading_signals", {symbol: signal}
)
except Exception as e:
logger.error(f"Signal generation error: {e}")
class RiskAgent(Agent):
def __init__(self, shared_memory: SharedMemory):
super().__init__(
agent_name="Risk-Agent",
system_prompt="You are a risk management specialist. Evaluate trading risks and set position sizes.",
)
self.shared_memory = shared_memory
async def run(self, symbol: str) -> None:
try:
if not (
self.shared_memory.market_data
and self.shared_memory.trading_signals
):
return
signal = self.shared_memory.trading_signals[symbol]
risk_metrics = {
"symbol": symbol,
"position_size": Decimal(
"0.01"
), # Default conservative size
"risk_score": 0.5,
"timestamp": pd.Timestamp.now(),
}
# Get AI risk assessment
assessment = await self.run(
f"Evaluate risk for {symbol} trade with signal confidence {signal['confidence']}"
)
risk_metrics.update(assessment)
self.shared_memory.update(
"risk_metrics", {symbol: risk_metrics}
)
except Exception as e:
logger.error(f"Risk assessment error: {e}")
class QueenAgent(Agent):
def __init__(self, shared_memory: SharedMemory):
super().__init__(
agent_name="Queen-Agent",
system_prompt="You are the queen bee coordinator. Make final trading decisions based on all available information.",
)
self.shared_memory = shared_memory
self.market_agent = MarketDataAgent(shared_memory)
self.signal_agent = SignalAgent(shared_memory)
self.risk_agent = RiskAgent(shared_memory)
async def run(self, symbol: str) -> Dict:
"""Coordinate the swarm and make final decisions"""
try:
# Run all agents sequentially
await self.market_agent.run(symbol)
await self.signal_agent.run(symbol)
await self.risk_agent.run(symbol)
# Make final decision
if all(
[
self.shared_memory.market_data,
self.shared_memory.trading_signals,
self.shared_memory.risk_metrics,
]
):
signal = self.shared_memory.trading_signals[symbol]
risk = self.shared_memory.risk_metrics[symbol]
# Get AI final decision
decision = await self.run(
f"Make final trading decision for {symbol}:\n"
f"Signal: {signal['action']}\n"
f"Confidence: {signal['confidence']}\n"
f"Risk Score: {risk['risk_score']}"
)
# Update positions if trade is approved
if decision.get("execute", False):
self.shared_memory.update(
"positions",
{
symbol: {
"action": signal["action"],
"size": risk["position_size"],
"timestamp": pd.Timestamp.now(),
}
},
)
return decision
except Exception as e:
logger.error(f"Queen agent error: {e}")
return {"execute": False, "reason": str(e)}
async def main():
# Initialize shared memory
shared_memory = SharedMemory()
# Initialize queen agent
queen = QueenAgent(shared_memory)
symbols = ["BTC/USD", "ETH/USD"]
try:
while True:
for symbol in symbols:
decision = await queen.run(symbol)
logger.info(f"Decision for {symbol}: {decision}")
await asyncio.sleep(1) # Rate limiting
await asyncio.sleep(60) # Main loop interval
except KeyboardInterrupt:
logger.info("Shutting down swarm...")
except Exception as e:
logger.error(f"Critical error: {e}")
raise
if __name__ == "__main__":
asyncio.run(main())