示例#1
0
stock_returns_col='11'

daily_mean_SI=twt.groupby(['TIMESTAMP_UTC', 'ClusterID']).mean()
daily_mean_SI=daily_mean_SI.reset_index()
daily_mean_SI.columns=['TIMESTAMP_UTC', 'ClusterID', 'MeanSI']


for stock in stocks_to_check.values.ravel().tolist(): # twt['SYMBOL'].unique():
    print(stock)
    stock_r = u.get_stock_returns(stock_returns_db, stock, stock_returns_col, start, end, api_token)

    if not stock_r.empty:
        print('Downloaded stock data for ' + stock)
        stock_r = stock_r.resample(period, how=u.volatility,label='right')
        stock_r = stock_r.loc[stock_r.index > actualstart]
        stock_r = u.detrend(1, stock_r)
        custom_mi = mi.resample(period, how=u.cumret,label='right')
        custom_mi = custom_mi.loc[custom_mi.index > actualstart]
        custom_mi = u.detrend(1, custom_mi) #this alligns market timeseries to stock returns

        mask = (twt['SYMBOL'] == stock) #& (twt['TIMESTAMP_UTC']>=pd.to_datetime(start)) & (twt['TIMESTAMP_UTC']<pd.to_datetime(end))
        stock_si = twt[mask]
        stock_si = stock_si[['TIMESTAMP_UTC', 'mean']]
        stock_si.set_index('TIMESTAMP_UTC', inplace=True)
        stock_si = u.detrend(1, stock_si)

        market_model_reg = u.market_model(stock_r, custom_mi)
        sentiment_model_reg = u.sentiment_model(market_model_reg, stock_r, stock_si)

        results_dict[stock]={'degree of integration':1,
                             'returns_ts':stock_r,
示例#2
0
results_dict = {}
stock_returns_col = '11'

daily_mean_SI = twt.groupby(['TIMESTAMP_UTC', 'ClusterID']).mean()
daily_mean_SI = daily_mean_SI.reset_index()
daily_mean_SI.columns = ['TIMESTAMP_UTC', 'ClusterID', 'MeanSI']

for stock in ['AIG', 'AMD']:  # twt['SYMBOL'].unique():
    stock_r = u.get_stock_returns(stock_returns_db, stock, stock_returns_col, start, end, api_token)

    if not stock_r.empty:
        print('Downloaded stock data for ' + stock)
        stock_r = stock_r.resample(period, how=u.cumret)
        r_degree = u.integration_degree(stock_r[returns_column_name], max_degree, df_reg_type, alpha)
        stock_r = u.detrend(r_degree, stock_r)
        custom_mi = mi.resample(period, how=u.cumret)
        custom_mi = u.detrend(r_degree, custom_mi)  # this alligns market timeseries to stock returns

        mask = (twt['SYMBOL'] == stock) & (twt['TIMESTAMP_UTC'] >= pd.to_datetime(start)) & (
        twt['TIMESTAMP_UTC'] < pd.to_datetime(end))
        stock_daily_clusters = twt[mask]
        stock_si = pd.merge(stock_daily_clusters, daily_mean_SI, on=['TIMESTAMP_UTC', 'ClusterID'], how='left')
        stock_si = stock_si[['TIMESTAMP_UTC', 'MeanSI']]
        stock_si.set_index('TIMESTAMP_UTC', inplace=True)
        stock_si = stock_si.resample(period, how='mean')

        market_model_reg = u.market_model(stock_r, custom_mi)
        sentiment_model_reg = u.sentiment_model(market_model_reg, stock_r, stock_si)

        results_dict[stock] = {'degree of integration': r_degree,