import pandas as pd import pandas_datareader.data as web import numpy as np import math import matplotlib.pyplot as plt import datetime # Stock Investors Financial Math # https://www.fmlabs.com/reference/default.htm?url=SimpleMA.htm # Alpha Vantage # https://www.alphavantage.co/documentation/ # Pandas # https://pandas.pydata.org/pandas-docs/stable/index.html # Knowledge # https://www.investopedia.com/articles/technical/02/050602.asp # https://ntguardian.wordpress.com/2016/09/19/introduction-stock-market-data-python-1 key = '0UO7Z2MVZ2YSQSVE' def sin(f, a, N): result = np.empty(0) for n in range(0, N): v = a*math.sin(2*math.pi*f*n/N) result = np.append(result, v) return result def ema(data, alpha=0.75): result = np.empty(0) r = data[0] for v in data: r = alpha*r + (1-alpha)*v result = np.append(result, r) return result def macd(data): ema_short = ema(data, alpha=0.85) ema_long = ema(data, alpha=0.925) return ema_short - ema_long if 0: # Get stock price via data reader start = datetime.datetime(2016,1,1) end = datetime.date.today() apple = web.DataReader("AAPL", "av-monthly", start, end, api_key=key) type(apple) pd_data = pd.read_csv("aapl.csv", names=['Dates', 'Price']) np_data = np.flip(pd_data['Price'].to_numpy()) plt.plot(np_data, label='Price') plt.plot(ema(np_data), label='EMA') plt.plot(macd(np_data), label='MACD') plt.legend() plt.grid() plt.show()