Create Your Own Trading Bot
Xtreders webhooks make it easy to build custom trading automation. This guide shows you how to create a basic trading bot that reacts to price alerts.
Architecture
The flow works like this:
- You create a price alert on Xtreders with a webhook URL pointing to your server
- When the price condition is met, Xtreders sends a POST request to your URL
- Your server receives the alert data and executes a trade via your broker API
Example: Node.js Webhook Receiver
const express = require('express');
const app = express();
app.use(express.json());
app.post('/webhook', (req, res) => {
const alert = req.body;
console.log('Alert received:', alert.symbol,
alert.condition, 'at', alert.trigger_price);
// Your trading logic here
if (alert.condition === 'crossing_up') {
placeBuyOrder(alert.symbol, alert.trigger_price);
} else if (alert.condition === 'crossing_down') {
placeSellOrder(alert.symbol, alert.trigger_price);
}
res.status(200).json({ received: true });
});
app.listen(3000, () => console.log('Webhook server running'));
Example: Python Webhook Receiver
from flask import Flask, request, jsonify
app = Flask(__name__)
@app.route('/webhook', methods=['POST'])
def webhook():
alert = request.json
print(f"Alert: {alert['symbol']} {alert['condition']} "
f"at {alert['trigger_price']}")
if alert['condition'] == 'crossing_up':
place_buy_order(alert['symbol'], alert['trigger_price'])
return jsonify({'received': True})
if __name__ == '__main__':
app.run(port=3000)
Security Best Practices
- Verify the
X-Alert-IdandX-User-Idheaders - Use HTTPS for your webhook endpoint
- Validate the payload structure before executing trades
- Implement idempotency to prevent duplicate trades
- Add logging for all received webhooks and executed trades
- Set position size limits in your bot to prevent accidents
Testing Your Webhook
Before going live:
- Use a service like webhook.site to inspect incoming payloads
- Create a test alert on a demo account
- Verify the payload format matches your parser
- Test error handling (what if your server is down?)