-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopulate_stocks.py
34 lines (26 loc) · 952 Bytes
/
populate_stocks.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
from shutil import ExecError
import sqlite3, config
import alpaca_trade_api as tradeapi
connection = sqlite3.connect(config.DB_FILE)
connection.row_factory = sqlite3.Row
cursor = connection.cursor()
cursor.execute("""
SELECT symbol, name FROM stock
""")
rows = cursor.fetchall()
symbols = {row['symbol'] for row in rows}
api = tradeapi.REST(config.API_KEY, config.SECRET_KEY, base_url=config.BASE_URL)
# get the list of the assets from the alpaca database
assets = api.list_assets()
# insert the data to the database
for asset in assets:
if '/' in asset.symbol:
continue
try:
if asset.status == 'active' and asset.tradable and asset.symbol not in symbols:
print("Added stocks")
cursor.execute("INSERT INTO stock (symbol, name, exchange) VALUES (?, ?, ?)", (asset.symbol, asset.name, asset.exchange))
except Exception as e:
print(asset.symbol)
print(e)
connection.commit()