From 907a7d6224cf479f7df319cd0ee2056a7e74beef Mon Sep 17 00:00:00 2001 From: Dobromir Popov Date: Tue, 4 Nov 2025 13:30:39 +0200 Subject: [PATCH] adding current position PnL to the T model --- NN/models/advanced_transformer_trading.py | 32 +++++++++++++++-------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/NN/models/advanced_transformer_trading.py b/NN/models/advanced_transformer_trading.py index ca88e94..96e0439 100644 --- a/NN/models/advanced_transformer_trading.py +++ b/NN/models/advanced_transformer_trading.py @@ -355,6 +355,18 @@ class AdvancedTradingTransformer(nn.Module): self.tech_projection = nn.Linear(config.tech_features, config.d_model) self.market_projection = nn.Linear(config.market_features, config.d_model) + # Position state projection - properly learns to embed position info + # Input: [has_position, pnl, size, entry_price_norm, time_in_position] = 5 features + self.position_projection = nn.Sequential( + nn.Linear(5, config.d_model // 4), # 5 -> 256 + nn.GELU(), + nn.Dropout(config.dropout), + nn.Linear(config.d_model // 4, config.d_model // 2), # 256 -> 512 + nn.GELU(), + nn.Dropout(config.dropout), + nn.Linear(config.d_model // 2, config.d_model) # 512 -> 1024 + ) + # Positional encoding if config.use_relative_position: self.pos_encoding = RelativePositionalEncoding(config.d_model) @@ -516,19 +528,17 @@ class AdvancedTradingTransformer(nn.Module): # Add position state if provided - critical for loss minimization and profit taking if position_state is not None: - # Project position state to model dimension and add to all sequence positions - # This allows the model to condition all predictions on current position state - position_emb = torch.tanh(position_state) # Normalize to [-1, 1] - position_emb = position_emb.unsqueeze(1).expand(batch_size, seq_len, -1) # (batch, seq_len, 5) + # Project position state through learned embedding network + # Input: [batch, 5] -> Output: [batch, d_model] + position_emb = self.position_projection(position_state) # [batch, d_model] - # Pad to match model dimension if needed - if position_emb.size(-1) < self.config.d_model: - padding = torch.zeros(batch_size, seq_len, self.config.d_model - position_emb.size(-1), - device=position_emb.device, dtype=position_emb.dtype) - position_emb = torch.cat([position_emb, padding], dim=-1) + # Expand to sequence length and add as bias to all positions + # This conditions the entire sequence on current position state + position_emb = position_emb.unsqueeze(1).expand(batch_size, seq_len, -1) # [batch, seq_len, d_model] - # Add position state as a bias to the embeddings - x = x + position_emb[:, :, :self.config.d_model] + # Add position embedding to the combined embeddings + # This allows the model to modulate its predictions based on position state + x = x + position_emb # Add positional encoding if isinstance(self.pos_encoding, RelativePositionalEncoding):