from dateutil.relativedelta import relativedelta
import pandas as pd
from pandas import DataFrame
from pandas.io.data import DataReader
import os
import matplotlib.pyplot as plt
from matplotlib import font_manager
## NBI->IBB
current_path = os.path.dirname(os.path.realpath(__file__))
ticker = 'ibb'
from yahoo_finance import Share
symbols=[]
r = DataReader(ticker, "yahoo",start=datetime.datetime.today()-relativedelta(months=12),end=datetime.datetime.today())
# add a symbol column
r['Symbol'] = ticker
symbols.append(r)
# concatenate all the dfs
df = pd.concat(symbols)
## Move Average
df['MA_1M'] = pd.rolling_mean(df['Close'], window=10, min_periods=1)
##
## Bollinger Bands
df['tmp_std'] = (df['Close']-df['MA_1M'])**2
df['tmp_std1'] = pd.rolling_mean(df['tmp_std'], window=10, min_periods=1)
df['Bollinger_STD'] = df['tmp_std1']**0.5
df['Bollinger_top'] = df['MA_1M']+df['Bollinger_STD']
df['Bollinger_bot'] = df['MA_1M']-df['Bollinger_STD']
##
def plot_line(df):
plt.figure()
fig, axes = plt.subplots(nrows=2, ncols=1,sharex=False, figsize=(16,8))
ax_long = df.plot(kind='line',
y = ['Close','MA_1M','Bollinger_top','Bollinger_bot'],
#x = ['Date'],
ax = axes[0],
title = 'Close'
)
ax_short = df[-60:].plot(kind='line',
y = ['Close','MA_1M','Bollinger_top','Bollinger_bot'],
#x = ['Date'],
ax = axes[1],
title = 'Close'
)
fig.savefig(current_path+'/test.png')
plt.close('all')
plot_line(df)
沒有留言:
張貼留言