UNPKG

skayn-trading-sdk

Version:

Professional Bitcoin trading strategy backtesting framework with institutional-grade architecture

99 lines (84 loc) 2.86 kB
const IDataProvider = require('../src/interfaces/IDataProvider'); const BinanceDataProvider = require('../src/backtesting/providers/binance-data-provider'); const logger = require('../src/utils/logger'); /** * Real Data Provider for Examples * * PRODUCTION-READY: Uses only real market data from Binance * - No synthetic fallbacks * - Clear error messages for geoblocking * - Validates data quality * * US users: Enable VPN to access Binance API */ class RealDataProvider extends IDataProvider { constructor() { super(); this.binanceProvider = new BinanceDataProvider(); } /** * Get historical data - REAL DATA ONLY */ async getData(startDate, endDate, interval = '1d', symbol = 'BTCUSDT') { logger.info('📊 Fetching real market data from Binance...'); try { const data = await this.binanceProvider.getData(startDate, endDate, interval, symbol); if (!data || data.length === 0) { throw new Error('No data returned from Binance API'); } // Validate data quality this.validateData(data); logger.info('✅ Real market data loaded successfully', { bars: data.length, period: `${startDate} to ${endDate}`, firstPrice: `$${data[0].close}`, lastPrice: `$${data[data.length - 1].close}`, totalReturn: `${(((data[data.length - 1].close / data[0].close) - 1) * 100).toFixed(2)}%` }); return data; } catch (error) { logger.error('❌ Failed to fetch real market data', { error: error.message, symbol, interval, startDate, endDate, solution: 'For US users: Enable VPN to access Binance API' }); throw new Error(`Real data fetch failed: ${error.message}. US users need VPN.`); } } /** * Validate data quality */ validateData(data) { if (!Array.isArray(data) || data.length === 0) { throw new Error('Invalid data: empty or not array'); } for (let i = 0; i < Math.min(10, data.length); i++) { const bar = data[i]; if (!bar.open || !bar.high || !bar.low || !bar.close || !bar.volume) { throw new Error(`Invalid OHLCV data at index ${i}`); } if (bar.high < bar.low || bar.open < 0 || bar.close < 0) { throw new Error(`Malformed price data at index ${i}`); } } // Check for reasonable price ranges (Bitcoin should be > $1000) const firstPrice = data[0].close; if (firstPrice < 1000 || firstPrice > 1000000) { throw new Error(`Unrealistic Bitcoin price: $${firstPrice}`); } } /** * Get usage info */ getUsageInfo() { return { provider: 'RealDataProvider', mode: 'real', description: 'Using real market data from Binance' }; } } module.exports = RealDataProvider;