Created
January 27, 2020 16:32
-
-
Save D01010111/9cd027e1a1104aafd385423488807966 to your computer and use it in GitHub Desktop.
Python script to pull daily historic data on Oil and Gas Assets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import datetime | |
from datetime import datetime, timedelta | |
from datetime import date | |
import pandas as pd | |
from yahoofinancials import YahooFinancials | |
#just some boring date stuff so I can input a number of days of historic data I want | |
days = 100 | |
date = (f'{datetime.now():%Y-%m-%d}') | |
pdate = datetime.now() - timedelta(days=days) | |
pdate = (f'{pdate:%Y-%m-%d}') | |
#List of assets we want data on | |
oil_stocks = ['XOM', 'CVX', 'COP', 'SLB', 'MRO', 'PSX', 'KMI', 'HAL'] | |
oil = YahooFinancials(oil_stocks) | |
daily_oil = oil.get_historical_price_data(pdate, date, 'daily') | |
oil_prices = oil.get_current_price() | |
for x in oil_stocks: | |
print(x) | |
df = pd.DataFrame(daily_oil[x]['prices']) | |
print(df) | |
df.to_csv(r'/Users/D01010111/Documents/Python/Personal Research/'+x+'.csv') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment