192 lines
3.6 KiB
Markdown
192 lines
3.6 KiB
Markdown
# 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
|
|
|
|
```bash
|
|
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
|
|
|
|
```bash
|
|
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
|
|
|
|
```bash
|
|
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
|
|
|
|
```bash
|
|
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
|
|
|
|
```bash
|
|
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
|
|
|
|
```bash
|
|
python examples/06_portfolio_risk.py
|
|
```
|
|
|
|
## Running All Examples
|
|
|
|
To run all examples at once:
|
|
|
|
```bash
|
|
# Make executable
|
|
chmod +x examples/run_all.sh
|
|
|
|
# Run all
|
|
./examples/run_all.sh
|
|
```
|
|
|
|
Or manually:
|
|
|
|
```bash
|
|
for script in examples/0*.py; do
|
|
echo "Running $script..."
|
|
python "$script"
|
|
echo ""
|
|
done
|
|
```
|
|
|
|
## Custom Examples
|
|
|
|
### Creating a Custom Agent
|
|
|
|
```python
|
|
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
|
|
|
|
```python
|
|
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
|
|
|
|
```python
|
|
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:
|
|
|
|
1. `01_getting_started.ipynb` - Introduction to OpenClaw
|
|
2. `02_agent_comparison.ipynb` - Comparing different agents
|
|
3. `03_backtesting.ipynb` - Backtesting strategies
|
|
4. `04_custom_strategies.ipynb` - Creating custom strategies
|
|
|
|
To start Jupyter:
|
|
|
|
```bash
|
|
jupyter notebook notebooks/
|
|
```
|
|
|
|
## Prerequisites
|
|
|
|
Ensure you have OpenClaw installed:
|
|
|
|
```bash
|
|
pip install -e "."
|
|
```
|
|
|
|
Or set PYTHONPATH:
|
|
|
|
```bash
|
|
export PYTHONPATH=/path/to/openclaw/src:$PYTHONPATH
|
|
```
|
|
|
|
## Additional Resources
|
|
|
|
- [Full Documentation](../docs/)
|
|
- [API Reference](../docs/source/api.rst)
|
|
- [Architecture Guide](../docs/source/architecture.rst)
|