-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_api.py
More file actions
48 lines (40 loc) · 1.52 KB
/
test_api.py
File metadata and controls
48 lines (40 loc) · 1.52 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
from alpaca.data.historical import CryptoHistoricalDataClient
from alpaca.data.requests import CryptoBarsRequest
from alpaca.data.timeframe import TimeFrame
from config import ALPACA_API_KEY, ALPACA_API_SECRET, SYMBOL
def main():
print("\n=== Alpaca API Connection Test ===")
# Initialize client
print("\nInitializing client...")
client = CryptoHistoricalDataClient()
print(f"\nRequesting current price for {SYMBOL}")
try:
# Get latest bar (no time parameters needed)
print("\nSending request to Alpaca...")
bars = client.get_crypto_bars(
CryptoBarsRequest(
symbol_or_symbols=SYMBOL,
timeframe=TimeFrame.Minute,
limit=1 # Just get the latest bar
)
)
if bars is not None:
print("\nResponse received!")
df = bars.df.reset_index()
if not df.empty:
print("\nCurrent Market Data:")
latest = df.iloc[-1]
print(f"Price: ${latest['close']:,.2f}")
print(f"Volume: {latest['volume']:.3f}")
print(f"VWAP: ${latest.get('vwap', 0):,.2f}")
else:
print("\nNo data in response")
else:
print("\nNo response from API")
except Exception as e:
print(f"\nError: {str(e)}")
import traceback
print("\nFull error traceback:")
print(traceback.format_exc())
if __name__ == "__main__":
main()