<jupyter_start><jupyter_text>MSCA 32016 Stock News Investigation<jupyter_code>import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
df = pd.read_csv('finance/finance.csv')
df.head()<jupyter_output><empty_output><jupyter_text>Playbook1. For a given symbol, order id, exchange, and side run an OLS on the price and news<jupyter_code>import statsmodels.formula.api as smf
temp_50 = {50:True, 100:False, 0:False}
temp_100 = {50:False, 100:True, 0:False}
temp_mod = {'A': True, 'M': False}

df['is_50'] = df['News'].map(temp_50).map(int)
df['is_100'] = df['News'].map(temp_100).map(int)
df['is_add'] = df['Action'].map(temp_mod).map(int)<jupyter_output><empty_output><jupyter_text>Find stocks that are highly correlated with newsSymbol, Side?, Rsquared<jupyter_code>UniqueTimeFrames = (df.Symbol + '-' + df.Side + '-' + df.OrderID.map(str)).unique()

dfSymbol = {q : df for q in UniqueTimeFrames}

for key in dfSymbol.keys():
    dfSymbol[key] = df[:][df['Symbol'] + '-' + df['Side'].map(str) + '-' + df['OrderID'].map(str) == key]
for key in dfSymbol.keys():
    dfSymbol[key]['delta'] = dfSymbol[key]['Price'].diff()
    
for key in dfSymbol.keys():
    dfSymbol[key].fillna(0)
fig, ax1 = plt.subplots()
s1 = dfSymbol['EA-S-2234']['Price']
ax1.plot(s1, 'b-')
ax1.set_xlabel('')
# Make the y-axis label, ticks and tick labels match the line color.
ax1.set_ylabel('price', color='b')
ax1.tick_params('y', colors='b')

ax2 = ax1.twinx()
s2 = dfSymbol['EA-S-2234']['News']
ax2.plot(s2, 'ro')
ax2.set_ylabel('news', color='r')
ax2.tick_params('y', colors='r')

fig.tight_layout()
plt.show()
result_rsquared = {}
intercepts = {}
is_50_cf = {}
is_100_cf = {}

for key in dfSymbol:
    result = smf.ols(formula = "delta ~ is_50 + is_100", data = dfSymbol[key]).fit()      
    result_rsquared[key] = result.rsquared
    intercepts[key] = result.params[0]
    is_50_cf[key] = result.params[1]
    is_100_cf[key] = result.params[2]

names_trade = pd.DataFrame(list(result_rsquared.items()), \
                           columns=['Symbol', 'RSquared']).sort_values(by='RSquared', ascending=False)

intercepts = pd.DataFrame(list(intercepts.items()), \
                           columns=['Symbol', 'Intercept']).sort_values(by='Symbol', ascending=False)

is_50_cf = pd.DataFrame(list(is_50_cf.items()), \
                           columns=['Symbol', 'is_50_coefficient']).sort_values(by='Symbol', ascending=False)

is_100_cf = pd.DataFrame(list(is_100_cf.items()), \
                           columns=['Symbol', 'is_100_coefficient']).sort_values(by='Symbol', ascending=False)

names_trade = names_trade[names_trade['RSquared'] > 0.4]

names_trade<jupyter_output>/Users/albhu/venv/vir_env/lib/python3.5/site-packages/statsmodels-0.8.0-py3.5-macosx-10.10-x86_64.egg/statsmodels/regression/linear_model.py:1353: RuntimeWarning: invalid value encountered in double_scalars
  return 1 - self.ssr/self.centered_tss<jupyter_text>General Investigation1. Trade on the RSquared values that are relatively high. These are in the names_trade dataframe. Symbol + Action2. Figure out what happens to the stock price for a News value: find the high/low and relative price to those values when there is a News Value3. For the specific Symbol, Side, codify the action that should be taken when there is news<jupyter_code>df_model = names_trade.merge(intercepts, on='Symbol').merge(is_50_cf, on='Symbol').merge(is_100_cf, on='Symbol')
df_model<jupyter_output><empty_output><jupyter_text>General Strategy1. When the news coefficients are negatives, the prices will be lower<jupyter_code>df.groupby(['Symbol', 'Side'], sort=False)['Price'].max()
df.groupby(['Symbol', 'Side'], sort=False)['Price'].min()

dfPivot = pd.pivot_table(df, values = "Price", 
                             columns = ["Side"], index = ["Symbol", "News"])
dfPivot<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15986570247933884, 'fraction_numerical': 0.030991735537190084, 'mean_word_length': 4.886018237082067, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '625705', 'license': 'MIT', 'n_tokens_mistral': 1461, 'n_tokens_neox': 1375, 'n_words': 284, 'path': 'analyze_stock.ipynb', 'repo_name': 'albhu/finance', 'size': '3872'}