UNPKG

candlestick-chart-generator

Version:

A Node.js library for generating candlestick chart screenshots using financial data

155 lines (136 loc) • 6.61 kB
const CandlestickChartGenerator = require('../index'); /** * Enhanced Chart Features Example * * This example demonstrates all the enhanced chart features including: * - Volume bars below the main chart * - Current price and time display * - OHLC values for the latest candle * - Buy/Sell signals based on moving average crossover * - Formatted volume display */ async function generateEnhancedChartExamples() { const generator = new CandlestickChartGenerator({ headless: true }); try { console.log('=== Enhanced Chart Features Demo ===\n'); console.log('šŸš€ Generating charts with advanced features...\n'); // Configuration for different assets with enhanced features const assets = [ { symbol: 'BTC-USD', name: 'Bitcoin', interval: '4h', startDate: new Date(Date.now() - 14 * 24 * 60 * 60 * 1000), // Last 14 days endDate: new Date(), outputPath: 'examples/btc_enhanced_4h.png', description: 'Bitcoin with volume analysis and trading signals' }, { symbol: 'AAPL', name: 'Apple Inc.', interval: '1d', startDate: new Date(Date.now() - 180 * 24 * 60 * 60 * 1000), // Last 30 days endDate: new Date(), outputPath: 'examples/aapl_enhanced_daily.png', description: 'Apple daily chart with enhanced features' }, { symbol: 'TSLA', name: 'Tesla', interval: '1h', startDate: new Date(Date.now() - 7 * 24 * 60 * 60 * 1000), // Last 7 days endDate: new Date(), outputPath: 'examples/tsla_enhanced_hourly.png', description: 'Tesla hourly chart with volume and signals' } ]; // Enhanced chart styling const chartOptions = { backgroundColor: '#0a0a0a', // Dark background textColor: '#ffffff' }; // Generate enhanced charts for each asset console.log('šŸ“Š Generating enhanced charts...\n'); for (let i = 0; i < assets.length; i++) { const asset = assets[i]; console.log(`${i + 1}. ${asset.name} (${asset.symbol})`); console.log(` ${asset.description}`); console.log(` Timeframe: ${asset.interval}`); console.log(` Period: ${asset.startDate.toDateString()} → ${asset.endDate.toDateString()}`); try { await generator.generateChartScreenshot({ symbol: asset.symbol, interval: asset.interval, startDate: asset.startDate, endDate: asset.endDate, outputPath: asset.outputPath, width: 1600, height: 900, chartOptions: chartOptions }); console.log(` āœ… Enhanced chart saved: ${asset.outputPath}`); } catch (error) { console.log(` āŒ Error generating ${asset.name} chart: ${error.message}`); } console.log(''); // Empty line for spacing } // Generate a comprehensive analysis chart console.log('4. Generating comprehensive analysis chart...'); try { const analysisData = await generator.generateChartBase64({ symbol: 'ETH-USD', interval: '4h', startDate: new Date(Date.now() - 10 * 24 * 60 * 60 * 1000), // Last 10 days endDate: new Date(), width: 1400, height: 800, chartOptions: { backgroundColor: '#1a1a1a' } }); console.log(` āœ… Analysis chart generated (${analysisData.length} characters)`); console.log(' šŸ’” This chart includes all enhanced features for comprehensive analysis\n'); } catch (error) { console.log(` āŒ Error generating analysis chart: ${error.message}\n`); } // Display feature summary console.log('šŸŽ‰ Enhanced Chart Features Demo Complete!\n'); console.log('šŸ“ Generated Files:'); assets.forEach((asset, index) => { console.log(` ${index + 1}. ${asset.outputPath} (${asset.name})`); }); console.log('\n✨ Enhanced Features Included:'); console.log(' šŸ“Š Volume Analysis:'); console.log(' • Volume bars below main chart'); console.log(' • Color-coded volume (green/red based on price movement)'); console.log(' • Formatted volume display (K, M, B)'); console.log(' • Volume scale indicators'); console.log('\n šŸ’° Price Information:'); console.log(' • Current price display'); console.log(' • OHLC values for latest candle'); console.log(' • Real-time price updates'); console.log('\n ā° Time Information:'); console.log(' • Last update timestamp'); console.log(' • Formatted date/time display'); console.log(' • Time zone awareness'); console.log('\n šŸ“ˆ Trading Signals:'); console.log(' • Buy signals (🟢) based on MA crossover'); console.log(' • Sell signals (šŸ”“) based on MA crossover'); console.log(' • 10-period vs 20-period moving average'); console.log(' • Signal strength indicators'); console.log('\nšŸ’” Trading Benefits:'); console.log(' • Volume confirms price movements'); console.log(' • OHLC data for precise entry/exit points'); console.log(' • Automated signal generation'); console.log(' • Comprehensive market analysis'); console.log(' • Professional chart appearance'); } catch (error) { console.error('āŒ Fatal Error:', error.message); console.error('Stack trace:', error.stack); } finally { // Always close the generator to free resources await generator.close(); } } // Run the example generateEnhancedChartExamples();