Spaces:
Runtime error
Runtime error
| from flask import Flask, jsonify, render_template | |
| import yfinance as yf | |
| import numpy as np | |
| from datetime import datetime | |
| import time | |
| app = Flask(__name__) | |
| def index(): | |
| return render_template('index.html') | |
| def fetch_stock_data(): | |
| end_date = datetime.now().strftime('%Y-%m-%d') | |
| sharpe_threshold = 0.04 | |
| top_ten_tickers = ['AAPL', 'MSFT', 'GOOGL', 'AMZN', 'FB', 'TSLA', 'JPM', 'BAC', 'WMT', 'V'] | |
| sharpe_ratios = [] | |
| # Simulating a delay to demonstrate progress | |
| total_tasks = len(top_ten_tickers) | |
| for i, ticker_symbol in enumerate(top_ten_tickers): | |
| time.sleep(1) # Simulate a delay of 1 second (adjust for real data retrieval) | |
| # Simulated progress update | |
| progress = int((i + 1) / total_tasks * 100) | |
| print(f"Progress: {progress}%") | |
| # Rest of your code to fetch data, calculate Sharpe ratio, and append to sharpe_ratios | |
| # Once all tasks are completed, return the results | |
| sorted_sharpe_ratios = sorted(sharpe_ratios, key=lambda x: x[1], reverse=True) | |
| top_ten = [{'Ticker': ticker, 'Sharpe Ratio': ratio} for ticker, ratio in sorted_sharpe_ratios[:10]] | |
| return jsonify({'top_ten_sharpe_ratios': top_ten}) | |
| if __name__ == '__main__': | |
| app.run(debug=True) | |