tv2bt: Bitfinex Example, without leverage

This post provides a complete example script for live trading on Bitfinex (without leverage). It uses tv2btso that you can mix and match between using Tradingview and Backtrader! This means that it also follows on from our last post which introduced thetv2btbridge. So if you have not read that yet, you should start there to learn about tv2bt and how to set it up. To whet your appetite and encourage you to click that linktv2btprovides a way to use Tradingview’s alert system with Backtrader to trigger trades on live exchanges.

For those looking for leveraged trading, be patient, an example of that will come in the future!


The code in this article requires that you have bt-ccxt-store installed. You also must have a quite recent version for it to work correctly. If you have not updated since November the 20th 2019, then go and grab the latest source code from here:


As mentioned in the introduction, we will be trading without leverage. To speak more simply, that means just buying and selling coins. It is important to note that when we do this, we are not really in a “position” as most people would see it. We are just swapping assets. With currency pairs, you are always simultaneously long and short. For example, if you use USD to buy some Euro’s then you are long EURO and Short USD at the same time. When you sell your Euro’s, the opposite is true.

See the following article for a good explanation:

Having said all that, in the example code below, Backtrader will still report being in a position when we buy the base currency. In other words, our position size will be greater than zero. When we sell the currency pair, it will go back to zero. However, for the reasons mentioned above, we are technically not in a position and you will not see an open position in Bitfinex. You will just see your wallet balances change.

The Code


Before testing anything, you will need to insert your own Bitfinex API key and secret into the script. You can do that near the top of the script.

After this, testing the code should be done in a controlled way. As such, it is recommended that you test the script using fire_alerts.pyfrom the tv2btrepository. It will be much quicker than setting up alerts on Tradingview and waiting for them to fire. Once you confirm that it is working, then setup Tradingivew to send alerts to your server.

When buying coins, the script uses a percentage of your available counter currency (e.g USD) to calculate the qty. We then use that quantity to buy the base currency (e.g BTC). This is important to point out for two reasons

  1. You must have some counter currency available to purchase the base currency. If you do not, you will get an error.
  2. You must provide a fake close price using fire_alerts.pythat is close to the current price. This is so that the right QTY size can be calculated for the amount of cash in your account.

Note: fire_alerts.pyhas recently been updated to support sending OHLCVdata. So be sure to grab an updated version from the repository.

Once you fire a signal to purchase or sell, then you should follow it up with a second signal to receive the order notification that it was completed. So if the first alert has an action of 1 (to buy), then follow up with an actionof 0. Alternatively, you can load up Bitfinex in your web browser and monitor the order history.

Successful Buy Order

If all goes well, you can expect to see some output like this:

An image showing a successful buy order sent via tv2bt

Successful Sell Order

And like this for a sell order:

Showing a sell order submitted via tv2bt

That’s it! Happy Trading.