stock/docs/source/configuration.rst
2026-02-27 03:17:12 +08:00

401 lines
7.9 KiB
ReStructuredText

Configuration Guide
===================
OpenClaw Trading can be configured through environment variables, configuration files, or programmatically.
Configuration Sources
-------------------
Priority Order (highest to lowest):
1. Environment variables
2. Configuration files
3. Default values
Environment Variables
---------------------
Core Settings
~~~~~~~~~~~~~
.. list-table::
:header-rows: 1
* - Variable
- Description
- Default
* - ``ENV``
- Environment name (development, staging, production)
- ``development``
* - ``DEBUG``
- Enable debug mode
- ``false``
* - ``LOG_LEVEL``
- Logging level (DEBUG, INFO, WARNING, ERROR)
- ``INFO``
Economic Settings
~~~~~~~~~~~~~~~~~
.. list-table::
:header-rows: 1
* - Variable
- Description
- Default
* - ``INITIAL_CAPITAL``
- Starting capital for new agents
- ``10000.0``
* - ``TOKEN_COST_PER_1M_INPUT``
- Cost per 1M input tokens
- ``2.5``
* - ``TOKEN_COST_PER_1M_OUTPUT``
- Cost per 1M output tokens
- ``10.0``
* - ``TRADE_FEE_RATE``
- Trading fee as decimal (0.001 = 0.1%)
- ``0.001``
* - ``DATA_COST_PER_CALL``
- Cost per market data API call
- ``0.01``
Trading Settings
~~~~~~~~~~~~~~~~
.. list-table::
:header-rows: 1
* - Variable
- Description
- Default
* - ``ENABLE_LIVE_TRADING``
- Enable live trading (vs paper trading)
- ``false``
* - ``DEFAULT_POSITION_SIZE``
- Default position size as portfolio %
- ``0.1``
* - ``MAX_POSITION_SIZE``
- Maximum position size as portfolio %
- ``0.2``
* - ``MAX_DRAWDOWN``
- Maximum allowed drawdown before stopping
- ``0.15``
* - ``STOP_LOSS_PCT``
- Default stop loss percentage
- ``0.05``
* - ``TAKE_PROFIT_PCT``
- Default take profit percentage
- ``0.10``
Exchange Settings
~~~~~~~~~~~~~~~~~
.. list-table::
:header-rows: 1
* - Variable
- Description
- Default
* - ``EXCHANGE_NAME``
- Exchange to use (alpaca, ibkr, binance)
- ``alpaca``
* - ``EXCHANGE_API_KEY``
- Exchange API key
- Required
* - ``EXCHANGE_SECRET_KEY``
- Exchange API secret
- Required
* - ``PAPER_TRADING``
- Use paper trading account
- ``true``
API Settings
~~~~~~~~~~~~
.. list-table::
:header-rows: 1
* - Variable
- Description
- Default
* - ``API_HOST``
- API server host
- ``0.0.0.0``
* - ``API_PORT``
- API server port
- ``8000``
* - ``API_WORKERS``
- Number of API workers
- ``4``
Configuration Files
-------------------
YAML Configuration
~~~~~~~~~~~~~~~~~~
Create ``config/default.yaml``:
.. code-block:: yaml
# Environment
environment: development
debug: false
# Logging
logging:
level: INFO
format: json
file: logs/openclaw.log
# Economy
economy:
initial_capital: 10000.0
token_cost_per_1m_input: 2.5
token_cost_per_1m_output: 10.0
trade_fee_rate: 0.001
data_cost_per_call: 0.01
survival_thresholds:
thriving: 1.5
stable: 1.1
struggling: 0.8
bankrupt: 0.3
# Trading
trading:
enable_live_trading: false
paper_trading: true
default_position_size: 0.1
max_position_size: 0.2
max_drawdown: 0.15
stop_loss_pct: 0.05
take_profit_pct: 0.10
# Workflow
workflow:
enable_parallel: true
timeout_seconds: 300
max_retries: 3
# Agents
agents:
default_skill_level: 0.5
skill_improvement_rate: 0.01
market_analyst:
indicators:
- sma
- rsi
- macd
risk_manager:
max_position_size: 0.2
max_portfolio_heat: 0.5
daily_loss_limit: 0.03
# Exchange
exchange:
name: alpaca
paper_trading: true
rate_limit: 200
# Data
data:
cache_enabled: true
cache_ttl: 3600
default_source: yahoo
# Monitoring
monitoring:
enable_metrics: true
enable_alerts: true
metrics_retention_days: 30
alert_thresholds:
drawdown_warning: 0.05
drawdown_critical: 0.10
loss_streak_warning: 3
loss_streak_critical: 5
Environment-Specific Configs
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Create separate configs for different environments:
**config/development.yaml**:
.. code-block:: yaml
environment: development
debug: true
logging:
level: DEBUG
format: text
economy:
initial_capital: 1000.0 # Lower for testing
**config/production.yaml**:
.. code-block:: yaml
environment: production
debug: false
logging:
level: WARNING
format: json
file: /var/log/openclaw/trading.log
economy:
initial_capital: 100000.0
trading:
paper_trading: false
enable_live_trading: true
Loading Configuration
---------------------
From File
~~~~~~~~~
.. code-block:: python
from openclaw.core.config import load_config
# Load default config
config = load_config()
# Load specific environment
config = load_config("production")
# Access values
initial_capital = config.economy.initial_capital
log_level = config.logging.level
Programmatic Configuration
~~~~~~~~~~~~~~~~~~~~~~~~~~
.. code-block:: python
from openclaw.core.config import Config
# Create config object
config = Config(
environment="staging",
economy={
"initial_capital": 5000.0,
"trade_fee_rate": 0.002
},
trading={
"max_position_size": 0.15
}
)
# Override specific values
config.economy.initial_capital = 7500.0
# Save to file
config.save("config/staging.yaml")
Validation
----------
Schema Validation
~~~~~~~~~~~~~~~~~
.. code-block:: python
from openclaw.core.config import validate_config
# Validate configuration
errors = validate_config(config)
if errors:
for error in errors:
print(f"Validation error: {error}")
else:
print("Configuration valid")
Type Checking
~~~~~~~~~~~~~
.. code-block:: python
from pydantic import ValidationError
try:
config = Config(**config_dict)
except ValidationError as e:
print(f"Invalid configuration: {e}")
Secure Configuration
--------------------
Environment File
~~~~~~~~~~~~~~~~
Create ``.env`` file (don't commit to version control):
.. code-block:: bash
# Exchange API credentials
EXCHANGE_API_KEY=your_api_key_here
EXCHANGE_SECRET_KEY=your_secret_here
# Database password
DATABASE_URL=postgresql://user:password@localhost/openclaw
# Other secrets
SLACK_WEBHOOK_URL=https://hooks.slack.com/services/...
Loading from .env
~~~~~~~~~~~~~~~~~
.. code-block:: python
from dotenv import load_dotenv
import os
# Load .env file
load_dotenv()
# Access variables
api_key = os.getenv("EXCHANGE_API_KEY")
secret = os.getenv("EXCHANGE_SECRET_KEY")
Secret Management
~~~~~~~~~~~~~~~~~
Use secret management for production:
.. code-block:: python
# AWS Secrets Manager
import boto3
client = boto3.client("secretsmanager")
response = client.get_secret_value(SecretId="openclaw/production")
secrets = json.loads(response["SecretString"])
# HashiCorp Vault
import hvac
client = hvac.Client(url="https://vault.example.com")
secret = client.secrets.kv.v2.read_secret_version(
path="openclaw/production"
)
Configuration Best Practices
----------------------------
1. **Separate environments**: Different configs for dev/staging/prod
2. **Use environment variables**: For sensitive data and deployment-specific values
3. **Validate on startup**: Fail fast on invalid configuration
4. **Document changes**: Keep example configs updated
5. **Version control**: Commit non-sensitive config templates
6. **Secure secrets**: Never commit API keys or passwords