# -*- coding: utf-8 -*-
"""
Created on Sun Jan 5 11:02:36 2020
@author: Bruce1
"""
# =======================================================================
# Import Pandas Library
# =======================================================================
import pandas as pd
import pandas_datareader.data as web
from pathlib import Path
import matplotlib.pyplot as plt
# =======================================================================
# Setup Variables to data fetch
# =======================================================================
sym = 'GLD'
startDate = '01/01/2000'
savedFile = Path('./{}.xlsx'.format(sym))
refreshData = False
lookback = 21
# =======================================================================
# Gather data
# =======================================================================
if savedFile.exists() == False or refreshData == True:
print("")
print("-> Fetching data from the web")
df = web.DataReader(sym, data_source='yahoo', start=startDate)
print("")
print("-> Save data to file")
df.to_excel("{}.xlsx".format(sym))
else:
print("")
print("-> Fetching data from file")
df = pd.read_excel(savedFile, index_col='Date', parse_dates=True)
# =======================================================================
# Inspect/Report on data
# =======================================================================
firstIndex = df.index.min()
lastIndex = df.index.max()
records = len(df)
print("")
print("-> Importing ", sym)
print("First Date = ", firstIndex)
print("Last Date = ", lastIndex)
print("Total Days = ", records)
if df.isnull().values.any() == True:
print("WARNING: there are {} NaN in the data".format(df.isnull().values.sum()))
print(df.isnull().values)
# =======================================================================
# Add Indicator Columns
# =======================================================================
print("")
print("-> Add Indicator Columns")
df.loc[:,'SMA'] = df.Close.rolling(lookback).mean()
df.loc[:,'HistVol'] = df.Close.pct_change().rolling(lookback).std()
#Weighted On Balance Volume
df.loc[:,'WB'] = df.Close.diff() * df.Volume
df.loc[:,'WOBV'] = df.WB.cumsum()
# =======================================================================
# Visualize Data
# =======================================================================
#import pdb; pdb.set_trace()
print("")
print("-> Generating Plot")
plt.figure()
df.Close.plot(kind='line')
plt.show()
plt.figure()
df[['Close','SMA']].plot(kind='line')
plt.show()
plt.figure()
df['2019'][['Close','SMA']].plot(kind='line')
plt.show
plt.figure()
df.HistVol.plot(kind='hist', bins=50)
plt.show()
plt.figure()
df[['Close','WOBV']].plot(kind='line', secondary_y='WOBV')
plt.show()
# =======================================================================
# Store data
# =======================================================================
df.to_excel("{}_processed.xlsx".format(sym))