<jupyter_start><jupyter_text><jupyter_code># 3 moving average crosover strategy to trade stocks
# trade parameters: fast, med slow exponential moving averages



import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
plt.style.use('fivethirtyeight')
#load the data spy 2008-2020
from google.colab import files
uploaded = files.upload()

#store data into a variable
spy = pd.read_csv('SPY201820.csv')
spy
#Set the data index to be Date
spy = spy.set_index(pd.DatetimeIndex(spy['Date'].values))
spy
#display the SPY chart
plt.figure(figsize=(12.2, 4.5))
plt.title('SPY Closing Price Oct 2018-2020', fontsize = 20)
plt.plot(spy['Close'], color='black')
plt.xlabel('Date', fontsize = 20)
plt.ylabel('USD Price', fontsize = 20)
plt.show()
# calculate EMAs, default params
fast = 5
med = 20
slow = 100

fastEMA = spy.Close.ewm(span=fast, adjust = False).mean()
medEMA = spy.Close.ewm(span=med, adjust = False).mean()
slowEMA = spy.Close.ewm(span=slow, adjust = False).mean()

#show the EMAs
plt.figure(figsize=(12.2, 4.5))
plt.title('SPY Closing Price Oct 2018-2020', fontsize = 20, color='black')
plt.plot(spy['Close'], color='black')
plt.plot(fastEMA, label='fast EMA', color='yellow', alpha=0.6)
plt.plot(medEMA,  label='medium EMA', color='red', alpha=0.6)
plt.plot(slowEMA, label='slow EMA', color='green', alpha=0.6)
plt.xlabel('Date', fontsize = 20, color='blue')
plt.ylabel('USD Price', fontsize = 20, color='blue')
plt.show()
# Add ema columns to data set for calculations

# trade parameters: fast, med slow exponential moving averages
#trade rules: BTO when fast crosses above med and med is above slow,  STC when fast crosses below med, STO when fast below med and med crosses below slow, BTC when med crosses above slow<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.136568848758465, 'fraction_numerical': 0.03668171557562077, 'mean_word_length': 5.566666666666666, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '613934', 'license': 'MIT', 'n_tokens_mistral': 689, 'n_tokens_neox': 595, 'n_words': 195, 'path': 'Copy_of_3smacross_SPY.ipynb', 'repo_name': 'peggyc3/python_3ema_tradealgorithm', 'size': '1772'}