File size: 9,292 Bytes
75e9048 6927998 e6dc26c 6927998 8cc10f3 152e0b0 8cc10f3 6927998 48b4ed2 6927998 48b4ed2 6927998 48b4ed2 6927998 e6dc26c 6927998 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 |
const express = require('express');
const cors = require('cors');
const yahooFinance = require('yahoo-finance2').default;
const fetch = require('node-fetch');
const app = express();
// Constants
const PORT = 7860;
const HOST = '0.0.0.0';
app.use(cors());
app.use(express.json());
app.get('/', (req, res) => {
res.setHeader('Content-Type', 'text/plain');
res.end('This is Yahoo finance api \n\nSearch : /api/search/:<symbol/name>\nQuote : /api/quote/:<symbol>\nChart : /api/chart/:<symbol>?interval=<interval>&range=<range>\nSummary : /api/summary/:<symbol>\nAssetProfile : /api/assetprofile/:<symbol>\nBalancesheet : /api/balancesheet/:<symbol>\nCalendar events : /api/calendar/:<symbol>\nCashflow statements : /api/cashflow/:<symbol>');
})
app.get('/api/search/:query', async (req, res) => {
const query = req.params.query;
try {
const results = await yahooFinance.search(query);
res.json(results);
} catch (error) {
res.status(500).json({ message: 'Error searching stocks.' });
}
});
app.get('/api/quote/:symbol', async (req, res) => {
const symbol = req.params.symbol;
try {
const quote = await yahooFinance.quote(symbol);
res.json(quote);
} catch (error) {
res.status(500).json({ message: 'Error fetching quote data.' });
}
});
app.get('/api/chart/:symbol', async (req, res) => {
const symbol = req.params.symbol;
const interval = req.query.interval || '1d'; // Default to 1 day
const range = req.query.range || '1y'; // Default to 1 year
try {
const url = `https://query1.finance.yahoo.com/v8/finance/chart/${symbol}?interval=${interval}&range=${range}`;
const response = await fetch(url);
const data = await response.json();
if (!data.chart.result) {
return res.status(404).json({ message: `No historical data found for ${symbol}` });
}
const timestamps = data.chart.result[0].timestamp;
const closePrices = data.chart.result[0].indicators.quote[0].close;
const openPrices = data.chart.result[0].indicators.quote[0].open;
const highPrices = data.chart.result[0].indicators.quote[0].high;
const lowPrices = data.chart.result[0].indicators.quote[0].low;
const adjustClosePrices = data.chart.result[0].indicators.adjclose[0].adjclose;
const volume = data.chart.result[0].indicators.quote[0].volume;
const historicalData = timestamps.map((time, index) => ({
date: new Date(time * 1000).toISOString().slice(0, 10), // Convert timestamp to date format
open: openPrices[index],
high: highPrices[index],
low: lowPrices[index],
close: closePrices[index],
adjustclose: adjustClosePrices[index],
volume: volume[index]
}));
res.json(historicalData.reverse());
} catch (error) {
console.error(`Error fetching historical data for ${symbol}:`, error);
res.status(500).json({ message: 'Error fetching historical data.', error: error.message });
}
});
app.get('/api/summary/:symbol', async (req, res) => {
const symbol = req.params.symbol;
try {
const queryOptions = { modules: ['summaryDetail', 'summaryProfile'] };
const quote = await yahooFinance.quoteSummary(symbol, queryOptions);
res.json(quote);
} catch (error) {
res.status(500).json({ message: 'Error fetching summary data.' });
}
})
app.get('/api/assetprofile/:symbol', async (req, res) => {
const symbol = req.params.symbol;
try {
const queryOptions = { modules: ['assetProfile'] };
const quote = await yahooFinance.quoteSummary(symbol, queryOptions);
res.json(quote);
} catch (error) {
res.status(500).json({ message: 'Error fetching assetProfile data.' });
}
})
app.get('/api/balancesheet/:symbol', async (req, res) => {
const symbol = req.params.symbol;
try {
const queryOptions = { modules: ['balanceSheetHistory', 'balanceSheetHistoryQuarterly'] };
const quote = await yahooFinance.quoteSummary(symbol, queryOptions);
res.json(quote);
} catch (error) {
res.status(500).json({ message: 'Error fetching balanceSheet data.' });
}
})
app.get('/api/calendar/:symbol', async (req, res) => {
const symbol = req.params.symbol;
try {
const queryOptions = { modules: ['calendarEvents'] };
const quote = await yahooFinance.quoteSummary(symbol, queryOptions);
res.json(quote);
} catch (error) {
res.status(500).json({ message: 'Error fetching calendarEvents data.' });
}
})
app.get('/api/cashflow/:symbol', async (req, res) => {
const symbol = req.params.symbol;
try {
const queryOptions = { modules: ['cashflowStatementHistory', 'cashflowStatementHistoryQuarterly'] };
const quote = await yahooFinance.quoteSummary(symbol, queryOptions);
res.json(quote);
} catch (error) {
res.status(500).json({ message: 'Error fetching cashflowstatement data.' });
}
})
app.get('/api/statistic/:symbol', async (req, res) => {
const symbol = req.params.symbol;
try {
const queryOptions = { modules: ['defaultKeyStatistics'] };
const quote = await yahooFinance.quoteSummary(symbol, queryOptions);
res.json(quote);
} catch (error) {
res.status(500).json({ message: 'Error fetching statistic data.' });
}
})
app.get('/api/earnings/:symbol', async (req, res) => {
const symbol = req.params.symbol;
try {
const queryOptions = { modules: ['earnings', 'earningsHistory', 'earningsTrend'] };
const quote = await yahooFinance.quoteSummary(symbol, queryOptions);
res.json(quote);
} catch (error) {
res.status(500).json({ message: 'Error fetching earnings data.' });
}
})
app.get('/api/financial/:symbol', async (req, res) => {
const symbol = req.params.symbol;
try {
const queryOptions = { modules: ['financialData'] };
const quote = await yahooFinance.quoteSummary(symbol, queryOptions);
res.json(quote);
} catch (error) {
res.status(500).json({ message: 'Error fetching financial data.' });
}
})
app.get('/api/fund/:symbol', async (req, res) => {
const symbol = req.params.symbol;
try {
const queryOptions = { modules: ['fundOwnership', 'fundPerformance', 'fundProfile'] };
const quote = await yahooFinance.quoteSummary(symbol, queryOptions);
res.json(quote);
} catch (error) {
res.status(500).json({ message: 'Error fetching fund data.' });
}
})
app.get('/api/income/:symbol', async (req, res) => {
const symbol = req.params.symbol;
try {
const queryOptions = { modules: ['incomeStatementHistory', 'incomeStatementHistoryQuarterly'] };
const quote = await yahooFinance.quoteSummary(symbol, queryOptions);
res.json(quote);
} catch (error) {
res.status(500).json({ message: 'Error fetching incomestatement data.' });
}
})
app.get('/api/trend/:symbol', async (req, res) => {
const symbol = req.params.symbol;
try {
const queryOptions = { modules: ['indexTrend', 'industryTrend'] };
const quote = await yahooFinance.quoteSummary(symbol, queryOptions);
res.json(quote);
} catch (error) {
res.status(500).json({ message: 'Error fetching trend data.' });
}
})
app.get('/api/insider/:symbol', async (req, res) => {
const symbol = req.params.symbol;
try {
const queryOptions = { modules: ['insiderHolders', 'insiderTransactions', 'institutionOwnership'] };
const quote = await yahooFinance.quoteSummary(symbol, queryOptions);
res.json(quote);
} catch (error) {
res.status(500).json({ message: 'Error fetching insider data.' });
}
})
app.get('/api/major/:symbol', async (req, res) => {
const symbol = req.params.symbol;
try {
const queryOptions = { modules: ['topHoldings', 'majorDirectHolders', 'majorHoldersBreakdown'] };
const quote = await yahooFinance.quoteSummary(symbol, queryOptions);
res.json(quote);
} catch (error) {
res.status(500).json({ message: 'Error fetching majorholders data.' });
}
})
app.get('/api/netshare/:symbol', async (req, res) => {
const symbol = req.params.symbol;
try {
const queryOptions = { modules: ['netSharePurchaseActivity', 'price'] };
const quote = await yahooFinance.quoteSummary(symbol, queryOptions);
res.json(quote);
} catch (error) {
res.status(500).json({ message: 'Error fetching netshare data.' });
}
})
app.get('/api/recommend/:symbol', async (req, res) => {
const symbol = req.params.symbol;
try {
const queryOptions = { modules: ['recommendationTrend'] };
const quote = await yahooFinance.quoteSummary(symbol, queryOptions);
res.json(quote);
} catch (error) {
res.status(500).json({ message: 'Error fetching recommendationTrend data.' });
}
})
app.listen(PORT, HOST, () => {
if (HOST == '0.0.0.0') {
console.log(`Running on http://127.0.0.1:${PORT}`);
} else {
console.log(`Running on http://${HOST}:${PORT}`);
}
});
|