-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathalpaca_broker.py
252 lines (221 loc) · 9.16 KB
/
alpaca_broker.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
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
250
251
252
try:
from alpaca.trading.client import TradingClient
from alpaca.data.historical import StockHistoricalDataClient
from alpaca.trading.requests import MarketOrderRequest, GetAssetsRequest
from alpaca.trading.enums import OrderSide, TimeInForce, AssetClass
import alpaca_trade_api as tradeapi
from alpaca.common.exceptions import APIError
except ImportError as e:
raise ImportError(
f"Failed to import Alpaca SDK: {str(e)}. "
"Please install required packages: pip install alpaca-py alpaca-trade-api"
)
import pandas as pd
from datetime import datetime
import pytz
import logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
class AlpacaBroker:
def __init__(self, api_key, api_secret=None, paper=True):
"""Initialize Alpaca broker with proper error handling"""
if not api_key or not api_secret:
raise ValueError("Both API key and secret are required for Alpaca")
self.api_key = api_key
self.api_secret = api_secret
self.paper = paper
self.base_url = "https://paper-api.alpaca.markets" if paper else "https://api.alpaca.markets"
self.trading_client = None
self.data_client = None
self.rest_api = None
self.initialize_clients()
def initialize_clients(self):
"""Initialize all Alpaca API clients"""
try:
# Initialize REST API client
self.rest_api = tradeapi.REST(
key_id=self.api_key,
secret_key=self.api_secret,
base_url=self.base_url,
api_version='v2'
)
# Initialize Trading client
self.trading_client = TradingClient(
api_key=self.api_key,
secret_key=self.api_secret,
paper=self.paper
)
# Initialize Data client
self.data_client = StockHistoricalDataClient(
api_key=self.api_key,
secret_key=self.api_secret
)
logger.info("Successfully initialized Alpaca clients")
except Exception as e:
logger.error(f"Failed to initialize Alpaca clients: {str(e)}")
raise
def test_connection(self):
"""Test connection to Alpaca API with detailed error handling"""
try:
if not self.rest_api:
raise ValueError("REST API client not initialized")
# Test authentication and get account
account = self.rest_api.get_account()
if account.status != 'ACTIVE':
logger.error(f"Account not active. Status: {account.status}")
return False
return True
except APIError as e:
logger.error(f"Alpaca API error: {str(e)}")
return False
except Exception as e:
logger.error(f"Alpaca connection test failed: {str(e)}")
return False
def get_account_details(self):
"""Get account details with enhanced error handling"""
try:
if not self.trading_client:
raise ValueError("Trading client not initialized")
account = self.rest_api.get_account()
return {
'balance': float(account.equity),
'currency': 'USD',
'marginAvailable': float(account.buying_power),
'pl': float(account.equity) - float(account.last_equity),
'openPositionCount': len(self.rest_api.list_positions()),
'openTradeCount': len(self.rest_api.list_orders(status='open')),
'marginRate': float(account.multiplier)
}
except Exception as e:
logger.error(f"Error getting account details: {str(e)}")
return {"error": str(e)}
def create_order(self, order_data):
"""Create a new trading order"""
try:
order = order_data.get('order', {})
side = 'buy' if float(order.get('units', 0)) > 0 else 'sell'
qty = abs(float(order.get('units', 0)))
# Map OANDA order types to Alpaca order types
order_type_map = {
"MARKET": "market",
"LIMIT": "limit",
"STOP": "stop"
}
order_type = order_type_map.get(order.get('type', 'MARKET'), 'market')
response = self.api.submit_order(
symbol=order.get('instrument', '').replace('_', ''),
qty=qty,
side=side,
type=order_type,
time_in_force='day',
limit_price=order.get('price') if order_type == 'limit' else None,
stop_price=order.get('price') if order_type == 'stop' else None
)
return {
"orderFillTransaction": {
"id": response.id,
"instrument": response.symbol,
"units": response.qty if side == 'buy' else -float(response.qty),
"time": response.submitted_at.isoformat()
}
}
except Exception as err:
raise Exception(f"Failed to create order: {err}")
def get_positions(self):
"""Get current positions"""
try:
positions = self.api.list_positions()
return {
"positions": [
{
"instrument": pos.symbol,
"units": float(pos.qty),
"current_price": float(pos.current_price),
"unrealized_pl": float(pos.unrealized_pl)
} for pos in positions
]
}
except Exception as err:
return {"error": f"Failed to get positions: {str(err)}"}
def close_position(self, instrument):
"""Close a position for given instrument"""
try:
response = self.api.close_position(instrument.replace('_', ''))
return {
"orderFillTransaction": {
"id": response.id,
"instrument": response.symbol,
"units": response.qty,
"time": response.submitted_at.isoformat()
}
}
except Exception as err:
return {"error": f"Failed to close position: {str(err)}"}
def get_candlestick_data(self, instrument, timeframe="1Min", limit=100):
"""Get candlestick data for an instrument"""
try:
# Convert OANDA timeframe to Alpaca timeframe
timeframe_map = {
"M1": "1Min",
"M5": "5Min",
"M15": "15Min",
"M30": "30Min",
"H1": "1Hour",
"H4": "4Hour",
"D": "1Day"
}
alpaca_timeframe = timeframe_map.get(timeframe, timeframe)
bars = self.api.get_barset(
instrument.replace('_', ''),
alpaca_timeframe,
limit=limit
)
return {
"candles": [
{
"time": bar.t.isoformat(),
"volume": bar.v,
"mid": {
"o": str(bar.o),
"h": str(bar.h),
"l": str(bar.l),
"c": str(bar.c)
}
} for bar in bars[instrument.replace('_', '')]
]
}
except Exception as err:
raise ValueError(f"Failed to fetch candlestick data: {str(err)}")
def get_trades(self):
"""Get open trades"""
try:
trades = self.api.list_orders(status='open')
return {
"trades": [
{
"id": trade.id,
"instrument": trade.symbol,
"units": float(trade.qty),
"price": float(trade.limit_price or trade.stop_price or 0),
"time": trade.submitted_at.isoformat()
} for trade in trades
]
}
except Exception as err:
return {"error": f"Failed to get trades: {str(err)}"}
def process_order_request(self, data):
"""Process order request data"""
try:
order = data.get('order', {})
side = OrderSide.BUY if float(order.get('units', 0)) > 0 else OrderSide.SELL
# Create market order request
request = MarketOrderRequest(
symbol=order.get('instrument', '').replace('_', ''),
qty=abs(float(order.get('units', 0))),
side=side,
time_in_force=TimeInForce.DAY
)
return request
except Exception as e:
logger.error(f"Error processing order request: {str(e)}")
raise ValueError(f"Invalid order data: {str(e)}")