UNPKG

candlestick-chart-generator

Version:

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

132 lines (116 loc) • 5.81 kB
const CandlestickChartGenerator = require('../index'); /** * 4-Hour Timeframe Chart Generator Example * * This example demonstrates generating charts with the new 4-hour timeframe support. * The 4h interval is created by aggregating 1-hour data from Yahoo Finance. */ async function generate4HourTimeframeCharts() { const generator = new CandlestickChartGenerator({ headless: true }); try { console.log('=== 4-Hour Timeframe Chart Generator ===\n'); console.log('šŸš€ Generating charts with 4-hour timeframe support...\n'); // Configuration for different assets with 4h timeframe const assets = [ { symbol: 'BTC-USD', name: 'Bitcoin', startDate: new Date(Date.now() - 30 * 24 * 60 * 60 * 1000), // Last 30 days endDate: new Date(), outputPath: 'examples/btc_4h_chart.png', description: 'Bitcoin 4-hour chart - Perfect for swing trading' }, { symbol: 'AAPL', name: 'Apple Inc.', startDate: new Date(Date.now() - 14 * 24 * 60 * 60 * 1000), // Last 14 days endDate: new Date(), outputPath: 'examples/aapl_4h_chart.png', description: 'Apple 4-hour chart - Intraday trend analysis' }, { symbol: 'EURUSD=X', name: 'EUR/USD', startDate: new Date(Date.now() - 7 * 24 * 60 * 60 * 1000), // Last 7 days endDate: new Date(), outputPath: 'examples/eurusd_4h_chart.png', description: 'EUR/USD 4-hour chart - Forex swing trading' } ]; // Custom chart styling optimized for 4-hour analysis with enhanced features const chartOptions = { backgroundColor: '#0a0a0a', // Dark background textColor: '#ffffff' }; // Generate charts for each asset console.log('šŸ“Š Generating 4-hour 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(` Period: ${asset.startDate.toDateString()} → ${asset.endDate.toDateString()}`); try { await generator.generateChartScreenshot({ symbol: asset.symbol, interval: '4h', // Using the new 4h timeframe startDate: asset.startDate, endDate: asset.endDate, outputPath: asset.outputPath, width: 1400, height: 800, chartOptions: chartOptions }); console.log(` āœ… Chart saved: ${asset.outputPath}`); } catch (error) { console.log(` āŒ Error generating ${asset.name} chart: ${error.message}`); } console.log(''); // Empty line for spacing } // Generate a comparison chart showing different timeframes console.log('4. Generating timeframe comparison chart...'); try { const comparisonData = await generator.generateChartBase64({ symbol: 'BTC-USD', interval: '4h', startDate: new Date(Date.now() - 7 * 24 * 60 * 60 * 1000), // Last 7 days endDate: new Date(), width: 1200, height: 600, chartOptions: { backgroundColor: '#1a1a1a' } }); console.log(` āœ… Comparison chart generated (${comparisonData.length} characters)`); console.log(' šŸ’” This shows how 4h timeframe provides optimal detail for swing trading\n'); } catch (error) { console.log(` āŒ Error generating comparison chart: ${error.message}\n`); } // Display summary and trading insights console.log('šŸŽ‰ 4-Hour Timeframe Analysis Complete!\n'); console.log('šŸ“ Generated Files:'); assets.forEach((asset, index) => { console.log(` ${index + 1}. ${asset.outputPath} (${asset.name})`); }); console.log('\nšŸ“ˆ 4-Hour Timeframe Trading Insights:'); console.log(' • Perfect for swing trading strategies'); console.log(' • Reduces market noise compared to 1h charts'); console.log(' • Provides better trend visibility than daily charts'); console.log(' • Ideal for crypto and forex markets'); console.log(' • Great for identifying key support/resistance levels'); console.log(' • Enhanced with volume analysis and buy/sell signals'); console.log('\nšŸ’” Pro Tips for 4H Trading:'); console.log(' • Use 4h charts to identify major trend direction'); console.log(' • Combine with 1h charts for precise entry timing'); console.log(' • 4h candles show significant price action'); console.log(' • Perfect for traders who can\'t monitor markets constantly'); console.log(' • Excellent for weekend gap analysis'); } 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 generate4HourTimeframeCharts();