-
-
Save Industrial/597379bd4ba5168c26e49a9be8817a91 to your computer and use it in GitHub Desktop.
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 json | |
from matplotlib import style, pyplot as plt | |
import pandas | |
import urllib.request | |
import finta | |
style.use('default') | |
STOCK_SYMBOL = 'AAPL' | |
INPUT_URL = 'https://www.quandl.com/api/v3/datasets/WIKI/%s.json?order=asc' % (STOCK_SYMBOL) | |
OUTPUT_PATH = './data/OHLCV/%s.xlsx' % (STOCK_SYMBOL) | |
def import_data(): | |
response = urllib.request.urlopen(INPUT_URL) | |
response_data = json.loads(response.read()) | |
sanitized_data = response_data['dataset']['data'] | |
return sanitized_data | |
def create_dataframe(dataframe_data): | |
df = pandas.read_json(json.dumps(dataframe_data)) | |
df.columns = ['date', 'open', 'high', 'low', 'close', 'volume', 'ex-dividend', 'split ratio', 'adj. open', 'adj. high', 'adj. low', 'adj. close', 'adj. volume'] | |
df['std'] = df['close'].std() | |
df['ema7'] = finta.TA.EMA(df, period=7) | |
df['ema20'] = finta.TA.EMA(df, period=20) | |
df = df.join(finta.TA.BBANDS(df, MA=df['ema20'], column='close')) | |
return df | |
def draw_image(df): | |
fig, ax = plt.subplots() | |
df = df[[ | |
'date', | |
'close', | |
'std', | |
'upper_bband', | |
'middle_bband', | |
'lower_bband', | |
'b_bandwith' | |
]].copy() | |
df.date = pandas.to_datetime(df.date, format='%Y-%m-%d') | |
df.set_index(['date'], inplace=True) | |
ax.set(title=STOCK_SYMBOL, xlabel='time (days)', ylabel='price ($)') | |
ax.grid() | |
df.plot(ax=ax) | |
plt.show() | |
def write_excel_file(df): | |
df.to_excel(OUTPUT_PATH, sheet_name=STOCK_SYMBOL) | |
data = import_data() | |
ohlc = create_dataframe(data) | |
draw_image(ohlc) | |
write_excel_file(ohlc) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment