|
import os |
|
import sqlite3 |
|
import pandas as pd |
|
import numpy as np |
|
import gradio as gr |
|
import yfinance as yf |
|
import tensorflow as tf |
|
from sklearn.model_selection import train_test_split |
|
from sklearn.preprocessing import StandardScaler, MinMaxScaler |
|
from sklearn.ensemble import RandomForestRegressor, RandomForestClassifier |
|
from sklearn.metrics import mean_squared_error, accuracy_score |
|
import joblib |
|
from datetime import datetime, timedelta |
|
|
|
class AdvancedHedgeFundManagementSystem: |
|
def __init__(self, db_path='data/advanced_hedge_fund_database.sqlite'): |
|
""" |
|
Initialize the Advanced Hedge Fund Management System |
|
|
|
Args: |
|
db_path (str): Path to the SQLite database |
|
""" |
|
self.db_path = db_path |
|
self.conn = None |
|
self.cursor = None |
|
|
|
|
|
self.investment_types = [ |
|
'Stocks', 'Bonds', 'ETFs', 'Mutual Funds', |
|
'Real Estate', 'Cryptocurrencies', 'Commodities', |
|
'Private Equity', 'Hedge Funds', 'Derivatives' |
|
] |
|
|
|
|
|
self.market_indices = [ |
|
'^GSPC', |
|
'^DJI', |
|
'^IXIC', |
|
'^RUT', |
|
] |
|
|
|
|
|
self.initialize_database() |
|
|
|
|
|
self.load_or_train_models() |
|
|
|
|
|
self.equity_etfs = ['SPY', 'QQQ', 'IWM', 'VOO', 'IVV'] |
|
self.bond_etfs = ['BND', 'AGG', 'LQD', 'TLT', 'SHY'] |
|
|
|
|
|
def initialize_database(self): |
|
""" |
|
Create database connection and initialize tables |
|
""" |
|
try: |
|
self.conn = sqlite3.connect(self.db_path) |
|
self.cursor = self.conn.cursor() |
|
|
|
|
|
self.cursor.execute(''' |
|
CREATE TABLE IF NOT EXISTS clients ( |
|
id INTEGER PRIMARY KEY, |
|
name TEXT NOT NULL, |
|
email TEXT UNIQUE, |
|
age INTEGER, |
|
income REAL, |
|
risk_tolerance INTEGER, |
|
total_investment REAL, |
|
investment_goals TEXT, |
|
created_at DATETIME DEFAULT CURRENT_TIMESTAMP |
|
) |
|
''') |
|
|
|
|
|
self.cursor.execute(''' |
|
CREATE TABLE IF NOT EXISTS portfolios ( |
|
id INTEGER PRIMARY KEY, |
|
client_id INTEGER, |
|
investment_type TEXT, |
|
asset_symbol TEXT, |
|
investment_amount REAL, |
|
purchase_price REAL, |
|
current_price REAL, |
|
performance REAL, |
|
risk_score REAL, |
|
allocation_percentage REAL, |
|
last_updated DATETIME DEFAULT CURRENT_TIMESTAMP, |
|
FOREIGN KEY (client_id) REFERENCES clients (id) |
|
) |
|
''') |
|
|
|
|
|
self.cursor.execute(''' |
|
CREATE TABLE IF NOT EXISTS transactions ( |
|
id INTEGER PRIMARY KEY, |
|
portfolio_id INTEGER, |
|
transaction_type TEXT, |
|
asset_symbol TEXT, |
|
amount REAL, |
|
quantity REAL, |
|
price REAL, |
|
date DATETIME DEFAULT CURRENT_TIMESTAMP, |
|
FOREIGN KEY (portfolio_id) REFERENCES portfolios (id) |
|
) |
|
''') |
|
|
|
|
|
self.cursor.execute(''' |
|
CREATE TABLE IF NOT EXISTS performance_tracking ( |
|
id INTEGER PRIMARY KEY, |
|
client_id INTEGER, |
|
total_investment REAL, |
|
current_value REAL, |
|
total_return REAL, |
|
annual_return REAL, |
|
last_updated DATETIME DEFAULT CURRENT_TIMESTAMP, |
|
FOREIGN KEY (client_id) REFERENCES clients (id) |
|
) |
|
''') |
|
|
|
self.conn.commit() |
|
except sqlite3.Error as e: |
|
print(f"Database initialization error: {e}") |
|
|
|
def get_real_time_market_data(self, symbols): |
|
""" |
|
Fetch real-time market data using Yahoo Finance |
|
|
|
Args: |
|
symbols (list): List of stock/asset symbols |
|
|
|
Returns: |
|
dict: Real-time market data |
|
""" |
|
market_data = {} |
|
for symbol in symbols: |
|
try: |
|
ticker = yf.Ticker(symbol) |
|
|
|
hist = ticker.history(period="1mo") |
|
market_data[symbol] = { |
|
'current_price': ticker.info.get('regularMarketPrice', 0), |
|
'previous_close': ticker.info.get('previousClose', 0), |
|
'volume': ticker.info.get('volume', 0), |
|
'market_cap': ticker.info.get('marketCap', 0), |
|
'beta': ticker.info.get('beta', 1.0), |
|
'volatility': hist['Close'].std() if not hist.empty else 0, |
|
'returns_1m': (hist['Close'][-1] / hist['Close'][0] - 1) * 100 if len(hist) > 1 else 0 |
|
} |
|
except Exception as e: |
|
print(f"Error fetching data for {symbol}: {e}") |
|
return market_data |
|
|
|
def generate_advanced_training_data(self, n_samples=5000): |
|
""" |
|
Generate advanced synthetic training data with more features |
|
|
|
Returns: |
|
tuple: Features and target variables |
|
""" |
|
np.random.seed(42) |
|
|
|
|
|
age = np.random.randint(25, 65, n_samples) |
|
income = np.random.uniform(30000, 1000000, n_samples) |
|
investment_experience = np.random.randint(0, 30, n_samples) |
|
current_investments = np.random.uniform(0, 5000000, n_samples) |
|
|
|
|
|
education_level = np.random.randint(1, 5, n_samples) |
|
family_size = np.random.randint(1, 6, n_samples) |
|
debt_to_income = np.random.uniform(0, 0.5, n_samples) |
|
|
|
|
|
X = np.column_stack([ |
|
age, income, investment_experience, current_investments, |
|
education_level, family_size, debt_to_income |
|
]) |
|
|
|
|
|
y_risk = ( |
|
(income / 50000) + |
|
(investment_experience / 10) - |
|
(debt_to_income * 10) + |
|
(education_level * 0.5) - |
|
(current_investments / 500000) |
|
) |
|
y_risk = np.clip(y_risk, 1, 10) |
|
|
|
|
|
y_portfolio_performance = ( |
|
(income / 25000) + |
|
(investment_experience / 5) + |
|
(current_investments / 250000) - |
|
(debt_to_income * 20) |
|
) |
|
y_portfolio_performance = np.clip(y_portfolio_performance, 0, 100) |
|
|
|
return X, y_risk, y_portfolio_performance |
|
|
|
def train_advanced_models(self): |
|
""" |
|
Train advanced machine learning models for risk and portfolio prediction |
|
""" |
|
|
|
X, y_risk, y_portfolio_performance = self.generate_advanced_training_data() |
|
|
|
|
|
X_train, X_test, y_risk_train, y_risk_test, y_perf_train, y_perf_test = train_test_split( |
|
X, y_risk, y_portfolio_performance, test_size=0.2, random_state=42 |
|
) |
|
|
|
|
|
scaler = StandardScaler() |
|
X_train_scaled = scaler.fit_transform(X_train) |
|
X_test_scaled = scaler.transform(X_test) |
|
|
|
|
|
risk_model = RandomForestRegressor(n_estimators=200, random_state=42) |
|
risk_model.fit(X_train_scaled, y_risk_train) |
|
y_risk_pred = risk_model.predict(X_test_scaled) |
|
risk_mse = mean_squared_error(y_risk_test, y_risk_pred) |
|
|
|
|
|
portfolio_model = RandomForestRegressor(n_estimators=200, random_state=42) |
|
portfolio_model.fit(X_train_scaled, y_perf_train) |
|
y_perf_pred = portfolio_model.predict(X_test_scaled) |
|
perf_mse = mean_squared_error(y_perf_test, y_perf_pred) |
|
|
|
|
|
|
|
y_investment_type = np.random.choice(len(self.investment_types), len(X)) |
|
X_type_train, X_type_test, y_type_train, y_type_test = train_test_split( |
|
X, y_investment_type, test_size=0.2, random_state=42 |
|
) |
|
X_type_train_scaled = scaler.transform(X_type_train) |
|
X_type_test_scaled = scaler.transform(X_type_test) |
|
|
|
investment_type_model = RandomForestClassifier(n_estimators=200, random_state=42) |
|
investment_type_model.fit(X_type_train_scaled, y_type_train) |
|
y_type_pred = investment_type_model.predict(X_type_test_scaled) |
|
type_accuracy = accuracy_score(y_type_test, y_type_pred) |
|
|
|
|
|
joblib.dump({ |
|
'risk_model': risk_model, |
|
'portfolio_model': portfolio_model, |
|
'investment_type_model': investment_type_model, |
|
'scaler': scaler |
|
}, 'advanced_hedge_fund_models.joblib') |
|
|
|
|
|
print(f"Risk Prediction MSE: {risk_mse}") |
|
print(f"Portfolio Performance MSE: {perf_mse}") |
|
print(f"Investment Type Recommendation Accuracy: {type_accuracy}") |
|
|
|
return risk_model, portfolio_model, investment_type_model, scaler |
|
|
|
def load_or_train_models(self): |
|
""" |
|
Load existing models or train new predictive models |
|
""" |
|
try: |
|
|
|
models = joblib.load('advanced_hedge_fund_models.joblib') |
|
self.risk_model = models['risk_model'] |
|
self.portfolio_model = models['portfolio_model'] |
|
self.investment_type_model = models['investment_type_model'] |
|
self.scaler = models['scaler'] |
|
except (FileNotFoundError, KeyError): |
|
|
|
(self.risk_model, |
|
self.portfolio_model, |
|
self.investment_type_model, |
|
self.scaler) = self.train_advanced_models() |
|
|
|
def recommend_portfolio_allocation(self, client_features, target_return=0.15): |
|
""" |
|
Recommend portfolio allocation with 15% target return |
|
|
|
Args: |
|
client_features (numpy.ndarray): Client features |
|
target_return (float): Target annual return percentage |
|
|
|
Returns: |
|
dict: Recommended portfolio allocation |
|
""" |
|
|
|
scaled_features = self.scaler.transform(client_features.reshape(1, -1)) |
|
|
|
|
|
risk_score = float(self.risk_model.predict(scaled_features)[0]) |
|
investment_type_index = int(self.investment_type_model.predict(scaled_features)[0]) |
|
recommended_investment_type = self.investment_types[investment_type_index] |
|
|
|
|
|
market_data = self.get_real_time_market_data(self.market_indices) |
|
|
|
|
|
base_allocation = { |
|
'Stocks': 0.4, |
|
'Bonds': 0.3, |
|
'ETFs': 0.15, |
|
'Real Estate': 0.1, |
|
'Other Alternatives': 0.05 |
|
} |
|
|
|
|
|
if risk_score <= 3: |
|
base_allocation = { |
|
'Bonds': 0.6, |
|
'Stocks': 0.2, |
|
'ETFs': 0.1, |
|
'Real Estate': 0.05, |
|
'Other Alternatives': 0.05 |
|
} |
|
elif risk_score >= 8: |
|
base_allocation = { |
|
'Stocks': 0.5, |
|
'ETFs': 0.2, |
|
'Real Estate': 0.1, |
|
'Cryptocurrencies': 0.1, |
|
'Other Alternatives': 0.1 |
|
} |
|
|
|
|
|
portfolio_recommendation = { |
|
'risk_score': risk_score, |
|
'recommended_investment_type': recommended_investment_type, |
|
'allocation': base_allocation, |
|
'target_return': target_return, |
|
'market_indices': market_data |
|
} |
|
|
|
return portfolio_recommendation |
|
|
|
def analyze_equity_bonds(self, risk_tolerance, investment_amount, investment_timeframe, sector_preference = None): |
|
"""Analyze Equities and Bonds and recommend investments |
|
Args: |
|
risk_tolerance: (str) "Low", "Medium", "High" |
|
investment_amount: (float) total investment amount |
|
investment_timeframe: (str) "Short term", "Medium term", "Long term" |
|
sector_preference: (str) for equities, can be "Tech", "Healthcare", "Finance", etc. |
|
""" |
|
|
|
|
|
equity_data = self.get_real_time_market_data(self.equity_etfs) |
|
equity_df = pd.DataFrame.from_dict(equity_data, orient='index') |
|
|
|
|
|
bond_data = self.get_real_time_market_data(self.bond_etfs) |
|
bond_df = pd.DataFrame.from_dict(bond_data, orient='index') |
|
|
|
|
|
for symbol in self.equity_etfs + self.bond_etfs: |
|
ticker = yf.Ticker(symbol) |
|
hist = ticker.history(period="1y") |
|
if not hist.empty: |
|
returns = hist['Close'].pct_change().dropna() |
|
equity_df.loc[symbol, 'annual_return'] = returns.mean() * 252 |
|
else: |
|
equity_df.loc[symbol, 'annual_return'] = 0 |
|
|
|
|
|
equity_df['risk_adjusted_return'] = equity_df['annual_return'] / equity_df['volatility'] if equity_df['volatility'].any() > 0 else 0 |
|
if sector_preference: |
|
|
|
sector_etfs_dict = { |
|
"Technology": ["XLK","VGT"], |
|
"Healthcare":["XLV","VHT"], |
|
"Finance": ["XLF", "VFH"] |
|
} |
|
sector_etfs = sector_etfs_dict.get(sector_preference, None) |
|
if sector_etfs: |
|
sector_data = self.get_real_time_market_data(sector_etfs) |
|
sector_df = pd.DataFrame.from_dict(sector_data, orient='index') |
|
|
|
|
|
for symbol in sector_etfs: |
|
ticker = yf.Ticker(symbol) |
|
hist = ticker.history(period="1y") |
|
if not hist.empty: |
|
returns = hist['Close'].pct_change().dropna() |
|
sector_df.loc[symbol, 'annual_return'] = returns.mean() * 252 |
|
else: |
|
sector_df.loc[symbol, 'annual_return'] = 0 |
|
|
|
sector_df['risk_adjusted_return'] = sector_df['annual_return'] / sector_df['volatility'] if sector_df['volatility'].any() > 0 else 0 |
|
|
|
|
|
equity_df = pd.concat([equity_df, sector_df]) |
|
|
|
bond_df['risk_adjusted_return'] = bond_df['returns_1m'] / bond_df['volatility'] if bond_df['volatility'].any() > 0 else 0 |
|
|
|
|
|
|
|
if risk_tolerance == "Low": |
|
equity_recommendations = equity_df.sort_values(by=['volatility']).head(3).index.tolist() |
|
bond_recommendations = bond_df.sort_values(by=['risk_adjusted_return'], ascending=False).head(2).index.tolist() |
|
elif risk_tolerance == "Medium": |
|
equity_recommendations = equity_df.sort_values(by=['risk_adjusted_return'], ascending=False).head(3).index.tolist() |
|
bond_recommendations = bond_df.sort_values(by=['risk_adjusted_return'], ascending=False).head(2).index.tolist() |
|
else: |
|
equity_recommendations = equity_df.sort_values(by=['risk_adjusted_return'], ascending=False).head(3).index.tolist() |
|
bond_recommendations = bond_df.sort_values(by=['volatility'], ascending=False).head(2).index.tolist() |
|
|
|
|
|
|
|
if investment_timeframe == 'Short term': |
|
allocation = {'Bonds':0.6, 'Equities':0.4} |
|
elif investment_timeframe == 'Medium term': |
|
allocation = {'Bonds': 0.4, 'Equities':0.6} |
|
else: |
|
allocation = {'Bonds':0.3, 'Equities':0.7} |
|
|
|
formatted_output = f"Recommended allocation: Bonds {allocation['Bonds'] * 100:.2f}%, Equities: {allocation['Equities'] * 100:.2f}% \n" |
|
formatted_output += f"Equity Recommendations: \n" |
|
for rec in equity_recommendations: |
|
formatted_output += f" - {rec}\n" |
|
formatted_output += f"Bond Recommendations: \n" |
|
for rec in bond_recommendations: |
|
formatted_output += f" - {rec}\n" |
|
|
|
return formatted_output |
|
|
|
|
|
def create_gradio_interface(self): |
|
""" |
|
Create Gradio interface for the Advanced Hedge Fund Management System |
|
|
|
Returns: |
|
gr.Interface: Gradio web interface |
|
""" |
|
def analyze_investment_profile(age, income, investment_experience, |
|
current_investments, education_level, |
|
family_size, debt_to_income): |
|
""" |
|
Comprehensive investment profile analysis |
|
""" |
|
|
|
client_features = np.array([ |
|
age, income, investment_experience, current_investments, |
|
education_level, family_size, debt_to_income |
|
]) |
|
|
|
|
|
recommendation = self.recommend_portfolio_allocation(client_features) |
|
|
|
|
|
allocation_str = "\n".join([ |
|
f"{k}: {v*100:.2f}%" for k, v in recommendation['allocation'].items() |
|
]) |
|
|
|
output = ( |
|
f"Risk Score: {recommendation['risk_score']:.2f}/10\n" |
|
f"Recommended Investment Type: {recommendation['recommended_investment_type']}\n" |
|
f"Target Annual Return: {recommendation['target_return']*100:.2f}%\n\n" |
|
"Portfolio Allocation:\n" + allocation_str + "\n\n" |
|
"Market Indices Performance:\n" + |
|
"\n".join([ |
|
f"{symbol}: {data['returns_1m']:.2f}% (1M Return)" |
|
for symbol, data in recommendation['market_indices'].items() |
|
]) |
|
) |
|
|
|
return output |
|
|
|
def analyze_equity_bond_investments(risk_tolerance, investment_amount, investment_timeframe, sector_preference): |
|
""" |
|
Analyze equity and bond investments based on users preferences |
|
""" |
|
|
|
return self.analyze_equity_bonds(risk_tolerance, investment_amount, investment_timeframe, sector_preference) |
|
|
|
interface = gr.TabbedInterface( |
|
[ |
|
gr.Interface( |
|
fn=analyze_investment_profile, |
|
inputs=[ |
|
gr.Number(label="Age"), |
|
gr.Number(label="Annual Income"), |
|
gr.Number(label="Investment Experience (Years)"), |
|
gr.Number(label="Current Investments"), |
|
gr.Dropdown(label="Education Level", choices=[1, 2, 3, 4]), |
|
gr.Number(label="Family Size"), |
|
gr.Number(label="Debt-to-Income Ratio") |
|
], |
|
outputs=gr.Textbox(label="Investment Profile Analysis"), |
|
title="Investment Profile Analysis", |
|
description="Get personalized recommendations based on your profile" |
|
), |
|
gr.Interface( |
|
fn=analyze_equity_bond_investments, |
|
inputs=[ |
|
gr.Dropdown(label="Risk Tolerance", choices=["Low", "Medium", "High"]), |
|
gr.Number(label="Investment Amount"), |
|
gr.Dropdown(label="Investment Timeframe", choices=["Short term", "Medium term", "Long term"]), |
|
gr.Dropdown(label="Sector Preference", choices = ["Technology","Healthcare", "Finance", None] ) |
|
], |
|
outputs=gr.Textbox(label="Equity and Bond Analysis"), |
|
title="Equity and Bond Analysis", |
|
description="Analyze and get recommended Equities and Bonds based on your parameters" |
|
) |
|
], |
|
tab_names=["Investment Profile","Equity and Bond Analyzer"], |
|
title="Advanced Hedge Fund Management System", |
|
|
|
) |
|
|
|
return interface |
|
|
|
def run_gradio_app(self): |
|
""" |
|
Run the Gradio web application |
|
""" |
|
interface = self.create_gradio_interface() |
|
interface.launch() |
|
|
|
if __name__ == "__main__": |
|
hedge_fund_system = AdvancedHedgeFundManagementSystem() |
|
hedge_fund_system.run_gradio_app() |