Coding Trading Decisions

# -*- 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
import numpy as np

# =======================================================================
# Setup Variables to data fetch
# =======================================================================
sym = 'GLD'

startDate = '01/01/2000'

savedFile = Path('./{}.xlsx'.format(sym))
refreshData = False

lookback     = 21
lookbackSlow = 200
lookbackEMA  = 9

# =======================================================================
# 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[:,'SMASlow'] = df.Close.rolling(lookbackSlow).mean()
df.loc[:,'EMA'] = df.Close.ewm(span=lookbackEMA, adjust=False).mean()

# =======================================================================
# Visualize Data
# =======================================================================
#import pdb; pdb.set_trace()
print("")
print("-> Generating Plot")


plt.figure()
df[['Close','SMA', 'SMASlow', 'EMA']].plot(kind='line')
plt.show()

plt.figure()
df['2019'][['Close','SMA', 'SMASlow', 'EMA']].plot(kind='line')
plt.show()

# =======================================================================
# Trading Rules
# =======================================================================
df.loc[:,'e1'] = (df.Close > df.SMA) & (df.Close.shift(1) < df.SMA.shift(1))
df.loc[:,'e2'] = (df.Close > df.SMASlow)
df.loc[:,'x1'] = (df.Close < df.EMA) & (df.Close.shift(1) > df.EMA.shift(1))


df.loc[:,'enterNow'] = df.e1 & df.e2

# =======================================================================
# Trading Decisions Loop
# =======================================================================
import pdb; pdb.set_trace()
# Variables 
inTrade   = False
exitTrade = False
tradeList = []

#Loop through each day
for idx,row in df.iterrows():
    
    if not inTrade:

        # Enter Trade?
        if row.enterNow == True:
            
            inTrade   = True
            
            eDate = idx
            ePrice = row.Close
            xDate = ""
            xPrice = np.NaN
    
    else:

        # Exit Trade?
        if row.x1 == True:
            
            inTrade = False
            exitTrade = True
            
            xDate = idx
            xPrice = row.Close
            

    # Record Trade
    if exitTrade == True:
        
        exitTrade = False
        
        tradeList.append (
                {'eDate'  : eDate,
                 'ePrice' : ePrice,
                 'xDate'  : xDate,
                 'xPrice' : xPrice })
    
    
tdf = pd.DataFrame(tradeList)
import pdb; pdb.set_trace()
# =======================================================================
# Store data
# =======================================================================
df.to_excel("{}_processed.xlsx".format(sym))
tdf.to_excel("{}_trades.xlsx".format(sym))