-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess_emotions_and_returns.py
96 lines (83 loc) · 4.38 KB
/
process_emotions_and_returns.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#%% Imports
import glob
import pandas as pd
import os
#--Scripts
import core.utils as utils
#%% Inputs
# You need to run frames_2_emotions.py with the same which_youtube_videos before running this script
which_youtube_videos = 'trump'
if which_youtube_videos == 'trump':
#---Final directory and file name
final_dir = 'data/emotions_vs_returns/trump'
final_fn = 'returns_volumes_negative_emotions.csv'
if not os.path.exists(final_dir):
os.makedirs(final_dir)
#---Paths related to emotions
emotions_dir = 'data/emotions/trump'
emotion_excel_files = sorted(glob.glob(os.path.join(emotions_dir, '*')))
yt_id_excel_path = 'data/yt_ids/trump_yt_ids.xlsx'
elif which_youtube_videos == 'FOMC':
#---Final directory and file name
final_dir = 'data/emotions_vs_returns/FOMC'
final_fn = 'returns_volumes_negative_emotions.csv'
if not os.path.exists(final_dir):
os.makedirs(final_dir)
#---Paths related to emotions
emotions_dir = 'data/emotions/FOMC'
emotion_excel_files = sorted(glob.glob(os.path.join(emotions_dir, '*')))
yt_id_excel_path = 'data/yt_ids/fomc_all.xlsx'
else:
raise "%s is a not a valid input to which_youtube_ids" % which_youtube_videos
#---Paths related to financial data
spy_path = 'data/prices/SPY_1min_2008-2021.csv'
# Read in the process SPY intraday information
spy_data = pd.read_csv(spy_path, index_col=0)
#%% Loop through the excel files and calculate the negative emotion parameters
if which_youtube_videos == 'trump':
# Initialize the final DataFrame
final_df = pd.DataFrame()
# Loop through the excel files
for excel_file in emotion_excel_files:
# Read in the emotions excel file
emotions = pd.read_excel(excel_file, index_col=0)
# Isolate the date from the emotions excel file
date = emotions.index[0].split()[0]
# Get the intraday returns and volumes on that particular date
returns_and_volumes = utils.get_intraday_returns_and_volumes(spy_data, date)
# Calculate the overall mean, the overall std, and the mean PCA for the particular chairperson
overall_mean_neg_emotions, overall_std_neg_emotions, overall_mean_pca = utils.get_mean_negative_emotions(emotions_dir)
neg_emotion_normalizers = [overall_mean_neg_emotions, overall_std_neg_emotions, overall_mean_pca]
# Calculate and store the mean_neg_emotion, std_neg_emotion, pca_emotion, and dmd_neg_emotion in a DataFrame for that particular conference
output_df = utils.negative_emotions_df(emotions, returns_and_volumes, date, neg_emotion_normalizers)
# Concatenate the DataFrame to the final DataFrame
final_df = pd.concat([final_df, output_df])
# Print the name and date
print(date)
# Save the final DataFrame as a csv
final_df.to_csv(os.path.join(final_dir, final_fn))
elif which_youtube_videos == 'FOMC':
# Initialize the final DataFrame
final_df = pd.DataFrame()
# Loop through the excel files
for excel_file in emotion_excel_files:
# Read in the emotions excel file
emotions = pd.read_excel(excel_file, index_col=0)
# Isolate the date from the emotions excel file
date = emotions.index[0].split()[0]
# Get the intraday returns and volumes on that particular date
returns_and_volumes = utils.get_intraday_returns_and_volumes(spy_data, date)
# Get the name of the chair person given the date of the conference
name = utils.date_2_fedchair(yt_id_excel_path, date)
# Calculate the overall mean, the overall std, and the mean PCA for the particular chairperson
overall_mean_neg_emotions, overall_std_neg_emotions, overall_mean_pca = utils.get_mean_negative_emotions_FOMC(yt_id_excel_path, name, emotions_dir)
neg_emotion_normalizers = [overall_mean_neg_emotions, overall_std_neg_emotions, overall_mean_pca]
# Calculate and store the mean_neg_emotion, std_neg_emotion, pca_emotion, and dmd_neg_emotion in a DataFrame for that particular conference
output_df = utils.negative_emotions_df(emotions, returns_and_volumes, date, neg_emotion_normalizers)
# Concatenate the DataFrame to the final DataFrame
final_df = pd.concat([final_df, output_df])
# Print the name and date
print(date)
print(name)
# Save the final DataFrame as a csv
final_df.to_csv(os.path.join(final_dir, final_fn))