avatarEsteban Thilliez

Summary

The article provides a guide on integrating live trading capabilities into a Python-based trading bot using Backtrader, with a focus on Oanda and CCXT for connecting to various exchanges like Binance and Coinbase.

Abstract

This article is the fourth installment in the "Build a Trading Bot" series, which assumes prior knowledge of Backtrader, a Python framework for backtesting and live trading. It guides readers through the process of transitioning from backtesting to live trading with their trading bot. The author, Esteban Thilliez, provides code examples and explanations on how to configure a store for live trading with Backtrader, using Oanda and the CCXT library for cryptocurrency exchanges. The article also covers the differences between backtesting and live trading, the concept of a "Store" in Backtrader, and how to handle live data feeds. Additionally, Thilliez offers practical advice on installing necessary dependencies, creating live trading scripts, and addresses potential performance discrepancies between backtesting and live trading. The article concludes with encouragement for readers to practice by building their own trading bots and invites them to engage with the author for further assistance.

Opinions

  • The author emphasizes the importance of understanding Backtrader before attempting to implement live trading.
  • Live trading with Backtrader requires proper configuration of a "Store" to interface with a broker.
  • The article suggests that while live trading maintains similar features to backtesting, it involves real-time data which may lead to performance differences.
  • Encryption of requests (via a nonce) and rate limiting are important considerations to secure and responsibly manage live trading bots.
  • The author acknowledges the possibility of developing custom integrations with other brokers but notes that this is beyond the scope of the series.
  • Readers are encouraged to practice building trading bots and to reach out for support if needed.
  • Thilliez provides a rationale for not implementing other brokers in the series but offers guidance for those interested in creating their own integrations.
  • The article promotes the author's other content and Medium membership, suggesting a commitment to providing value and fostering a community of engaged readers.

Build a Trading Bot with Python — 4. Live Trading

Oanda and CCXT Integration

Photo by Maxim Hopman on Unsplash

This is the fourth story of the “Build a Trading Bot” series. You need to know Backtrader to understand this story. If you don’t know what Backtrader is or if you want to find the other stories of this series, you should check the following story:

There’s a GitHub repo I’ve made for this series. If you want to use it to follow the code, you can find it here: Trading Bot Series.

Generally, the ultimate goal of a trading bot is to enable automatic and live trading. So far, we have only implemented backtesting functionality. It’s time to implement live trading capability to our bot.

It’s possible to make a trading bot compatible with any broker you want. However, our bot works with Backtrader, and there are not a lot of brokers compatible with Backtrader. One of them is Oanda. There are also Interactive Brokers and Visual Charts. There is also a Backtrader CCXT integration allowing us to live trade on most of the cryptocurrency exchanges (Binance, Coinbase, etc…).

Of course, it is possible to develop your own integration compatible with Backtrader and your broker, but that is beyond the scope of this series.

I’ll start by explaining the differences between live trading and backtesting.

Live Trading

Overall, all of Backtrader’s features remain similar. That is, orders are processed the same way, your strategy works the same way, etc…

Behind live trading, there’s a concept of “Store”. The store is the gateway between Backtrader and your broker. It provides the data and the Backtrader’s broker.

So, when live trading, you will have to configure a store. Here is an example to live trade on Binance:

All stores work almost the same way, so their configuration should look almost the same. Most of the time you need to pass at least an API key and an account name or a secret as parameters. The other parameters depend on the store.

Some parameters used in the example above are:

  • exchange: Binance, Coinbase, etc…
  • currency: the quote currency.
  • nonce: a parameter used to encrypt the requests.
  • enableRateLimit: to prevent the bot from spamming requests.
  • sandbox: to paper trade.

Then, you can get a Datafeed from the store using store.getdata() . You can also get a broker using store.getbroker() .

One thing to know about these two objects is that they get data from live, so data can sometimes be outdated or inaccurate. That’s why there can be performance differences when backtesting a strategy and when paper trading with it.

Also, data you get sometimes is delayed data because it can be data Backtrader needs to perform calculations. For example, if you’re using an EMA 200, Backtrader needs some historical data to calculate this moving average.

When data status changes, the Cerebro sends a notification to the strategy through notify_data . So, let’s implement this in our base strategy:

Now, we can add a method to our bot to implement live trading, using everything above. The method will be very similar to the one to backtest. To avoid code repetition, I refactored it a bit using a static method.

Live Script

Now, all we have to do is to create a script to make live trading easy. But first, we have to install some dependencies.

The first is oandapy . If you try to install oandapy using just pip install oandapy it won’t work, instead, do it this way:

pip install git+https://github.com/oanda/oandapy.git

Then, there is ccxt :

pip install ccxt

Finally, there is ccxt-bt :

pip install git+https://github.com/Dave-Vallance/bt-ccxt-store.git

Now, let’s create our little script:

You should understand everything. As you can see, the Oanda store and the CCXT store are not very different.

If you have API keys to try the code, it should work. If you don’t, you can create APIs from your broker’s website.

Final Note

If you don’t use Oanda or cryptocurrency exchanges, I’m sorry, I won’t implement other brokers through this series. But if you have questions and want to use this trading bot with your broker, feel free to leave a comment or contact me and I will try to solve your issues.

You can also try to create your own stores or your own live data feeds and brokers.

I guess this story is the last from the series. I have some ideas of things to implement in the bot, but it’s more related to algorithmic trading than trading bots.

I hope you found this series useful. There are a lot of ways to build trading bots, and I suggest you try to create your own, in a different way than I did, because it’s good to practice.

To find the other stories of this series and more about mixing trading and Python, check this: Improve your Trading with Python

To explore more of my Python stories, click here!

If you liked the story, don’t forget to clap and maybe follow me if you want to explore more of my content :)

You can also subscribe to me via email to be notified every time I publish a new story, just click here!

If you’re not subscribed to Medium yet and wish to support me or get access to all my stories, you can use my link:

A Message from InsiderFinance

Thanks for being a part of our community! Before you go:

Trading
Python
Trading Bot
Algorithmic Trading
Programming
Recommended from ReadMedium