total PNL

This commit is contained in:
Dobromir Popov 2025-02-05 10:21:18 +02:00
parent fc2f834b32
commit 13c4f72b01

View File

@ -365,7 +365,7 @@ def update_live_html(candles, trade_history, epoch):
Generate an HTML page with a live chart.
The chart displays price (line) and volume (bar chart on a secondary y-axis),
and includes buy/sell markers with dotted lines connecting entries and exits.
The page auto-refreshes every 10 seconds.
The page auto-refreshes every 1 second.
"""
from io import BytesIO
import base64
@ -383,7 +383,7 @@ def update_live_html(candles, trade_history, epoch):
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="refresh" content="10">
<meta http-equiv="refresh" content="1">
<title>Live Trading Chart - Epoch {epoch}</title>
<style>
body {{
@ -535,6 +535,7 @@ class BacktestEnvironment:
# --- Enhanced Training Loop ---
def train_on_historical_data(env, model, device, args, start_epoch, optimizer, scheduler):
lambda_trade = args.lambda_trade
total_pnl = 0.0
for epoch in range(start_epoch, args.epochs):
env.reset()
loss_accum = 0.0
@ -563,10 +564,12 @@ def train_on_historical_data(env, model, device, args, start_epoch, optimizer, s
loss_accum += loss.item()
scheduler.step()
epoch_loss = loss_accum / steps
print(f"Epoch {epoch+1} Loss: {epoch_loss:.4f}")
total_pnl += sum(trade["pnl"] for trade in env.trade_history)
print(f"Epoch {epoch+1} Loss: {epoch_loss:.4f} | Total PnL: {total_pnl:.2f}")
save_checkpoint(model, optimizer, epoch, loss_accum)
simulate_trades(model, env, device, args)
update_live_html(env.candle_window, env.trade_history, epoch+1)
# --- Live Plotting (for Live Mode) ---
def live_preview_loop(candles, env):