You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

99 lines
2.9 KiB
TypeScript

//import { ingestOptions, ingestStocks, } from './ingest';
import { sql } from './db.js';
/*
const sourceDataDir = '/home/avraham/programming/calendar-optimizer-csv';
//await ingestStocks(sourceDataDir);
await ingestOptions(sourceDataDir);
*/
import fastify from 'fastify'
import cors from '@fastify/cors'
const server = fastify()
await server.register(cors, {origin:true});
server.get('/ping', async (request, reply) => {
return 'pong\n'
})
server.get('/option_quotes/underlyings', async (request, reply) => {
const underlyings = (await sql`SELECT * FROM "underlyings";`).map(x=>x.underlying);
reply.status(200).send(underlyings);
})
server.get<{
Params: {underlying:string},
}>('/option_quotes/:underlying/quote_dates', async (request, reply) => {
const options_quotes = (await sql`
SELECT DISTINCT "quote_date"
FROM "option_quote"
WHERE
"underlying" = ${request.params.underlying}
ORDER BY "quote_date";`
).map(x=>x.quote_date);
reply.status(200).send(options_quotes);
})
server.get<{
Params: {underlying:string, quote_date:string},
}>('/option_quotes/:underlying/:quote_date', async (request, reply) => {
const options_quotes = await sql`
SELECT *
FROM "option_quote"
WHERE
"underlying" = ${request.params.underlying}
AND
"quote_date" = ${request.params.quote_date}
ORDER BY "strike";`;
reply.status(200).send(options_quotes);
})
server.get<{
Params: {underlying:string, quote_date:string},
}>('/option_quotes/:underlying/:quote_date/strikes', async (request, reply) => {
const strikes = (await sql`
SELECT DISTINCT "strike"
FROM "option_quote"
WHERE
"underlying" = ${request.params.underlying}
AND
"quote_date" = ${request.params.quote_date}
ORDER BY "strike";`
).map(x=>x.strike);
reply.status(200).send(strikes);
})
server.get<{
Params: {underlying:string, quote_date:string, strike:string},
}>('/option_quotes/:underlying/:quote_date/:strike', async (request, reply) => {
const strikes = await sql`
SELECT *
FROM "option_quote"
WHERE
"underlying" = ${request.params.underlying}
AND
"quote_date" = ${request.params.quote_date}
AND
"strike" = ${request.params.strike}
ORDER BY "strike";`;
reply.status(200).send(strikes);
})
server.get<{
Params: {underlying:string, quote_date:string},
}>('/option_quotes/:underlying/:quote_date/expirations', async (request, reply) => {
const expirations = (await sql`
SELECT DISTINCT "expiration"
FROM "option_quote"
WHERE
"underlying" = ${request.params.underlying}
AND
"quote_date" = ${request.params.quote_date}
ORDER BY "expiration";`
).map(x=>x.expiration);
reply.status(200).send(expirations);
})
server.listen({ port: 8234, host: '127.0.0.1' }, (err, address) => {
if (err) {
console.error(err)
process.exit(1)
}
console.log(`Server listening at ${address}`)
})
//await sql.end({timeout:60});