76 lines
2.5 KiB
Python
76 lines
2.5 KiB
Python
|
import os
|
||
|
import time
|
||
|
import statistics
|
||
|
from matplotlib.pyplot import legend, ylabel
|
||
|
import pandas as pd
|
||
|
import numpy as np
|
||
|
from pandas_datareader import data
|
||
|
|
||
|
dir_path = os.path.dirname(os.path.realpath(__file__))
|
||
|
|
||
|
start_date = '2014-01-01'
|
||
|
end_date = '2018-01-01'
|
||
|
SRC_DATA_FILENAME = dir_path+'/goog_data.pkl'
|
||
|
|
||
|
try:
|
||
|
goog_data = pd.read_pickle(SRC_DATA_FILENAME)
|
||
|
print('File data found...reading GOOG data')
|
||
|
except FileNotFoundError:
|
||
|
print('File not found...downloading the GOOG data')
|
||
|
goog_data = data.DataReader('GOOG', 'yahoo', start_date, end_date)
|
||
|
goog_data.to_pickle(SRC_DATA_FILENAME)
|
||
|
|
||
|
goog_data_signal = pd.DataFrame(index=goog_data.index)
|
||
|
goog_data_signal['price'] = goog_data['Adj Close']
|
||
|
close = goog_data_signal['price']
|
||
|
|
||
|
exe_start_time = time.time()
|
||
|
|
||
|
""" Calculate APO """
|
||
|
|
||
|
num_periods_fast = 10 # time period for the fast EMA
|
||
|
K_fast = 2/(num_periods_fast + 1)
|
||
|
ema_fast = 0
|
||
|
|
||
|
num_periods_slow = 40
|
||
|
K_slow = 2/(num_periods_slow + 1)
|
||
|
ema_slow = 0
|
||
|
|
||
|
ema_fast_values = [] # Hold fast EMA values for visualization purposes
|
||
|
ema_slow_values = [] # Hold slow EMA values for visualization purposes
|
||
|
apo_values = []
|
||
|
|
||
|
for close_price in close:
|
||
|
if (ema_fast == 0):
|
||
|
ema_fast = close_price
|
||
|
ema_slow = close_price
|
||
|
else:
|
||
|
ema_fast = (close_price - ema_fast) * K_fast + ema_fast
|
||
|
ema_slow = (close_price - ema_slow) * K_slow + ema_slow
|
||
|
|
||
|
ema_fast_values.append(ema_fast)
|
||
|
ema_slow_values.append(ema_slow)
|
||
|
apo_values.append(ema_fast - ema_slow)
|
||
|
|
||
|
""" Visualizing """
|
||
|
goog_data = goog_data.assign(ClosePrice=pd.Series(close, index=goog_data.index))
|
||
|
goog_data = goog_data.assign(FastExponential10DayMovingAverage=pd.Series(ema_fast_values, index=goog_data.index))
|
||
|
goog_data = goog_data.assign(SlowExponential140DayMovingAverage=pd.Series(ema_slow_values, index=goog_data.index))
|
||
|
goog_data = goog_data.assign(AbsolutePriceOscillator=pd.Series(apo_values, index=goog_data.index))
|
||
|
close_price = goog_data['ClosePrice']
|
||
|
ema_f = goog_data['FastExponential10DayMovingAverage']
|
||
|
ema_s = goog_data['SlowExponential140DayMovingAverage']
|
||
|
apo = goog_data['AbsolutePriceOscillator']
|
||
|
|
||
|
import matplotlib.pyplot as plt
|
||
|
fig = plt.figure()
|
||
|
ax1 = fig.add_subplot(211, ylabel='Google price in $')
|
||
|
close_price.plot(ax=ax1, color='g', lw=2., legend=True)
|
||
|
ema_f.plot(ax=ax1, color='b', lw=2., legend=True)
|
||
|
ema_s.plot(ax=ax1, color='r', lw=2., legend=True)
|
||
|
ax2 = fig.add_subplot(212, ylabel='APO')
|
||
|
apo.plot(ax=ax2, color='black', lw=2., legend=True)
|
||
|
plt.savefig(dir_path + "/apo.png")
|
||
|
plt.show()
|
||
|
|