-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalpaca_client.py
More file actions
51 lines (40 loc) · 1.44 KB
/
alpaca_client.py
File metadata and controls
51 lines (40 loc) · 1.44 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
import os
from alpaca.data.historical import StockHistoricalDataClient
from alpaca.data.requests import StockBarsRequest
from alpaca.data.timeframe import TimeFrame, TimeFrameUnit
from datetime import datetime, timedelta
from dotenv import load_dotenv
load_dotenv()
API_KEY = os.getenv("APCA_API_KEY_ID")
API_SECRET = os.getenv("APCA_API_SECRET_KEY")
client = StockHistoricalDataClient(API_KEY, API_SECRET)
def get_premarket_high_low(symbols):
today = datetime.now().date()
start_time = datetime.combine(today, datetime.min.time()) + timedelta(
hours=4
) # 4 am
end_time = datetime.combine(today, datetime.min.time()) + timedelta(
hours=9, minutes=25
) # 9:25 am
timeframe_5min = TimeFrame(5, TimeFrameUnit.Minute)
request = StockBarsRequest(
symbol_or_symbols=symbols,
start=start_time,
end=end_time,
timeframe=timeframe_5min,
)
bars = client.get_stock_bars(request).df.reset_index()
high_low_map = {}
if bars.empty:
print(f"No premarket data returned.")
return {}
for symbol in symbols:
symbol_data = bars[bars["symbol"] == symbol]
if symbol_data.empty:
print(f"No premarket data for {symbol}.")
high_low_map[symbol] = (None, None)
else:
high = symbol_data["high"].max()
low = symbol_data["low"].min()
high_low_map[symbol] = (high, low)
return high_low_map