Spaces:
				
			
			
	
			
			
		Sleeping
		
	
	
	
			
			
	
	
	
	
		
		
		Sleeping
		
	Update app.py
Browse files
    	
        app.py
    CHANGED
    
    | @@ -5,6 +5,7 @@ import numpy as np | |
| 5 | 
             
            import pandas as pd
         | 
| 6 | 
             
            import matplotlib.pyplot as plt
         | 
| 7 | 
             
            from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
         | 
|  | |
| 8 |  | 
| 9 | 
             
            # Polymarket GraphQL API endpoint
         | 
| 10 | 
             
            POLYMARKET_API = "https://api.polymarket.com/graphql"
         | 
| @@ -40,7 +41,7 @@ def fetch_polymarket_data(search_term="S&P"): | |
| 40 | 
             
                    for market in markets:
         | 
| 41 | 
             
                        node = market["node"]
         | 
| 42 | 
             
                        outcomes = node["outcomes"]
         | 
| 43 | 
            -
                        if len(outcomes) >= 2: | 
| 44 | 
             
                            return {
         | 
| 45 | 
             
                                "question": node["question"],
         | 
| 46 | 
             
                                "outcomes": {outcome["name"]: float(outcome["price"]) for outcome in outcomes}
         | 
| @@ -49,21 +50,27 @@ def fetch_polymarket_data(search_term="S&P"): | |
| 49 | 
             
                except Exception as e:
         | 
| 50 | 
             
                    return None
         | 
| 51 |  | 
| 52 | 
            -
            # Function to fetch Yahoo Finance data  | 
| 53 | 
            -
            def fetch_yahoo_data(ticker):
         | 
| 54 | 
            -
                 | 
| 55 | 
            -
                     | 
| 56 | 
            -
             | 
| 57 | 
            -
                         | 
| 58 | 
            -
             | 
| 59 | 
            -
             | 
| 60 | 
            -
                         | 
| 61 | 
            -
             | 
| 62 | 
            -
             | 
| 63 | 
            -
             | 
| 64 | 
            -
             | 
| 65 | 
            -
             | 
| 66 | 
            -
                     | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 67 |  | 
| 68 | 
             
            # Monte Carlo Simulation with GBM
         | 
| 69 | 
             
            def monte_carlo_simulation(S0, mu, sigma, T, N, sims, risk_factor, pm_data):
         | 
| @@ -102,7 +109,7 @@ def run_simulation(investment, ticker, horizon, num_sims, risk_factor): | |
| 102 | 
             
                if mu is None:
         | 
| 103 | 
             
                    return None, error_msg
         | 
| 104 |  | 
| 105 | 
            -
                pm_data = fetch_polymarket_data("S&P") | 
| 106 |  | 
| 107 | 
             
                # Run Monte Carlo simulation
         | 
| 108 | 
             
                sim_paths = monte_carlo_simulation(S0, mu, sigma, horizon, num_sims, num_sims, risk_factor, pm_data)
         | 
|  | |
| 5 | 
             
            import pandas as pd
         | 
| 6 | 
             
            import matplotlib.pyplot as plt
         | 
| 7 | 
             
            from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
         | 
| 8 | 
            +
            import time
         | 
| 9 |  | 
| 10 | 
             
            # Polymarket GraphQL API endpoint
         | 
| 11 | 
             
            POLYMARKET_API = "https://api.polymarket.com/graphql"
         | 
|  | |
| 41 | 
             
                    for market in markets:
         | 
| 42 | 
             
                        node = market["node"]
         | 
| 43 | 
             
                        outcomes = node["outcomes"]
         | 
| 44 | 
            +
                        if len(outcomes) >= 2:
         | 
| 45 | 
             
                            return {
         | 
| 46 | 
             
                                "question": node["question"],
         | 
| 47 | 
             
                                "outcomes": {outcome["name"]: float(outcome["price"]) for outcome in outcomes}
         | 
|  | |
| 50 | 
             
                except Exception as e:
         | 
| 51 | 
             
                    return None
         | 
| 52 |  | 
| 53 | 
            +
            # Function to fetch Yahoo Finance data with retry
         | 
| 54 | 
            +
            def fetch_yahoo_data(ticker, retries=3, delay=2):
         | 
| 55 | 
            +
                for attempt in range(retries):
         | 
| 56 | 
            +
                    try:
         | 
| 57 | 
            +
                        stock = yf.download(ticker, period="1y", auto_adjust=False, progress=False)
         | 
| 58 | 
            +
                        if stock.empty or len(stock) < 2:
         | 
| 59 | 
            +
                            return None, None, None, f"No data returned for ticker '{ticker}'. It may be invalid or lack sufficient history."
         | 
| 60 | 
            +
                        daily_returns = stock["Close"].pct_change().dropna()
         | 
| 61 | 
            +
                        if daily_returns.empty:
         | 
| 62 | 
            +
                            return None, None, None, f"No valid returns calculated for ticker '{ticker}'. Insufficient price data."
         | 
| 63 | 
            +
                        mu = daily_returns.mean() * 252  # Annualized drift
         | 
| 64 | 
            +
                        sigma = daily_returns.std() * np.sqrt(252)  # Annualized volatility
         | 
| 65 | 
            +
                        last_price = stock["Close"][-1]  # Use most recent unadjusted Close
         | 
| 66 | 
            +
                        return mu, sigma, last_price, None
         | 
| 67 | 
            +
                    except Exception as e:
         | 
| 68 | 
            +
                        error_msg = f"Attempt {attempt + 1}/{retries} failed for ticker '{ticker}': {str(e)}"
         | 
| 69 | 
            +
                        if attempt < retries - 1:
         | 
| 70 | 
            +
                            time.sleep(delay)  # Wait before retrying
         | 
| 71 | 
            +
                        else:
         | 
| 72 | 
            +
                            return None, None, None, error_msg
         | 
| 73 | 
            +
                return None, None, None, f"Failed to fetch data for '{ticker}' after {retries} attempts."
         | 
| 74 |  | 
| 75 | 
             
            # Monte Carlo Simulation with GBM
         | 
| 76 | 
             
            def monte_carlo_simulation(S0, mu, sigma, T, N, sims, risk_factor, pm_data):
         | 
|  | |
| 109 | 
             
                if mu is None:
         | 
| 110 | 
             
                    return None, error_msg
         | 
| 111 |  | 
| 112 | 
            +
                pm_data = fetch_polymarket_data("S&P")
         | 
| 113 |  | 
| 114 | 
             
                # Run Monte Carlo simulation
         | 
| 115 | 
             
                sim_paths = monte_carlo_simulation(S0, mu, sigma, horizon, num_sims, num_sims, risk_factor, pm_data)
         | 
