import numpy as np
import pandas as pd
import yfinance as yf
from sklearn.preprocessing import StandardScaler
from sklearn.feature_selection import SelectKBest, f_classif

# Function to fetch historical stock data
def fetch_stock_data(ticker, start_date, end_date):
    stock_data = yf.download(ticker, start=start_date, end=end_date)
    return stock_data

# Function to compute the Relative Strength Index (RSI)
def compute_rsi(close_prices):
    price_diff = close_prices.diff()
    positive_diff = price_diff.where(price_diff > 0, 0)
    negative_diff = -price_diff.where(price_diff < 0, 0)
    average_gain = positive_diff.rolling(window=14).mean()
    average_loss = negative_diff.rolling(window=14).mean()
    relative_strength = average_gain / average_loss
    rsi = 100 - (100 / (1 + relative_strength))
    return rsi

# Function to compute the Stochastic Oscillator
def compute_stochastic_oscillator(high_prices, low_prices, close_prices):
    highest_high = high_prices.rolling(window=14).max()
    lowest_low = low_prices.rolling(window=14).min()
    percent_k = ((close_prices - lowest_low) / (highest_high - lowest_low)) * 100
    percent_d = percent_k.rolling(window=3).mean()
    return percent_k, percent_d

# Function to compute technical indicators
def compute_indicators(stock_data):
    stock_data['MA_5'] = stock_data['Adj Close'].rolling(window=5).mean().shift()
    stock_data['MA_15'] = stock_data['Adj Close'].rolling(window=15).mean().shift()
    stock_data['RSI'] = compute_rsi(stock_data['Adj Close'])
    stock_data['%K'], stock_data['%D'] = compute_stochastic_oscillator(stock_data['High'], stock_data['Low'], stock_data['Close'])
    return stock_data

# Function to compute feature scores and rank stocks
def rank_stocks(start_date, end_date):
    # Fetch historical data for all available tickers
    all_tickers = yf.download('^GSPC', start=start_date, end=end_date)
    all_tickers = pd.DataFrame(all_tickers['Adj Close'])
    all_tickers = all_tickers.columns

    feature_scores = []

    for ticker in all_tickers:
        stock_data = fetch_stock_data(ticker, start_date, end_date)
        stock_data = compute_indicators(stock_data)
        features = stock_data[['MA_5', 'MA_15', 'RSI', '%K', '%D']].tail(1).values

        # Standardize the features
        scaler = StandardScaler()
        standardized_features = scaler.fit_transform(features)

        # Evaluate features using ANOVA F-value
        kbest = SelectKBest(f_classif, k=5)
        kbest.fit(standardized_features, np.ones((1,)))
        feature_scores.append((ticker, kbest.scores_.sum()))

    # Sort stocks by feature scores
    ranked_stocks = sorted(feature_scores, key=lambda x: x[1], reverse=True)[:10]

    return ranked_stocks


if __name__ == "__main__":
    # Specify the date range for stock data
    start_date = "2019-01-01"
    end_date = "2023-06-03"

    # Rank stocks based on technical indicators
    ranked_stocks = rank_stocks(start_date, end_date)

    # Print results
    print("Top 10 stocks based on technical indicators:")
    for rank, stock in enumerate(ranked_stocks, start=1):
        print(f"{rank}. {stock[0]} (Score: {stock[1]})")
 what is wrong with my code