-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
using another service for lookup symbols
- Loading branch information
Showing
7 changed files
with
152 additions
and
91 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,27 @@ | ||
import { lookupSymbol } from "@/lib/tradierService"; | ||
import { NextRequest, NextResponse } from "next/server"; | ||
import yf from 'yahoo-finance2'; | ||
|
||
export async function GET(request: NextRequest, p: { params: { symbol: string } }) { | ||
export async function GET(request: NextRequest) { | ||
const q = request.nextUrl.searchParams.get('q'); | ||
if (!q) return NextResponse.json({ 'error': 'q parameter is not provided in the request.' }, { | ||
status: 400 | ||
}); | ||
const resp = await yf.search(q); | ||
|
||
const res = await lookupSymbol(q) | ||
if (!res.securities) return NextResponse.json({ items: [] }); | ||
const { security } = res.securities | ||
const t1 = Array.isArray(security) ? security : [security]; | ||
return NextResponse.json({ | ||
items: resp | ||
items: t1.map(j => { | ||
return { | ||
symbol: j.symbol, | ||
name: j.description | ||
}; | ||
}) | ||
}); | ||
|
||
// const resp = await yf.search(q); | ||
// return NextResponse.json({ | ||
// items: resp | ||
// }); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import ky from "ky"; | ||
import { TradierOptionData } from "./types"; | ||
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 optionsExpiration = `${tradierBaseUri}v1/markets/options/expirations`; | ||
const getQuotes = `${tradierBaseUri}v1/markets/quotes`; | ||
|
||
const client = ky.create({ | ||
headers: { | ||
'Authorization': `Bearer ${process.env.TRADIER_TOKEN}`, | ||
'Accept': 'application/json' | ||
}, | ||
cache: 'no-cache' | ||
}); | ||
|
||
export const getOptionExpirations = (symbol: string) => { | ||
return client(optionsExpiration, { | ||
searchParams: { | ||
symbol | ||
} | ||
}).json<{ expirations: { date: string[] } }>(); | ||
|
||
} | ||
|
||
export const getOptionData = (symbol: string, expiration: string) => { | ||
return client(optionsChain, { | ||
searchParams: { | ||
symbol, | ||
expiration, | ||
'greeks': 'true' | ||
} | ||
}).json<TradierOptionData>(); | ||
} | ||
|
||
export const getCurrentPrice = async (symbol: string) => { | ||
const cp = await client(getQuotes, { | ||
searchParams: { | ||
symbols: symbol | ||
} | ||
}).json<{ | ||
quotes: { | ||
quote: { | ||
symbol: string, | ||
last: number | ||
} | ||
} | ||
}>(); | ||
return cp.quotes.quote | ||
//.find(x => x.symbol === symbol)? | ||
.last; | ||
} | ||
|
||
type Symbol = { | ||
symbol: string, | ||
description: string | ||
} | ||
|
||
type LookupSymbolResponse = { securities: { | ||
security: Symbol | Symbol[] | ||
} } | ||
|
||
export const lookupSymbol = (q: string) => { | ||
return client(lookup, { | ||
searchParams: { | ||
q, | ||
//'types': 'stock, etf, index' | ||
} | ||
}).json<LookupSymbolResponse>(); | ||
|
||
} |