- added some new indicators
git-svn-id: http://moon:8086/svn/projects/Stock@297 fda53097-d464-4ada-af97-ba876c37ca34
This commit is contained in:
+37
-28
@@ -1,35 +1,44 @@
|
||||
'''
|
||||
On your terminal run:
|
||||
pip install alpha_vantage
|
||||
|
||||
This also uses the pandas dataframe, and matplotlib, commonly used python packages
|
||||
pip install pandas
|
||||
pip install matplotlib
|
||||
|
||||
For the develop version run:
|
||||
pip install git+https://github.com/RomelTorres/alpha_vantage.git@develop
|
||||
'''
|
||||
|
||||
from alpha_vantage.timeseries import TimeSeries
|
||||
from alpha_vantage.techindicators import TechIndicators
|
||||
|
||||
from matplotlib.pyplot import figure
|
||||
import pandas as pd
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
# Your key here
|
||||
key = '0UO7Z2MVZ2YSQSVE'
|
||||
# Chose your output format, or default to JSON (python dict)
|
||||
ts = TimeSeries(key, output_format='pandas')
|
||||
def ema(data, key, alpha=0.5, ic=None):
|
||||
result = []
|
||||
if ic is None:
|
||||
r = data[key][0]
|
||||
else:
|
||||
r = ic
|
||||
for v in data[key]:
|
||||
r = alpha*r + (1-alpha)*v
|
||||
result.append(r)
|
||||
|
||||
# Get the data, returns a tuple
|
||||
# aapl_data is a pandas dataframe, aapl_meta_data is a dict
|
||||
aapl_data, aapl_meta_data = ts.get_daily(symbol='AAPL')
|
||||
return np.array(result)
|
||||
|
||||
def colsum(data, keys):
|
||||
result = np.zeros(data.shape[0])
|
||||
for key in keys:
|
||||
result += data[key]
|
||||
|
||||
# Visualization
|
||||
figure(num=None, figsize=(15, 6), dpi=80, facecolor='w', edgecolor='k')
|
||||
aapl_data['4. close'].plot()
|
||||
dataArray = aapl_data['4. close'].to_numpy()
|
||||
plt.tight_layout()
|
||||
return result
|
||||
|
||||
store = pd.HDFStore('test.h5')
|
||||
if 1:
|
||||
df = pd.DataFrame(np.array([[1, 2, 3, 101], [4, 5, 6, 102], [7, 8, 9, 103], [10, 11, 12, 104]]),columns=['high', 'low', 'open', 'close'])
|
||||
store['df'] = df
|
||||
else:
|
||||
df = store['df']
|
||||
|
||||
df['ema(close)'] = ema(df, 'close')
|
||||
df['colsum'] = colsum(df, ['high', 'low', 'open', 'close'])
|
||||
df['ema(colsum)'] = ema(df, 'colsum')
|
||||
|
||||
# Print table
|
||||
print(df)
|
||||
|
||||
# Plot graph
|
||||
df.plot()
|
||||
plt.legend()
|
||||
plt.grid()
|
||||
plt.show()
|
||||
|
||||
store.close()
|
||||
Reference in New Issue
Block a user