Dmitry Beresnev
commited on
Commit
·
7b70d02
1
Parent(s):
a3e694b
add app
Browse files- app/main.py +67 -0
app/main.py
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import plotly.graph_objects as go
|
| 4 |
+
from openbb_terminal.sdk import openbb
|
| 5 |
+
from dotenv import load_dotenv
|
| 6 |
+
import os
|
| 7 |
+
|
| 8 |
+
load_dotenv()
|
| 9 |
+
token = os.getenv("OPENBB_TOKEN")
|
| 10 |
+
if token:
|
| 11 |
+
openbb.account.login(token)
|
| 12 |
+
|
| 13 |
+
st.title("TradingView + OpenBB Indicators Dashboard")
|
| 14 |
+
|
| 15 |
+
symbol = st.text_input("Ticker", "AAPL")
|
| 16 |
+
period = st.slider("SMA/EMA/RSI period", 5, 50, 20)
|
| 17 |
+
|
| 18 |
+
if st.button("Load Dashboard"):
|
| 19 |
+
|
| 20 |
+
# Load free stock data via OpenBB
|
| 21 |
+
df = openbb.stocks.load(symbol)
|
| 22 |
+
|
| 23 |
+
# ---- Technical Indicators ----
|
| 24 |
+
df["SMA"] = df["close"].rolling(period).mean()
|
| 25 |
+
df["EMA"] = df["close"].ewm(span=period, adjust=False).mean()
|
| 26 |
+
delta = df["close"].diff()
|
| 27 |
+
gain = delta.clip(lower=0)
|
| 28 |
+
loss = -1 * delta.clip(upper=0)
|
| 29 |
+
avg_gain = gain.rolling(period).mean()
|
| 30 |
+
avg_loss = loss.rolling(period).mean()
|
| 31 |
+
rs = avg_gain / avg_loss
|
| 32 |
+
df["RSI"] = 100 - (100 / (1 + rs))
|
| 33 |
+
|
| 34 |
+
# ---- Tabs ----
|
| 35 |
+
tab1, tab2, tab3 = st.tabs(["TradingView", "Price + SMA/EMA", "RSI"])
|
| 36 |
+
|
| 37 |
+
# ---- Tab 1: Interactive TradingView ----
|
| 38 |
+
with tab1:
|
| 39 |
+
tradingview_html = f"""
|
| 40 |
+
<div class="tradingview-widget-container">
|
| 41 |
+
<div id="tradingview_{symbol}"></div>
|
| 42 |
+
<script type="text/javascript" src="https://s3.tradingview.com/tv.js"></script>
|
| 43 |
+
<script type="text/javascript">
|
| 44 |
+
new TradingView.widget({{
|
| 45 |
+
"width": "100%",
|
| 46 |
+
"height": 600,
|
| 47 |
+
"symbol": "{symbol}",
|
| 48 |
+
"interval": "D",
|
| 49 |
+
"timezone": "Etc/UTC",
|
| 50 |
+
"theme": "light",
|
| 51 |
+
"style": "1",
|
| 52 |
+
"locale": "en",
|
| 53 |
+
"toolbar_bg": "#f1f3f6",
|
| 54 |
+
"enable_publishing": false,
|
| 55 |
+
"allow_symbol_change": true,
|
| 56 |
+
"container_id": "tradingview_{symbol}"
|
| 57 |
+
}});
|
| 58 |
+
</script>
|
| 59 |
+
</div>
|
| 60 |
+
"""
|
| 61 |
+
st.components.v1.html(tradingview_html, height=650)
|
| 62 |
+
|
| 63 |
+
# ---- Tab 2: Price + SMA/EMA ----
|
| 64 |
+
with tab2:
|
| 65 |
+
fig = go.Figure()
|
| 66 |
+
fig.add_trace(go.Scatter(x=df.index, y=df["close"], name="Close"))
|
| 67 |
+
fig.add_trace(go.Scatter(x=df.index, y=df["SMA"], name=f"SMA {period}"))
|