natexcvi
Set portfolio index
fe8f4f7 unverified
import datetime
import json
from datetime import tzinfo
import pandas as pd
import streamlit as st
from tzlocal import get_localzone
from api import TradeAssistantAPI
from style import portfolio_table_styler, recommendation_table_styler
st.title("πŸ“Š Trading Assistant")
if st.sidebar.button("Refresh"):
st.experimental_rerun()
st.sidebar.title("Settings")
param_token = st.experimental_get_query_params().get("token", [""])[0]
token = st.sidebar.text_input("Token", type="password", value=param_token)
if not token:
st.error("Access denied")
st.stop()
api = TradeAssistantAPI(token)
st.write("## Recommendations")
recommendation_res = None
try:
recommendation_res = api.get_recommendations()
updated = recommendation_res["updated"]
recommendations = json.loads(recommendation_res["recommendations"])
recommendations = pd.DataFrame(recommendations)
st.dataframe(recommendations.style.pipe(recommendation_table_styler))
tz = str(get_localzone())
updated_time_ago = pd.Timestamp.now().tz_localize(tz) - pd.Timestamp(
updated, tz="UTC"
)
st.write(f"Updated: {updated_time_ago} ago")
except Exception as e:
st.error(e)
st.expander("Details").write(recommendation_res)
portfolio = None
try:
portfolio = api.get_portfolio()
portfolio = portfolio[list(portfolio.keys())[0]]
portfolio = pd.DataFrame(portfolio)
portfolio.set_index("contractDesc", inplace=True)
st.write("## Portfolio")
st.dataframe(portfolio.style.pipe(portfolio_table_styler))
except Exception as e:
st.error(e)
st.expander("Details").write(portfolio)
st.sidebar.write("## Actions")
schedule_training_res = None
if st.sidebar.button("Train model"):
schedule_training_res = api.train_model()
st.sidebar.success(
f"Training job scheduled. Job ID: {schedule_training_res['job_id']}"
)
st.write("## Training jobs")
try:
training_jobs_res = api.list_training_jobs()
# training_jobs = pd.DataFrame(training_jobs_res)
# st.dataframe(training_jobs)
st.write(training_jobs_res)
except Exception as e:
st.error(e)
st.expander("Details").write(training_jobs_res)