Documentation
WebsiteReleases
  • What is TradersPost?
  • Getting Started
  • What's new?
  • Core Concepts
    • Brokers
      • TradeStation
      • Alpaca
      • Tradier
      • Robinhood
      • Tradovate
      • NinjaTrader
      • E*TRADE
      • tastytrade
      • Interactive Brokers
      • Coinbase
      • Bybit
      • Kraken
      • Webull
      • Binance
      • TDAmeritrade
      • Broker Roadmap
        • Schwab
    • Strategies
    • Subscriptions
    • Webhooks
  • Learn
    • Rate Limits
    • TradingView
    • Prop Firm Trading
    • TrendSpider
    • MetaTrader 5 - MT5
    • Backtesting
    • Order Behavior
    • Order Queueing
    • Order Classes
    • Paper Trading
    • Position Sizing
    • Fractional Shares
    • Custom Code Examples
    • Discord Trading Bot
    • API
  • Assets
    • Stocks
    • Futures
    • Options
    • Crypto
    • Forex
  • Strategies & Indicators
    • Strategy Developers
    • Example Strategies
      • Simple Strategy
      • EMA Crossovers
      • Automate Channel Trading
      • Range Trade Heikin Ashi RSI
      • Scheduled Alerts
      • Simple Trend Lines
      • 8-55 EMA Cross Strategy
      • High-Low Range Trend-Following
      • LuxAlgo SMC Trade Signals
      • Confluence of Alerts
      • Automated Channel
      • SSL Hybrid
      • Engulfing Candles
      • Supertrend and Bollinger Bands
    • Premium
    • Free
    • Build Your Own
  • Additional Information
    • Glossary
    • Error Messages
    • Troubleshooting
    • Frequently Asked Questions
    • Office Hours
    • Known Limitations
    • Trading Communities
    • Trade Journaling
    • Useful URLs
    • WebhookMessage Library
    • Maintenance Windows
    • Releases
  • Links
    • Discord
    • Community
    • Pricing
    • Register
    • Login
Powered by GitBook
On this page

Was this helpful?

Edit on GitHub
  1. Learn

Discord Trading Bot

This example shows you how to setup an automated trading bot where the signals are powered by messages received in a Discord channel.

PreviousCustom Code ExamplesNextAPI

Last updated 11 months ago

Was this helpful?

To get started, create a new directory where you can test your bot.

$ mkdir discord-bot
$ cd discord-bot

Now install the symfony/http-client and team-reflex/discord-php dependencies using .

$ composer require symfony/http-client
$ composer require team-reflex/discord-php

Now create a new file named bot.php and paste the following code inside. Be sure to replace the $token and $webhookUrl variables with your own values.

<?php

include __DIR__.'/vendor/autoload.php';

use Discord\Discord;
use Discord\Parts\Channel\Message;
use Symfony\Component\HttpClient\HttpClient;

$token = 'paste your discord bot token here';

$discord = new Discord([
    'token' => $token,
]);

$discord->on('ready', function ($discord) {
    $discord->on('message', function ($message, $discord) {
        $content = $message->content;

        preg_match_all('/(BUY|SELL) ([\d+]) ([A-Z]{1,5})/i', $content, $matches);

        if (!isset($matches[3][0])) {
            return;
        }

        $action = strtolower($matches[1][0]);
        $quantity = $matches[2][0];
        $ticker = strtoupper($matches[3][0]);

        $payload = [
            'action' => $action,
            'quantity' => $quantity,
            'ticker' => $ticker,
        ];

        $webhookUrl = 'paste your TradersPost webhook url here';

        $client = HttpClient::create();

        $response = $client->request('POST', $webhookUrl, [
            'json' => $payload
        ]);

        $message->reply($response->getContent());
    });
});

$discord->run();

Now you can run the bot.

$ php bot.php

Now if you go to the channel in Discord where your bot is, you can type messages like the following to send signals to TradersPost.

This is a simple example, but it should demonstrate to you how you can use TradersPost webhooks to send trade signals from any source with a little bit of custom code. Enjoy!

composer