This variant directly forecasts portfolio weights using multi‑modal inputs (news + OHLCV) and fuses the streams with Cross‑Gated Attention (CGA).
CGA lets each stream attend to the other via gates that modulate information flow, improving robustness over naive concatenation.
- Direct Portfolio Weight Forecasting
- Architecture Overview
- Training Objective
- Repository Layout
- Setup
- Script Cheat-Sheet
- Pipeline (Direct Weights)
- Dependencies
- Outputs
- Notes
- Appendix
- Authors & Citation
- Support
- Timeframe: 3‑minute bars
- Input Window: 80 timestamps (~4 hours)
- Prediction Horizon: next 80 timestamps (~4 hours)
- Assets: configurable universe
-
News stream (single LSTM)
- Each article → BigBird embedding
- Average embeddings of all articles per 3-min window
- If no news: use learned [NO_NEWS] embedding
- Coverage one-hot (which stocks are mentioned) is concatenated to the news embedding at each timestamp
- The sequence is fed to one LSTM → produces news sequence embedding
-
OHLCV stream (TimesNet)
- A TimesNetBlock processes per-asset OHLCV sequences → produces market embedding
- Let N be the news embedding and M the market (OHLCV) embedding
- Compute cross‑attention in both directions (N→M and M→N)
- Apply gates (sigmoid/tanh) to the attended features before adding residuals:
- Concatenate or sum
$\tilde{N}$ and$\tilde{M}$ to form the fused embedding
- A linear layer maps the fused embedding to portfolio weights for all assets
The model uses a top-k long/short portfolio construction and optimizes a risk-adjusted return loss with regularization.
Let:
-
$w \in \mathbb{R}^N$ be the portfolio weights computed from logits -
$R \in \mathbb{R}^{H \times N}$ be the returns matrix for a batch (H time steps, N assets) -
$k$ be the number of assets to select for active trading -
$\epsilon$ a small constant for numerical stability
Weights are computed as:
where the function topk_long_short_abs selects the top-k absolute logits and normalizes them.
Only the top-k largest absolute values of logits are selected, and the weights are normalized:
Portfolio returns:
Sharpe ratio:
Sharpe loss:
- Distribution regularizer (prevents concentration):
- Net exposure regularizer (encourages market-neutral portfolio):
- Turnover regularizer (optional, penalizes large changes in weights):
The total loss optimized:
- Only the turnover regularizer (
L_turnover) is applied if previous weightsw_prevare provided. -
$\lambda_{\text{div}}, \lambda_{\text{net}}, \lambda_{\text{turnover}}$ control the regularization strength.
NeuralFusionCore/
├── data/
│ ├── outputs/
│ │ └── model_weights.pt
│ └── processed/
│ └── show_files.py
│
├── lib/
│ ├── backtest.py
│ ├── backtest_weights.py
│ ├── dataset.py
│ ├── loss_weights.py
│ ├── model.py
│ ├── train.py
│ └── utils.py
├──_init__.py
├── README.md
├── requirements.txt
├── config.py
└── scripts/
├── train_service.py
├── finetune_service.py
├── prediction_service.py
├── backtesting_service.py
└── api_service.py
Any folders missing on your machine will be created by the scripts if needed.
# Clone repository
git clone https://github.com/Novoxpert/NeuralFusionCore.git
cd NeuralFusionCore
# (optional) create a virtual environment
python -m venv .venv
# Linux/macOS:
source .venv/bin/activate
# Windows (PowerShell):
.\.venv\Scripts\Activate.ps1
# install exact dependencies
pip install -r requirements.txtlib/*.py— internal modules for datasets, models,training loops, utilities, and backtesting specialized for direct weights.config.py— central configuration / argument helpers used by the scripts.scripts/train_service.py— Train from scratch on processed/train.parquet and processed/val.parquet Usage Example:
python -m scripts.train_service --epocha 50 scripts/finetune_service.py—Fine-tune an existing saved model using the latest features. If validation loss improves, replace saved model and keep previous version with timestamp.
Usage Example:
python -m scripts.finetune_service --epocha 10 --save_bestscripts/prediction_service.py—Scheduled inference: fetch latest data, compute features, infer model, transform logits into portfolio weights, and save predictions to MongoDB and Redis.
Usage Example:
python -m scripts.prediction_service --hours 4 scripts/backtesting_service.py—Backtesting & Model Evaluation Service for Market-News Fusion Model.
Usage Example:
python -m scripts.backtesting_service --epochs 50 --mode fetch --hours 12 scripts/api_service.py— create API for Get NeuralFusion weights from Mongodb.
- Python 3.12+
- PyTorch 2.x
- Hugging Face
transformers(BigBird)
-
Predicted weights per timestamp
-
Performance metrics:
- Sharpe ratio
- Cumulative P&L
- Max Drawdown
- Turnover
-
Plots: equity curve, rolling Sharpe, weights heatmap
- CGA allows directional, gated cross‑attention between news and market signals
- The distribution loss helps prevent one-asset collapse
- Total Loss combines Sharpe ratio maximization and regularizations
Influential upstream repositories:
- BigBird: A sparse-attention transformer model enabling efficient processing of longer sequences
- finBERT: A pre-trained NLP model fine-tuned for financial sentiment analysis
- Time-Series-Library (TSlib): Library providing deep learning-based time series analysis, covering forecasting, anomaly detection, and classification
This work is inspired by the article:
- Stock Movement Prediction with Multimodal Stable Fusion via Gated Cross-Attention Mechanism: Introduces the Multimodal Stable Fusion with Gated Cross-Attention (MSGCA) architecture, designed to robustly integrate multimodal inputs for stock movement prediction.
Developed by the Novoxpert Research Team
If you use this repository or build upon our work, please cite:
Novoxpert Research (2025). NeuralFusionCore: Direct Portfolio Weight Forecasting with Cross-Gated Attention Fusion.
GitHub: https://github.com/Novoxpert/NeuralFusionCore
@software{novoxpert_neuralfusioncore_2025,
author = {Elham Esmaeilnia and Hamidreza Naeini},
title = {NeuralFusionCore: Direct Portfolio Weight Forecasting with Cross-Gated Attention Fusion},
organization = {Novoxpert Research},
year = {2025},
url = {https://github.com/Novoxpert/NeuralFusionCore}
}- Issues & Bugs: Open on GitHub
- Discussions: GitHub Discussions
- Feature Requests: Open a feature request issue