fixed up prices, symbols / exchanges
This commit is contained in:
parent
d8e7449605
commit
f196c5dcf4
9 changed files with 191 additions and 202 deletions
|
|
@ -3,19 +3,15 @@ import type { EodHandler } from '../eod.handler';
|
|||
import { EOD_CONFIG } from '../shared';
|
||||
|
||||
interface FetchIntradayInput {
|
||||
symbol: string;
|
||||
exchange: string;
|
||||
eodSearchCode: string;
|
||||
interval: '1m' | '5m' | '1h';
|
||||
fromDate?: Date;
|
||||
toDate?: Date;
|
||||
country?: string;
|
||||
}
|
||||
|
||||
interface CrawlIntradayInput {
|
||||
symbol: string;
|
||||
exchange: string;
|
||||
eodSearchCode: string;
|
||||
interval: '1m' | '5m' | '1h';
|
||||
country?: string;
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -53,7 +49,7 @@ export async function scheduleIntradayCrawl(
|
|||
}).toArray();
|
||||
|
||||
// Add interval info to each symbol
|
||||
symbolsForInterval.forEach(symbol => {
|
||||
symbolsForInterval.forEach((symbol: any) => {
|
||||
// Check if this interval needs processing (not finished or needs new data)
|
||||
const operationStatus = symbol.operations?.[operationName];
|
||||
const shouldProcess = !operationStatus || !operationStatus.finished ||
|
||||
|
|
@ -101,10 +97,8 @@ export async function scheduleIntradayCrawl(
|
|||
const { symbol, interval } = item;
|
||||
|
||||
await this.scheduleOperation('crawl-intraday', {
|
||||
symbol: symbol.Code,
|
||||
exchange: symbol.eodExchange || symbol.Exchange, // Use eodExchange if available
|
||||
interval,
|
||||
country: symbol.Country
|
||||
eodSearchCode: symbol.eodSearchCode,
|
||||
interval
|
||||
}, {
|
||||
priority: 5, // Initial crawl jobs get priority 5 (lower priority)
|
||||
attempts: 3,
|
||||
|
|
@ -134,26 +128,31 @@ export async function crawlIntraday(
|
|||
input: CrawlIntradayInput
|
||||
): Promise<{ success: boolean; recordsProcessed: number; finished: boolean }> {
|
||||
const logger = this.logger;
|
||||
const { symbol, exchange, interval, country } = input;
|
||||
const { eodSearchCode, interval } = input;
|
||||
|
||||
try {
|
||||
// Lookup symbol using eodSearchCode
|
||||
const symbolDoc = await this.mongodb.collection('eodSymbols').findOne({
|
||||
eodSearchCode: eodSearchCode
|
||||
});
|
||||
|
||||
if (!symbolDoc) {
|
||||
logger.error(`Symbol not found for eodSearchCode: ${eodSearchCode}`);
|
||||
throw new Error(`Symbol not found: ${eodSearchCode}`);
|
||||
}
|
||||
|
||||
const symbol = symbolDoc.Code;
|
||||
const exchange = symbolDoc.eodExchange || symbolDoc.Exchange;
|
||||
const country = symbolDoc.Country;
|
||||
|
||||
logger.info(`Starting intraday crawl for ${symbol}.${exchange} - ${interval}`, {
|
||||
symbol,
|
||||
exchange,
|
||||
interval,
|
||||
country
|
||||
country,
|
||||
eodSearchCode
|
||||
});
|
||||
|
||||
// Get symbol to check if it exists
|
||||
const symbolDoc = await this.mongodb.collection('eodSymbols').findOne({
|
||||
Code: symbol,
|
||||
eodExchange: exchange
|
||||
});
|
||||
|
||||
if (!symbolDoc) {
|
||||
throw new Error(`Symbol ${symbol}.${exchange} not found`);
|
||||
}
|
||||
|
||||
logger.debug('Found symbol document', {
|
||||
symbol,
|
||||
exchange,
|
||||
|
|
@ -202,12 +201,10 @@ export async function crawlIntraday(
|
|||
|
||||
// Fetch data for this batch
|
||||
const result = await fetchIntraday.call(this, {
|
||||
symbol,
|
||||
exchange,
|
||||
eodSearchCode,
|
||||
interval,
|
||||
fromDate,
|
||||
toDate,
|
||||
country
|
||||
toDate
|
||||
});
|
||||
|
||||
// Prepare update data
|
||||
|
|
@ -270,7 +267,6 @@ export async function crawlIntraday(
|
|||
finished: updateData.finished
|
||||
});
|
||||
|
||||
const eodSearchCode = `${symbol}.${exchange}`;
|
||||
await this.operationRegistry.updateOperation('eod', eodSearchCode, operationName, updateData);
|
||||
|
||||
logger.info(`Operation tracker updated for ${symbol}.${exchange} - ${interval}`);
|
||||
|
|
@ -278,10 +274,8 @@ export async function crawlIntraday(
|
|||
// If not finished, schedule next batch
|
||||
if (!updateData.finished) {
|
||||
await this.scheduleOperation('crawl-intraday', {
|
||||
symbol,
|
||||
exchange,
|
||||
interval,
|
||||
country
|
||||
eodSearchCode,
|
||||
interval
|
||||
}, {
|
||||
priority: 3, // Continuation jobs get higher priority (3) than initial jobs (5)
|
||||
attempts: 3,
|
||||
|
|
@ -319,9 +313,27 @@ export async function fetchIntraday(
|
|||
input: FetchIntradayInput
|
||||
): Promise<{ success: boolean; recordsSaved: number; recordsFetched: number }> {
|
||||
const logger = this.logger;
|
||||
const { symbol, exchange, interval, fromDate, toDate, country } = input;
|
||||
const { eodSearchCode, interval, fromDate, toDate } = input;
|
||||
|
||||
// Declare variables for catch block
|
||||
let symbol: string = '';
|
||||
let exchange: string = '';
|
||||
|
||||
try {
|
||||
// Lookup symbol using eodSearchCode
|
||||
const symbolDoc = await this.mongodb.collection('eodSymbols').findOne({
|
||||
eodSearchCode: eodSearchCode
|
||||
});
|
||||
|
||||
if (!symbolDoc) {
|
||||
logger.error(`Symbol not found for eodSearchCode: ${eodSearchCode}`);
|
||||
throw new Error(`Symbol not found: ${eodSearchCode}`);
|
||||
}
|
||||
|
||||
symbol = symbolDoc.Code;
|
||||
exchange = symbolDoc.eodExchange || symbolDoc.Exchange;
|
||||
const country = symbolDoc.Country;
|
||||
|
||||
logger.info(`Fetching intraday data for ${symbol}.${exchange} - ${interval}`, {
|
||||
symbol,
|
||||
exchange,
|
||||
|
|
@ -332,20 +344,6 @@ export async function fetchIntraday(
|
|||
url: `https://eodhd.com/api/intraday/${symbol}.${exchange}`
|
||||
});
|
||||
|
||||
// Get country if not provided
|
||||
let symbolCountry = country;
|
||||
if (!symbolCountry) {
|
||||
const symbolDoc = await this.mongodb.collection('eodSymbols').findOne({
|
||||
Code: symbol,
|
||||
eodExchange: exchange
|
||||
});
|
||||
|
||||
if (!symbolDoc) {
|
||||
throw new Error(`Symbol ${symbol}.${exchange} not found in database`);
|
||||
}
|
||||
symbolCountry = symbolDoc.Country;
|
||||
}
|
||||
|
||||
// Get API key
|
||||
const apiKey = EOD_CONFIG.API_TOKEN;
|
||||
if (!apiKey) {
|
||||
|
|
@ -392,7 +390,7 @@ export async function fetchIntraday(
|
|||
const recordsWithMetadata = data.map(bar => ({
|
||||
symbol,
|
||||
exchange,
|
||||
symbolExchange: `${symbol}.${exchange}`,
|
||||
eodSearchCode,
|
||||
datetime: bar.datetime,
|
||||
timestamp: bar.timestamp,
|
||||
gmtoffset: bar.gmtoffset,
|
||||
|
|
@ -404,12 +402,12 @@ export async function fetchIntraday(
|
|||
source: 'eod'
|
||||
}));
|
||||
|
||||
// Save to MongoDB - use timestamp and symbolExchange as unique identifier
|
||||
// Save to MongoDB - use timestamp and eodSearchCode as unique identifier
|
||||
const collectionName = `eodIntraday${interval.toUpperCase()}`;
|
||||
const result = await this.mongodb.batchUpsert(
|
||||
collectionName,
|
||||
recordsWithMetadata,
|
||||
['timestamp', 'symbolExchange']
|
||||
['timestamp', 'eodSearchCode']
|
||||
);
|
||||
|
||||
logger.info(`Saved ${result.insertedCount} intraday records`, {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue