Ejemplo n.º 1
0
def get_benchmark():
    c()
    start = start_date1
    end = end_date1
    source = 'yahoo'
    bench = ['^GSPC']
    adj_close = DataReader(bench, source, start, end)['Adj Close']
    bench_returns = adj_close.pct_change()
    bench_returns = bench_returns.dropna()
    return bench_returns
Ejemplo n.º 2
0
 def __init__(self, stock_list):
     self.stock_list=stock_list
     # Set up End and Start times for data grab
     end = datetime.now()
     start = datetime(end.year - 1, end.month, end.day)
     Stocks = {}
     for stock in stock_list:
         # Set DataFrame as the Stock Ticker
         Stocks[stock] = DataReader(stock, 'google', start, end)
     self.Stocks=Stocks
     closing_df = DataReader(stock_list, 'google', start, end)['Close']
     self.closing_df=closing_df
     self.stock_rets = closing_df.pct_change()
Ejemplo n.º 3
0
def stock_quote(name=None, source=None, start=None, end=None, percentage=True):
    """
    调用pandas_datareader的API以获取相应股票数据, 加入微小的修改以适应个人需要
    :param name: follow YahooDailyReader standard
    :param source: follow YahooDailyReader standard
    :param start: follow YahooDailyReader standard
    :param end: follow YahooDailyReader standard
    :param percentage: Boole, if 'True', return daily return, or return original daily price
    :return: 返回每日收益率或者daily price
    """
    from pandas_datareader.data import DataReader
    raw = DataReader(name=name, data_source=source, start=start, end=end)
    if percentage is True:
        data = raw.pct_change()
        data = data.iloc[1:, ]
        return data
    else:
        return raw
Ejemplo n.º 4
0
def compare_stocks(dfs, timestamps):
    '''
        Input: dfs - list of dataframes for the different stocks to be compared
               timestamps - list of start and end time of the time period to be analysed

        Output: daily_returns - dataframe of the daily returns of all the stocks
                fig1 - correlation grid of the adjusted closing price of all the stocks
                fig2 - correlation matrix of the daily returns of all the stocks

    '''

    closing = DataReader(dfs, 'yahoo', timestamps[0], timestamps[1])['Adj Close']
    daily_returns = closing.pct_change()
    x = [str(daily_returns.dropna().index[i]).split()[0] for i in range(len(daily_returns.dropna()))]

    fig1 = sns.PairGrid(daily_returns.dropna(), )
    fig1.map_upper(plt.scatter, color='#330C73')

    fig1.map_lower(sns.kdeplot, cmap='RdPu_r')

    fig1.map_diag(plt.hist, bins=30)
    fig1.fig.suptitle(
        f'Graphical correlation between the different stocks for the daily returns from {x[0]} to {x[len(x) - 1]}',
        fontsize=18, y=1.03)

    fig2, (ax1, ax2) = plt.subplots(1, 2, figsize=(16, 7))
    sns.heatmap(closing.corr(), annot=True, cmap='PuBu', ax=ax1)
    sns.heatmap(daily_returns.corr(), annot=True, cmap='PuRd', ax=ax2)

    fig2.suptitle(
        f'Correlation between the different stocks for the closing price and the daily returns from {x[0]} to {x[len(x) - 1]}',
        fontsize=18)
    ax1.set_title('Adjusted Closing Price USD ($)')
    ax2.set_title('Daily returns USD ($)')
    ax1.set_xlabel('')
    ax2.set_xlabel('')
    ax1.set_ylabel('')
    ax2.set_ylabel('')

    return daily_returns, fig1, fig2
Ejemplo n.º 5
0
def tech_summary():
    closing_df = DataReader(['AAPL', 'GOOG', 'MSFT', 'AMZN'], 'yahoo', start,
                            end)['Adj Close']
    tech_rets = closing_df.pct_change()

    # from IPython.display import SVG
    # SVG(url='http://upload.wikimedia.org/wikipedia/commons/d/d4/Correlation_examples2.svg')
    sns.jointplot('GOOG', 'GOOG', tech_rets, kind='scatter', color='seagreen')
    sns.jointplot('GOOG', 'MSFT', tech_rets, kind='scatter')
    sns.pairplot(tech_rets.dropna())

    # Set up our figure by naming it returns_fig, call PairPLot on the DataFrame
    returns_fig = sns.PairGrid(tech_rets.dropna())

    # Using map_upper we can specify what the upper triangle will look like.
    returns_fig.map_upper(plt.scatter, color='purple')

    # We can also define the lower triangle in the figure, inclufing the plot type (kde) or the color map (BluePurple)
    returns_fig.map_lower(sns.kdeplot, cmap='cool_d')

    # Finally we'll define the diagonal as a series of histogram plots of the daily return
    returns_fig.map_diag(plt.hist, bins=30)

    # Set up our figure by naming it returns_fig, call PairPLot on the DataFrame
    returns_fig = sns.PairGrid(closing_df)

    # Using map_upper we can specify what the upper triangle will look like.
    returns_fig.map_upper(plt.scatter, color='purple')

    # We can also define the lower triangle in the figure, inclufing the plot type (kde) or the color map (BluePurple)
    returns_fig.map_lower(sns.kdeplot, cmap='cool_d')

    # Finally we'll define the diagonal as a series of histogram plots of the closing price
    returns_fig.map_diag(plt.hist, bins=30)

    # Let's go ahead and use sebron for a quick correlation plot for the daily returns
    sns.corrplot(tech_rets.dropna(), annot=True)

    return tech_rets
Ejemplo n.º 6
0
# Time period of import, start and end dates 
start = date(2017,10,01)
end = date(2017,11,06)


stockApl = DataReader('AAPL', 'yahoo', start, end)['Close']
stockApl.head()


#plotting
stockApl.plot(title='APPLE')
plt.show()

#calculating daily returns of the stock

dr_apl = stockApl.pct_change(1)

#encoding for comparison
dr_apl[ dr_apl <0 ] = 0   

dr_apl[ dr_apl >0 ] = 4  

#removing Nan
dr_apl=dr_apl[1:]

check['stock']=dr_apl

check=check.dropna()


check['flag'] = np.where(check.sent == check.stock, 1,0) 
axes[1, 0].set_title('SCB')
sns.histplot(scb['Daily_Return'].dropna(), bins=30, color='purple', kde=True, ax=axes[1, 0])
axes[1, 1].set_title('TMB')
sns.histplot(tmb['Daily_Return'].dropna(), bins=30, color='#F28E2B', kde=True, ax=axes[1, 1])
plt.suptitle('Histograms of Percentage Change')
plt.show()

#Note: Pct_change can tell us about the risks of each stock. However, most of them are similar. (Sideway)


### Correlation ###
bank_list = ['KBANK.BK','BBL.BK','SCB.BK','TMB.BK']
closing_df = DataReader(bank_list, 'yahoo', start, end)['Adj Close']
closing_df

bank_returns_df = closing_df.pct_change()
bank_returns_df

# Pairplot
sns.pairplot(bank_returns_df, kind='reg')
plt.show()

#Note: The pairplot shows that most of them are related, AKA, they go along together.

# Correlation Plot
fig, axes = plt.subplots(2,1, figsize=(16,8))
sns.heatmap(closing_df.corr(), annot=True, cmap='summer', ax=axes[0])
axes[0].set_title('Correlation of Percentage Return')
sns.heatmap(bank_returns_df.corr(), annot=True, cmap='summer', ax=axes[1])
axes[1].set_title('Correlation of Closing Price (Adj Close)')
plt.suptitle('Correlation')
# In[32]:

closing_df = DataReader(['AAPL', 'GOOG', 'MSFT', 'AMZN'], 'yahoo', start,
                        end)['Adj Close']

# In[33]:

# Let's take a quick look
closing_df.head()

# Now that we have all the closing prices, let's go ahead and get the daily return for all the stocks, like we did for the Apple stock.

# In[34]:

# Make a new tech returns DataFrame
tech_rets = closing_df.pct_change()

# In[36]:

tech_rets.head()

# Now we can compare the daily percentage return of two stocks to check how correlated. First let's see a sotck compared to itself.

# So now we can see that if two stocks are perfectly (and positivley) correlated with each other a linear relationship bewteen its daily return values should occur. So let's go ahead and compare Google and Microsoft the same way.

# In[37]:

# We'll use joinplot to compare the daily returns of Google and Microsoft
sns.jointplot('GOOG', 'MSFT', tech_rets, kind='scatter')

# In[38]:
Ejemplo n.º 9
0
AAPL['Volume'].plot(legend=True,figsize=(15,6))
plt.show()
ma_day=[5,20,60]
for ma in ma_day:
	column_name=f"MA for {str(ma)} days"
	AAPL[column_name]=AAPL['Adj Close'].rolling(ma).mean()
AAPL[['Adj Close','MA for 5 days','MA for 20 days','MA for 60 days']].plot(subplots=False,legend=True,figsize=(15,6))
plt.show()

AAPL['Daily Return']=AAPL['Adj Close'].pct_change()
#AAPL['Daily Return'].plot(figsize=(15,6),legend=True)
sns.distplot(AAPL['Daily Return'].dropna(),bins=100,color='green')
plt.show()
'''
closing_df = DataReader(com_list, 'yahoo', start, end)['Adj Close']
tech_rtrn = closing_df.pct_change()
rets = tech_rtrn.dropna()
'''
closing_df.plot(figsize=(15,6))
plt.show()
#sns.jointplot('AAPL','AMZN',tech_rtrn,kind='scatter',s=3)
rtrnfig=sns.PairGrid(closing_df)
rtrnfig.map_diag(sns.distplot,bins=40,color='green')
rtrnfig.map_upper(plt.scatter,s=2)
rtrnfig.map_lower(sns.kdeplot,cmap='coolwarm')
plt.show()

corr=closing_df.dropna().corr()
sns.heatmap(corr,annot=True,center=0.5,cmap='coolwarm')
plt.show()
Ejemplo n.º 10
0
from matplotlib import style
style.use('ggplot')

from matplotlib import style
style.use('ggplot')
import time
day = dt.datetime.utcfromtimestamp(time.time()).day
data = pd.read_csv('huobi_usdt_spot_btc_usdt_1min_{}.txt'.format(day))
prices = data.iloc[-500::, 1]
returns = prices.pct_change()
last_price = prices.values[-1]

start = dt.datetime(2020, 3, 9)
end = dt.datetime(2021, 3, 9)
prices = DataReader("XLM-USD", "yahoo", start, end)["Close"]
returns = prices.pct_change()
last_price = prices[-1]

number_simulations = 1000
number_days = len(prices.index)
simulation_df = pd.DataFrame()
daily_volatility = returns.std()

for x in range(number_simulations):
    count = 0
    price_series = []

    price = last_price * (1 + np.random.normal(0, daily_volatility))
    price_series.append(price)

    for y in range(number_days):
Ejemplo n.º 11
0
plt.show()
#random walk with drift
steps2 = np.random.normal(loc=0.001, scale=.01, size=1000) + 1
steps2[0] = 1
p2 = 100 * np.cumprod(steps2)
plt.plot(p2)
plt.title("Simulated Random Walk with Drift")
plt.show()
#are stock prices random walks test with amazon?
from pandas_datareader.data import DataReader
import pandas as pd
from datetime import date
series = 'AMZN'
source = 'yahoo'
start = date(1997, 5, 15)
end = date(2017, 8, 2)
amzn = DataReader(series, source, start, end)
#import the adfuller module from statsmodel
from statsmodels.tsa.stattools import adfuller
results = adfuller(amzn['Adj Close'])
print(results)
#print out just the p-value
print("The p-value of the test on prices is " + str(results[1]))
#prices are random, but what about returns?
amznRet = amzn.pct_change()
amznRet = amznRet.dropna()
results2 = adfuller(amznRet['Adj Close'])
print("The p-value of the test on returns is " + str(results2[1]))

import zScore
amznRet['zAdj'] = zScore(amznRet['Adj Close'])