Skip to content

Instantly share code, notes, and snippets.

@D01010111
Created January 27, 2020 16:32
Show Gist options
  • Save D01010111/9cd027e1a1104aafd385423488807966 to your computer and use it in GitHub Desktop.
Save D01010111/9cd027e1a1104aafd385423488807966 to your computer and use it in GitHub Desktop.
Python script to pull daily historic data on Oil and Gas Assets
Display the source blob
Display the rendered blob
Raw
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