-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathbasic_trade.py
More file actions
executable file
·105 lines (83 loc) · 2.73 KB
/
Copy pathbasic_trade.py
File metadata and controls
executable file
·105 lines (83 loc) · 2.73 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
#!/usr/bin/env python3
"""
Basic Trade Example
===================
Demonstrates how to place a paper trade using the Trading Buddy API.
Usage:
python examples/basic_trade.py
Prerequisites:
- Trading Buddy server running (make run)
- Alpaca paper trading keys in .env
"""
import requests
import json
# Configuration
BASE_URL = "http://localhost:8000"
def get_quote(symbol: str) -> dict:
"""Fetch current quote for a symbol."""
response = requests.get(f"{BASE_URL}/api/quotes/{symbol}")
return response.json()
def propose_trade(symbol: str, action: str, quantity: int, price: float) -> dict:
"""
Propose a trade for risk assessment.
The trade goes through the risk manager before execution.
"""
payload = {
"symbol": symbol,
"action": action, # "BUY" or "SELL"
"quantity": quantity,
"price": price,
"conviction": 0.75, # Confidence level (0-1)
"rationale": "Example trade from basic_trade.py"
}
response = requests.post(
f"{BASE_URL}/api/trade/propose",
json=payload,
headers={"Content-Type": "application/json"}
)
return response.json()
def get_portfolio() -> dict:
"""Get current portfolio status."""
response = requests.get(f"{BASE_URL}/api/portfolio")
return response.json()
def main():
print("=" * 50)
print("Trading Buddy - Basic Trade Example")
print("=" * 50)
# 1. Check portfolio status
print("\n📊 Current Portfolio:")
portfolio = get_portfolio()
if "error" not in portfolio:
print(f" Cash: ${portfolio.get('cash', 0):,.2f}")
print(f" Equity: ${portfolio.get('equity', 0):,.2f}")
else:
print(f" Error: {portfolio.get('error')}")
# 2. Get a quote
symbol = "AAPL"
print(f"\n📈 Getting quote for {symbol}...")
quote = get_quote(symbol)
if "error" not in quote:
price = quote.get("price", 0)
print(f" Current Price: ${price:.2f}")
else:
print(f" Error: {quote.get('error')}")
price = 185.00 # Fallback for demo
# 3. Propose a trade
print(f"\n🔄 Proposing trade: BUY 10 shares of {symbol}...")
trade_result = propose_trade(
symbol=symbol,
action="BUY",
quantity=10,
price=price
)
print("\n📋 Trade Proposal Result:")
print(json.dumps(trade_result, indent=2))
# 4. Check if approved
if trade_result.get("approved"):
print("\n✅ Trade was APPROVED by risk manager")
print(f" Risk Score: {trade_result.get('risk_score', 'N/A')}")
else:
print("\n❌ Trade was REJECTED by risk manager")
print(f" Reason: {trade_result.get('reason', 'Unknown')}")
if __name__ == "__main__":
main()