Member-only story
IB Portfolio Insights with Streamlit and Trading View Charts

In this article, I’ll guide you through the creation of a Streamlit app, demonstrating how to display our Interactive Brokers paper and live portfolios, along with comprehensive historical charts for each symbol, using TradingView charts.
First of all, we install all the needed libraries:
poetry add streamlit
poetry add plotly
poetry add lightweight_charts
poetry add streamlit-lightweight-charts
poetry add ib-insync
Home page
We’ll create a multi page app, as we’ll leverage all the code created in this article for our live trading bot.
Create a file named Home.py
:
import streamlit as st
with open('style.css') as f:
st.markdown(f'<style>{f.read()}</style>', unsafe_allow_html=True)
And run it executing in the terminal:
poetry run streamlit run Home.py
Portfolio page
The primary aim of this article is to generate a portfolio page capable of seamlessly displaying either our paper or live portfolio from our Interactive Brokers account. To accomplish this, create a new page named 1_💼_Portfolio.py
within the pages
folder.
Event loop for Interactive Brokers
Using ib_insync
with Streamlit can present challenges due to the asynchronous nature of the ib_insync
library and the synchronous execution model of Streamlit. The problem arises because ib_insync
relies on an event loop to handle asynchronous tasks, which conflicts with Streamlit's execution model.
To address this problem, we’ll run an event loop at the beginning of our page:
st.set_page_config(
layout='wide',
page_title='My Dashboard'
)
def get_or_create_eventloop():
try:
return asyncio.get_event_loop()
except RuntimeError as ex:
create_loop = "There is no current event loop in thread" in str(ex)
create_loop = create_loop or "Event loop is closed" in str(ex)
if create_loop:
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
return asyncio.get_event_loop()
loop =…