Alpaca Paper Trading

KlawTrade uses Alpaca as its brokerage provider. Alpaca offers a free paper trading environment with real market data, which makes it the recommended way to test strategies before risking real capital.

Step 1: Create an Alpaca Account

Sign up for a free account at alpaca.markets. You do not need to fund the account or complete identity verification for paper trading. After creating your account, you will have access to both paper and live trading environments.

Step 2: Get Your API Keys

Navigate to the Paper Trading section in your Alpaca dashboard. Generate a new API key pair. You will receive two values:

  • API Key ID -- a public identifier for your account
  • Secret Key -- a private key shown only once at creation time. Copy it immediately.

Make sure you are generating keys for the paper trading environment, not the live environment. The paper trading base URL is https://paper-api.alpaca.markets.

Step 3: Add Keys to Your Environment

Add your API credentials to the .env file in the root of your KlawTrade project directory.

ALPACA_API_KEY=PKxxxxxxxxxxxxxxxx
ALPACA_SECRET_KEY=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

If you used klawtrade init, this file was already created for you during the setup wizard. You can also set these as system environment variables instead of using a file.

Step 4: Restart KlawTrade

If the trading engine is already running, stop it and start again so the new credentials are loaded.

klawtrade start

Auto-Detection

KlawTrade automatically detects whether your API keys belong to a paper or live trading account by checking the base URL configured in settings.yaml. If the URL points to paper-api.alpaca.markets, the system operates in paper mode. If it points to api.alpaca.markets, it operates in live mode.

On startup, the engine validates the credentials by making an authenticated request to the Alpaca API. If the keys are invalid or the connection fails, KlawTrade falls back to simulation mode and logs a warning. You can verify the connection status on the dashboard or by running klawtrade status.