Receives webhook alerts from TradingView and automatically executes trades on the Capital.com demo (or live) account.
TradingView Alert
│
▼ (HTTP POST JSON)
Webhook Server (this bot, running on your laptop)
│
▼ (Capital.com REST API)
Capital.com Demo Account
- Your Pine Script strategy fires an alert with a JSON payload.
- The webhook server receives it and decides to open or close a position.
- The order is sent to Capital.com via their REST API.
- Python 3.10+
- TradingView Premium (webhooks require at least Pro plan)
- Capital.com account with API access enabled
- ngrok (to expose your laptop to the internet)
pip install -r requirements.txtcp .env.example .envEdit .env and fill in:
| Variable | Description |
|---|---|
CAPITAL_API_KEY |
Your Capital.com API key (Settings → API integrations) |
CAPITAL_IDENTIFIER |
Your Capital.com login email |
CAPITAL_PASSWORD |
Your Capital.com password |
CAPITAL_DEMO |
true for demo, false for live |
WEBHOOK_SECRET |
Any random string (e.g. my-secret-2024) |
POSITION_SIZE_PERCENT |
% of account equity per trade (default: 70) |
STOP_LOSS_PERCENT |
Stop loss % below entry price (default: 0.45) |
- Log in to Capital.com
- Go to Settings → API Integrations
- Create a new API key and copy it into your
.env
python app.pyYou should see:
Starting webhook server on port 5000 (demo=True)
Download and install ngrok from https://ngrok.com/download, then run:
ngrok http 5000ngrok will give you a public URL like:
https://abc123.ngrok-free.app
Your webhook URL will be:
https://abc123.ngrok-free.app/webhook?secret=YOUR_WEBHOOK_SECRET
Important: Every time you restart ngrok, the URL changes. You will need to update the TradingView alert each time — unless you have a paid ngrok plan with a fixed domain.
- Open your chart with the Gold RSI strategy loaded.
- Click the Alerts clock icon → Create Alert.
- Set Condition to your strategy name.
- Set Trigger to Once Per Bar Close (recommended for 1-minute chart).
Under Notifications:
- Enable Webhook URL
- Enter your full webhook URL:
https://abc123.ngrok-free.app/webhook?secret=YOUR_WEBHOOK_SECRET
The alert message must match the alert_message fields already in your Pine Script.
Your script already sends the correct JSON — leave the Message field as:
{{strategy.order.alert_message}}
This tells TradingView to use the alert_message you defined in strategy.entry, strategy.exit, and strategy.close.
| Event | JSON sent to webhook |
|---|---|
| Buy signal | {"action":"buy","symbol":"XAUUSD"} |
| Stop loss hit | {"action":"close","symbol":"XAUUSD"} |
| Time-based close | {"action":"close","symbol":"XAUUSD"} |
TradingView uses XAUUSD for gold; Capital.com uses GOLD. The mapping is defined in config.py under SYMBOL_MAP. To add new instruments:
SYMBOL_MAP = {
"XAUUSD": "GOLD",
"EURUSD": "EURUSD",
# Add more here
}- Open
.env - Change
CAPITAL_DEMO=truetoCAPITAL_DEMO=false - Restart the bot
No other changes needed.
All activity is logged to logs/bot.log and printed to the terminal. Check this file if a trade is not executing as expected.
To verify the bot is running:
curl http://localhost:5000/healthExpected response:
{"demo": true, "status": "running"}- Start the bot:
python app.py - Start ngrok:
ngrok http 5000 - Update the TradingView alert URL if ngrok URL changed
- Keep both terminal windows open during trading hours (08:00–22:30 CET)
- Stop both when done for the day
tradingview-capitalcom-bot/
├── app.py # Webhook server (main entry point)
├── capital_client.py # Capital.com API client
├── config.py # Configuration loader
├── requirements.txt # Python dependencies
├── .env.example # Template for credentials
├── .env # Your actual credentials (never commit this)
├── .gitignore
├── README.md
└── logs/
└── bot.log # Runtime logs