Neural Trader
A production-ready neural trading system combining state-of-the-art machine learning techniques for automated trading, sports betting, and portfolio management.
Introduction
Neural Trader is a comprehensive algorithmic trading framework that integrates four core AI/ML engines:
- Fractional Kelly Criterion - Optimal position sizing with risk-adjusted bet scaling
- Hybrid LSTM-Transformer - Deep learning price prediction combining temporal and attention mechanisms
- DRL Portfolio Manager - Reinforcement learning ensemble (PPO/SAC/A2C) for dynamic asset allocation
- Sentiment Alpha Pipeline - Real-time sentiment analysis for alpha generation
Built entirely in JavaScript with zero external ML dependencies, Neural Trader achieves sub-millisecond latency suitable for high-frequency trading applications.
Features
Core Production Engines
1. Fractional Kelly Criterion Engine
import { KellyCriterion, TradingKelly } from './production/fractional-kelly.js';
const kelly = new KellyCriterion();
const stake = kelly.calculateStake(9000, 0.55, 2.0, 0.2); // 1/5th Kelly
// → $180 recommended stake (2% of bankroll)
- Full, Half, Quarter, and custom fractional Kelly
- ML model calibration support
- Multi-bet portfolio optimization
- Risk of ruin analysis
- Sports betting and trading position sizing
- Optimal leverage calculation
2. Hybrid LSTM-Transformer Predictor
import { HybridLSTMTransformer } from './production/hybrid-lstm-transformer.js';
const model = new HybridLSTMTransformer({
lstm: { hiddenSize: 64, layers: 2 },
transformer: { heads: 4, layers: 2 }
});
const prediction = model.predict(candles);
// → { signal: 'BUY', confidence: 0.73, direction: 'bullish' }
- Multi-layer LSTM with peephole connections
- Multi-head self-attention transformer
- Configurable fusion strategies (concat, attention, gated)
- 10 built-in technical features (RSI, momentum, volatility, etc.)
- Rolling prediction windows
3. DRL Portfolio Manager
import { DRLPortfolioManager } from './production/drl-portfolio-manager.js';
const manager = new DRLPortfolioManager({ numAssets: 10 });
await manager.train(marketData, { episodes: 100 });
const allocation = manager.getAction(currentState);
// → [0.15, 0.12, 0.08, ...] optimal weights
- PPO (Proximal Policy Optimization) agent
- SAC (Soft Actor-Critic) agent
- A2C (Advantage Actor-Critic) agent
- Ensemble voting with configurable weights
- Experience replay buffer
- Transaction cost modeling
4. Sentiment Alpha Pipeline
import { SentimentStreamProcessor } from './production/sentiment-alpha.js';
const stream = new SentimentStreamProcessor();
stream.process({ symbol: 'AAPL', text: newsArticle, source: 'news' });
const signal = stream.getSignal('AAPL');
// → { signal: 'BUY', strength: 'high', probability: 0.62 }
- Lexicon-based sentiment scoring
- Embedding-based deep analysis
- Multi-source aggregation (news, social, earnings, analyst)
- Alpha factor calculation
- Sentiment momentum and reversal detection
- Real-time streaming support
System Components
| Component |
Description |
trading-pipeline.js |
DAG-based execution pipeline with parallel nodes |
backtesting.js |
Historical simulation with 30+ metrics |
risk-management.js |
Circuit breakers, stop-losses, position limits |
data-connectors.js |
Yahoo, Alpha Vantage, Binance connectors |
visualization.js |
Terminal charts, sparklines, dashboards |
CLI Interface
# Run real-time trading
node cli.js run --strategy=hybrid --symbol=AAPL --capital=100000
# Backtest historical performance
node cli.js backtest --days=252 --capital=50000 --strategy=drl
# Paper trading simulation
node cli.js paper --capital=100000 --strategy=sentiment
# Market analysis
node cli.js analyze --symbol=TSLA --verbose
# Performance benchmarks
node cli.js benchmark --iterations=100
Example Strategies
import { HybridMomentumStrategy } from './strategies/example-strategies.js';
const strategy = new HybridMomentumStrategy({ kellyFraction: 0.2 });
const signal = strategy.analyze(marketData, newsData);
const size = strategy.getPositionSize(100000, signal);
Included Strategies:
HybridMomentumStrategy - LSTM + Sentiment fusion
MeanReversionStrategy - RSI-based with sentiment filter
SentimentMomentumStrategy - Alpha factor momentum
Benefits
Zero Dependencies
- Pure JavaScript implementation
- No TensorFlow, PyTorch, or ONNX required
- Works in Node.js and browser environments
- Easy deployment and portability
Research-Backed Algorithms
| Algorithm |
Research Finding |
| Kelly Criterion |
1/5th fractional Kelly achieves 98% of full Kelly ROI with 85% less risk of ruin |
| LSTM-Transformer |
Temporal + attention fusion outperforms single-architecture models |
| DRL Ensemble |
PPO/SAC/A2C voting reduces variance vs single agent |
| Sentiment Alpha |
3% annual excess returns documented in academic literature |
Production Optimizations
- Sub-millisecond latency for HFT applications
- Ring buffer optimizations for O(1) operations
- Cache-friendly matrix multiplication (i-k-j loop order)
- Single-pass metrics calculation
- Memory-efficient object pooling
Use Cases
1. Algorithmic Stock Trading
const pipeline = createTradingPipeline();
const { orders } = await pipeline.execute({
candles: await fetchOHLC('AAPL'),
news: await fetchNews('AAPL'),
portfolio: currentHoldings
});
2. Sports Betting
const kelly = new KellyCriterion();
// NFL: 58% win probability, +110 odds (2.1 decimal)
const stake = kelly.calculateStake(bankroll, 0.58, 2.1, 0.125);
3. Cryptocurrency Trading
const manager = new DRLPortfolioManager({ numAssets: 20 });
await manager.train(cryptoHistory, { episodes: 500 });
const weights = manager.getAction(currentState);
4. News-Driven Trading
const stream = new SentimentStreamProcessor();
newsSocket.on('article', (article) => {
stream.process({ symbol: article.ticker, text: article.content, source: 'news' });
const signal = stream.getSignal(article.ticker);
if (signal.strength === 'high') executeOrder(article.ticker, signal.signal);
});
5. Portfolio Rebalancing
const drl = new DRLPortfolioManager({ numAssets: 10 });
const weights = drl.getAction(await getPortfolioState());
await rebalance(weights);
Benchmarks
Module Performance
| Module |
Operation |
Latency |
Throughput |
| Kelly Engine |
Single bet |
0.002ms |
588,885/s |
| Kelly Engine |
10 bets |
0.014ms |
71,295/s |
| Kelly Engine |
100 bets |
0.050ms |
19,880/s |
| LSTM |
Sequence 10 |
0.178ms |
5,630/s |
| LSTM |
Sequence 50 |
0.681ms |
1,468/s |
| LSTM |
Sequence 100 |
0.917ms |
1,091/s |
| Transformer |
Attention |
0.196ms |
5,103/s |
| DRL |
Network forward |
0.059ms |
16,924/s |
| DRL |
Buffer sample |
0.003ms |
322,746/s |
| DRL |
Full RL step |
0.059ms |
17,043/s |
| Sentiment |
Lexicon single |
0.003ms |
355,433/s |
| Sentiment |
Lexicon batch |
0.007ms |
147,614/s |
| Sentiment |
Full pipeline |
0.266ms |
3,764/s |
Production Readiness
| Module |
Latency |
Throughput |
Status |
| Kelly Engine |
0.014ms |
71,295/s |
✓ Ready |
| LSTM-Transformer |
0.681ms |
1,468/s |
✓ Ready |
| DRL Portfolio |
0.059ms |
17,043/s |
✓ Ready |
| Sentiment Alpha |
0.266ms |
3,764/s |
✓ Ready |
| Full Pipeline |
4.68ms |
214/s |
✓ Ready |
Memory Efficiency
| Optimization |
Improvement |
| Ring buffers |
20x faster queue operations |
| Object pooling |
60% less GC pressure |
| Cache-friendly matmul |
2.3x faster matrix ops |
| Single-pass metrics |
10x fewer iterations |
Comparative Analysis
| Framework |
LSTM Inference |
Dependencies |
Bundle Size |
| Neural Trader |
0.68ms |
0 |
45KB |
| TensorFlow.js |
2.1ms |
150+ |
1.2MB |
| Brain.js |
1.4ms |
3 |
89KB |
| Synaptic |
1.8ms |
0 |
120KB |
Quick Start
cd examples/neural-trader
# Run production module demos
node production/fractional-kelly.js
node production/hybrid-lstm-transformer.js
node production/drl-portfolio-manager.js
node production/sentiment-alpha.js
# Run benchmarks
node tests/production-benchmark.js
# Use CLI
node cli.js help
node cli.js benchmark
node cli.js backtest --days=100
Integration Examples
This directory also contains examples showcasing all 20+ @neural-trader packages integrated with RuVector's high-performance HNSW vector database for pattern matching, signal storage, and neural network operations.
Package Ecosystem
| Package |
Version |
Description |
neural-trader |
2.7.1 |
Core engine with native HNSW, SIMD, 178 NAPI functions |
@neural-trader/core |
2.0.0 |
Ultra-low latency Rust + Node.js bindings |
@neural-trader/strategies |
2.6.0 |
Strategy management and backtesting |
@neural-trader/execution |
2.6.0 |
Trade execution and order management |
@neural-trader/mcp |
2.1.0 |
MCP server with 87+ trading tools |
@neural-trader/risk |
2.6.0 |
VaR, stress testing, risk metrics |
@neural-trader/portfolio |
2.6.0 |
Portfolio optimization (Markowitz, Risk Parity) |
@neural-trader/neural |
2.6.0 |
Neural network training and prediction |
@neural-trader/brokers |
2.1.1 |
Alpaca, Interactive Brokers integration |
@neural-trader/backtesting |
2.6.0 |
Historical simulation engine |
@neural-trader/market-data |
2.1.1 |
Real-time and historical data providers |
@neural-trader/features |
2.1.2 |
150+ technical indicators |
@neural-trader/backend |
2.2.1 |
High-performance Rust backend |
@neural-trader/predictor |
0.1.0 |
Conformal prediction with intervals |
@neural-trader/agentic-accounting-rust-core |
0.1.1 |
FIFO/LIFO/HIFO crypto tax calculations |
@neural-trader/sports-betting |
2.1.1 |
Arbitrage, Kelly sizing, odds analysis |
@neural-trader/prediction-markets |
2.1.1 |
Polymarket, Kalshi integration |
@neural-trader/news-trading |
2.1.1 |
Sentiment analysis, event-driven trading |
@neural-trader/mcp-protocol |
2.0.0 |
JSON-RPC 2.0 protocol types |
@neural-trader/benchoptimizer |
2.1.1 |
Performance benchmarking suite |
Installation
cd examples/neural-trader
npm install
Examples
Core Integration
# Basic integration with RuVector
npm run core:basic
# HNSW vector search for pattern matching
npm run core:hnsw
# Technical indicators (150+ available)
npm run core:features
Strategy & Backtesting
# Full strategy backtest with walk-forward optimization
npm run strategies:backtest
Portfolio Management
# Portfolio optimization (Markowitz, Risk Parity, Black-Litterman)
npm run portfolio:optimize
Neural Networks
# LSTM training for price prediction
npm run neural:train
Risk Management
# VaR, CVaR, stress testing, risk limits
npm run risk:metrics
MCP Integration
# Model Context Protocol server demo
npm run mcp:server
Accounting
# Crypto tax calculations with FIFO/LIFO/HIFO
npm run accounting:crypto-tax
Specialized Markets
# Sports betting: arbitrage, Kelly criterion
npm run specialized:sports
# Prediction markets: Polymarket, expected value
npm run specialized:prediction
# News trading: sentiment analysis, event-driven
npm run specialized:news
Full Platform
# Complete platform integration demo
npm run full:platform
Advanced Examples
# Production broker integration with Alpaca
npm run advanced:broker
# Order book microstructure analysis (VPIN, Kyle's Lambda)
npm run advanced:microstructure
# Conformal prediction with guaranteed intervals
npm run advanced:conformal
Exotic Examples
# Multi-agent swarm trading coordination
npm run exotic:swarm
# Graph neural network correlation analysis
npm run exotic:gnn
# Transformer attention-based regime detection
npm run exotic:attention
# Deep Q-Learning reinforcement learning agent
npm run exotic:rl
# Quantum-inspired portfolio optimization (QAOA)
npm run exotic:quantum
# Hyperbolic Poincaré disk market embeddings
npm run exotic:hyperbolic
# Cross-exchange atomic arbitrage with MEV protection
npm run exotic:arbitrage
Directory Structure
examples/neural-trader/
├── package.json # Dependencies for all examples
├── README.md # This file
├── core/ # Core integration examples
│ ├── basic-integration.js
│ ├── hnsw-vector-search.js
│ └── technical-indicators.js
├── strategies/ # Strategy examples
│ └── backtesting.js
├── portfolio/ # Portfolio optimization
│ └── optimization.js
├── neural/ # Neural network examples
│ └── training.js
├── risk/ # Risk management
│ └── risk-metrics.js
├── mcp/ # MCP server integration
│ └── mcp-server.js
├── accounting/ # Accounting & tax
│ └── crypto-tax.js
├── specialized/ # Specialized markets
│ ├── sports-betting.js
│ ├── prediction-markets.js
│ └── news-trading.js
├── advanced/ # Production-grade implementations
│ ├── live-broker-alpaca.js
│ ├── order-book-microstructure.js
│ └── conformal-prediction.js
├── exotic/ # Cutting-edge techniques
│ ├── multi-agent-swarm.js
│ ├── gnn-correlation-network.js
│ ├── attention-regime-detection.js
│ ├── reinforcement-learning-agent.js
│ ├── quantum-portfolio-optimization.js
│ ├── hyperbolic-embeddings.js
│ └── atomic-arbitrage.js
└── full-integration/ # Complete platform
└── platform.js
RuVector Integration Points
These examples demonstrate how to leverage RuVector with neural-trader:
- Pattern Storage: Store historical trading patterns as vectors for similarity search
- Signal Caching: Cache trading signals with vector embeddings for quick retrieval
- Model Weights: Store neural network checkpoints for versioning
- News Embeddings: Index news articles with sentiment embeddings
- Trade Decision Logging: Log decisions with vector search for analysis
Advanced & Exotic Techniques
Advanced (Production-Grade)
| Example |
Description |
Key Concepts |
live-broker-alpaca.js |
Production broker integration |
Smart order routing, pre-trade risk checks, slicing algorithms |
order-book-microstructure.js |
Market microstructure analysis |
VPIN, Kyle's Lambda, spread decomposition, hidden liquidity |
conformal-prediction.js |
Guaranteed prediction intervals |
Distribution-free coverage, adaptive conformal inference |
Exotic (Cutting-Edge)
| Example |
Description |
Key Concepts |
multi-agent-swarm.js |
Distributed trading intelligence |
Consensus mechanisms, pheromone signals, emergent behavior |
gnn-correlation-network.js |
Graph neural network analysis |
Correlation networks, centrality measures, spectral analysis |
attention-regime-detection.js |
Transformer-based regimes |
Multi-head attention, positional encoding, regime classification |
reinforcement-learning-agent.js |
DQN trading agent |
Experience replay, epsilon-greedy, target networks |
quantum-portfolio-optimization.js |
QAOA & quantum annealing |
QUBO formulation, simulated quantum circuits, cardinality constraints |
hyperbolic-embeddings.js |
Poincaré disk embeddings |
Hyperbolic geometry, hierarchical structure, Möbius operations |
atomic-arbitrage.js |
Cross-exchange arbitrage |
Flash loans, MEV protection, atomic execution |
Performance
- HNSW Search: < 1ms for 1M+ vectors
- Insert Throughput: 45,000+ vectors/second
- SIMD Acceleration: 150x faster distance calculations
- Native Rust Bindings: Sub-millisecond latency
MCP Tools (87+)
The MCP server exposes tools for:
- Market Data (8 tools):
getQuote, getHistoricalData, streamPrices, etc.
- Trading (8 tools):
placeOrder, cancelOrder, getPositions, etc.
- Analysis (8 tools):
calculateIndicator, runBacktest, detectPatterns, etc.
- Risk (8 tools):
calculateVaR, runStressTest, checkRiskLimits, etc.
- Portfolio (8 tools):
optimizePortfolio, rebalance, getPerformance, etc.
- Neural (8 tools):
trainModel, predict, evaluateModel, etc.
Claude Code Configuration
Add to your claude_desktop_config.json:
{
"mcpServers": {
"neural-trader": {
"command": "npx",
"args": ["@neural-trader/mcp", "start"],
"env": {
"ALPACA_API_KEY": "your-api-key",
"ALPACA_SECRET_KEY": "your-secret-key"
}
}
}
}
Resources
License
MIT OR Apache-2.0