Skip to content

Commit

Permalink
get rid of yf historical data feed
Browse files Browse the repository at this point in the history
  • Loading branch information
mnsrulz committed Sep 10, 2024
1 parent dda9297 commit 88a9b5e
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 14 deletions.
16 changes: 8 additions & 8 deletions src/app/api/symbols/[symbol]/historical/route.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { getPriceAtDate } from "@/lib/tradierService";
import { NextResponse } from "next/server";
import yf from 'yahoo-finance2';

export async function GET(request: Request, p: { params: { symbol: string } }) {
const resp = await yf.historical(p.params.symbol, {
interval: '1d',
period1: new Date(2023, 0, 1),
period2: new Date()
});
export async function GET(request: Request, p: { params: { symbol: string, dt: string } }) {
const { symbol } = p.params;
const { searchParams } = new URL(request.url);
const dt = searchParams.get("dt");
if (!dt) return NextResponse.json({ error: 'dt is null' }, { status: 400 });
const resp = await getPriceAtDate(symbol, dt);
return NextResponse.json({
data: resp,
price: resp,
symbol: p.params.symbol
});
}
11 changes: 8 additions & 3 deletions src/lib/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,10 +228,15 @@ export const useDeltaGammaHedging = (symbol: string, dte: number, sc: number, da
s: symbol,
dt: dataMode
}
}).json<{ data: TradierOptionData[], currentPrice: number }>().then(r => {
}).json<{ data: TradierOptionData[] }>().then(async r => {
const allDates = [...new Set(r.data.flatMap(j => j.options.option.map(s => s.expiration_date)))];
const allStrikes = getCalculatedStrikes(r.currentPrice, sc, [...new Set(r.data.flatMap(j => j.options.option.map(s => s.strike)))]);
const finalResponse = calculateHedging(r.data, allStrikes, allDates, r.currentPrice);
const { price } = await ky(`/api/symbols/${symbol}/historical`, {
searchParams: {
dt: dataMode
}
}).json<{ price: number }>();
const allStrikes = getCalculatedStrikes(price, sc, [...new Set(r.data.flatMap(j => j.options.option.map(s => s.strike)))]);
const finalResponse = calculateHedging(r.data, allStrikes, allDates, price);
setOd(finalResponse.exposureData);
}).finally(() => setIsLoading(false));
}
Expand Down
35 changes: 32 additions & 3 deletions src/lib/tradierService.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import ky from "ky";
import { TradierOptionData } from "./types";
import dayjs from "dayjs";
const tradierBaseUri = process.env.TRADIER_BASE_URI || 'https://sandbox.tradier.com/';
const optionsChain = `${tradierBaseUri}v1/markets/options/chains`;
const lookup = `${tradierBaseUri}v1/markets/lookup`;
const historical = `${tradierBaseUri}v1/markets/history`;
const optionsExpiration = `${tradierBaseUri}v1/markets/options/expirations`;
const getQuotes = `${tradierBaseUri}v1/markets/quotes`;

Expand Down Expand Up @@ -56,9 +58,11 @@ type Symbol = {
description: string
}

type LookupSymbolResponse = { securities: {
security: Symbol | Symbol[]
} }
type LookupSymbolResponse = {
securities: {
security: Symbol | Symbol[]
}
}

export const lookupSymbol = (q: string) => {
return client(lookup, {
Expand All @@ -68,4 +72,29 @@ export const lookupSymbol = (q: string) => {
}
}).json<LookupSymbolResponse>();

}

export const getPriceAtDate = async (s: string, dt: string) => {
console.log(`${s} -- ${dt}`);
const start = dayjs(dt.substring(0, 10)).format('YYYY-MM-DD');

const result = await client(historical, {
searchParams: {
'symbol': s,
'interval': 'daily',
'start': start,
'end': start, //dayjs(dt.substring(0, 10)).add(1, 'days').format('YYYY-MM-DD'),
'session_filter': 'all'
}
}).json<{
"history": {
"day": {
"date": string,
"open": number
}
}
}>();
const dtresult = result.history.day;
if (dtresult) return dtresult.open;
throw new Error('unable to determine price');
}

0 comments on commit 88a9b5e

Please sign in to comment.