From 75c4d6602af46dacfe62a12b94ede981cac4e842 Mon Sep 17 00:00:00 2001 From: Dobromir Popov Date: Tue, 4 Feb 2025 22:27:33 +0200 Subject: [PATCH] more strong model response --- crypto/brian/index-deep-new.py | 22 +++++++++++----------- crypto/brian/live_chart.html | 6 +++--- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/crypto/brian/index-deep-new.py b/crypto/brian/index-deep-new.py index cf23b20..5e60a3f 100644 --- a/crypto/brian/index-deep-new.py +++ b/crypto/brian/index-deep-new.py @@ -327,6 +327,8 @@ def update_live_chart(ax, candles, trade_history): def simulate_trades(model, env, device, args): """ Run a simulation on the current sliding window using the model's outputs and a decision rule. + Here we force the simulation to always take an action by comparing the predicted potentials, + ensuring that the model is forced to trade (either BUY or SELL) rather than HOLD. This simulation updates env.trade_history and is used for visualization only. """ env.reset() # resets the window and index @@ -339,12 +341,11 @@ def simulate_trades(model, env, device, args): pred_high, pred_low = model(state_tensor, timeframe_ids) pred_high = pred_high.item() pred_low = pred_low.item() - if (pred_high - current_open) >= (current_open - pred_low) and (pred_high - current_open) > args.threshold: + # Force a trade: choose BUY if predicted up-move is higher (or equal), else SELL. + if (pred_high - current_open) >= (current_open - pred_low): action = 2 # BUY - elif (current_open - pred_low) > (pred_high - current_open) and (current_open - pred_low) > args.threshold: - action = 0 # SELL else: - action = 1 # HOLD + action = 0 # SELL _, _, _, done, _, _ = env.step(action) if done: break @@ -481,9 +482,9 @@ def parse_args(): parser.add_argument('--mode', choices=['train', 'live', 'inference'], default='train') parser.add_argument('--epochs', type=int, default=100) parser.add_argument('--lr', type=float, default=3e-4) - parser.add_argument('--threshold', type=float, default=0.005, help="Minimum predicted move to trigger trade.") + parser.add_argument('--threshold', type=float, default=0.005, help="Minimum predicted move to trigger trade (used in loss).") parser.add_argument('--lambda_trade', type=float, default=1.0, help="Weight for trade surrogate loss.") - parser.add_argument('--penalty_noaction', type=float, default=10.0, help="Penalty if no action is taken.") + parser.add_argument('--penalty_noaction', type=float, default=10.0, help="Penalty if no action is taken (used in loss).") parser.add_argument('--start_fresh', action='store_true', help="Start training from scratch.") return parser.parse_args() @@ -545,7 +546,7 @@ async def main(): env = BacktestEnvironment(candles_dict, base_tf="1m", timeframes=timeframes, window_size=100) preview_thread = threading.Thread(target=live_preview_loop, args=(env.candle_window, env), daemon=True) preview_thread.start() - print("Starting live trading loop. (Using model-based decision rule.)") + print("Starting live trading loop. (Forcing trade actions based on highest potential.)") while True: state = env.get_state(env.current_index) current_open = env.candle_window[env.current_index]["open"] @@ -554,12 +555,11 @@ async def main(): pred_high, pred_low = model(state_tensor, timeframe_ids) pred_high = pred_high.item() pred_low = pred_low.item() - if (pred_high - current_open) >= (current_open - pred_low) and (pred_high - current_open) > args.threshold: + # Force a trade (choose BUY if upward potential >= downward, else SELL) + if (pred_high - current_open) >= (current_open - pred_low): action = 2 - elif (current_open - pred_low) > (pred_high - current_open) and (current_open - pred_low) > args.threshold: - action = 0 else: - action = 1 + action = 0 _, _, _, done, _, _ = env.step(action) if done: print("Reached end of simulation window; resetting environment.") diff --git a/crypto/brian/live_chart.html b/crypto/brian/live_chart.html index da26419..8986a39 100644 --- a/crypto/brian/live_chart.html +++ b/crypto/brian/live_chart.html @@ -3,7 +3,7 @@ - + Live Trading Chart - Epoch 100