Skip to content

Commit

Permalink
s
Browse files Browse the repository at this point in the history
  • Loading branch information
sehyunc committed Oct 24, 2024
1 parent 9406d41 commit 7ac2f61
Showing 1 changed file with 29 additions and 33 deletions.
62 changes: 29 additions & 33 deletions app/trade/[base]/components/charts/tradingview/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export async function fetchBarsForPeriod(
timeInterval: string,
countBack: number,
): Promise<Bar[]> {
// Reduce startTimeMs by 10%
// Reduce startTimeMs by 10% to ensure we fetch enough bars
const adjustedStartTimeMs = startTimeMs - (endTimeMs - startTimeMs) * 0.1

const timeChunks = splitTimeRange(
Expand All @@ -88,14 +88,12 @@ export async function fetchBarsForPeriod(
new Map(allBars.map((bar) => [bar.time, bar])).values(),
).sort((a, b) => a.time - b.time)

if (uniqueBars.length >= countBack) {
return uniqueBars
} else {
if (uniqueBars.length < countBack) {
console.warn(
`Not enough bars fetched: ${uniqueBars.length}, requested ${countBack}`,
)
return uniqueBars // Return what we have instead of throwing an error
}
return uniqueBars
}

/**
Expand All @@ -122,7 +120,6 @@ async function fetchBarsForTimeChunk(
countBack,
)
} else {
// Split the time range into two equal parts
const midTimeMs = startTimeMs + Math.floor(timeDiff / 2)
const [firstHalf, secondHalf] = await Promise.all([
fetchBarsForTimeChunk(
Expand All @@ -144,33 +141,6 @@ async function fetchBarsForTimeChunk(
}
}

/**
* Splits a time range into smaller chunks for parallel processing.
* @param startTimeMs The start time in milliseconds.
* @param endTimeMs The end time in milliseconds.
* @param maxChunks The maximum number of chunks to create.
* @returns An array of time chunks, each with a start and end time.
*/
function splitTimeRange(
startTimeMs: number,
endTimeMs: number,
maxChunks: number,
) {
const totalDuration = endTimeMs - startTimeMs
const chunkSize = Math.ceil(totalDuration / maxChunks)
const chunks = []

for (let i = 0; i < maxChunks; i++) {
const chunkStart = startTimeMs + i * chunkSize
const chunkEnd = Math.min(chunkStart + chunkSize, endTimeMs)
chunks.push({ start: chunkStart, end: chunkEnd })
if (chunkEnd === endTimeMs) break
}

return chunks
}

// TODO: Start time MS recursively decrements and gets stuck in loop. Need to figure out where it's being decremented and avoid.
/**
* Helper function to fetch trading bars for periods of 2 years or less.
* @param ticker The ticker symbol for the asset pair.
Expand Down Expand Up @@ -215,3 +185,29 @@ async function fetchBarsForTwoYearsOrLess(

return allBars
}

/**
* Splits a time range into smaller chunks for parallel processing.
* @param startTimeMs The start time in milliseconds.
* @param endTimeMs The end time in milliseconds.
* @param maxChunks The maximum number of chunks to create.
* @returns An array of time chunks, each with a start and end time.
*/
function splitTimeRange(
startTimeMs: number,
endTimeMs: number,
maxChunks: number,
) {
const totalDuration = endTimeMs - startTimeMs
const chunkSize = Math.ceil(totalDuration / maxChunks)
const chunks = []

for (let i = 0; i < maxChunks; i++) {
const chunkStart = startTimeMs + i * chunkSize
const chunkEnd = Math.min(chunkStart + chunkSize, endTimeMs)
chunks.push({ start: chunkStart, end: chunkEnd })
if (chunkEnd === endTimeMs) break
}

return chunks
}

0 comments on commit 7ac2f61

Please sign in to comment.