import pandas as pd
import numpy as np
import yfinance as yf
import datetime as dt
from pandas_datareader import data as pdr

yf.pdr_override() # Activates workaround to use yf
stock=input("Enter a stock ticker symbol: ")

startyear=2019
startmonth=1
startday=1

start=dt.datetime(startyear,startmonth,startday)
now=dt.datetime.now()

df = pdr.get_data_yahoo(stock,start,now)

# ma=50
# smaString="Sma_"+str(ma)

# df[smaString]=df.iloc[:,4].rolling(window=ma).mean()

emasUsed = [3,5,8,10,12,15,30,35,40,45,50,60]
for x in emasUsed:
    ema=x
    df["Ema_"+str(ema)]=round(df.iloc[:,4].ewm(span=ema, adjust=False).mean(),2)

print(df.tail())

pos=0 #determines whether we enter position 
num=0 #row we are in
percentchange=[] #track results of trades

for i in df.index:
    cmin=min(df["Ema_3"][i],df["Ema_5"][i],df["Ema_8"][i],df["Ema_10"][i],df["Ema_12"][i],df["Ema_15"][i])
    cmax=max(df["Ema_30"][i],df["Ema_35"][i],df["Ema_40"][i],df["Ema_45"][i],df["Ema_50"][i],df["Ema_60"][i]) 
    

    close=df["Adj Close"][i]

    if(cmin>cmax):
        print("Red White Blue")
        if(pos==0):
            bp=close
            pos=1
            print("Buying now at "+str(bp))

    elif(cmin<cmax):
        print("Blue White Red")
        if(pos==1):
            pos=0
            sp=close
            print("Selling now at "+str(sp))
