Building a Trading Bot with Freqtrade
Set up Freqtrade, configure a strategy, run a backtest, and deploy a dry-run bot.
Installation and Setup
Freqtrade is a Python-based crypto trading bot. ```bash python -m venv freqtrade-venv source freqtrade-venv/bin/activate pip install freqtrade
Initialize a new bot configuration:
```bash
freqtrade create-userdir --userdir user_data
freqtrade new-config --config user_data/config.jsonThe wizard prompts for exchange, API keys (optional for backtesting), and trading pairs.
Creating a Custom Strategy
Strategies are Python classes defining buy/sell signals. ```bash freqtrade new-strategy --strategy MyFirstStrategy
```python
from freqtrade.strategy import IStrategy
from pandas import DataFrame
import talib.abstract as ta
class MyFirstStrategy(IStrategy):
minimal_roi = {"60": 0.01, "30": 0.02, "0": 0.04}
stoploss = -0.10
timeframe = '5m'
def populate_indicators(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
dataframe['rsi'] = ta.RSI(dataframe, timeperiod=14)
return dataframe
def populate_buy_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
dataframe.loc[dataframe['rsi'] < 30, 'buy'] = 1
return dataframe
def populate_sell_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
dataframe.loc[dataframe['rsi'] > 70, 'sell'] = 1
return dataframeRunning a Backtest
First download historical data: ```bash freqtrade download-data --exchange binance --pairs BTC/USDT --days 120 --timeframe 5m
Run the backtest:
```bash
freqtrade backtesting --strategy MyFirstStrategy --timeframe 5m --timerange 20250101-20250601Output includes total trades, win rate, average profit, max drawdown, Sharpe ratio, and profit factor.
Dry-Run Deployment
Configure dry-run mode: ```json {"dry_run": true, "dry_run_wallet": 1000, "exchange": {"name": "binance", "pair_whitelist": ["BTC/USDT"]},"max_open_trades": 3, "stake_amount": 50}
Start the bot:
```bash
freqtrade trade --config user_data/config.json --strategy MyFirstStrategyThe bot simulates trades without placing real orders.
Verified by
editor
Last verified
2026-06-24
