forked from lab/TPM
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
36 lines
1.2 KiB
36 lines
1.2 KiB
import psycopg2 |
|
import yfinance as yf |
|
import numpy as np |
|
from psycopg2.extensions import register_adapter, AsIs |
|
psycopg2.extensions.register_adapter(np.int64, psycopg2._psycopg.AsIs) |
|
|
|
ticker = 'SPY' |
|
start = '2011-8-1' |
|
end = '2021-9-1' |
|
stock_data = yf.download(ticker, start=start, end=end ) |
|
stock_data.index = np.datetime_as_string(stock_data.index, unit='D') |
|
print(stock_data.index) |
|
stock_data['Ticker'] = ticker |
|
stock_data = stock_data.rename(columns={"Adj Close": "Adj_Close"}) |
|
column = ["Open" , "High" , "Low" , "Close" , "Volume"] |
|
stock_data = stock_data.drop(column, axis=1) |
|
records = stock_data.to_records(index=True) |
|
print(records) |
|
|
|
conn = psycopg2.connect( |
|
database="profolio_platform", user='postgres', password='password', host='db',port ='5432' |
|
) |
|
conn.autocommit = True |
|
cur = conn.cursor() |
|
# print(stock_data["Date"]) |
|
# print(stock_data["Adj_Close"]) |
|
# print(stock_data["Ticker"]) |
|
# print(type(stock_data["Date"])) |
|
# print(type(stock_data["Adj_Close"])) |
|
# print(type(stock_data["Ticker"])) |
|
query = """INSERT INTO stock_price (date,price,ticker) |
|
VALUES (%s, %s, %s)""" |
|
cur = conn.cursor() |
|
cur.executemany(query, records) |
|
conn.close() |
|
print("Data Insert Successfully")
|
|
|