-
Notifications
You must be signed in to change notification settings - Fork 3
/
alpaca.py
110 lines (81 loc) · 2.95 KB
/
alpaca.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
# System libraries
import os, time, sys, glob
from pathlib import Path
from datetime import date, datetime, timedelta
# API libraries
from dotenv import load_dotenv
import alpaca_trade_api as tradeapi
# Data libraries
import pandas as pd
from pandas import DataFrame, Timestamp, concat
alpaca_secret_key = str()
alpaca_secret_key = str()
def load_api_keys(api_path : str = '../resources/api_keys.env',
debug : bool = False):
'''Instantiates connection to Alpaca Trade API'''
# load API keys
load_dotenv(api_path)
# set Alpaca API key and secret
alpaca_api_key = os.getenv("ALPACA_API_KEY")
alpaca_secret_key = os.getenv("ALPACA_SECRET_KEY")
if debug:
print(f"Testing Apaca Trade API key by data type:")
print(f"ALPACA_API_KEY: {type(alpaca_api_key)}")
print(f"ALPACA_SECRET_KEY: {type(alpaca_secret_key)}")
# create the Alpaca API object
api = tradeapi.REST(
alpaca_api_key,
alpaca_secret_key,
api_version = "v2"
)
return api
def ohlcv(tickers: list or DataFrame,
start_date : str = '2020-01-01',
end_date : str = datetime.now().strftime('%Y-%m-%d'),
timeframe : str = '1D',
api_key_path : str = '../../../resources/api_keys.env',
debug : bool = False
) -> DataFrame:
'''Returns pd.DataFrame with prices for the given tickers
...
Parameters
----------
tickers : list of str
list of tickers
start_date : str
string with date in following format YYYY-MM-DD; default = '2020-01-01'
end_date : str
string with date in following format YYYY-MM-DD; default = today's date {datetime.now.strftime('%Y-%m-%d')}
timeframe : str
timeframe for the ohlcv barset; default = '1D'. The valid intervals are: 1Min, 5Min, 15Min and 1D
api_key_path : str
path for the .env file containing Alpaca Trade API key and secret key
Returns
-------
ohlcv_df : DataFrame with securities price data
'''
api = load_api_keys(api_key_path, debug=debug)
# parse start and end dates
start_date = Timestamp(start_date, tz="America/New_York").isoformat()
end_date = Timestamp(end_date, tz="America/New_York").isoformat()
# connect to Alpaca Trade API and get ohlcv
"""Condition handling: Alpaca API 422 Client Error if more than 100 tickers are passed"""
ohlcv_df = DataFrame()
tickers_n = 50
for i in range(0, len(tickers), tickers_n):
sliced_tickers = tickers[i:i + tickers_n]
df = api.get_barset(
sliced_tickers,
timeframe = timeframe,
start = start_date,
end = end_date,
limit = 1000).df
ohlcv_df = concat([ohlcv_df, df], axis = "columns", join = "outer")
time.sleep(0.1)
return ohlcv_df
def test():
pass
def main():
pass
if __name__ == '__main__':
main()