StephanAkkerman / fintwit-bot

FinTwit-Bot is a Discord bot designed to track and analyze financial markets by pulling data from platforms like Twitter, Reddit, and Binance. It features customizable tools for sentiment analysis, market trends, and portfolio tracking to help traders stay informed and make data-driven decisions.
http://akkerman.ai/fintwit-bot/
MIT License
63 stars 14 forks source link

Improve ticker handling for multiple tickers #507

Open StephanAkkerman opened 6 months ago

StephanAkkerman commented 6 months ago

We could make it quicker because many libraries support querying multiple tickers at the same time: yahooquery:

from yahooquery import Ticker

symbols = ['fb', 'aapl', 'amzn', 'nflx', 'goog']

faang = Ticker(symbols)

faang.summary_detail

tradingview_ta also supports it, ccxt might, coingecko?

StephanAkkerman commented 1 month ago

Yahoo: (from https://stackoverflow.com/questions/76065035/yahoo-finance-v7-api-now-requiring-cookies-python)

import requests

apiBase = "https://query2.finance.yahoo.com"
headers = {"User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64)"}

def getCredentials(
    cookieUrl="https://fc.yahoo.com", crumbUrl=apiBase + "/v1/test/getcrumb"
):
    cookie = requests.get(cookieUrl).cookies
    crumb = requests.get(url=crumbUrl, cookies=cookie, headers=headers).text
    return {"cookie": cookie, "crumb": crumb}

def quote(symbols, credentials):
    url = apiBase + "/v7/finance/quote"
    params = {"symbols": ",".join(symbols), "crumb": credentials["crumb"]}
    response = requests.get(
        url, params=params, cookies=credentials["cookie"], headers=headers
    )
    quotes = response.json()["quoteResponse"]["result"]
    return quotes

credentials = getCredentials()
quotes = quote(["GOOG", "TSLA"], credentials)
print(quotes)