UNPKG

candlestick-chart-generator

Version:

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

232 lines (207 loc) • 8.87 kB
const CandlestickChartGenerator = require('../index'); /** * Bitcoin Multi-Timeframe Chart Generator * * This example demonstrates generating Bitcoin charts across multiple timeframes: * - 1H (1 hour) - Last 7 days of hourly data * - 1D (1 day) - Last 180 days of daily data * - 1W (1 week) - Last 2 years of weekly data */ async function generateBitcoinMultiTimeframeCharts() { const generator = new CandlestickChartGenerator({ headless: true }); try { console.log('=== Bitcoin Multi-Timeframe Chart Generator ===\n'); console.log('šŸš€ Generating comprehensive Bitcoin (BTC-USD) charts across multiple timeframes...\n'); // Configuration for different timeframes const timeframes = [ { label: '1H (Hourly)', interval: '1h', startDate: new Date(Date.now() - 7 * 24 * 60 * 60 * 1000), // Last 7 days endDate: new Date(), outputPath: 'examples/btc_1h_chart.png', description: 'Short-term price action - Last 7 days' }, { label: '1D (Daily)', interval: '1d', startDate: new Date(Date.now() - 180 * 24 * 60 * 60 * 1000), // Last 180 days endDate: new Date(), outputPath: 'examples/btc_1d_chart.png', description: 'Long-term trends - Last 6 months' }, { label: '1W (Weekly)', interval: '1wk', startDate: new Date(Date.now() - 2 * 365 * 24 * 60 * 60 * 1000), // Last 2 years endDate: new Date(), outputPath: 'examples/btc_1w_chart.png', description: 'Major trend analysis - Last 2 years' } ]; // Custom chart styling for Bitcoin const bitcoinChartOptions = { layout: { background: { color: '#0a0a0a' }, // Dark background textColor: '#ffffff' }, grid: { vertLines: { color: '#1a1a1a' }, horzLines: { color: '#1a1a1a' } }, timeScale: { borderColor: '#f7931a' // Bitcoin orange }, rightPriceScale: { borderColor: '#f7931a' } }; // Generate charts for each timeframe console.log('šŸ“Š Generating charts...\n'); for (let i = 0; i < timeframes.length; i++) { const timeframe = timeframes[i]; console.log(`${i + 1}. ${timeframe.label} Chart`); console.log(` ${timeframe.description}`); console.log(` Period: ${timeframe.startDate.toDateString()} → ${timeframe.endDate.toDateString()}`); try { await generator.generateChartScreenshot({ symbol: 'BTC-USD', interval: timeframe.interval, startDate: timeframe.startDate, endDate: timeframe.endDate, outputPath: timeframe.outputPath, width: 1400, height: 800, chartOptions: bitcoinChartOptions }); console.log(` āœ… Chart saved: ${timeframe.outputPath}`); } catch (error) { console.log(` āŒ Error generating ${timeframe.label} chart: ${error.message}`); } console.log(''); // Empty line for spacing } // Generate a combined summary chart as base64 for web applications console.log('5. Generating sample base64 chart for web integration...'); try { const base64Data = await generator.generateChartBase64({ symbol: 'BTC-USD', interval: '1d', startDate: new Date(Date.now() - 90 * 24 * 60 * 60 * 1000), // Last 90 days endDate: new Date(), width: 1000, height: 500, chartOptions: bitcoinChartOptions }); console.log(` āœ… Base64 chart generated (${base64Data.length} characters)`); console.log(' šŸ’” This data can be embedded directly in web applications\n'); } catch (error) { console.log(` āŒ Error generating base64 chart: ${error.message}\n`); } // Display summary console.log('šŸŽ‰ Bitcoin Multi-Timeframe Analysis Complete!\n'); console.log('šŸ“ Generated Files:'); timeframes.forEach((tf, index) => { console.log(` ${index + 1}. ${tf.outputPath} (${tf.label})`); }); } 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(); } } // Alternative function for generating charts with different styling themes async function generateBitcoinChartsWithThemes() { const generator = new CandlestickChartGenerator({ headless: true }); try { console.log('\n=== Bitcoin Charts with Different Themes ===\n'); const themes = { dark: { layout: { background: { color: '#000000' }, textColor: '#ffffff' }, grid: { vertLines: { color: '#1a1a1a' }, horzLines: { color: '#1a1a1a' } }, name: 'Dark Theme' }, light: { layout: { background: { color: '#ffffff' }, textColor: '#000000' }, grid: { vertLines: { color: '#f0f0f0' }, horzLines: { color: '#f0f0f0' } }, name: 'Light Theme' }, bitcoin: { layout: { background: { color: '#0d0d0d' }, textColor: '#f7931a' }, grid: { vertLines: { color: '#1a1a1a' }, horzLines: { color: '#1a1a1a' } }, timeScale: { borderColor: '#f7931a' }, rightPriceScale: { borderColor: '#f7931a' }, name: 'Bitcoin Orange Theme' } }; for (const [themeKey, themeOptions] of Object.entries(themes)) { console.log(`šŸ“Š Generating ${themeOptions.name} chart...`); await generator.generateChartScreenshot({ symbol: 'BTC-USD', interval: '1d', startDate: new Date(Date.now() - 30 * 24 * 60 * 60 * 1000), endDate: new Date(), outputPath: `examples/btc_${themeKey}_theme.png`, width: 1200, height: 600, chartOptions: themeOptions }); console.log(` āœ… Saved: examples/btc_${themeKey}_theme.png`); } } catch (error) { console.error('āŒ Error in theme generation:', error.message); } finally { await generator.close(); } } // Main execution async function main() { console.log('šŸ”„ Starting Bitcoin Chart Generation Suite...\n'); // Generate multi-timeframe charts await generateBitcoinMultiTimeframeCharts(); // Generate themed charts await generateBitcoinChartsWithThemes(); // Demonstrate desiredBars usage explicitly const generator = new CandlestickChartGenerator({ headless: true }); try { console.log('\n6. Demonstrating desiredBars for BTC 1h (~7 days) and NVDA 1h (~35 days)...'); // BTC (crypto): we want ~1 week of 1h bars (~168 bars). Target ~170 bars. await generator.generateChartScreenshot({ symbol: 'BTC-USD', interval: '1h', endDate: new Date(), desiredBars: 170, outputPath: 'examples/btc_1h_chart.png', width: 1400, height: 800 }); console.log(' āœ… BTC-USD 1h with desiredBars saved'); // NVDA (stock): we want ~35 calendar days of 1h bars (~160 bars for stocks) await generator.generateChartScreenshot({ symbol: 'NVDA', interval: '1h', endDate: new Date(), desiredBars: 160, outputPath: 'examples/nvda_1h_chart.png', width: 1400, height: 800 }); console.log(' āœ… NVDA 1h with desiredBars saved'); } catch (err) { console.error('āŒ Error in desiredBars demo:', err.message); } finally { await generator.close(); } console.log('\nšŸš€ All Bitcoin charts generated successfully!'); console.log('šŸ“ Check the examples/ folder for all generated chart files.'); } // Run the example if (require.main === module) { main().catch(console.error); } module.exports = { generateBitcoinMultiTimeframeCharts, generateBitcoinChartsWithThemes, main };