diff --git a/.env.local_SAMPLE b/.env.local_SAMPLE index 8a7b4b1..0478bc5 100644 --- a/.env.local_SAMPLE +++ b/.env.local_SAMPLE @@ -1 +1,3 @@ -POSTGRES_PRISMA_URL= \ No newline at end of file +POSTGRES_PRISMA_URL= +AUTH_SECRET=test123 +NEXTAUTH_URL=https://p63xdw4l-3000.use.devtunnels.ms/ \ No newline at end of file diff --git a/src/app/api/schedules/updateOpenTradesLastContractPrice/route.ts b/src/app/api/schedules/updateOpenTradesLastContractPrice/route.ts index c2bf171..2f16d8f 100644 --- a/src/app/api/schedules/updateOpenTradesLastContractPrice/route.ts +++ b/src/app/api/schedules/updateOpenTradesLastContractPrice/route.ts @@ -16,7 +16,7 @@ export async function POST(request: Request) { strike: m.strikePrice.toNumber(), type: m.contractType.startsWith('CALL') ? 'CALLS' : 'PUTS' }) - const lastContractPrice = optionPrice?.lastPrice; + const lastContractPrice = optionPrice?.l; if (lastContractPrice) { await prisma.trade.update({ where: { diff --git a/src/app/api/symbols/[symbol]/options/analyze/route.ts b/src/app/api/symbols/[symbol]/options/analyze/route.ts index 5610145..386ef04 100644 --- a/src/app/api/symbols/[symbol]/options/analyze/route.ts +++ b/src/app/api/symbols/[symbol]/options/analyze/route.ts @@ -1,11 +1,10 @@ import ky from "ky"; import { NextResponse } from "next/server"; -import yf from 'yahoo-finance2'; -// export const runtime = 'edge'; +//export const runtime = 'edge'; export async function GET(request: Request, p: { params: { symbol: string } }) { - const resp = await ky(`https://www.optionsprofitcalculator.com/ajax/getOptions?stock=${p.params.symbol}&reqId=1`).json<{currentPrice: number | undefined}>(); - const priceResponse = await yf.quoteSummary(p.params.symbol); - resp.currentPrice = priceResponse.price?.regularMarketPrice; + const resp = await ky(`https://www.optionsprofitcalculator.com/ajax/getOptions?stock=${p.params.symbol}&reqId=1`).json<{ currentPrice: number | undefined }>(); + const priceresp = await ky(`https://www.optionsprofitcalculator.com/ajax/getStockPrice?stock=${p.params.symbol}&reqId=${new Date().getTime()}`).json<{ price: { last: number | undefined } }>(); + resp.currentPrice = priceresp.price.last; return NextResponse.json(resp); } diff --git a/src/app/api/symbols/[symbol]/summary/route.ts b/src/app/api/symbols/[symbol]/summary/route.ts index 09755e7..84e94d9 100644 --- a/src/app/api/symbols/[symbol]/summary/route.ts +++ b/src/app/api/symbols/[symbol]/summary/route.ts @@ -1,11 +1,17 @@ +import { StockPriceData } from "@/lib/types"; +import ky from "ky"; import { NextResponse } from "next/server"; -import yf from 'yahoo-finance2'; - -// export const runtime = 'edge'; //This specifies the runtime environment that the middleware function will be executed in. +//export const runtime = 'edge'; //This specifies the runtime environment that the middleware function will be executed in. export async function GET(request: Request, p: { params: { symbol: string } }) { - const resp = await yf.quoteSummary(p.params.symbol); + const priceresp = await ky(`https://www.optionsprofitcalculator.com/ajax/getStockPrice?stock=${p.params.symbol}&reqId=${new Date().getTime()}`, { + retry: { + limit: 3 + } + }).json<{ price: { last: number | undefined } }>(); return NextResponse.json({ - quoteSummary: resp - }); + quoteSummary: { + price: { regularMarketPrice: priceresp.price.last as number } + } + } as StockPriceData); } diff --git a/src/app/api/trades/route.ts b/src/app/api/trades/route.ts index 52e3521..f1e64a8 100644 --- a/src/app/api/trades/route.ts +++ b/src/app/api/trades/route.ts @@ -1,4 +1,3 @@ -import { getOptionPrice } from "@/lib/optionPriceHelper"; import prisma from "@/lib/prisma"; import { NextRequest, NextResponse } from "next/server"; // export const runtime = 'edge'; //This specifies the runtime environment that the middleware function will be executed in. diff --git a/src/lib/optionPriceHelper.ts b/src/lib/optionPriceHelper.ts index 31f2d9b..db16983 100644 --- a/src/lib/optionPriceHelper.ts +++ b/src/lib/optionPriceHelper.ts @@ -1,20 +1,33 @@ -import yf from 'yahoo-finance2'; +import ky from "ky"; + +type OptionsData = Record; + type optionPriceType = { symbol: string, strike: number, expirationDate: Date, type: 'CALLS' | 'PUTS' } export const getOptionPrice = async (args: optionPriceType) => { const { expirationDate, strike, symbol, type } = args; - const resp = await yf.options(args.symbol, { - date: expirationDate - }); - const existingOption = resp.options.find(r => r.expirationDate.getDate() == expirationDate.getDate()); + + const resp = await ky(`https://www.optionsprofitcalculator.com/ajax/getOptions?stock=${args.symbol}&reqId=1`).json<{ + options: Record + }>(); + + // console.log(resp); + const expiry = expirationDate.toISOString().substring(0, 10); + console.log(expiry); + + const existingOption = resp.options[expiry]; const pp = (() => { switch (type) { case 'CALLS': - return existingOption?.calls + return existingOption?.c case 'PUTS': - return existingOption?.puts + return existingOption?.p default: return null } - })()?.find(c => c.strike == strike) - return pp; + })(); + const sk = strike.toLocaleString(undefined, { minimumFractionDigits: 2 }); + return pp && pp[sk]; } \ No newline at end of file diff --git a/src/lib/socket.ts b/src/lib/socket.ts index 234f90f..b272135 100644 --- a/src/lib/socket.ts +++ b/src/lib/socket.ts @@ -2,6 +2,7 @@ import { useCallback, useEffect, useState } from 'react'; import { io } from 'socket.io-client'; import ky from 'ky'; +import { StockPriceData } from './types'; const URL = `https://tidy-spider-52.deno.dev` // const URL = `https://studious-telegram-4qq55vqj74hqgwp-8000.app.github.dev` @@ -107,15 +108,6 @@ type OptionsData = { }> } -type StockPriceData = { - quoteSummary: { - price: { - regularMarketPrice: number - } - - } -} - export type NumberRange = { start: number, end: number } export const useOptionTracker = (symbol: string) => { diff --git a/src/lib/types.ts b/src/lib/types.ts index cb7c893..121ca9d 100644 --- a/src/lib/types.ts +++ b/src/lib/types.ts @@ -14,3 +14,11 @@ export interface ITradeView extends Trade { isClosed: boolean, contractCurrentPrice?: number } + +export type StockPriceData = { + quoteSummary: { + price: { + regularMarketPrice: number + } + } +}