candlestick-chart-generator
Version:
A Node.js library for generating candlestick chart screenshots using financial data
345 lines (295 loc) ⢠12.9 kB
JavaScript
const CandlestickChartGenerator = require('../index');
/**
* Consolidated Examples - Chart Generator SDK
*
* This comprehensive example demonstrates all major features of the
* Candlestick Chart Generator SDK in a single, organized script.
*/
class ChartExamplesRunner {
constructor() {
this.generator = new CandlestickChartGenerator({ headless: true });
}
async runAllExamples() {
console.log('šÆ Starting Comprehensive Chart Generator Examples\n');
try {
await this.stockExamples();
await this.cryptoExamples();
await this.forexExamples();
await this.stylingExamples();
await this.multiTimeframeExamples();
await this.base64Examples();
console.log('\nš All examples completed successfully!');
console.log('š Check the examples/ folder for generated charts.');
} catch (error) {
console.error('ā Error running examples:', error.message);
} finally {
await this.generator.close();
}
}
async stockExamples() {
console.log('=== š STOCK CHART EXAMPLES ===\n');
const stocks = [
{ symbol: 'AAPL', name: 'Apple Inc.' },
{ symbol: 'GOOGL', name: 'Google/Alphabet' },
{ symbol: 'TSLA', name: 'Tesla Inc.' },
{ symbol: 'MSFT', name: 'Microsoft' }
];
console.log('Generating popular stock charts...');
for (const stock of stocks) {
try {
await this.generator.generateChartScreenshot({
symbol: stock.symbol,
interval: '1d',
startDate: new Date(Date.now() - 90 * 24 * 60 * 60 * 1000), // 3 months
endDate: new Date(),
outputPath: `examples/stocks_${stock.symbol.toLowerCase()}_3m.png`,
width: 1200,
height: 600,
chartOptions: {
layout: { background: { color: '#ffffff' }, textColor: '#000000' },
grid: { vertLines: { color: '#f0f0f0' }, horzLines: { color: '#f0f0f0' } }
}
});
console.log(`ā
${stock.name} (${stock.symbol}) chart generated`);
} catch (error) {
console.log(`ā Error generating ${stock.symbol}: ${error.message}`);
}
}
console.log('');
}
async cryptoExamples() {
console.log('=== š„ CRYPTOCURRENCY EXAMPLES ===\n');
const cryptos = [
{ symbol: 'BTC-USD', name: 'Bitcoin', period: 180 },
{ symbol: 'ETH-USD', name: 'Ethereum', period: 90 },
{ symbol: 'ADA-USD', name: 'Cardano', period: 60 },
{ symbol: 'SOL-USD', name: 'Solana', period: 30 }
];
console.log('Generating cryptocurrency charts with dark theme...');
const cryptoStyle = {
layout: { background: { color: '#0a0a0a' }, textColor: '#ffffff' },
grid: { vertLines: { color: '#1a1a1a' }, horzLines: { color: '#1a1a1a' } },
timeScale: { borderColor: '#333333' },
rightPriceScale: { borderColor: '#333333' }
};
for (const crypto of cryptos) {
try {
await this.generator.generateChartScreenshot({
symbol: crypto.symbol,
interval: '1d',
startDate: new Date(Date.now() - crypto.period * 24 * 60 * 60 * 1000),
endDate: new Date(),
outputPath: `examples/crypto_${crypto.symbol.replace('-', '_').toLowerCase()}_${crypto.period}d.png`,
width: 1200,
height: 600,
chartOptions: cryptoStyle
});
console.log(`ā
${crypto.name} (${crypto.symbol}) - ${crypto.period} days`);
} catch (error) {
console.log(`ā Error generating ${crypto.symbol}: ${error.message}`);
}
}
console.log('');
}
async forexExamples() {
console.log('=== š± FOREX PAIR EXAMPLES ===\n');
const forexPairs = [
{ symbol: 'EURUSD=X', name: 'EUR/USD' },
{ symbol: 'GBPUSD=X', name: 'GBP/USD' },
{ symbol: 'USDJPY=X', name: 'USD/JPY' },
{ symbol: 'AUDUSD=X', name: 'AUD/USD' }
];
console.log('Generating major forex pair charts...');
const forexStyle = {
layout: { background: { color: '#f8f9fa' }, textColor: '#333333' },
grid: { vertLines: { color: '#e9ecef' }, horzLines: { color: '#e9ecef' } }
};
for (const pair of forexPairs) {
try {
await this.generator.generateChartScreenshot({
symbol: pair.symbol,
interval: '1h',
startDate: new Date(Date.now() - 7 * 24 * 60 * 60 * 1000), // 7 days
endDate: new Date(),
outputPath: `examples/forex_${pair.name.replace('/', '').toLowerCase()}_7d.png`,
width: 1200,
height: 600,
chartOptions: forexStyle
});
console.log(`ā
${pair.name} hourly chart generated`);
} catch (error) {
console.log(`ā Error generating ${pair.symbol}: ${error.message}`);
}
}
console.log('');
}
async stylingExamples() {
console.log('=== šØ CHART STYLING EXAMPLES ===\n');
const themes = {
'professional_dark': {
layout: { background: { color: '#1e1e1e' }, textColor: '#ffffff' },
grid: { vertLines: { color: '#2a2a2a' }, horzLines: { color: '#2a2a2a' } },
timeScale: { borderColor: '#404040' },
rightPriceScale: { borderColor: '#404040' },
name: 'Professional Dark'
},
'modern_blue': {
layout: { background: { color: '#0d1421' }, textColor: '#a0aec0' },
grid: { vertLines: { color: '#1a202c' }, horzLines: { color: '#1a202c' } },
timeScale: { borderColor: '#2d3748' },
rightPriceScale: { borderColor: '#2d3748' },
name: 'Modern Blue'
},
'trading_green': {
layout: { background: { color: '#0f1419' }, textColor: '#00ff88' },
grid: { vertLines: { color: '#1e2328' }, horzLines: { color: '#1e2328' } },
timeScale: { borderColor: '#00ff88' },
rightPriceScale: { borderColor: '#00ff88' },
name: 'Trading Green'
}
};
console.log('Generating charts with different styling themes...');
for (const [themeKey, theme] of Object.entries(themes)) {
try {
await this.generator.generateChartScreenshot({
symbol: 'SPY',
interval: '1d',
startDate: new Date(Date.now() - 30 * 24 * 60 * 60 * 1000),
endDate: new Date(),
outputPath: `examples/theme_${themeKey}.png`,
width: 1200,
height: 600,
chartOptions: theme
});
console.log(`ā
${theme.name} theme applied to SPY chart`);
} catch (error) {
console.log(`ā Error generating ${themeKey} theme: ${error.message}`);
}
}
console.log('');
}
async multiTimeframeExamples() {
console.log('=== ā° MULTI-TIMEFRAME EXAMPLES ===\n');
const timeframes = [
{ interval: '5m', period: 1, label: '5-minute (1 day)' },
{ interval: '1h', period: 7, label: '1-hour (1 week)' },
{ interval: '4h', period: 14, label: '4-hour (2 weeks)' },
{ interval: '1d', period: 90, label: '1-day (3 months)' },
{ interval: '1wk', period: 365, label: '1-week (1 year)' }
];
console.log('Generating NVIDIA charts across multiple timeframes...');
for (const tf of timeframes) {
try {
const params = {
symbol: 'NVDA',
interval: tf.interval,
endDate: new Date(),
outputPath: `examples/nvda_${tf.interval}_chart.png`,
width: 1400,
height: 700
};
// Demonstrate desiredBars for 1h NVDA to fetch ~35 calendar days (ā 160 bars for stocks)
if (tf.interval === '1h') {
params.desiredBars = 160;
} else {
// Keep original behavior for others
params.startDate = new Date(Date.now() - tf.period * 24 * 60 * 60 * 1000);
}
await this.generator.generateChartScreenshot(params);
console.log(`ā
NVIDIA ${tf.label} chart generated`);
} catch (error) {
console.log(`ā Error generating ${tf.interval} chart: ${error.message}`);
}
}
console.log('');
}
async base64Examples() {
console.log('=== š» BASE64 OUTPUT EXAMPLES ===\n');
const symbols = ['AMZN', 'META', 'NFLX'];
console.log('Generating charts as base64 data for web integration...');
for (const symbol of symbols) {
try {
const base64Data = await this.generator.generateChartBase64({
symbol: symbol,
interval: '1d',
startDate: new Date(Date.now() - 60 * 24 * 60 * 60 * 1000), // 2 months
endDate: new Date(),
width: 800,
height: 400
});
console.log(`ā
${symbol} base64 chart generated (${base64Data.length} characters)`);
// Example: save first few characters to show format
const preview = base64Data.substring(0, 50) + '...';
console.log(` Preview: ${preview}`);
} catch (error) {
console.log(`ā Error generating ${symbol} base64: ${error.message}`);
}
}
console.log('');
}
}
// Utility function for running specific example categories
async function runSpecificExamples(categories = ['all']) {
const runner = new ChartExamplesRunner();
try {
if (categories.includes('all')) {
await runner.runAllExamples();
return;
}
console.log('šÆ Running specific example categories...\n');
if (categories.includes('stocks')) await runner.stockExamples();
if (categories.includes('crypto')) await runner.cryptoExamples();
if (categories.includes('forex')) await runner.forexExamples();
if (categories.includes('styling')) await runner.stylingExamples();
if (categories.includes('timeframes')) await runner.multiTimeframeExamples();
if (categories.includes('base64')) await runner.base64Examples();
console.log('\nš Selected examples completed!');
} catch (error) {
console.error('ā Error:', error.message);
} finally {
await runner.generator.close();
}
}
// Main execution
async function main() {
const args = process.argv.slice(2);
if (args.length === 0) {
// Run all examples
const runner = new ChartExamplesRunner();
await runner.runAllExamples();
} else {
// Run specific categories
await runSpecificExamples(args);
}
}
// CLI usage information
function showUsage() {
console.log(`
š Chart Generator SDK - Consolidated Examples
Usage:
node consolidated_examples.js [categories...]
Categories:
stocks - Stock chart examples (AAPL, GOOGL, TSLA, MSFT)
crypto - Cryptocurrency examples (BTC, ETH, ADA, SOL)
forex - Forex pair examples (EUR/USD, GBP/USD, etc.)
styling - Different chart themes and styling
timeframes - Multi-timeframe analysis examples
base64 - Base64 output for web integration
all - Run all examples (default)
Examples:
node consolidated_examples.js
node consolidated_examples.js stocks crypto
node consolidated_examples.js styling timeframes
`);
}
// Show help if requested
if (process.argv.includes('--help') || process.argv.includes('-h')) {
showUsage();
} else if (require.main === module) {
main().catch(console.error);
}
module.exports = {
ChartExamplesRunner,
runSpecificExamples,
main
};