Spaces:
Running
Running
Robert Castagna
commited on
Commit
•
024034d
1
Parent(s):
641a9e4
local env enhacements
Browse files- fin_data.db-journal +0 -0
- pages/2_Portfolio_Builder.py +21 -17
- pages/3_Sentiment_Data_Input.py +13 -4
fin_data.db-journal
ADDED
Binary file (29.2 kB). View file
|
|
pages/2_Portfolio_Builder.py
CHANGED
@@ -24,11 +24,18 @@ def open_nested_event_loop():
|
|
24 |
nest_asyncio.apply()
|
25 |
try:
|
26 |
loop = asyncio.get_event_loop()
|
|
|
27 |
except RuntimeError as ex:
|
28 |
if "There is no current event loop in thread" in str(ex):
|
29 |
loop = asyncio.new_event_loop()
|
30 |
asyncio.set_event_loop(loop)
|
|
|
31 |
return
|
|
|
|
|
|
|
|
|
|
|
32 |
|
33 |
def get_positions(account):
|
34 |
positions = ib.portfolio(account)
|
@@ -65,36 +72,33 @@ load_dotenv()
|
|
65 |
|
66 |
obb.account.login(pat=os.environ['open_bb_pat'])
|
67 |
|
68 |
-
#
|
69 |
|
70 |
-
if 'tickers' not in st.session_state:
|
71 |
-
tickers = [
|
72 |
-
"SPY", "QQQ","BND","GLD","VTI"
|
73 |
-
]
|
74 |
-
st.session_state['tickers'] = tickers
|
75 |
-
|
76 |
-
if not platform.processor(): # take inputs from the user for hosted application
|
77 |
-
tickers = st.session_state['tickers']
|
78 |
-
|
79 |
-
elif not re.search('AuthenticAMD', platform.processor()): # use live ibkr portfolio if ran from local machine
|
80 |
try:
|
81 |
# Initialize IB connection here
|
82 |
-
|
83 |
-
from ib_insync import *
|
84 |
-
ib = IB()
|
85 |
# Assuming you're connecting to a local TWS instance with default settings
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
position_data = get_positions(paper_acct)
|
90 |
for pos in position_data:
|
91 |
tickers = [pos[0] for pos in position_data]
|
|
|
92 |
|
93 |
except Exception as e:
|
94 |
st.write('IB Workstation is not running. Please start the IB Workstation and try again.')
|
95 |
st.write('Error: ', e)
|
96 |
st.stop()
|
97 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
98 |
|
99 |
list_of_tickers = get_list_of_tickers()
|
100 |
|
|
|
24 |
nest_asyncio.apply()
|
25 |
try:
|
26 |
loop = asyncio.get_event_loop()
|
27 |
+
st.write("event loop exists")
|
28 |
except RuntimeError as ex:
|
29 |
if "There is no current event loop in thread" in str(ex):
|
30 |
loop = asyncio.new_event_loop()
|
31 |
asyncio.set_event_loop(loop)
|
32 |
+
st.write("event loop created")
|
33 |
return
|
34 |
+
|
35 |
+
open_nested_event_loop()
|
36 |
+
|
37 |
+
from ib_insync import *
|
38 |
+
|
39 |
|
40 |
def get_positions(account):
|
41 |
positions = ib.portfolio(account)
|
|
|
72 |
|
73 |
obb.account.login(pat=os.environ['open_bb_pat'])
|
74 |
|
75 |
+
# -------------------------------- (Tickers) -------------------------------- #
|
76 |
|
77 |
+
if re.search('AuthenticAMD', platform.processor()) and 'tickers' not in st.session_state: # use live ibkr portfolio if ran from local machine
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
78 |
try:
|
79 |
# Initialize IB connection here
|
80 |
+
with IB().connect('127.0.0.1', 7497, clientId=0, timeout=10) as ib:
|
|
|
|
|
81 |
# Assuming you're connecting to a local TWS instance with default settings
|
82 |
+
paper_acct = ib.managedAccounts()[0]
|
83 |
+
|
|
|
84 |
position_data = get_positions(paper_acct)
|
85 |
for pos in position_data:
|
86 |
tickers = [pos[0] for pos in position_data]
|
87 |
+
st.session_state.tickers = tickers
|
88 |
|
89 |
except Exception as e:
|
90 |
st.write('IB Workstation is not running. Please start the IB Workstation and try again.')
|
91 |
st.write('Error: ', e)
|
92 |
st.stop()
|
93 |
|
94 |
+
if 'tickers' not in st.session_state:
|
95 |
+
tickers = [
|
96 |
+
"SPY", "QQQ","BND","GLD","VTI"
|
97 |
+
]
|
98 |
+
st.session_state['tickers'] = tickers
|
99 |
+
|
100 |
+
if not platform.processor(): # take inputs from the user for hosted application
|
101 |
+
tickers = st.session_state['tickers']
|
102 |
|
103 |
list_of_tickers = get_list_of_tickers()
|
104 |
|
pages/3_Sentiment_Data_Input.py
CHANGED
@@ -68,10 +68,11 @@ c.execute("""CREATE TABLE IF NOT EXISTS company_news (
|
|
68 |
sentiment_score real
|
69 |
)""")
|
70 |
|
|
|
|
|
|
|
71 |
|
72 |
-
|
73 |
-
|
74 |
-
start_date_str = (datetime.datetime.today() - datetime.timedelta(days=5)).strftime('%Y-%m-%d')
|
75 |
end_date_str = datetime.datetime.today().strftime('%Y-%m-%d')
|
76 |
dates = set(pd.date_range(start=start_date_str, end=end_date_str).strftime('%Y-%m-%d'))
|
77 |
|
@@ -84,11 +85,19 @@ if st.button('Load') and ticker:
|
|
84 |
unique_dates = dates - existing_dates
|
85 |
|
86 |
if unique_dates:
|
|
|
87 |
for date in unique_dates:
|
88 |
try:
|
89 |
res_news = get_finnhub_data(f"/company-news?symbol={ticker}&from={date}&to={date}")
|
90 |
except:
|
91 |
-
st.error('
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
92 |
st.write(f"Proessing {len(res_news)} headlines for ", ticker,' on ', date)
|
93 |
|
94 |
for item in res_news:
|
|
|
68 |
sentiment_score real
|
69 |
)""")
|
70 |
|
71 |
+
num_days_history=5
|
72 |
+
num_days_history = st.number_input(label='Number of Historical days to process. Beware of time.', value=5, min_value=1, max_value=30)
|
73 |
+
ticker = st.text_input(label=f'Entering ticker will add last {num_days_history} days of news sentiment data to database.')
|
74 |
|
75 |
+
start_date_str = (datetime.datetime.today() - datetime.timedelta(days=num_days_history)).strftime('%Y-%m-%d')
|
|
|
|
|
76 |
end_date_str = datetime.datetime.today().strftime('%Y-%m-%d')
|
77 |
dates = set(pd.date_range(start=start_date_str, end=end_date_str).strftime('%Y-%m-%d'))
|
78 |
|
|
|
85 |
unique_dates = dates - existing_dates
|
86 |
|
87 |
if unique_dates:
|
88 |
+
error_counter = 0
|
89 |
for date in unique_dates:
|
90 |
try:
|
91 |
res_news = get_finnhub_data(f"/company-news?symbol={ticker}&from={date}&to={date}")
|
92 |
except:
|
93 |
+
st.error('Data Source Error.')
|
94 |
+
|
95 |
+
if res_news == []:
|
96 |
+
error_counter += 1
|
97 |
+
if error_counter > 4:
|
98 |
+
st.error('Invalid Ticker.')
|
99 |
+
break
|
100 |
+
|
101 |
st.write(f"Proessing {len(res_news)} headlines for ", ticker,' on ', date)
|
102 |
|
103 |
for item in res_news:
|