Skip to content

Stock

Functions:

get_stock_data(symbol, start_date, end_date)

Get stock proces for a given symbol and date range.

Parameters:

  • symbol (str) –

    Stock symbol.

  • start_date (str) –

    Start date in the format 'YYYY-MM-DD'.

  • end_date (str) –

    End date in the format 'YYYY-MM-DD'.

Returns:

  • list[tuple]

    List of tuples, each tuple contains a 'YYYY-MM-DD' date and the stock price.

Source code in tapeagents/tools/stock.py
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
def get_stock_data(symbol: str, start_date: str, end_date: str):
    """Get stock proces for a given symbol and date range.

    Args:
        symbol (str): Stock symbol.
        start_date (str): Start date in the format 'YYYY-MM-DD'.
        end_date (str): End date in the format 'YYYY-MM-DD'.

    Returns:
        (list[tuple]): List of tuples, each tuple contains a 'YYYY-MM-DD' date and the stock price.
    """
    symbol = symbol.upper()
    # parse timestamps using datetime
    start_timestamp = int(datetime.datetime.strptime(start_date, "%Y-%m-%d").timestamp())
    end_timestamp = int(datetime.datetime.strptime(end_date, "%Y-%m-%d").timestamp())

    url = f"https://query1.finance.yahoo.com/v8/finance/chart/{symbol}?period1={start_timestamp}&period2={end_timestamp}&interval=1d"

    try:
        # make a request to Yahoo Finance API
        response = requests.get(url, headers={"User-Agent": "Mozilla/5.0"})
        response.raise_for_status()
        data = response.json()

        timestamps = data["chart"]["result"][0]["timestamp"]
        prices = data["chart"]["result"][0]["indicators"]["quote"][0]["close"]
        while len(timestamps) > 100:
            timestamps = timestamps[::2]
            prices = prices[::2]

        return list(
            zip(
                [datetime.datetime.fromtimestamp(ts, datetime.timezone.utc).strftime("%Y-%m-%d") for ts in timestamps],
                prices,
            )
        )
    except Exception as e:
        print(f"An error occurred: {e}")
        return None

get_stock_ticker(company_name)

Get company stock ticker from its name.

Source code in tapeagents/tools/stock.py
 6
 7
 8
 9
10
11
12
13
14
15
16
def get_stock_ticker(company_name: str):
    """Get company stock ticker from its name."""
    yfinance = "https://query2.finance.yahoo.com/v1/finance/search"
    user_agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36"
    params = {"q": company_name, "quotes_count": 1, "country": "United States"}

    res = requests.get(url=yfinance, params=params, headers={"User-Agent": user_agent})
    data = res.json()

    company_code = data["quotes"][0]["symbol"]
    return company_code