somebody please help me earlier I was using this code to fetch data from yfinance when I was working with windows and still its working in windows vs code , but recently I shifted to a MacBook where I am getting continuously error somebody please help me to solve this problem, is this any permission settings need to be changed on Mac so that I can fetch data from finance
import numpy as np
import pandas as pd
import yfinance as yf
from scipy.optimize import minimize
# Define the tickers for the Indian stocks
# Example tickers for Reliance, TCS, and HDFC Bank
tickers = ['HAPPSTMNDS.NS', 'SBICARD.BO', 'NCC.BO']
# Retrieve historical prices using yfinance
prices = yf.download(tickers, start='2022-01-01',
end='2022-12-31')['Adj Close']
# Calculate logarithmic returns
returns = np.log(prices / prices.shift(1)).dropna()
# Calculate expected returns and covariance matrix
expected_returns = returns.mean().values
cov_matrix = returns.cov().values
# Define the objective function to minimize portfolio volatility
def portfolio_volatility(weights, cov_matrix):
portfolio_variance = np.dot(weights.T, np.dot(cov_matrix, weights))
return np.sqrt(portfolio_variance)
# Define the constraints for portfolio optimization
# Sum of weights equals 1
constraints = [{'type': 'eq', 'fun': lambda x: np.sum(x) - 1}]
bounds = tuple((0, 1) for _ in range(len(tickers))) # Bounds for each weight
# Perform portfolio optimization
# Initial equal weights for all assets
initial_weights = len(tickers) * [1 / len(tickers)]
optimal_weights = minimize(portfolio_volatility, initial_weights, args=(cov_matrix,),
method='SLSQP', constraints=constraints, bounds=bounds)
# Print the optimal portfolio weights and expected return
print("Optimal Portfolio Weights:")
for i, weight in enumerate(optimal_weights.x):
print(f"Asset {tickers[i]}: {weight:.2f}")
optimal_return = np.dot(optimal_weights.x, expected_returns)
print("\nOptimal Portfolio Expected Return:", optimal_return)
the error I am getting is this
[ 0%% ]
[**********************67%%****** ] 2 of 3 completed
[*********************100%%**********************] 3 of 3 completed
3 Failed downloads:
['NCC.BO', 'HAPPSTMNDS.NS', 'SBICARD.BO']: OperationalError('unable to open database file')
/Users/akhiltom/anaconda3/lib/python3.11/site-packages/numpy/lib/function_base.py:518: RuntimeWarning: Mean of empty slice.
avg = a.mean(axis, **keepdims_kw)
/Users/akhiltom/anaconda3/lib/python3.11/site-packages/numpy/core/_methods.py:184: RuntimeWarning: invalid value encountered in divide
ret = um.true_divide(
/Users/akhiltom/anaconda3/lib/python3.11/site-packages/pandas/core/frame.py:10474: RuntimeWarning: Degrees of freedom <= 0 for slice
base_cov = np.cov(mat.T, ddof=ddof)
/Users/akhiltom/anaconda3/lib/python3.11/site-packages/numpy/lib/function_base.py:2705: RuntimeWarning: divide by zero encountered in divide
c *= np.true_divide(1, fact)
/Users/akhiltom/anaconda3/lib/python3.11/site-packages/numpy/lib/function_base.py:2705: RuntimeWarning: invalid value encountered in multiply
c *= np.true_divide(1, fact)
Optimal Portfolio Weights:
Asset HAPPSTMNDS.NS: 0.33
Asset SBICARD.BO: 0.33
Asset NCC.BO: 0.33
in every code related to yfinance I am getting this same error OperationalError(‘unable to open database file’)