numpy
pandas
import gradio as gr
import numpy as np
import pandas as pd
def run_simulation(steps, trading_style):
# Seed for reproducibility
np.random.seed(42)
prices = [1800.0]
# Generate simulated market XAU/USD data
for _ in range(int(steps)):
prices.append(round(prices[-1] + np.random.normal(0, 12), 2))
decisions = []
capital = 10000.0
position = 0
history = []
# Simulating the behavior of the PPO Reinforcement Learning model policy
# based on trading styles (Conservative, Balanced, Aggressive)
for i in range(1, len(prices)):
price_diff = prices[i] - prices[i-1]
# Simulated RL Model Decision Policy Logic
if trading_style == "Conservative":
if price_diff > 15:
action = "BUY"
elif price_diff < -15:
action = "SELL"
else:
action = "HOLD"
elif trading_style == "Aggressive":
if price_diff > 5:
action = "BUY"
elif price_diff < -5:
action = "SELL"
else:
action = "HOLD"
else: # Balanced PPO Strategy
if price_diff > 10:
action = "BUY"
elif price_diff < -10:
action = "SELL"
else:
action = "HOLD"
decisions.append(action)
df = pd.DataFrame({
"Step": list(range(1, int(steps) + 1)),
"Simulated Gold Price (XAU/USD)": prices[1:],
"RL Model Action": decisions
})
return "In-browser simulation successfully executed using Pyodide WebAssembly with mock PPO policy behaviors!", df
with gr.Blocks() as demo:
gr.Markdown("# Reinforcement Learning for Gold Trading (XAU/USD) Demo")
gr.Markdown("This app simulates model trading actions on random gold price fluctuations using the PPO policy logic entirely in WebAssembly.")
with gr.Row():
steps = gr.Slider(minimum=10, maximum=100, value=30, step=10, label="Simulation Steps")
trading_style = gr.Dropdown(["Conservative", "Balanced", "Aggressive"], value="Balanced", label="Model Strategy Variation")
btn = gr.Button("Run In-Browser Simulation")
status = gr.Textbox(label="System Status")
output_table = gr.DataFrame(label="Trading Log")
btn.click(run_simulation, inputs=[steps, trading_style], outputs=[status, output_table])
demo.launch()