-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patht10.py
131 lines (111 loc) · 4.76 KB
/
t10.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import streamlit as st
import threading
from datetime import datetime
# LumiBot imports
from lumibot.backtesting import YahooDataBacktesting
from lumibot.brokers import Alpaca
from lumibot.traders import Trader
from lumibot.credentials import ALPACA_CREDS
# Import your strategy (example)
from lumibot.example_strategies.stock_sentiment import StockSentiment
st.set_page_config(
page_title="FinBERT: Financial Sentiment Analysis with BERT",
layout="wide",
page_icon=':bar_chart:',
)
st.title("Sentiment-Based Trading Bot with Live Trading")
st.subheader("FinBERT pre-trained NLP model to analyze sentiment of financial text.")
st.write("""
Automated sentiment or polarity analysis of texts produced by financial actors using NLP methods.
""")
# Store results in session_state so we can display them from the main thread
if "backtest_results" not in st.session_state:
st.session_state["backtest_results"] = None
if "tear_sheet_figure" not in st.session_state:
st.session_state["tear_sheet_figure"] = None
if "backtest_running" not in st.session_state:
st.session_state["backtest_running"] = False
def run_backtest():
"""Long-running backtest & live trading in a background thread."""
try:
st.session_state["backtest_running"] = True
# Define the backtest period
start_date = datetime(2020,1,1)
end_date = datetime(2020,11,12)
print(f"[Thread] Starting backtest from {start_date} to {end_date}...")
# Set up broker and strategy
broker = Alpaca(ALPACA_CREDS)
strategy = StockSentiment(
name="mlstrat",
broker=broker,
parameters={
"symbol": "SPY",
"cash_at_risk": 0.5
}
)
# Run the backtest
results = strategy.backtest(
YahooDataBacktesting,
start_date,
end_date,
parameters={"symbol":"SPY", "cash_at_risk":0.5},
)
st.session_state["backtest_results"] = results
# OPTIONAL: Generate a tear sheet if LumiBot (or you) provide a function
# Adjust this to your actual tear sheet method:
# e.g. `fig = results.create_tear_sheet()`
# or `fig = results.display_tear_sheet()`
# Make sure the function returns a Matplotlib figure rather than printing
# or displaying inline. If it displays inline, you need to adapt it to return a figure.
if hasattr(results, "create_tear_sheet"):
fig = results.create_tear_sheet()
st.session_state["tear_sheet_figure"] = fig
elif hasattr(results, "display_tear_sheet"):
# If display_tear_sheet returns a figure
fig = results.display_tear_sheet(return_fig=True)
st.session_state["tear_sheet_figure"] = fig
else:
# If there's no tear sheet method, you'll need custom logic:
print("[Thread] No tear sheet method found in results.")
st.session_state["tear_sheet_figure"] = None
print("[Thread] Backtest finished. Starting live trading...")
# Start live trading (This is a blocking call)
trader = Trader()
trader.add_strategy(strategy)
# Start the trading in a separate thread to avoid blocking Streamlit
trader_thread = threading.Thread(target=trader.run_all)
trader_thread.start()
# Display a progress bar in the Streamlit app
while trader_thread.is_alive():
st.progress(100 * (1 - trader_thread._stop.__self__.is_alive()))
st.write("Running live trading...")
finally:
st.session_state["backtest_running"] = False
def start_background_backtest():
# Reset old results
st.session_state["backtest_results"] = None
st.session_state["tear_sheet_figure"] = None
# Start a thread to run the backtest
worker_thread = threading.Thread(target=run_backtest, args=())
worker_thread.start()
# Button to trigger backtest
if st.button("Start Backtest"):
# Prevent multiple backtest runs
if not st.session_state["backtest_running"]:
st.write("Backtest is starting in the background...")
start_background_backtest()
else:
st.warning("A backtest is already running!")
# Status / Results display
if st.session_state["backtest_running"]:
st.info("Backtest is running... (this might take a while).")
elif st.session_state["backtest_results"] is not None:
st.success("Backtest completed!")
st.write("### Backtest Results")
st.write(st.session_state["backtest_results"])
# Display the tear sheet if we have it
if st.session_state["tear_sheet_figure"] is not None:
st.write("### Tear Sheet")
st.pyplot(st.session_state["tear_sheet_figure"])
else:
st.warning("No tear sheet figure available.")