From 88a9b5e05c780d65e07d414aff19f4e7c276b045 Mon Sep 17 00:00:00 2001 From: mnsrulz Date: Tue, 10 Sep 2024 03:14:22 +0000 Subject: [PATCH] get rid of yf historical data feed --- .../api/symbols/[symbol]/historical/route.ts | 16 ++++----- src/lib/socket.ts | 11 ++++-- src/lib/tradierService.ts | 35 +++++++++++++++++-- 3 files changed, 48 insertions(+), 14 deletions(-) diff --git a/src/app/api/symbols/[symbol]/historical/route.ts b/src/app/api/symbols/[symbol]/historical/route.ts index 3679164..9fdcd63 100644 --- a/src/app/api/symbols/[symbol]/historical/route.ts +++ b/src/app/api/symbols/[symbol]/historical/route.ts @@ -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 }); } diff --git a/src/lib/socket.ts b/src/lib/socket.ts index fd1e9a0..7facdd6 100644 --- a/src/lib/socket.ts +++ b/src/lib/socket.ts @@ -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)); } diff --git a/src/lib/tradierService.ts b/src/lib/tradierService.ts index 9615f6c..b811757 100644 --- a/src/lib/tradierService.ts +++ b/src/lib/tradierService.ts @@ -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`; @@ -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, { @@ -68,4 +72,29 @@ export const lookupSymbol = (q: string) => { } }).json(); +} + +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'); } \ No newline at end of file