-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
409 lines (336 loc) · 13.2 KB
/
app.py
File metadata and controls
409 lines (336 loc) · 13.2 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
import os
import json
from datetime import datetime
from typing import Dict, Any
import numpy as np
import pandas as pd
import streamlit as st
from openai import OpenAI
MEMORY_FILE = "journal_memory.json"
REQUIRED_COLUMNS = ["date", "symbol", "side", "qty", "entry", "exit"]
# -----------------------------
# JSON safety
# -----------------------------
def make_json_safe(obj):
"""Recursively convert pandas/numpy objects into JSON-serializable types."""
if isinstance(obj, dict):
return {k: make_json_safe(v) for k, v in obj.items()}
if isinstance(obj, list):
return [make_json_safe(v) for v in obj]
if isinstance(obj, pd.Timestamp):
return obj.isoformat()
if isinstance(obj, (np.integer, np.int64)):
return int(obj)
if isinstance(obj, (np.floating, np.float64)):
return float(obj)
if isinstance(obj, np.ndarray):
return obj.tolist()
return obj
# -----------------------------
# Memory
# -----------------------------
def load_memory():
if os.path.exists(MEMORY_FILE):
try:
with open(MEMORY_FILE, "r", encoding="utf-8") as f:
return json.load(f)
except Exception:
return []
return []
def save_memory(data):
with open(MEMORY_FILE, "w", encoding="utf-8") as f:
json.dump(make_json_safe(data), f, indent=2)
# -----------------------------
# Parsing + normalization
# -----------------------------
def normalize_side(x: str) -> str:
x = str(x).strip().upper()
if x in ["BUY", "B", "LONG", "L"]:
return "LONG"
if x in ["SELL", "S", "SHORT", "SH"]:
return "SHORT"
return x
def safe_float(x):
try:
if x is None or (isinstance(x, str) and x.strip() == ""):
return np.nan
return float(x)
except Exception:
return np.nan
def load_trades_df(df: pd.DataFrame) -> pd.DataFrame:
df = df.copy()
df.columns = [c.strip().lower() for c in df.columns]
missing = [c for c in REQUIRED_COLUMNS if c not in df.columns]
if missing:
raise ValueError(f"Missing required columns: {missing}. Required: {REQUIRED_COLUMNS}")
df["date"] = pd.to_datetime(df["date"], errors="coerce")
df["symbol"] = df["symbol"].astype(str).str.upper().str.strip()
df["side"] = df["side"].apply(normalize_side)
for col in ["qty", "entry", "exit"]:
df[col] = df[col].apply(safe_float)
if "fees" in df.columns:
df["fees"] = df["fees"].apply(safe_float).fillna(0.0)
else:
df["fees"] = 0.0
df = df.dropna(subset=["date", "symbol", "side", "qty", "entry", "exit"])
df = df[df["qty"] > 0]
# PnL
df["pnl_gross"] = np.where(
df["side"] == "SHORT",
(df["entry"] - df["exit"]) * df["qty"],
(df["exit"] - df["entry"]) * df["qty"],
)
df["pnl"] = df["pnl_gross"] - df["fees"]
# Return
df["notional"] = df["entry"] * df["qty"]
df["ret"] = df["pnl"] / df["notional"].replace(0, np.nan)
# Time features
df["weekday"] = df["date"].dt.day_name()
df["hour"] = df["date"].dt.hour
df = df.sort_values("date").reset_index(drop=True)
return df
# -----------------------------
# Metrics
# -----------------------------
def compute_equity_curve(df: pd.DataFrame) -> pd.DataFrame:
out = df[["date", "pnl"]].copy()
out["cum_pnl"] = out["pnl"].cumsum()
out["equity"] = out["cum_pnl"] # relative equity (starts at 0)
out["peak"] = out["equity"].cummax()
out["drawdown"] = out["equity"] - out["peak"]
return out
def profit_factor(df: pd.DataFrame) -> float:
gains = df.loc[df["pnl"] > 0, "pnl"].sum()
losses = df.loc[df["pnl"] < 0, "pnl"].sum()
if losses == 0:
return float("inf") if gains > 0 else np.nan
return float(gains / abs(losses))
def streaks(df: pd.DataFrame) -> Dict[str, Any]:
signs = np.where(df["pnl"] > 0, 1, np.where(df["pnl"] < 0, -1, 0))
max_win = max_loss = 0
cur_win = cur_loss = 0
for s in signs:
if s == 1:
cur_win += 1
cur_loss = 0
elif s == -1:
cur_loss += 1
cur_win = 0
else:
cur_win = 0
cur_loss = 0
max_win = max(max_win, cur_win)
max_loss = max(max_loss, cur_loss)
return {"max_win_streak": int(max_win), "max_loss_streak": int(max_loss)}
def summary_metrics(df: pd.DataFrame) -> Dict[str, Any]:
n = len(df)
wins = int((df["pnl"] > 0).sum())
losses = int((df["pnl"] < 0).sum())
breakeven = int((df["pnl"] == 0).sum())
win_rate = wins / n if n else np.nan
avg_win = float(df.loc[df["pnl"] > 0, "pnl"].mean()) if wins else np.nan
avg_loss = float(df.loc[df["pnl"] < 0, "pnl"].mean()) if losses else np.nan
expectancy = (win_rate * avg_win) + ((1 - win_rate) * avg_loss) if (n and wins and losses) else np.nan
pf = profit_factor(df)
eq = compute_equity_curve(df)
max_dd = float(eq["drawdown"].min()) if not eq.empty else np.nan
stks = streaks(df)
return {
"trades": int(n),
"wins": wins,
"losses": losses,
"breakeven": breakeven,
"win_rate": float(win_rate) if win_rate == win_rate else None,
"avg_win": float(avg_win) if avg_win == avg_win else None,
"avg_loss": float(avg_loss) if avg_loss == avg_loss else None,
"profit_factor": (float(pf) if pf == pf else None) if pf != float("inf") else "inf",
"expectancy": float(expectancy) if expectancy == expectancy else None,
"total_pnl": float(df["pnl"].sum()) if n else 0.0,
"max_drawdown": float(max_dd) if max_dd == max_dd else None,
**stks,
}
def breakdown_tables(df: pd.DataFrame) -> Dict[str, pd.DataFrame]:
by_symbol = (
df.groupby("symbol")
.agg(trades=("pnl", "count"), total_pnl=("pnl", "sum"), win_rate=("pnl", lambda x: (x > 0).mean()))
.sort_values("total_pnl", ascending=False)
.reset_index()
)
by_weekday = (
df.groupby("weekday")
.agg(trades=("pnl", "count"), total_pnl=("pnl", "sum"), win_rate=("pnl", lambda x: (x > 0).mean()))
.sort_values("total_pnl", ascending=False)
.reset_index()
)
by_hour = (
df.groupby("hour")
.agg(trades=("pnl", "count"), total_pnl=("pnl", "sum"), win_rate=("pnl", lambda x: (x > 0).mean()))
.sort_values("total_pnl", ascending=False)
.reset_index()
)
biggest_wins = df.sort_values("pnl", ascending=False).head(10)[
["date", "symbol", "side", "qty", "entry", "exit", "fees", "pnl"]
]
biggest_losses = df.sort_values("pnl", ascending=True).head(10)[
["date", "symbol", "side", "qty", "entry", "exit", "fees", "pnl"]
]
return {
"by_symbol": by_symbol,
"by_weekday": by_weekday,
"by_hour": by_hour,
"biggest_wins": biggest_wins,
"biggest_losses": biggest_losses,
}
# -----------------------------
# LLM coach report
# -----------------------------
def coach_report(openai_key: str, payload: Dict[str, Any]) -> str:
client = OpenAI(api_key=openai_key)
prompt = f"""
You are a pragmatic trading coach. Analyze the journal summary and provide coaching.
No financial advice. Do not recommend tickers to buy/sell.
DATA (JSON):
{json.dumps(payload, indent=2)}
OUTPUT FORMAT:
1) TL;DR (6 bullets)
2) Edge & risk diagnosis (based on win rate, avg win/loss, profit factor, expectancy, drawdown)
3) 3-5 behavioral patterns to improve (tie each to the metrics)
4) "Next 7 trades plan" checklist (6-10 bullets)
5) 3 journal prompts to answer after each trade
Keep it ~600-900 words.
"""
resp = client.chat.completions.create(
model="gpt-4o-mini",
messages=[
{"role": "system", "content": "You coach traders on process and risk management."},
{"role": "user", "content": prompt},
],
temperature=0.3,
)
return resp.choices[0].message.content
# -----------------------------
# Streamlit UI
# -----------------------------
st.set_page_config(page_title="📓 Trade Journal Coach Agent", page_icon="📓")
st.title("📓 Trade Journal Coach Agent")
st.caption("Upload your trades CSV and get performance metrics + an AI coaching report (educational only).")
memory = load_memory()
with st.sidebar:
st.header("🔑 OpenAI API Key")
openai_key = st.text_input("OpenAI API Key", type="password")
st.markdown("---")
save_to_memory = st.checkbox("Save reports to journal_memory.json", value=True)
st.subheader("1) Upload your trades CSV")
uploaded = st.file_uploader(
"CSV with columns: date, symbol, side, qty, entry, exit (fees optional)",
type=["csv"]
)
st.subheader("2) Or paste trades manually")
st.caption("Format: date,symbol,side,qty,entry,exit,fees(optional)")
manual = st.text_area("Paste rows", value="", height=120)
def parse_manual(text: str) -> pd.DataFrame:
rows = []
for line in (text or "").splitlines():
line = line.strip()
if not line:
continue
parts = [p.strip() for p in line.split(",")]
if len(parts) < 6:
continue
rows.append({
"date": parts[0],
"symbol": parts[1],
"side": parts[2],
"qty": parts[3],
"entry": parts[4],
"exit": parts[5],
"fees": parts[6] if len(parts) >= 7 else 0,
})
return pd.DataFrame(rows)
df_raw = None
if uploaded is not None:
try:
df_raw = pd.read_csv(uploaded)
except Exception as e:
st.error(f"Could not read CSV: {e}")
elif manual.strip():
df_raw = parse_manual(manual)
if df_raw is not None:
try:
df = load_trades_df(df_raw)
st.markdown("### Trades (normalized)")
st.dataframe(df, use_container_width=True)
metrics = summary_metrics(df)
tables = breakdown_tables(df)
eq = compute_equity_curve(df)
st.markdown("---")
st.subheader("📊 Key Metrics")
c1, c2, c3, c4 = st.columns(4)
c1.metric("Trades", str(metrics["trades"]))
c2.metric("Win rate", f"{metrics['win_rate']*100:.1f}%" if metrics["win_rate"] is not None else "—")
pf = metrics["profit_factor"]
c3.metric("Profit factor", "∞" if pf == "inf" else (f"{pf:.2f}" if pf is not None else "—"))
c4.metric("Total PnL", f"${metrics['total_pnl']:,.2f}")
c5, c6, c7 = st.columns(3)
c5.metric("Avg win", f"${metrics['avg_win']:,.2f}" if metrics["avg_win"] is not None else "—")
c6.metric("Avg loss", f"${metrics['avg_loss']:,.2f}" if metrics["avg_loss"] is not None else "—")
c7.metric("Max drawdown", f"${metrics['max_drawdown']:,.2f}" if metrics["max_drawdown"] is not None else "—")
st.subheader("📉 Equity Curve (cumulative PnL)")
st.line_chart(eq.set_index("date")["equity"])
st.subheader("📉 Drawdown")
st.line_chart(eq.set_index("date")["drawdown"])
st.markdown("---")
st.subheader("🔎 Breakdowns")
b1, b2 = st.columns(2)
with b1:
st.write("**By Symbol**")
st.dataframe(tables["by_symbol"], use_container_width=True)
with b2:
st.write("**By Weekday**")
st.dataframe(tables["by_weekday"], use_container_width=True)
st.write("**By Hour**")
st.dataframe(tables["by_hour"], use_container_width=True)
st.markdown("---")
st.subheader("🏆 Biggest Winners")
st.dataframe(tables["biggest_wins"], use_container_width=True)
st.subheader("🧨 Biggest Losers")
st.dataframe(tables["biggest_losses"], use_container_width=True)
st.markdown("---")
st.subheader("🧠 AI Coaching Report")
if st.button("Generate Coaching Report", disabled=not openai_key):
raw_payload = {
"generated_at": datetime.now().isoformat(),
"summary_metrics": metrics,
"top_symbols": tables["by_symbol"].head(10).to_dict(orient="records"),
"weekday_breakdown": tables["by_weekday"].to_dict(orient="records"),
"hour_breakdown": tables["by_hour"].to_dict(orient="records"),
"biggest_wins": tables["biggest_wins"].to_dict(orient="records"),
"biggest_losses": tables["biggest_losses"].to_dict(orient="records"),
"notes": [
"PnL computed from entry/exit, qty, and fees. No slippage modeled.",
"Side interpreted as LONG/SHORT; ensure your CSV matches reality.",
],
}
payload = make_json_safe(raw_payload)
with st.spinner("Generating coach report..."):
report = coach_report(openai_key, payload)
st.success("Report generated!")
st.write(report)
if save_to_memory:
memory.append({
"timestamp": datetime.now().isoformat(),
"payload": payload,
"report": report
})
save_memory(memory)
except Exception as e:
st.error(f"Error: {e}")
st.markdown("---")
st.subheader("📚 Saved Coach Reports")
if memory:
for item in reversed(memory[-10:]):
st.caption(item.get("timestamp", ""))
with st.expander("View report"):
st.write(item.get("report", ""))
else:
st.write("No saved reports yet.")