Building a Forex Trading Bot with Freqtrade and Sending Buy/Sell Signals to Telegram

In the world of forex trading, automation is key to staying ahead of the market. With the rise of open-source tools like Freqtrade, creating a custom trading bot has never been easier.

In this blog post, I’ll guide you through setting up a forex trading bot using Freqtrade and sending buy/sell signals to Telegram for real-time notifications.

Why Freqtrade?
Freqtrade is a free, open-source cryptocurrency trading bot written in Python. While it’s primarily designed for crypto trading, it can be adapted for forex trading with some modifications. Its key features include:

  • Backtesting: Test your strategies on historical data.
  • Live Trading: Execute trades in real-time.
  • Customizable Strategies: Write your own trading logic in Python.
  • Extensibility: Integrate with external APIs and services like Telegram.

Prerequisites
Before we begin, ensure you have the following:

  • Python 3.8+: Freqtrade runs on Python, so make sure it’s installed.
  • Telegram Bot: Create a bot using BotFather and note the API token.
  • Forex Data: Obtain forex data in a format Freqtrade can use (e.g., CSV or from an API like Alpha Vantage).

Step 1: Install Freqtrade
First, let’s install Freqtrade. Open your terminal and run the following commands:

# Clone the Freqtrade repository
git clone https://github.com/freqtrade/freqtrade.git
cd freqtrade

# Set up a virtual environment
python -m venv .env
source .env/bin/activate  # On Windows, use `.env\Scripts\activate`

# Install dependencies
pip install -r requirements.txt

Step 2: Configure Freqtrade
Freqtrade requires a configuration file to define your trading strategy, exchange, and other settings. Run the following command to generate a default config file:

freqtrade new-config --config config.json

Edit the config.json file to include your forex data and Telegram settings:

{
  "max_open_trades": 3,
  "stake_currency": "USD",
  "stake_amount": 100,
  "fiat_display_currency": "USD",
  "exchange": {
    "name": "binance",  // Use a forex-friendly exchange or adapt for forex
    "key": "your_api_key",
    "secret": "your_api_secret",
    "pair_whitelist": ["EUR/USD", "GBP/USD"]  // Add forex pairs
  },
  "telegram": {
    "enabled": true,
    "token": "your_telegram_bot_token",
    "chat_id": "your_chat_id"
  },
  "strategy": "MyForexStrategy"
}

Step 3: Create a Custom Strategy
Freqtrade allows you to define your own trading strategy in Python. Create a file named my_forex_strategy.py in the user_data/strategies directory:

from freqtrade.strategy.interface import IStrategy
from pandas import DataFrame

class MyForexStrategy(IStrategy):
    # Define your strategy parameters
    timeframe = '5m'
    minimal_roi = {
        "0": 0.1
    }
    stoploss = -0.1

    def populate_indicators(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
        # Add indicators (e.g., RSI, SMA)
        dataframe['rsi'] = ta.RSI(dataframe, timeperiod=14)
        dataframe['sma'] = ta.SMA(dataframe, timeperiod=20)
        return dataframe

    def populate_buy_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
        # Define buy signal logic
        dataframe.loc[
            (dataframe['rsi'] < 30) & (dataframe['close'] > dataframe['sma']),
            'buy'] = 1
        return dataframe

    def populate_sell_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
        # Define sell signal logic
        dataframe.loc[
            (dataframe['rsi'] > 70),
            'sell'] = 1
        return dataframe

Step 4: Integrate Telegram Notifications
Freqtrade has built-in support for Telegram. Once you’ve configured your config.json file, the bot will automatically send buy/sell signals to your Telegram chat. For example:

  • Buy Signal: Buy: EUR/USD at 1.1200
  • Sell Signal: Sell: EUR/USD at 1.1300

If you want to customize the messages, you can modify the send_msg function in Freqtrade’s Telegram module.

Step 5: Backtest Your Strategy
Before going live, backtest your strategy to ensure it performs well on historical data:

freqtrade backtesting --strategy MyForexStrategy --config config.json

Analyze the results and tweak your strategy as needed.

Step 6: Run the Bot in Live Mode
Once you’re satisfied with your strategy, start the bot in live trading mode:

freqtrade trade --strategy MyForexStrategy --config config.json

Your bot will now execute trades based on your strategy and send buy/sell signals to Telegram.

Conclusion
With Freqtrade, you can create a powerful forex trading bot and receive real-time notifications on Telegram. By combining Python’s flexibility with Freqtrade’s robust framework, you can automate your trading strategies and stay ahead in the forex market.

Welcome to connect with me to discuss your ideas!