Want to dive deeper into open source trading frameworks? Explore our detailed reviews and comparisons.


Why Trend Following?

Before building any bot, I wanted a strategy that’s simple, robust, and proven. Trend following checks all the boxes — it’s based on price momentum and works across markets. The core idea is simple:

Buy when the price is above a moving average. Sell when it’s below.

With that in mind, I built a complete pipeline using only open source tools.


Tools I Used

  • Backtrader – for backtesting and strategy logic
  • CCXT – for live trading API access (Binance)
  • Pandas/NumPy – for data manipulation
  • Python – for glue code and scripting
  • Screen – to run the bot on a VPS or Raspberry Pi

Step-by-Step: How I Built It

1. Strategy Logic (Backtrader)

class TrendStrategy(bt.Strategy):
    def __init__(self):
        self.sma = bt.indicators.SimpleMovingAverage(period=50)

    def next(self):
        if self.data.close[0] > self.sma[0] and not self.position:
            self.buy()
        elif self.data.close[0] < self.sma[0] and self.position:
            self.sell()

I kept it minimal: enter long when price crosses above the 50 SMA, exit when it falls below.


2. Backtesting

Using historical data from Binance (downloaded via CCXT), I tested the logic over months of BTC/USDT data. I evaluated:

  • Win/loss ratio
  • Max drawdown
  • Profit factor

3. Going Live with CCXT

import ccxt

exchange = ccxt.binance({
    'apiKey': 'YOUR_API_KEY',
    'secret': 'YOUR_SECRET',
    'enableRateLimit': True
})

ticker = exchange.fetch_ticker('BTC/USDT')
print("Current BTC price:", ticker['last'])

Once confident, I wired live prices and order placement through CCXT for real trading.


4. Deployment

I deployed the bot on a Raspberry Pi using screen, so it can run 24/7:

screen -S trendbot
python3 bot.py
# Ctrl+A then D to detach

Lessons Learned

  • Simple strategies work when paired with good risk management
  • Open source tools are powerful, but require hands-on debugging
  • Logging and testing are critical when real money is on the line

Want to Build Your Own?

If you’re interested in building your own trend bot with Backtrader or want to see our detailed review of it, check out:
Backtrader framework review and usage guide