-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
249 lines (224 loc) · 8.57 KB
/
main.py
File metadata and controls
249 lines (224 loc) · 8.57 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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
import questionary
from utils.data_fetcher import (
fetch_market_data,
fetch_news,
fetch_onchain_data,
fetch_nvt,
fetch_mvrv,
fetch_historical_prices,
calculate_rsi,
calculate_correlations,
calculate_var,
)
from agents.valuation_agent import valuation_agent
from agents.sentiment_agent import sentiment_agent
from agents.fundamentals_agent import fundamentals_agent
from agents.technicals_agent import technicals_agent
from agents.strategy_agent import strategy_agent
from agents.risk_manager import risk_manager
from agents.portfolio_manager import portfolio_manager
from utils.portfolio import Portfolio
from termcolor import colored
# all available agents
AGENT_CHOICES = [
{"name": "Valuation Agent (analyzes market valuation)", "value": "valuation"},
{"name": "Sentiment Agent (analyzes news sentiment)", "value": "sentiment"},
{"name": "Fundamentals Agent (analyzes on-chain metrics)", "value": "fundamentals"},
{"name": "Technicals Agent (analyzes price charts)", "value": "technicals"},
{"name": "Strategy Agent (synthesizes signals into trading strategies)", "value": "strategy"},
]
# all available cryptocurrencies
CRYPTO_CHOICES = [
{"name": "Binance Coin (BNB)", "value": "binancecoin"},
{"name": "Bitcoin (BTC)", "value": "bitcoin"},
{"name": "Ethereum (ETH)", "value": "ethereum"},
{"name": "Ripple (XRP)", "value": "xrp"},
{"name": "USD Coin (USDC)", "value": "usd-coin"}
]
# all available models
MODEL_CHOICES = [
{"name": "Qwen QwQ 32B (most accurate, may run into loop)", "value": "qwen-qwq-32b|131072|0.6|0.95"},
{"name": "Deepseek R1 Distil Llama 70B (even more accurate)", "value": "deepseek-r1-distill-llama-70b|131072|0.6|0.95"},
{"name": "Qwen 2.5 32B (more accurate)", "value": "qwen-2.5-32b|131072|0.7|0.8"},
{"name": "Llama 3.3 70B (fastest)", "value": "llama-3.3-70b-specdec|8192|0.6|0.9"},
]
def main():
print(colored("Welcome to the AI Crypto Fund", "red", attrs=["bold"]))
# user inputs
selected_agents = questionary.checkbox(
"Select agents to use:",
choices=AGENT_CHOICES,
validate=lambda x: len(x) > 0 or "Please select at least one agent.",
).ask()
selected_cryptos = questionary.checkbox(
"Select cryptocurrencies to trade:",
choices=CRYPTO_CHOICES,
validate=lambda x: len(x) > 0 or "Please select at least one cryptocurrency.",
).ask()
selected_model = questionary.select(
"Select model to use:", choices=MODEL_CHOICES
).ask()
# Extract the values (e.g., "valuation", "bitcoin") from the selected choices
selected_agents = selected_agents
selected_cryptos = selected_cryptos
available_money = questionary.text("How much money do you have to invest?").ask()
if (
available_money is None
or not available_money.isdigit()
or float(available_money) <= 0
):
print(colored("No money, no honey!", "red"))
return
portfolio = Portfolio(initial_cash=float(available_money))
# fetch market data
print(colored("\nFetching market data...", "blue"))
market_data = fetch_market_data(selected_cryptos)
# fetch news articles
print(colored("Fetching news articles...", "blue"))
news = fetch_news()
# fetch on-chain data
print(colored("Fetching on-chain data...", "blue"))
onchain_data = {crypto: fetch_onchain_data(crypto) for crypto in selected_cryptos}
# fetch historical prices
print(colored("Fetching historical prices...", "blue"))
historical_prices = {
crypto: fetch_historical_prices(crypto) for crypto in selected_cryptos
}
# calculate technical indicators
print(colored("Calculating technical indicators...", "blue"))
technical_indicators = {
crypto: {"RSI": calculate_rsi(historical_prices[crypto])}
for crypto in selected_cryptos
}
# fetch valuation metrics
print(colored("Fetching valuation metrics...", "blue"))
valuation_metrics = {
crypto: {"NVT": fetch_nvt(crypto), "MVRV": fetch_mvrv(crypto)}
for crypto in selected_cryptos
}
# generate signals from selected agents
signals = {}
for crypto in selected_cryptos:
signals[crypto] = {}
for agent_name in selected_agents:
prefix = colored(
f"Analyzing {crypto} with {agent_name} agent...", "magenta"
)
print(prefix, end="", flush=True)
current_price = market_data[crypto]["usd"]
if agent_name == "valuation":
signals[crypto][agent_name] = valuation_agent(
crypto,
current_price,
valuation_metrics[crypto],
prefix=prefix,
model=selected_model,
)
elif agent_name == "sentiment":
signals[crypto][agent_name] = sentiment_agent(
crypto, news, prefix=prefix, model=selected_model
)
elif agent_name == "fundamentals":
signals[crypto][agent_name] = fundamentals_agent(
crypto, onchain_data[crypto], prefix=prefix, model=selected_model
)
elif agent_name == "technicals":
signals[crypto][agent_name] = technicals_agent(
crypto,
historical_prices[crypto],
technical_indicators[crypto],
prefix=prefix,
model=selected_model,
)
elif agent_name == "strategy":
signals[crypto][agent_name] = strategy_agent(
crypto,
signals[crypto],
portfolio.get_state()["positions"],
prefix=prefix,
model=selected_model,
)
print()
# calculate correlations
print(colored("Calculating correlations...", "blue"))
correlations = calculate_correlations(historical_prices)
# calculate portfolio VaR
print(colored("Calculating portfolio VaR...", "blue"))
positions = portfolio.get_state()["positions"]
total_value = sum(
positions.get(crypto, {"long": 0, "short": 0})["long"]
* market_data[crypto]["usd"]
for crypto in selected_cryptos
)
if total_value == 0: # if no positions, assume equal weights
portfolio_weights = {
crypto: 1 / len(selected_cryptos) for crypto in selected_cryptos
}
else:
portfolio_weights = {
crypto: (
positions.get(crypto, {"long": 0, "short": 0})["long"]
* market_data[crypto]["usd"]
)
/ total_value
for crypto in selected_cryptos
}
var = calculate_var(
historical_prices, portfolio_weights, confidence_level=0.95, time_horizon=1
)
# risk management
prefix = colored("Assessing portfolio risk...", "blue")
print(prefix, end="", flush=True)
risk_output = risk_manager(
portfolio.get_state()["positions"],
historical_prices,
correlations,
{"var": var},
prefix=prefix,
model=selected_model,
)
print()
# make trading decisions
prefix = colored("Making trading decisions...", "blue")
print(prefix, end="", flush=True)
current_prices = {crypto: market_data[crypto]["usd"] for crypto in selected_cryptos}
decisions = portfolio_manager(
signals,
risk_output["max_amounts"],
portfolio.cash,
portfolio.get_state()["positions"],
current_prices,
margin_requirement=0.5,
prefix=prefix,
model=selected_model,
)
print()
print(colored("\nTrading Decisions:", "green", attrs=["bold"]))
for crypto in selected_cryptos:
decision = decisions["decisions"].get(
crypto,
{
"action": "hold",
"quantity": 0.0,
"confidence": 0.0,
"reasoning": "No signal provided",
},
)
action = decision["action"]
quantity = decision["quantity"]
confidence = decision["confidence"]
reasoning = decision["reasoning"]
color = (
"green"
if action in ["buy", "cover"]
else "red" if action in ["sell", "short"] else "yellow"
)
print(
colored(
f"{crypto.capitalize()}: {action.upper()} {quantity:.2f} USD (Confidence: {confidence}%)",
color,
)
)
print(colored(f"Reasoning: {reasoning}", "white"))
if __name__ == "__main__":
main()