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 data = {} if 0: # Get stock price via data reader start = datetime.datetime(2016,1,1) end = datetime.date.today() data = web.DataReader("AAPL", "av-monthly", start, end, api_key=key) # data = web.DataReader("AAPL", "stooq", start, end) type(data) hdf = pd.HDFStore('aapl.h5') hdf['open'] = data['open'] hdf['close'] = data['close'] hdf['high'] = data['high'] hdf['low'] = data['low'] hdf.close() else: hdf = pd.HDFStore('aapl.h5', 'r') data['open'] = hdf['open'] data['close'] = hdf['close'] data['high'] = hdf['high'] data['low'] = hdf['low'] hdf.close() df = pd.DataFrame({"A": ["a", "b", "c", "a"]}) print(df) v = data.values() i = data.items() print(data) data['ema'] = ema(data['close'], alpha=0.75) data['open'].plot() data['close'].plot() data['high'].plot() data['low'].plot() plt.legend() plt.grid() plt.show() np_data = np.flip(data['close'].to_numpy()) plt.subplot(211) plt.plot(np_data, label='Price') plt.plot(ema(np_data), label='EMA') plt.legend() plt.grid() plt.subplot(212) plt.plot(macd(np_data), label='MACD') plt.legend() plt.grid() plt.show()