Skip to content

Instantly share code, notes, and snippets.

@fl0wo
Created September 23, 2023 18:58
Show Gist options
  • Save fl0wo/aab2de3f74dfa3bbb5efeb4876c7b73b to your computer and use it in GitHub Desktop.
Save fl0wo/aab2de3f74dfa3bbb5efeb4876c7b73b to your computer and use it in GitHub Desktop.
LastKnownCandlesDictonary builder
const fromRowToDictonary = (rows: any[]) => {
const dictonary: LastKnownCandlesDictonary = {};
rows.forEach((row) => {
dictonary[row.symbol] = row.lastaction;
});
return dictonary;
}
const reformatRowToObj = (res: QueryCommandOutput) => {
return res.Rows?.map((row) => {
const lastTimeStamp = getSafeNull(
row.Data?.[1].ScalarValue, minutesBackInTime(60)
);
const symbol = row.Data?.[0].ScalarValue;
if (!symbol || !lastTimeStamp) {
return null;
}
return {
symbol: symbol,
lastaction: clapToNextMinute(new Date(lastTimeStamp).getTime()), // One-min shift
}
}).filter((row) => row !== null) ?? [];
};
async function getActualLastCandles(DatabaseName: string, TableName: string, desiredSymbols: string[]) {
// Get Batch Read all last candles for each desiredSymbol and group in a dictonary
const client = loadQueryTimeStreamClientBulk();
// This query gets last elements in the 3h window range
const query =
`select crypto,lastaction FROM (
SELECT crypto,time,first_value(time) over (partition by crypto order by time desc) lastaction
FROM "${DatabaseName}"."${TableName}"
WHERE time between ago(3h) and now()
) GROUP BY crypto,lastaction`;
console.log('Running query: ', query);
const input: QueryCommandInput = {
QueryString: query, // <-- Query here
MaxRows: desiredSymbols.length,
};
const command = new QueryCommand(input);
try {
return await execQuery(client, command);
} catch (e) {
console.log('ERROR FETCHING LAST CANDLES TIMES');
console.log(e);
throw e;
}
}
export async function getCurrentLastOHLCVForEachSymbolInTimeStream(
markets: Dictionary<Market>,
DatabaseName: string,
TableName: string,
): Promise<LastKnownCandlesDictonary> {
const desiredSymbols = getDesiredSymbols(markets);
// Default every crypto last 60min
const defaultLastKnownCandlesDictonary:LastKnownCandlesDictonary = {
...desiredSymbols.reduce((acc, symbol) => {
return {
...acc,
[symbol]: minutesBackInTime(60).getTime(),
}
}, {}),
}
const res = await getActualLastCandles(DatabaseName, TableName, desiredSymbols);
const rows = reformatRowToObj(res);
const dictonary = fromRowToDictonary(rows ?? []);
// Default first then override
return {
...defaultLastKnownCandlesDictonary,
...dictonary,
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment