Building a Trading Bot with Xtreders Webhooks

Step-by-step guide to building a trading bot using Xtreders webhook alerts. Includes Node.js and Python code examples with security best practices.

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:

  1. You create a price alert on Xtreders with a webhook URL pointing to your server
  2. When the price condition is met, Xtreders sends a POST request to your URL
  3. 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-Id and X-User-Id headers
  • 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:

  1. Use a service like webhook.site to inspect incoming payloads
  2. Create a test alert on a demo account
  3. Verify the payload format matches your parser
  4. Test error handling (what if your server is down?)

Xtreders Teams

26 Blog posts

Comments