Skip to content

Commit

Permalink
get rid of yahoo finance api for now
Browse files Browse the repository at this point in the history
  • Loading branch information
mnsrulz committed Apr 16, 2024
1 parent 1ec89e4 commit 743b7a7
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 32 deletions.
4 changes: 3 additions & 1 deletion .env.local_SAMPLE
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
POSTGRES_PRISMA_URL=
POSTGRES_PRISMA_URL=
AUTH_SECRET=test123
NEXTAUTH_URL=https://p63xdw4l-3000.use.devtunnels.ms/
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
9 changes: 4 additions & 5 deletions src/app/api/symbols/[symbol]/options/analyze/route.ts
Original file line number Diff line number Diff line change
@@ -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);
}
18 changes: 12 additions & 6 deletions src/app/api/symbols/[symbol]/summary/route.ts
Original file line number Diff line number Diff line change
@@ -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);
}
1 change: 0 additions & 1 deletion src/app/api/trades/route.ts
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
31 changes: 22 additions & 9 deletions src/lib/optionPriceHelper.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,33 @@
import yf from 'yahoo-finance2';
import ky from "ky";

type OptionsData = Record<string, { "l": 0.01, }>;

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<string, {
c: OptionsData,
p: OptionsData
}>
}>();

// 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];
}
10 changes: 1 addition & 9 deletions src/lib/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down Expand Up @@ -107,15 +108,6 @@ type OptionsData = {
}>
}

type StockPriceData = {
quoteSummary: {
price: {
regularMarketPrice: number
}

}
}

export type NumberRange = { start: number, end: number }

export const useOptionTracker = (symbol: string) => {
Expand Down
8 changes: 8 additions & 0 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,11 @@ export interface ITradeView extends Trade {
isClosed: boolean,
contractCurrentPrice?: number
}

export type StockPriceData = {
quoteSummary: {
price: {
regularMarketPrice: number
}
}
}

0 comments on commit 743b7a7

Please sign in to comment.