Viewing File: /home/ubuntu/codegamaai-test/broker_bot/resources/stock_apis/get_stock_info.py
import requests
import yfinance as yf
import datetime
import pandas as pd
import json
import numpy as np
def process_historical_data(historical_data, symbol):
# Convert Json to DataFrame
df = pd.DataFrame(historical_data)
# If 'Date' is not in datetime format, convert it
df['Date'] = pd.to_datetime(df['Date'])
# Calculate Daily Returns
df['Daily_Return'] = df['Close'].pct_change()
# Calculate Average Daily Returns
average_daily_return = df['Daily_Return'].mean()
# Calculate Volatility
volatility = df['Daily_Return'].std()
# Get the max and min of the 'Close' column
max_close = df['Close'].max()
min_close = df['Close'].min()
# Get the date of the max and min close
max_close_date = df[df['Close'] == max_close]['Date'].values[0]
min_close_date = df[df['Close'] == min_close]['Date'].values[0]
# Percentage change in the 'Close' column
percentage_change = ((max_close - min_close) / min_close) * 100
# Highest Stock Price
highest_stock_price = df['High'].max()
highest_stock_price_date = df[df['High'] == highest_stock_price]['Date'].values[0]
# Lowest Stock Price
lowest_stock_price = df['Low'].min()
lowest_stock_price_date = df[df['Low'] == lowest_stock_price]['Date'].values[0]
# Difference between the highest and lowest stock price
difference = highest_stock_price - lowest_stock_price
# Average Stock Price
average_stock_price = df['Close'].mean()
# Average Volume
average_volume = df['Volume'].mean()
# Average Dividends
average_dividends = df['Dividends'].mean()
# Average Stock Splits
average_stock_splits = df['Stock Splits'].mean()
response_string = f"""
Historical Data for {symbol}:
Average Daily Return: {average_daily_return}
Volatility: {volatility}
Max Close: {max_close} on {max_close_date}
Min Close: {min_close} on {min_close_date}
Percentage Change: {percentage_change}
Highest Stock Price: {highest_stock_price} on {highest_stock_price_date}
Lowest Stock Price: {lowest_stock_price} on {lowest_stock_price_date}
Difference: {difference}
Average Stock Price: {average_stock_price}
Average Volume: {average_volume}
Average Dividends: {average_dividends}
Average Stock Splits: {average_stock_splits}
"""
return response_string
def historical_stock_data(symbol, interval):
stock = yf.Ticker(symbol)
# data = stock.history(period='1mo')
data = stock.history(period = interval)
# 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)
response = process_historical_data(historical_data, symbol)
return response
def stock_info(symbol):
stock = yf.Ticker(symbol)
info = stock.info
response = {
"Current Price": info.get('currentPrice', 'N/A'),
"High Price": info.get('dayHigh', 'N/A'),
"Low Price": info.get('dayLow', 'N/A'),
"Open Price": info.get('open', 'N/A'),
"Previous Close": info.get('previousClose', 'N/A'),
"Fifty Two Week Low": info.get('fiftyTwoWeekLow', 'N/A'),
"Fifty Two Week High": info.get('fiftyTwoWeekHigh', 'N/A'),
"Currency": info.get('currency', 'N/A'),
"Market Cap": info.get('marketCap', 'N/A'),
"Target Mean Price": info.get('targetMeanPrice', 'N/A')
}
response_string = f"""Stock Info for {symbol}:\n
Current Price: {response['Current Price']}\n
High Price: {response['High Price']}\n
Low Price: {response['Low Price']}\n
Open Price: {response['Open Price']}\n
Previous Close: {response['Previous Close']}\n
52 Week Low: {response['Fifty Two Week Low']}\n
52 Week High: {response['Fifty Two Week High']}\n
Market Cap: {response['Market Cap']}\n
Currency: {response['Currency']}\n
Target Mean Price: {response['Target Mean Price']}\n"""
return response_string
Back to Directory
File Manager