:seo_title: Bitfinex Trading Bot in Python: AI Agent and Algorithmic Trading with LumiBot
.. Generated by scripts/generate_ccxt_exchange_pages.py from docsrc/_data/. Edit the data, not this page.
Bitfinex Trading Bot in Python
==============================
.. meta::
:description: Build a Bitfinex trading bot in Python with LumiBot and CCXT. Create Bitfinex API keys, connect them, and run a regular or AI agent strategy.
:keywords: Bitfinex trading bot, Bitfinex API, Bitfinex Python, agentic trading Bitfinex, AI trading bot Bitfinex, algorithmic trading Bitfinex, ccxt bitfinex
This guide shows how to build a Bitfinex trading bot in Python with LumiBot, for both AI agent trading and algorithmic trading. LumiBot connects to Bitfinex through CCXT, the open source library that talks to more than 100 crypto exchanges. You will create a Bitfinex account and API key, connect it, and run two examples: a simple algorithmic trading bot and an agentic trading bot where an AI agent makes the calls.
Bitfinex at a glance
--------------------
.. list-table::
:widths: 35 65
* - CCXT exchange id
- ``bitfinex``
* - Markets
- Spot, Margin, Perpetual swaps
* - Testnet (sandbox)
- No
* - Credentials
- API key, API secret
* - Live data streams
- prices, order book, trades, candles, your orders, your balance
* - CCXT request spacing
- about 250 ms between requests
* - Official site
- https://www.bitfinex.com
* - API docs
- https://docs.bitfinex.com/v2/docs/
* - Fees
- https://www.bitfinex.com/fees
What is Bitfinex?
-----------------
Bitfinex is a digital asset trading platform for traders and liquidity providers. It offers spot (exchange) trading, margin trading, margin funding and derivatives.
Sign up for Bitfinex
--------------------
#. On https://www.bitfinex.com/ click Sign Up, then enter a username, email and password. Confirm your email with the 6-digit code or the link Bitfinex sends you.
#. Turn on two-factor authentication. Bitfinex will not let you use its features until 2FA is set up.
#. Finish account verification. Bitfinex has no separate testnet, but you can create a Paper Trading sub-account for practice (see below).
`Open a Bitfinex account `_
Create a Bitfinex API key
-------------------------
#. Log in on the Bitfinex website and open the API Key page (https://www.bitfinex.com/api).
#. Choose the Create New Key tab.
#. Pick the permissions (see below). Under IP Access restrictions you can whitelist up to 20 IP addresses, such as the machine that runs your bot.
#. Give the key a label and click Generate API Key. Confirm with your 2FA code or security key.
#. Open the confirmation email and click Create API Key. Use the same browser you use for Bitfinex.
#. Copy the API Key and API Key Secret right away. Bitfinex shows them only once.
**Which permissions to turn on:** Turn on read access for Account Info, Account History, Orders and Wallets, and turn on write access for Orders so the bot can create and cancel orders. Add Margin Trading only if your bot trades margin or derivatives. Leave Withdrawals off. A bot never needs to move money out of your account. Make the key on the website: Bitfinex says logins and keys made through the mobile app start with every permission on.
.. warning::
Never turn on withdrawals for a Bitfinex trading bot key. Bind the key to your server's IP address when Bitfinex lets you, keep the secret out of your code and out of Git, and delete the key if it ever leaks.
Connect Bitfinex to LumiBot
---------------------------
The simplest way: put these lines in a ``.env`` file next to your strategy. LumiBot loads the file, sees ``TRADING_BROKER=bitfinex`` and connects to Bitfinex for you, so your strategy needs no broker code.
.. code-block:: bash
TRADING_BROKER=bitfinex
BITFINEX_API_KEY=your_apiKey
BITFINEX_API_SECRET=your_secret
LumiBot reads exactly the fields CCXT needs for Bitfinex (``apiKey``, ``secret``). If one is missing, LumiBot stops at start-up and names the variable you still need.
You can also build the broker in code. This is handy when one script talks to more than one exchange, or when you need extra CCXT options. The keys in the config are CCXT's own credential names for Bitfinex:
.. code-block:: python
import os
BITFINEX_CONFIG = {
"exchange_id": "bitfinex",
"apiKey": os.environ.get("BITFINEX_API_KEY"),
"secret": os.environ.get("BITFINEX_API_SECRET"),
"sandbox": False, # no sandbox for this exchange
}
from lumibot.brokers import Ccxt
broker = Ccxt(BITFINEX_CONFIG)
# then: MyStrategy(broker=broker, ...)
Either way, LumiBot turns on CCXT's built-in rate limiting for you.
Testing on Bitfinex
-------------------
Bitfinex has no sandbox, and CCXT lists no test URL for it, so test with a very small amount. With environment variables, leave ``BITFINEX_SANDBOX`` unset. In a code config, set ``"sandbox": False``. For practice, Bitfinex lets you create a Paper Trading sub-account with test balances; keys for it are made while you are switched into that sub-account.
Algorithmic trading example: a simple Bitfinex bot
--------------------------------------------------
This bot buys about 50 USD of BTC on Bitfinex once and then holds it. It is a starting point to check that your keys, balances and orders work. If the exchange's minimum order is larger, raise the amount.
.. code-block:: python
from lumibot.entities import Asset
from lumibot.strategies import Strategy
class BitfinexBuyOnce(Strategy):
"""Buy a small amount of BTC once, then hold it."""
def initialize(self):
self.sleeptime = "1H"
self.set_market("24/7") # crypto trades all day, every day
def on_trading_iteration(self):
coin = Asset("BTC", asset_type=Asset.AssetType.CRYPTO)
if self.get_position(coin) is not None:
return # already holding BTC
price = self.get_last_price(coin, quote=self.quote_asset)
if price:
quantity = round(50 / price, 6) # about 50 USD worth
order = self.create_order(coin, quantity, "buy", quote=self.quote_asset)
self.submit_order(order)
if __name__ == "__main__":
# LumiBot reads TRADING_BROKER and the keys from your .env file.
strategy = BitfinexBuyOnce(quote_asset=Asset("USD", asset_type=Asset.AssetType.CRYPTO))
strategy.run_live()
Agentic trading example: an AI agent that trades on Bitfinex
------------------------------------------------------------
Agentic trading means an AI agent reads the market and decides what to do. In this example one LumiBot agent looks at prices and your Bitfinex account, then places the orders itself. There is no Python logic in between: you set the rules in plain English in the prompt, such as what it may trade and how big each order can be. This follows the same pattern as LumiBot's AI trading team examples.
.. code-block:: python
from lumibot.entities import Asset
from lumibot.strategies import Strategy
class BitfinexAITrader(Strategy):
"""One AI agent researches the market and places the trades itself."""
def initialize(self):
self.sleeptime = "4H"
self.set_market("24/7")
self.agents.create(
name="trader",
model="openai/gpt-6-luna",
reasoning_effort="high",
allow_trading=True,
system_prompt=(
"You manage a small crypto account on Bitfinex. "
"You may trade BTC and ETH against USD (asset_type 'crypto', quote_symbol 'USD'), or hold cash. "
"Before every order, call account_portfolio, account_positions and "
"market_last_price for that exact symbol in the same run. "
"Keep each order under 10% of the account value. "
"Explain each decision in one short paragraph."
),
)
def on_trading_iteration(self):
self.agents["trader"].run(
task_prompt="Review recent BTC/USD and ETH/USD prices and your current holdings, then decide whether to buy, sell or hold.",
context={"exchange": "Bitfinex", "quote": "USD", "date": self.get_datetime().isoformat()},
)
if __name__ == "__main__":
# LumiBot reads TRADING_BROKER and the keys from your .env file.
strategy = BitfinexAITrader(quote_asset=Asset("USD", asset_type=Asset.AssetType.CRYPTO))
strategy.run_live()
The agent needs a model key such as ``OPENAI_API_KEY`` in your environment. See the :doc:`AI agents quickstart ` for models, tools and memory.
What you can trade on Bitfinex through CCXT
-------------------------------------------
.. list-table::
:header-rows: 1
:widths: 40 60
* - Market
- Listed by CCXT
* - Spot
- Yes
* - Margin
- Yes
* - Perpetual swaps
- Yes
* - Dated futures
- No
* - Options
- No
CCXT lists these order features for Bitfinex: market orders, limit orders, stop orders, reduce-only orders, editing open orders, canceling orders.
For perpetual contracts, use the full CCXT symbol so the settlement currency is clear, for example ``BTC/USDT:USDT``. See :doc:`the CCXT guide ` for contract quantities, position modes and leverage.
Bitfinex tips
-------------
* Bitfinex lists BTC against USD, USDT, EUR, GBP and others. Spot is plain ``BTC/USD``. Perpetuals use the CCXT form ``BTC/USDT:USDT``.
* A Paper Trading sub-account is created from Sub-Accounts, Create new sub-account, Paper. It needs its own email and username, and it can never be turned into a regular account. Its test tokens have no value.
* Bitfinex sub-accounts share the master account's trading volume and fee tier, and the master account can restrict sub-account withdrawals. A master account can have up to 100 sub-accounts.
* Bitfinex REST limits range from 10 to 90 requests per minute depending on the endpoint, and going over blocks your IP for 60 seconds. Use a separate API key for each bot, because each key keeps its own nonce sequence.
Frequently asked questions
--------------------------
**Can I build a Bitfinex trading bot in Python?**
Yes. Install LumiBot (``pip install lumibot``), set your Bitfinex keys and use the ``bitfinex`` CCXT exchange id as shown above.
**Does Bitfinex have a testnet?**
No testnet is set up for Bitfinex in LumiBot. Start with a small amount, and in a code config set ``"sandbox": False``.
**Which API permissions does a Bitfinex bot need?**
Turn on read access for Account Info, Account History, Orders and Wallets, and turn on write access for Orders so the bot can create and cancel orders. Add Margin Trading only if your bot trades margin or derivatives. Leave Withdrawals off. A bot never needs to move money out of your account. Make the key on the website: Bitfinex says logins and keys made through the mobile app start with every permission on.
**Can an AI agent trade on Bitfinex by itself?**
Yes. The agentic example above gives one agent permission to trade. You control what it may trade and how much through its prompt.
**Can I trade Bitfinex perpetual futures with LumiBot?**
CCXT lists perpetual swaps for this exchange. Use the full contract symbol, such as ``BTC/USDT:USDT``.
**Is LumiBot free?**
Yes. LumiBot is open source on GitHub at https://github.com/Lumiwealth/lumibot. You pay only your exchange's trading fees and, for AI agents, your model provider.
Run it in the cloud with BotSpot
--------------------------------
Do not want to keep a computer running for your Bitfinex bot? BotSpot is the hosted home for LumiBot strategies and AI trading agents: build with AI, backtest, connect a broker, and watch every trade with logs and alerts.
`Try BotSpot `_
Related guides
--------------
* :doc:`CCXT crypto trading with LumiBot `
* :doc:`AI agents quickstart `
* :doc:`Backpack trading bot `
* :doc:`BitMart trading bot `
* :doc:`CoinEx trading bot `
* :doc:`DigiFinex trading bot `
Sources
-------
* `Bitfinex Support: How to create and revoke a Bitfinex API Key `_ (read 2026-10-23)
* `Bitfinex Support: How to create an account on Bitfinex `_ (read 2026-10-23)
* `Bitfinex Support: Paper Trading at Bitfinex `_ (read 2026-10-23)
* `Bitfinex Support: master and sub-accounts `_ (read 2026-10-23)
* `Bitfinex API docs: requirements and limitations `_ (read 2026-10-23)
* `Bitfinex API docs: paper trading `_ (read 2026-10-23)
* Exchange id, URLs, markets, credentials and stream support: CCXT 4.5.64 metadata, generated 2026-10-23