Spaces:
Running
Running
Benjamin Consolvo
commited on
Commit
·
e77df83
1
Parent(s):
18cfdec
auto trades back in
Browse files
app.py
CHANGED
@@ -609,55 +609,55 @@ def main():
|
|
609 |
st.markdown(f"**Time until open:** {pd.to_timedelta(seconds_left, unit='s')}")
|
610 |
|
611 |
|
612 |
-
#
|
613 |
-
|
614 |
-
|
615 |
|
616 |
-
#
|
617 |
-
|
618 |
-
|
619 |
-
|
620 |
-
|
621 |
|
622 |
# Main area: plots and data
|
623 |
app.manual_trade()
|
624 |
app.display_charts()
|
625 |
|
626 |
# Read and display latest auto-trade actions
|
627 |
-
|
628 |
-
|
629 |
-
|
630 |
-
|
631 |
-
|
632 |
-
|
633 |
-
|
634 |
-
|
635 |
-
|
636 |
-
|
637 |
-
|
638 |
-
|
639 |
-
|
640 |
-
|
641 |
-
|
642 |
-
|
643 |
-
|
644 |
-
|
645 |
-
|
646 |
-
|
647 |
-
|
648 |
-
|
649 |
-
|
650 |
-
|
651 |
-
|
652 |
-
|
653 |
-
|
654 |
-
|
655 |
-
|
656 |
-
|
657 |
-
|
658 |
-
|
659 |
-
|
660 |
-
|
661 |
|
662 |
|
663 |
if __name__ == "__main__":
|
|
|
609 |
st.markdown(f"**Time until open:** {pd.to_timedelta(seconds_left, unit='s')}")
|
610 |
|
611 |
|
612 |
+
# Initialize auto trade log in session state if needed
|
613 |
+
if AUTO_TRADE_LOG_KEY not in st.session_state:
|
614 |
+
st.session_state[AUTO_TRADE_LOG_KEY] = []
|
615 |
|
616 |
+
# Only start the background thread once
|
617 |
+
if "auto_trade_thread_started" not in st.session_state:
|
618 |
+
thread = threading.Thread(target=background_auto_trade, args=(app,), daemon=True)
|
619 |
+
thread.start()
|
620 |
+
st.session_state["auto_trade_thread_started"] = True
|
621 |
|
622 |
# Main area: plots and data
|
623 |
app.manual_trade()
|
624 |
app.display_charts()
|
625 |
|
626 |
# Read and display latest auto-trade actions
|
627 |
+
st.write("Automatic Trading Actions Based on Sentiment (background):")
|
628 |
+
auto_trade_log = get_auto_trade_log()
|
629 |
+
if auto_trade_log:
|
630 |
+
# Show the most recent entry
|
631 |
+
last_entry = auto_trade_log[-1]
|
632 |
+
st.write(f"Last checked: {last_entry['timestamp']}")
|
633 |
+
df = pd.DataFrame(last_entry["actions"])
|
634 |
+
if "company_name" in df.columns:
|
635 |
+
df = df[["symbol", "company_name", "sentiment", "action"]]
|
636 |
+
st.dataframe(df)
|
637 |
+
st.write("Sentiment Analysis (latest):")
|
638 |
+
st.write(last_entry["sentiment"])
|
639 |
+
|
640 |
+
# Plot buy/sell actions over time
|
641 |
+
st.write("Auto-Trading History (Buy/Sell Actions Over Time):")
|
642 |
+
history = []
|
643 |
+
for entry in auto_trade_log:
|
644 |
+
ts = entry["timestamp"]
|
645 |
+
for act in entry["actions"]:
|
646 |
+
if act["action"] in ("Buy", "Sell"):
|
647 |
+
history.append({
|
648 |
+
"timestamp": ts,
|
649 |
+
"symbol": act["symbol"],
|
650 |
+
"action": act["action"]
|
651 |
+
})
|
652 |
+
if history:
|
653 |
+
hist_df = pd.DataFrame(history)
|
654 |
+
if not hist_df.empty:
|
655 |
+
hist_df["timestamp"] = pd.to_datetime(hist_df["timestamp"])
|
656 |
+
hist_df["action_value"] = hist_df["action"].replace({"Buy": 1, "Sell": -1}).astype(float)
|
657 |
+
pivot = hist_df.pivot_table(index="timestamp", columns="symbol", values="action_value", aggfunc="sum")
|
658 |
+
st.line_chart(pivot.fillna(0))
|
659 |
+
else:
|
660 |
+
st.info("Waiting for first background auto-trade run...")
|
661 |
|
662 |
|
663 |
if __name__ == "__main__":
|