3.6 KiB
3.6 KiB
OpenClaw Trading Examples
This directory contains example scripts demonstrating various features of OpenClaw Trading.
Quick Start
1. Basic Economic Tracking (01_quickstart.py)
Demonstrates the core economic tracking system:
- Creating an economic tracker
- Checking survival status
- Calculating decision costs
- Simulating trades
- Tracking balance history
python examples/01_quickstart.py
2. Workflow Demo (02_workflow_demo.py)
Shows how to use the LangGraph workflow system:
- Creating a trading workflow
- Running parallel analysis
- Getting trading signals
- Handling workflow results
python examples/02_workflow_demo.py
3. Factor Market (03_factor_market.py)
Demonstrates the factor market system:
- Browsing available factors
- Purchasing factors
- Using factors in analysis
- Factor unlocking mechanism
python examples/03_factor_market.py
4. Learning System (04_learning_system.py)
Shows how to use the learning system:
- Browsing available courses
- Enrolling agents in courses
- Completing courses
- Applying learned skills
python examples/04_learning_system.py
5. Work-Trade Balance (05_work_trade_balance.py)
Demonstrates the work-trade balance mechanism:
- Monitoring agent performance
- Switching to work mode
- Earning through work
- Returning to trading
python examples/05_work_trade_balance.py
6. Portfolio Risk Management (06_portfolio_risk.py)
Shows portfolio-level risk management:
- Managing multiple positions
- Calculating portfolio risk
- Risk-adjusted position sizing
- Stop-loss and take-profit
python examples/06_portfolio_risk.py
Running All Examples
To run all examples at once:
# Make executable
chmod +x examples/run_all.sh
# Run all
./examples/run_all.sh
Or manually:
for script in examples/0*.py; do
echo "Running $script..."
python "$script"
echo ""
done
Custom Examples
Creating a Custom Agent
from openclaw.agents.base import BaseAgent
class MyCustomAgent(BaseAgent):
def analyze(self, symbol: str):
# Your analysis logic here
return {"signal": "buy", "confidence": 0.8}
# Usage
agent = MyCustomAgent("my_agent", initial_capital=1000.0)
result = agent.analyze("AAPL")
Running a Backtest
from openclaw.backtest.engine import BacktestEngine
engine = BacktestEngine()
engine.configure(
symbols=["AAPL"],
start_date="2023-01-01",
end_date="2023-12-31",
initial_capital=10000.0
)
results = engine.run()
print(f"Total Return: {results.total_return:.2%}")
Using the Workflow
from openclaw.workflow.trading_workflow import TradingWorkflow
workflow = TradingWorkflow(
symbol="AAPL",
initial_capital=1000.0,
enable_parallel=True
)
result = await workflow.run()
print(f"Signal: {result['signal']}")
print(f"Confidence: {result['confidence']:.2%}")
Jupyter Notebook Tutorials
For interactive tutorials, see the notebooks/ directory:
01_getting_started.ipynb- Introduction to OpenClaw02_agent_comparison.ipynb- Comparing different agents03_backtesting.ipynb- Backtesting strategies04_custom_strategies.ipynb- Creating custom strategies
To start Jupyter:
jupyter notebook notebooks/
Prerequisites
Ensure you have OpenClaw installed:
pip install -e "."
Or set PYTHONPATH:
export PYTHONPATH=/path/to/openclaw/src:$PYTHONPATH