Tonic commited on
Commit
6655309
·
1 Parent(s): bd7c6bf

qol improvements, more info in the interface

Browse files
Files changed (1) hide show
  1. app.py +55 -34
app.py CHANGED
@@ -446,6 +446,12 @@ def create_interface():
446
  value="chronos"
447
  )
448
  hourly_predict_btn = gr.Button("Analyze Stock")
 
 
 
 
 
 
449
 
450
  with gr.Column():
451
  hourly_plot = gr.Plot(label="Analysis and Prediction")
@@ -476,7 +482,7 @@ def create_interface():
476
  )
477
  min15_lookback_days = gr.Slider(
478
  minimum=1,
479
- maximum=5, # Limited to 5 days for 15-minute data
480
  value=3,
481
  step=1,
482
  label="Historical Lookback (Days)"
@@ -487,6 +493,13 @@ def create_interface():
487
  value="chronos"
488
  )
489
  min15_predict_btn = gr.Button("Analyze Stock")
 
 
 
 
 
 
 
490
 
491
  with gr.Column():
492
  min15_plot = gr.Plot(label="Analysis and Prediction")
@@ -504,39 +517,47 @@ def create_interface():
504
  min15_sector_metrics = gr.JSON(label="Sector Metrics")
505
 
506
  def analyze_stock(symbol, timeframe, prediction_days, lookback_days, strategy):
507
- signals, fig = make_prediction(symbol, timeframe, prediction_days, strategy)
508
-
509
- # Get historical data for additional metrics
510
- df = get_historical_data(symbol, timeframe, lookback_days)
511
-
512
- # Calculate structured product metrics
513
- product_metrics = {
514
- "Market_Cap": df['Market_Cap'].iloc[-1],
515
- "Sector": df['Sector'].iloc[-1],
516
- "Industry": df['Industry'].iloc[-1],
517
- "Dividend_Yield": df['Dividend_Yield'].iloc[-1],
518
- "Avg_Daily_Volume": df['Avg_Daily_Volume'].iloc[-1],
519
- "Volume_Volatility": df['Volume_Volatility'].iloc[-1]
520
- }
521
-
522
- # Calculate risk metrics
523
- risk_metrics = {
524
- "Annualized_Volatility": df['Annualized_Vol'].iloc[-1],
525
- "Max_Drawdown": df['Max_Drawdown'].iloc[-1],
526
- "Current_Drawdown": df['Drawdown'].iloc[-1],
527
- "Sharpe_Ratio": (df['Returns'].mean() * 252) / (df['Returns'].std() * np.sqrt(252)),
528
- "Sortino_Ratio": (df['Returns'].mean() * 252) / (df['Returns'][df['Returns'] < 0].std() * np.sqrt(252))
529
- }
530
-
531
- # Calculate sector metrics
532
- sector_metrics = {
533
- "Sector": df['Sector'].iloc[-1],
534
- "Industry": df['Industry'].iloc[-1],
535
- "Market_Cap_Rank": "Large" if df['Market_Cap'].iloc[-1] > 1e10 else "Mid" if df['Market_Cap'].iloc[-1] > 1e9 else "Small",
536
- "Liquidity_Score": "High" if df['Avg_Daily_Volume'].iloc[-1] > 1e6 else "Medium" if df['Avg_Daily_Volume'].iloc[-1] > 1e5 else "Low"
537
- }
538
-
539
- return signals, fig, product_metrics, risk_metrics, sector_metrics
 
 
 
 
 
 
 
 
540
 
541
  # Daily analysis button click
542
  daily_predict_btn.click(
 
446
  value="chronos"
447
  )
448
  hourly_predict_btn = gr.Button("Analyze Stock")
449
+ gr.Markdown("""
450
+ **Note for Hourly Analysis:**
451
+ - Maximum lookback period: 30 days (Yahoo Finance limit)
452
+ - Maximum prediction period: 7 days
453
+ - Data is only available during market hours
454
+ """)
455
 
456
  with gr.Column():
457
  hourly_plot = gr.Plot(label="Analysis and Prediction")
 
482
  )
483
  min15_lookback_days = gr.Slider(
484
  minimum=1,
485
+ maximum=5, # Yahoo Finance limit for 15-minute data
486
  value=3,
487
  step=1,
488
  label="Historical Lookback (Days)"
 
493
  value="chronos"
494
  )
495
  min15_predict_btn = gr.Button("Analyze Stock")
496
+ gr.Markdown("""
497
+ **Note for 15-Minute Analysis:**
498
+ - Maximum lookback period: 5 days (Yahoo Finance limit)
499
+ - Maximum prediction period: 2 days
500
+ - Data is only available during market hours
501
+ - Requires at least 64 data points for Chronos predictions
502
+ """)
503
 
504
  with gr.Column():
505
  min15_plot = gr.Plot(label="Analysis and Prediction")
 
517
  min15_sector_metrics = gr.JSON(label="Sector Metrics")
518
 
519
  def analyze_stock(symbol, timeframe, prediction_days, lookback_days, strategy):
520
+ try:
521
+ signals, fig = make_prediction(symbol, timeframe, prediction_days, strategy)
522
+
523
+ # Get historical data for additional metrics
524
+ df = get_historical_data(symbol, timeframe, lookback_days)
525
+
526
+ # Calculate structured product metrics
527
+ product_metrics = {
528
+ "Market_Cap": df['Market_Cap'].iloc[-1],
529
+ "Sector": df['Sector'].iloc[-1],
530
+ "Industry": df['Industry'].iloc[-1],
531
+ "Dividend_Yield": df['Dividend_Yield'].iloc[-1],
532
+ "Avg_Daily_Volume": df['Avg_Daily_Volume'].iloc[-1],
533
+ "Volume_Volatility": df['Volume_Volatility'].iloc[-1]
534
+ }
535
+
536
+ # Calculate risk metrics
537
+ risk_metrics = {
538
+ "Annualized_Volatility": df['Annualized_Vol'].iloc[-1],
539
+ "Max_Drawdown": df['Max_Drawdown'].iloc[-1],
540
+ "Current_Drawdown": df['Drawdown'].iloc[-1],
541
+ "Sharpe_Ratio": (df['Returns'].mean() * 252) / (df['Returns'].std() * np.sqrt(252)),
542
+ "Sortino_Ratio": (df['Returns'].mean() * 252) / (df['Returns'][df['Returns'] < 0].std() * np.sqrt(252))
543
+ }
544
+
545
+ # Calculate sector metrics
546
+ sector_metrics = {
547
+ "Sector": df['Sector'].iloc[-1],
548
+ "Industry": df['Industry'].iloc[-1],
549
+ "Market_Cap_Rank": "Large" if df['Market_Cap'].iloc[-1] > 1e10 else "Mid" if df['Market_Cap'].iloc[-1] > 1e9 else "Small",
550
+ "Liquidity_Score": "High" if df['Avg_Daily_Volume'].iloc[-1] > 1e6 else "Medium" if df['Avg_Daily_Volume'].iloc[-1] > 1e5 else "Low"
551
+ }
552
+
553
+ return signals, fig, product_metrics, risk_metrics, sector_metrics
554
+ except Exception as e:
555
+ error_message = str(e)
556
+ if "Insufficient data points" in error_message:
557
+ error_message = f"Not enough data available for {symbol} in {timeframe} timeframe. Please try a different timeframe or symbol."
558
+ elif "no price data found" in error_message:
559
+ error_message = f"No data available for {symbol} in {timeframe} timeframe. Please try a different timeframe or symbol."
560
+ raise gr.Error(error_message)
561
 
562
  # Daily analysis button click
563
  daily_predict_btn.click(