Spaces:
Running
Running
Robert Castagna
commited on
Commit
•
31b2f5f
1
Parent(s):
b654348
adding local IBKR support
Browse files- pages/1_Fundamentals.py +4 -0
- pages/2_Portfolio_Builder.py +61 -14
- requirements.txt +2 -0
pages/1_Fundamentals.py
CHANGED
@@ -154,6 +154,8 @@ with st.form(key="selecting columns"):
|
|
154 |
df_final.rename({'finnhubIndustry':'Industry', 'eps':'EPS', 'roe':'ROE','pe':'P/E','ps':'P/S', 'pb':'P/B','pfcf':'P/FCF','beta':'Beta'}, inplace=True, axis=1)
|
155 |
st.write(df_final)
|
156 |
|
|
|
|
|
157 |
if submit_button and symbols and strategy_selection == 'Growth':
|
158 |
|
159 |
hash_map,gains_data = {},{}
|
@@ -190,3 +192,5 @@ with st.form(key="selecting columns"):
|
|
190 |
df_final['PE/G'] = df_final['pe'] / df_final['epsGrowth5Y']
|
191 |
df_final.rename({'finnhubIndustry':'Industry','marketCapitalization':'MarketCap','roe':'ROE', 'ev':'Enterp. Val', 'pe':'P/E', 'ps':'P/S', 'pb':'P/B', 'eps': 'EPS'}, inplace=True, axis=1)
|
192 |
st.write(df_final)
|
|
|
|
|
|
154 |
df_final.rename({'finnhubIndustry':'Industry', 'eps':'EPS', 'roe':'ROE','pe':'P/E','ps':'P/S', 'pb':'P/B','pfcf':'P/FCF','beta':'Beta'}, inplace=True, axis=1)
|
155 |
st.write(df_final)
|
156 |
|
157 |
+
st.session_state['tickers'] = symbols
|
158 |
+
|
159 |
if submit_button and symbols and strategy_selection == 'Growth':
|
160 |
|
161 |
hash_map,gains_data = {},{}
|
|
|
192 |
df_final['PE/G'] = df_final['pe'] / df_final['epsGrowth5Y']
|
193 |
df_final.rename({'finnhubIndustry':'Industry','marketCapitalization':'MarketCap','roe':'ROE', 'ev':'Enterp. Val', 'pe':'P/E', 'ps':'P/S', 'pb':'P/B', 'eps': 'EPS'}, inplace=True, axis=1)
|
194 |
st.write(df_final)
|
195 |
+
|
196 |
+
st.session_state['tickers'] = symbols
|
pages/2_Portfolio_Builder.py
CHANGED
@@ -2,6 +2,7 @@ import pandas as pd
|
|
2 |
from openbb import obb
|
3 |
import riskfolio as rp
|
4 |
import os
|
|
|
5 |
from dotenv import load_dotenv
|
6 |
import matplotlib.pyplot as plt
|
7 |
import pandas as pd
|
@@ -12,17 +13,35 @@ from plotly.subplots import make_subplots
|
|
12 |
import plotly.figure_factory as ff
|
13 |
import streamlit as st
|
14 |
import platform
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
|
16 |
-
st.write(platform.processor())
|
17 |
-
# online hf: AMD64 Family 23 Model 113 Stepping 0, AuthenticAMD
|
18 |
|
19 |
load_dotenv()
|
20 |
|
21 |
-
from openbb import obb
|
22 |
obb.account.login(pat=os.environ['open_bb_pat'])
|
23 |
|
24 |
-
|
25 |
-
# take stock inputs
|
26 |
tickers = [
|
27 |
"XLE", "XLF", "XLU", "XLI", "GDX",
|
28 |
"XLK", "XLV", "XLY", "XLP", "XLB",
|
@@ -30,9 +49,37 @@ tickers = [
|
|
30 |
"GDXJ", "IYE", "OIH", "XME", "XRT",
|
31 |
"SMH", "IBB", "KBE", "KRE", "XTL",
|
32 |
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
|
34 |
-
|
35 |
-
|
|
|
36 |
|
37 |
data = (
|
38 |
obb
|
@@ -132,13 +179,13 @@ Research has shown HRP to deliver lower out-of-sample variance than traditional
|
|
132 |
|
133 |
fig1, ax1 = plt.subplots()
|
134 |
ax1 = rp.plot_clusters(returns=returns,
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
|
143 |
st.pyplot(fig1)
|
144 |
|
|
|
2 |
from openbb import obb
|
3 |
import riskfolio as rp
|
4 |
import os
|
5 |
+
import regex as re
|
6 |
from dotenv import load_dotenv
|
7 |
import matplotlib.pyplot as plt
|
8 |
import pandas as pd
|
|
|
13 |
import plotly.figure_factory as ff
|
14 |
import streamlit as st
|
15 |
import platform
|
16 |
+
import datetime
|
17 |
+
import asyncio
|
18 |
+
import nest_asyncio
|
19 |
+
from openbb import obb
|
20 |
+
|
21 |
+
def open_nested_event_loop():
|
22 |
+
# Check if there is an existing event loop, if not, create a new one
|
23 |
+
nest_asyncio.apply()
|
24 |
+
try:
|
25 |
+
loop = asyncio.get_event_loop()
|
26 |
+
except RuntimeError as ex:
|
27 |
+
if "There is no current event loop in thread" in str(ex):
|
28 |
+
loop = asyncio.new_event_loop()
|
29 |
+
asyncio.set_event_loop(loop)
|
30 |
+
return
|
31 |
+
|
32 |
+
def get_positions(account):
|
33 |
+
positions = ib.portfolio(account)
|
34 |
+
tickers = []
|
35 |
+
for position in positions:
|
36 |
+
tickers.append([position.contract.symbol, position.position, position.marketPrice])
|
37 |
+
return tickers
|
38 |
|
|
|
|
|
39 |
|
40 |
load_dotenv()
|
41 |
|
|
|
42 |
obb.account.login(pat=os.environ['open_bb_pat'])
|
43 |
|
44 |
+
# -------------------------- (Tickers) -------------------------------- #
|
|
|
45 |
tickers = [
|
46 |
"XLE", "XLF", "XLU", "XLI", "GDX",
|
47 |
"XLK", "XLV", "XLY", "XLP", "XLB",
|
|
|
49 |
"GDXJ", "IYE", "OIH", "XME", "XRT",
|
50 |
"SMH", "IBB", "KBE", "KRE", "XTL",
|
51 |
]
|
52 |
+
st.session_state['tickers'] = tickers
|
53 |
+
|
54 |
+
if not platform.processor(): # take inputs from the user for hosted application
|
55 |
+
tickers = st.session_state['tickers']
|
56 |
+
|
57 |
+
elif re.search('AuthenticAMD', platform.processor()): # use live ibkr portfolio if ran from local machine
|
58 |
+
try:
|
59 |
+
# Initialize IB connection here
|
60 |
+
open_nested_event_loop()
|
61 |
+
from ib_insync import *
|
62 |
+
ib = IB()
|
63 |
+
# Assuming you're connecting to a local TWS instance with default settings
|
64 |
+
ib.connect('127.0.0.1', 7497, clientId=1)
|
65 |
+
paper_acct = ib.managedAccounts()[0]
|
66 |
+
|
67 |
+
position_data = get_positions(paper_acct)
|
68 |
+
for pos in position_data:
|
69 |
+
tickers = [pos[0] for pos in position_data]
|
70 |
+
|
71 |
+
except Exception as e:
|
72 |
+
st.write('IB Workstation is not running. Please start the IB Workstation and try again.')
|
73 |
+
st.write('Error: ', e)
|
74 |
+
st.stop()
|
75 |
+
|
76 |
+
|
77 |
+
|
78 |
+
st.write(tickers)
|
79 |
|
80 |
+
# define range as today - 365 days to today
|
81 |
+
start_date = (datetime.datetime.now() - datetime.timedelta(days=365)).strftime('%Y-%m-%d')
|
82 |
+
end_date = datetime.datetime.now().strftime('%Y-%m-%d')
|
83 |
|
84 |
data = (
|
85 |
obb
|
|
|
179 |
|
180 |
fig1, ax1 = plt.subplots()
|
181 |
ax1 = rp.plot_clusters(returns=returns,
|
182 |
+
codependence='pearson',
|
183 |
+
linkage='single',
|
184 |
+
k=None,
|
185 |
+
max_k=10,
|
186 |
+
leaf_order=True,
|
187 |
+
dendrogram=True,
|
188 |
+
ax=None)
|
189 |
|
190 |
st.pyplot(fig1)
|
191 |
|
requirements.txt
CHANGED
@@ -21,3 +21,5 @@ streamlit>=1.22.0
|
|
21 |
regex
|
22 |
yfinance==0.2.28
|
23 |
python-dotenv
|
|
|
|
|
|
21 |
regex
|
22 |
yfinance==0.2.28
|
23 |
python-dotenv
|
24 |
+
asyncio
|
25 |
+
nest_asyncios
|