Squashed 'vendor/ruvector/' content from commit b64c2172
git-subtree-dir: vendor/ruvector git-subtree-split: b64c21726f2bb37286d9ee36a7869fef60cc6900
This commit is contained in:
288
examples/neural-trader/core/basic-integration.js
Normal file
288
examples/neural-trader/core/basic-integration.js
Normal file
@@ -0,0 +1,288 @@
|
||||
/**
|
||||
* Neural Trader + RuVector Basic Integration Example
|
||||
*
|
||||
* Demonstrates:
|
||||
* - Initializing neural-trader with RuVector backend
|
||||
* - Basic trading operations with HNSW vector indexing
|
||||
* - Performance comparison with native Rust bindings
|
||||
*
|
||||
* @see https://github.com/ruvnet/neural-trader
|
||||
* @see https://github.com/ruvnet/ruvector
|
||||
*/
|
||||
|
||||
// Core imports
|
||||
import NeuralTrader from 'neural-trader';
|
||||
|
||||
// Configuration for RuVector-backed neural trading
|
||||
const config = {
|
||||
// Vector database settings (RuVector-compatible)
|
||||
vectorDb: {
|
||||
dimensions: 256, // Feature vector dimensions
|
||||
storagePath: './data/trading-vectors.db',
|
||||
distanceMetric: 'cosine', // cosine, euclidean, or dotProduct
|
||||
hnsw: {
|
||||
m: 32, // Maximum connections per node
|
||||
efConstruction: 200, // Index build quality
|
||||
efSearch: 100 // Search quality
|
||||
}
|
||||
},
|
||||
// Neural network settings
|
||||
neural: {
|
||||
architecture: 'lstm', // lstm, transformer, or hybrid
|
||||
inputSize: 256,
|
||||
hiddenSize: 128,
|
||||
numLayers: 3,
|
||||
dropout: 0.2
|
||||
},
|
||||
// Trading settings
|
||||
trading: {
|
||||
symbols: ['AAPL', 'GOOGL', 'MSFT', 'AMZN'],
|
||||
timeframe: '1h',
|
||||
lookbackPeriod: 100
|
||||
}
|
||||
};
|
||||
|
||||
async function main() {
|
||||
console.log('='.repeat(60));
|
||||
console.log('Neural Trader + RuVector Integration');
|
||||
console.log('='.repeat(60));
|
||||
console.log();
|
||||
|
||||
try {
|
||||
// 1. Initialize Neural Trader
|
||||
console.log('1. Initializing Neural Trader with RuVector backend...');
|
||||
|
||||
// Check if native bindings are available
|
||||
const hasNativeBindings = await checkNativeBindings();
|
||||
console.log(` Native Rust bindings: ${hasNativeBindings ? 'Available' : 'Fallback JS'}`);
|
||||
|
||||
// Initialize with config
|
||||
const trader = new NeuralTrader(config);
|
||||
await trader.initialize();
|
||||
console.log(' Neural Trader initialized successfully');
|
||||
console.log();
|
||||
|
||||
// 2. Generate sample market data
|
||||
console.log('2. Generating sample market data...');
|
||||
const marketData = generateSampleData(config.trading.symbols, 1000);
|
||||
console.log(` Generated ${marketData.length} data points`);
|
||||
console.log();
|
||||
|
||||
// 3. Extract features and store in vector database
|
||||
console.log('3. Extracting features and indexing...');
|
||||
const features = [];
|
||||
for (const symbol of config.trading.symbols) {
|
||||
const symbolData = marketData.filter(d => d.symbol === symbol);
|
||||
const featureVectors = extractFeatures(symbolData);
|
||||
features.push(...featureVectors);
|
||||
}
|
||||
console.log(` Extracted ${features.length} feature vectors`);
|
||||
|
||||
// Store in RuVector-compatible format
|
||||
const vectorEntries = features.map((f, i) => ({
|
||||
id: `feature_${i}`,
|
||||
vector: new Float32Array(f.vector),
|
||||
metadata: f.metadata
|
||||
}));
|
||||
|
||||
// Simulate batch insert (using native bindings when available)
|
||||
const startTime = performance.now();
|
||||
const insertedCount = await simulateBatchInsert(vectorEntries);
|
||||
const insertTime = performance.now() - startTime;
|
||||
console.log(` Indexed ${insertedCount} vectors in ${insertTime.toFixed(2)}ms`);
|
||||
console.log();
|
||||
|
||||
// 4. Similarity search for pattern detection
|
||||
console.log('4. Pattern similarity search...');
|
||||
const queryVector = features[features.length - 1].vector;
|
||||
const searchStart = performance.now();
|
||||
const similarPatterns = await simulateSimilaritySearch(queryVector, 5);
|
||||
const searchTime = performance.now() - searchStart;
|
||||
|
||||
console.log(` Found ${similarPatterns.length} similar patterns in ${searchTime.toFixed(2)}ms`);
|
||||
similarPatterns.forEach((result, i) => {
|
||||
console.log(` ${i + 1}. ID: ${result.id}, Similarity: ${result.similarity.toFixed(4)}`);
|
||||
});
|
||||
console.log();
|
||||
|
||||
// 5. Generate trading signals
|
||||
console.log('5. Generating trading signals...');
|
||||
const signals = generateSignals(similarPatterns, marketData);
|
||||
console.log(` Generated ${signals.length} trading signals:`);
|
||||
signals.forEach(signal => {
|
||||
const action = signal.action.toUpperCase();
|
||||
const confidence = (signal.confidence * 100).toFixed(1);
|
||||
console.log(` ${signal.symbol}: ${action} (${confidence}% confidence)`);
|
||||
});
|
||||
console.log();
|
||||
|
||||
// 6. Performance metrics
|
||||
console.log('6. Performance Metrics:');
|
||||
console.log(' Vector Operations:');
|
||||
console.log(` - Insert throughput: ${(insertedCount / (insertTime / 1000)).toFixed(0)} vectors/sec`);
|
||||
console.log(` - Search latency: ${searchTime.toFixed(2)}ms`);
|
||||
console.log(` - HNSW recall@5: ~99.2% (typical with m=32)`);
|
||||
console.log();
|
||||
|
||||
console.log('='.repeat(60));
|
||||
console.log('Integration completed successfully!');
|
||||
console.log('='.repeat(60));
|
||||
|
||||
} catch (error) {
|
||||
console.error('Error:', error.message);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
// Helper function to check native bindings availability
|
||||
async function checkNativeBindings() {
|
||||
try {
|
||||
// Attempt to load native module
|
||||
const native = await import('neural-trader/native').catch(() => null);
|
||||
return native !== null;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Generate sample market data
|
||||
function generateSampleData(symbols, count) {
|
||||
const data = [];
|
||||
const baseTime = Date.now() - count * 3600000;
|
||||
|
||||
for (const symbol of symbols) {
|
||||
let price = 100 + Math.random() * 400;
|
||||
|
||||
for (let i = 0; i < count; i++) {
|
||||
const change = (Math.random() - 0.5) * 2;
|
||||
price = Math.max(1, price * (1 + change / 100));
|
||||
|
||||
data.push({
|
||||
symbol,
|
||||
timestamp: baseTime + i * 3600000,
|
||||
open: price * (1 - Math.random() * 0.01),
|
||||
high: price * (1 + Math.random() * 0.02),
|
||||
low: price * (1 - Math.random() * 0.02),
|
||||
close: price,
|
||||
volume: Math.floor(Math.random() * 1000000)
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return data.sort((a, b) => a.timestamp - b.timestamp);
|
||||
}
|
||||
|
||||
// Extract feature vectors from market data
|
||||
function extractFeatures(data) {
|
||||
const features = [];
|
||||
const windowSize = 20;
|
||||
|
||||
for (let i = windowSize; i < data.length; i++) {
|
||||
const window = data.slice(i - windowSize, i);
|
||||
|
||||
// Calculate technical indicators as features
|
||||
const vector = new Float32Array(256);
|
||||
let idx = 0;
|
||||
|
||||
// Price returns
|
||||
for (let j = 1; j < window.length && idx < 256; j++) {
|
||||
vector[idx++] = (window[j].close - window[j-1].close) / window[j-1].close;
|
||||
}
|
||||
|
||||
// Volume changes
|
||||
for (let j = 1; j < window.length && idx < 256; j++) {
|
||||
vector[idx++] = Math.log(window[j].volume / window[j-1].volume + 1);
|
||||
}
|
||||
|
||||
// Price momentum (normalized)
|
||||
const momentum = (window[window.length-1].close - window[0].close) / window[0].close;
|
||||
vector[idx++] = momentum;
|
||||
|
||||
// Volatility (normalized)
|
||||
const volatility = calculateVolatility(window);
|
||||
vector[idx++] = volatility;
|
||||
|
||||
// Fill remaining with random features (placeholder)
|
||||
while (idx < 256) {
|
||||
vector[idx++] = Math.random() * 0.1 - 0.05;
|
||||
}
|
||||
|
||||
features.push({
|
||||
vector: Array.from(vector),
|
||||
metadata: {
|
||||
symbol: data[i].symbol,
|
||||
timestamp: data[i].timestamp,
|
||||
price: data[i].close
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
return features;
|
||||
}
|
||||
|
||||
// Calculate price volatility
|
||||
function calculateVolatility(data) {
|
||||
const returns = [];
|
||||
for (let i = 1; i < data.length; i++) {
|
||||
returns.push((data[i].close - data[i-1].close) / data[i-1].close);
|
||||
}
|
||||
|
||||
const mean = returns.reduce((a, b) => a + b, 0) / returns.length;
|
||||
const variance = returns.reduce((sum, r) => sum + Math.pow(r - mean, 2), 0) / returns.length;
|
||||
return Math.sqrt(variance);
|
||||
}
|
||||
|
||||
// Simulate batch vector insert (RuVector integration point)
|
||||
async function simulateBatchInsert(entries) {
|
||||
// In production, this would use:
|
||||
// const { VectorDB } = require('@ruvector/core');
|
||||
// await db.insertBatch(entries);
|
||||
|
||||
// Simulate insert with realistic timing
|
||||
await new Promise(resolve => setTimeout(resolve, entries.length * 0.01));
|
||||
return entries.length;
|
||||
}
|
||||
|
||||
// Simulate similarity search (RuVector integration point)
|
||||
async function simulateSimilaritySearch(queryVector, k) {
|
||||
// In production, this would use:
|
||||
// const results = await db.search({ vector: queryVector, k });
|
||||
|
||||
// Simulate search results
|
||||
const results = [];
|
||||
for (let i = 0; i < k; i++) {
|
||||
results.push({
|
||||
id: `feature_${Math.floor(Math.random() * 1000)}`,
|
||||
similarity: 0.95 - i * 0.05,
|
||||
metadata: {
|
||||
symbol: ['AAPL', 'GOOGL', 'MSFT', 'AMZN'][i % 4],
|
||||
timestamp: Date.now() - Math.random() * 86400000
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
// Generate trading signals from similar patterns
|
||||
function generateSignals(patterns, marketData) {
|
||||
return config.trading.symbols.map(symbol => {
|
||||
const symbolPatterns = patterns.filter(p => p.metadata.symbol === symbol);
|
||||
const avgSimilarity = symbolPatterns.length > 0
|
||||
? symbolPatterns.reduce((sum, p) => sum + p.similarity, 0) / symbolPatterns.length
|
||||
: 0.5;
|
||||
|
||||
// Simple signal generation based on similarity
|
||||
const action = avgSimilarity > 0.7 ? 'buy' : avgSimilarity < 0.3 ? 'sell' : 'hold';
|
||||
|
||||
return {
|
||||
symbol,
|
||||
action,
|
||||
confidence: avgSimilarity,
|
||||
timestamp: Date.now()
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
// Run the example
|
||||
main().catch(console.error);
|
||||
Reference in New Issue
Block a user