Viewing File: /home/ubuntu/codegamaai-test/broker_bot/archive/f_help.py

"""
import yfinance as yf

msft = yf.Ticker("MSFT")
msft.info
print(msft.info)

hist = msft.history(period="1d")
print(hist)
"""

import yfinance as yf


def fetch_stock_data(symbol, intent):
    if intent == 'view_stocks':
        return f"These are the stocks available on your account"
    elif intent == 'historical_stock_data':
        stock = yf.Ticker(symbol)
        data = stock.history(period='1mo')

        # Convert DataFrame to a list of dictionaries
        historical_data = []
        for date, row in data.iterrows():
            daily_data = {
                "Date": date.strftime('%Y-%m-%d'),
                "Open": row['Open'],
                "High": row['High'],
                "Low": row['Low'],
                "Close": row['Close'],
                "Volume": row['Volume'],
                "Dividends": row['Dividends'],
                "Stock Splits": row['Stock Splits']
            }
            historical_data.append(daily_data)

        return historical_data
    elif intent == 'stock_info':
        stock = yf.Ticker(symbol)
        info = stock.info
        stock_info = {
            "Current Price": info.get('currentPrice', 'N/A'),
            "High Price of the Day": info.get('dayHigh', 'N/A'),
            "Low Price of the Day": info.get('dayLow', 'N/A'),
            "Open Price of the Day": info.get('open', 'N/A'),
            "Previous Close Price": info.get('previousClose', 'N/A'),
        }
        return stock_info
    elif intent in ['buy_stocks', 'sell_stocks']:
        return "Trading actions ('buy_stocks', 'sell_stocks') are not supported through this API."
    else:
        return "Invalid intent specified"


# Example usages
symbol = "AAPL"
print(fetch_stock_data(symbol, "view_stocks"))
print(fetch_stock_data(symbol, "historical_stock_data"))
print(fetch_stock_data(symbol, "stock_info"))
Back to Directory File Manager