Fangzhi Xu commited on
Commit
4fe407c
·
1 Parent(s): fd05a84
Files changed (1) hide show
  1. app.py +14 -46
app.py CHANGED
@@ -314,6 +314,7 @@ def reset_env():
314
  custom_css = """
315
  .gradio-container { font-family: 'Arial', sans-serif; }
316
  """
 
317
  with gr.Blocks(theme=gr.themes.Soft(), css=custom_css, title="AI Trading Arena") as demo:
318
  gr.Markdown("# 🚀 AI Trading Arena\n### Interactive Stock Trading Simulator")
319
 
@@ -339,11 +340,10 @@ with gr.Blocks(theme=gr.themes.Soft(), css=custom_css, title="AI Trading Arena")
339
  init_btn = gr.Button("🚀 Load Config", variant="primary", size="lg")
340
 
341
  with gr.Accordion("💹 Trading Operations", open=True):
342
- # ✅ 这里是修改点:先初始化为空
343
  stock_dropdown = gr.Dropdown(
344
- choices=env.stocks if hasattr(env, "stocks") else [],
345
  label="Select Stock",
346
- value=None
347
  )
348
  action_radio = gr.Radio(choices=["Buy", "Sell"], label="Action", value="Buy")
349
  amount_input = gr.Number(label="Amount (shares)", value=10, minimum=1, step=1)
@@ -364,50 +364,18 @@ with gr.Blocks(theme=gr.themes.Soft(), css=custom_css, title="AI Trading Arena")
364
  with gr.Tab("💰 Portfolio Value"):
365
  value_chart = gr.Plot(label="Total Portfolio Value")
366
 
367
- # ✅ 修改 initialize_env,使其返回 stocks 列表用于更新 dropdown
368
- def initialize_env_with_stocks(config_json=None):
369
- status, portfolio, news, price_fig, value_fig = initialize_env(config_json)
370
- stocks = getattr(env, "stocks", [])
371
- return status, portfolio, news, price_fig, value_fig, gr.update(choices=stocks, value=stocks[0] if stocks else None)
372
-
373
  # 绑定逻辑
374
- load_file_btn.click(
375
- fn=load_config_from_file,
376
- inputs=[config_file_dropdown],
377
- outputs=[config_input]
378
- )
379
-
380
- init_btn.click(
381
- fn=initialize_env_with_stocks,
382
- inputs=[config_input],
383
- outputs=[status_output, portfolio_table, news_display, price_chart, value_chart, stock_dropdown]
384
- )
385
-
386
- reset_btn.click(
387
- fn=reset_env,
388
- inputs=[],
389
- outputs=[status_output, portfolio_table, news_display, price_chart, value_chart]
390
- )
391
-
392
- trade_btn.click(
393
- fn=execute_trade,
394
- inputs=[stock_dropdown, action_radio, amount_input],
395
- outputs=[status_output, portfolio_table, news_display, price_chart, value_chart]
396
- )
397
-
398
- advance_btn.click(
399
- fn=advance_day,
400
- inputs=[],
401
- outputs=[status_output, portfolio_table, news_display, price_chart, value_chart]
402
- )
403
-
404
- # ✅ 初始化时也更新 dropdown 选项
405
- demo.load(
406
- fn=initialize_env_with_stocks,
407
- inputs=[],
408
- outputs=[status_output, portfolio_table, news_display, price_chart, value_chart, stock_dropdown]
409
- )
410
-
411
 
412
  if __name__ == "__main__":
413
  demo.launch()
 
314
  custom_css = """
315
  .gradio-container { font-family: 'Arial', sans-serif; }
316
  """
317
+
318
  with gr.Blocks(theme=gr.themes.Soft(), css=custom_css, title="AI Trading Arena") as demo:
319
  gr.Markdown("# 🚀 AI Trading Arena\n### Interactive Stock Trading Simulator")
320
 
 
340
  init_btn = gr.Button("🚀 Load Config", variant="primary", size="lg")
341
 
342
  with gr.Accordion("💹 Trading Operations", open=True):
 
343
  stock_dropdown = gr.Dropdown(
344
+ choices=["S0", "S1", "S2", "S3", "S4", "S5"],
345
  label="Select Stock",
346
+ value=100
347
  )
348
  action_radio = gr.Radio(choices=["Buy", "Sell"], label="Action", value="Buy")
349
  amount_input = gr.Number(label="Amount (shares)", value=10, minimum=1, step=1)
 
364
  with gr.Tab("💰 Portfolio Value"):
365
  value_chart = gr.Plot(label="Total Portfolio Value")
366
 
 
 
 
 
 
 
367
  # 绑定逻辑
368
+ load_file_btn.click(fn=load_config_from_file, inputs=[config_file_dropdown], outputs=[config_input])
369
+ init_btn.click(fn=initialize_env, inputs=[config_input],
370
+ outputs=[status_output, portfolio_table, news_display, price_chart, value_chart])
371
+ reset_btn.click(fn=reset_env, inputs=[],
372
+ outputs=[status_output, portfolio_table, news_display, price_chart, value_chart])
373
+ trade_btn.click(fn=execute_trade, inputs=[stock_dropdown, action_radio, amount_input],
374
+ outputs=[status_output, portfolio_table, news_display, price_chart, value_chart])
375
+ advance_btn.click(fn=advance_day, inputs=[],
376
+ outputs=[status_output, portfolio_table, news_display, price_chart, value_chart])
377
+ demo.load(fn=initialize_env, inputs=[],
378
+ outputs=[status_output, portfolio_table, news_display, price_chart, value_chart])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
379
 
380
  if __name__ == "__main__":
381
  demo.launch()