A TypeScript implementation of a neural network for cryptocurrency price prediction using real-time market data from Binance. The system uses a 2-2-1 neural network architecture with sigmoid activation functions to predict future price movements based on historical OHLCV data.
- 📊 Real Market Data: Live BTC/USDT data from Binance exchange via CCXT
- 🧠 Neural Network: 2-2-1 architecture with sigmoid activation and backpropagation
- 🔮 Price Prediction: Predicts next price based on historical close prices
- 📈 Confidence Scoring: Calculates prediction confidence based on recent accuracy
- ⚡ TypeScript: Fully typed implementation with strict TypeScript configuration
# Install dependencies
npm install
# Development mode
npm run dev
# Build and run
npm run build
npm startimport { MarketPredictor, MarketDataProvider } from '@/core'
// Configure neural network
const config = {
epochs: 60,
learningRate: 0.1,
useSimpleBackprop: false
}
// Create predictor and data provider
const predictor = new MarketPredictor(config)
const dataProvider = new MarketDataProvider()
// Fetch real market data
const marketData = await dataProvider.fetch4H('BTC/USDT', 100)
// Add data and train
marketData.forEach(data => predictor.addMarketData(data))
const lossCurve = predictor.train()
// Make prediction
const prediction = predictor.predict()
console.log(`Predicted: $${prediction.predictedOutput}`)
console.log(`Confidence: ${prediction.confidence * 100}%`)- 📥 Input Layer: 2 nodes (previous close prices)
- 🔗 Hidden Layer: 2 nodes with sigmoid activation
- 📤 Output Layer: 1 node (predicted price)
- 🎯 Training: Backpropagation with MSE loss
- 📡 MarketDataProvider: Fetches real-time data from Binance
- 🎯 MarketPredictor: High-level prediction interface
- 🧠 NeuralNetwork: Core neural network implementation
- 🏦 Exchange: Binance (futures)
- 💰 Symbol: BTC/USDT
- ⏰ Timeframes: 1H, 4H, 1D
- 📊 Data: OHLCV (Open, High, Low, Close, Volume)
# Code quality
npm run lint
npm run format
# Type checking
npm run build
# Testing
npm testsrc/
├── core/
│ ├── MarketDataProvider.ts # Real-time data fetching
│ ├── MarketPredictor.ts # High-level prediction interface
│ └── NeuralNetwork.ts # Core neural network
├── types/
│ └── index.ts # TypeScript type definitions
└── index.ts # Main entry point
The neural network implements the following logic:
- Data Normalization: Scale prices to [0,1] range using max price scaling
- Feature Engineering: Use previous two close prices as input features
- Training: Backpropagation with gradient descent and MSE loss function
- Prediction: Sigmoid activation for price forecasting with denormalization
- Confidence: Error-based confidence calculation using recent prediction accuracy
- Weight Initialization: Seeded random weights for reproducible results
- Training Time: ~60 epochs per prediction with configurable learning rate
- Data Points: 100-500 historical OHLCV prices from Binance
- Accuracy: Varies based on market volatility and trend conditions
- Confidence: 0-100% based on recent prediction accuracy
- Memory Usage: Efficient with minimal memory footprint
- Network State: Saveable/loadable neural network weights and configuration
interface NeuralNetworkConfig {
epochs: number // Training iterations (10-1000)
learningRate: number // Gradient descent rate (0.00001-1.0)
useSimpleBackprop: boolean // Backpropagation algorithm
}MIT License - see LICENSE file for details