-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.py
More file actions
201 lines (163 loc) · 5.4 KB
/
main.py
File metadata and controls
201 lines (163 loc) · 5.4 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
#!/usr/bin/env python3
"""
Polymarket AI Trading Agent - CLI Entry Point
Usage:
python main.py # Run agent in dry-run mode
python main.py --analyze # Only fetch and analyze markets
python main.py --status # Check configuration status
python main.py --live # Run with live trading (caution!)
"""
import argparse
import sys
from datetime import datetime
from src.agent import PolymarketAgent, create_agent
from src.market.polymarket import fetch_active_markets, print_markets
from src.analysis.ai_analyzer import get_analyzer
from src.trading.wallet import get_wallet
from src.utils.kelly import KellyCriterion
def print_banner():
"""Print welcome banner"""
print()
print("=" * 70)
print(" POLYMARKET AI TRADING AGENT")
print(" Powered by BlockRun - Autonomous AI Payments")
print("=" * 70)
print(f" {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
print("=" * 70)
print()
def cmd_status():
"""Check and display configuration status"""
print_banner()
print("CONFIGURATION STATUS\n")
# Check AI (BlockRun)
print("[AI Analysis - BlockRun]")
analyzer = get_analyzer()
if analyzer:
print(f" Status: CONFIGURED")
print(f" Wallet: {analyzer.wallet_address}")
else:
print(f" Status: NOT CONFIGURED")
print(f" Action: Set BASE_CHAIN_WALLET_KEY in .env")
print()
# Check Trading Wallet
print("[Trading Wallet - Polygon]")
wallet = get_wallet()
if wallet:
print(f" Status: CONFIGURED")
print(f" Address: {wallet.address}")
balances = wallet.get_balances()
print(f" MATIC: {balances['matic']:.4f}")
print(f" USDC: {balances['usdc']:.2f}")
if wallet.check_approval():
print(f" Polymarket Approval: OK")
else:
print(f" Polymarket Approval: NEEDED")
print(f" Action: python -c \"from src.trading.wallet import *; w=get_wallet(); w.approve_usdc()\"")
else:
print(f" Status: NOT CONFIGURED")
print(f" Action: Set POLYGON_WALLET_PRIVATE_KEY in .env")
print()
# Check Kelly settings
print("[Position Sizing - Kelly Criterion]")
kelly = KellyCriterion()
print(f" Bankroll: ${kelly.bankroll}")
print(f" Max Bet: {kelly.max_bet_pct*100}%")
print(f" Min Edge: {kelly.min_edge_pct*100}%")
print()
def cmd_analyze():
"""Fetch markets and run AI analysis"""
print_banner()
print("MARKET ANALYSIS\n")
# Fetch markets
print("Fetching markets...")
markets = fetch_active_markets(limit=20)
if not markets:
print("Failed to fetch markets")
return
print(f"Found {len(markets)} active markets\n")
# Print top markets
print("Top Markets by End Date:")
print("-" * 60)
for i, m in enumerate(markets[:10], 1):
print(f"{i}. {m['question'][:55]}...")
vol = float(m.get('volume', 0) or 0)
print(f" End: {m['end_date']} | Vol: ${vol:,.0f}")
print()
# Run AI analysis
analyzer = get_analyzer()
if analyzer:
print("Running AI analysis...")
print(f"Using wallet: {analyzer.wallet_address}\n")
analysis = analyzer.analyze_markets(markets)
if analysis:
print("-" * 60)
print("AI ANALYSIS RESULTS")
print("-" * 60)
print(analysis)
else:
print("Analysis failed")
else:
print("AI analyzer not configured. Set BASE_CHAIN_WALLET_KEY in .env")
def cmd_run(live: bool = False):
"""Run the full agent"""
print_banner()
if live:
print("WARNING: LIVE TRADING MODE")
print("Real trades will be executed!")
confirm = input("Type 'yes' to confirm: ")
if confirm.lower() != 'yes':
print("Cancelled")
return
print()
agent = create_agent(
auto_trade=live,
dry_run=not live
)
# Check status first
status = agent.check_status()
if not status.get("ai_analyzer"):
print("Warning: AI analyzer not configured")
print("Set BASE_CHAIN_WALLET_KEY in .env for AI analysis\n")
if live and not status.get("wallet"):
print("Error: Trading wallet not configured")
print("Set POLYGON_WALLET_PRIVATE_KEY in .env")
return
if live and not status.get("approved"):
print("Error: USDC not approved for Polymarket")
print("Run: python -m src.trading.wallet")
return
# Run agent
results = agent.run()
# Summary
print("\nSUMMARY:")
print(f" Markets analyzed: {len(results.get('markets', []))}")
print(f" Recommendations: {len(results.get('recommendations', []))}")
print(f" Trades executed: {len(results.get('trades', []))}")
def main():
parser = argparse.ArgumentParser(
description="Polymarket AI Trading Agent"
)
parser.add_argument(
"--status",
action="store_true",
help="Check configuration status"
)
parser.add_argument(
"--analyze",
action="store_true",
help="Fetch markets and run AI analysis"
)
parser.add_argument(
"--live",
action="store_true",
help="Run with live trading (use with caution!)"
)
args = parser.parse_args()
if args.status:
cmd_status()
elif args.analyze:
cmd_analyze()
else:
cmd_run(live=args.live)
if __name__ == "__main__":
main()