Trality has been discontinued as of 31.7.2023. Thank you to all our users 💙.
Trality has been discontinued as of 31.7.2023. Thank you to all our users 💙.
Trality has been discontinued as of 31.7.2023. Thank you to all our users 💙.
Trality has been discontinued as of 31.7.2023. Thank you to all our users 💙.

Crypto Margin Trading Bots: Getting Started Using Python


22 November 202210 min read

Table of contents

Margin trading is very similar to spot trading. In both cases, a trade is simply the conversion of one currency for another.

The main difference with margin trading is that it introduces the ability for a trader to borrow a currency. While the currency is borrowed, interest is paid on the loan. Borrowing gives traders the ability to short sell the market as well as take more risk. In the case of short selling, an asset can be borrowed and sold immediately in the hope that the price will be lower at a later date. When the loan is repaid, the profit is the price for which the borrowed asset was sold and the price at the time the loan was repaid.

Create your own trading algorithm

Check out the Trality Code Editor. Our world-beating Code Editor is the world’s first browser-based Python Code Editor, which comes with a state-of-the-art Python API, numerous packages, a debugger and end-to-end encryption.

We offer the highest levels of flexibility and sophistication available in private trading. In fact, it’s the core of what we do at Trality.

Try out our lightning-fast backtester and the full python code described at the bottom of this artcle.

Let’s take an example. If a trader has a strong conviction that the price of BTC is going up, then they can borrow USDT to purchase BTC. This is known as leverage trading since it is possible to take a bigger position in BTC than would be possible without borrowing. This leverage multiplies both the profits and losses as a percentage of the traders account balance, which is why it can be risky.

In the following article, we'll explore how you can get started with crypto margin trading bots by using Python, covering topics such as margin trading interest and fees, examples of short selling and leverage trading, margin ratio limits, and the issue of liquidation before concluding with a number of Python code examples (e.g., OCO, long-short QQE strategy, and calculating daily interest, among others).

Margin Trading Bots Use Cases

Essentially, there are three main use cases for a margin trading bot.

  • Short selling: taking a position that profits from the price of the asset going down.
  • Buying on leverage: long-only strategy but is able to take more risk than would be possible without borrowing.
  • Long/short strategies: these are used by relative value strategies that buy one asset and sell another asset to make money from the relative performance.

Margin Trading and Borrowing

Margin trading can seem complicated, but it is much simpler to think of margin trading as similar to spot trading and separately understand the process of borrowing and repaying crypto loans. If the trader wants to buy ETH with BNB, for example, but currently does not have BNB, then they can either sell another asset to buy BNB or they can borrow the required BNB.

The ability to borrow cryptocurrencies the trader does not own is what makes short selling possible. To sell BTC in the spot markets, the trader is required to already own some. However, with margin trading, BTC can be borrowed and then sold without ever owning it. When the BTC is bought back at a later date, the BTC loan and interest can be repaid.

Margin Trading Interest and Fees

Borrowed currency accrues interest in the same currency. If BTC is borrowed, the interest is payable in BTC. The interest is computed by the exchange and, in the case of you run a Binance margin trading bot, it is compounded hourly at a rate of 1/24 times the daily interest rate. These rates can be found at Binance Margin Interest Rates.

The minimum amount of time that currency can be borrowed on Binance is 1 hour. It is also important to note that, when placing a limit order on margin, the currency is borrowed when the order is placed and not when it is filled. So if the order is not filled, interest will still be owed on the transaction.

An Example of Short Selling

Short selling using borrowing can be better understood by looking at the cash flow that occurs during a short sale.

short selling, Python, margin trading bots

In the end, the USDT balance has grown from 20,000 USDT to 30,000 USDT, which is the same magnitude as the price change of BTC from 35,000 USDT to 25,000 USDT.

An Example of Leverage Trading

A trader can also trade with up to 3x leverage on Binance Cross Margin, which allows a trader to buy more of an asset than they would otherwise.

leverage trading, Python, margin trading bots

The final balance is 55,000 USDT or a profit of 30,000 USDT from the starting balance of 25,000 USDT. The price of BTC during the trade changed from 25,000 USD to 40,000 USD, and so using 2x leverage doubles the profits.

Margin Account Balances

Margin balances are similar to spot balances with the addition of borrowed amount and an interest owed amount. Borrowed amount is the total amount borrowed of an asset and interest is the total interest accrued.

Spot balances example

spot balances, Python, margin trading bots

Margin balances example

margin balances, Python, margin trading bots

Free amount

This is the total owned and can be used to repay loans or buy assets, or allocated for limit orders.

Locked amount

This is the total of owned assets that are reserved for a limit or stop orders and cannot be used to trade or repay loans. When a limit order is placed, then the amount will be transferred from free to locked. Only free assets can be locked.

Borrowed amount

This is the total of all the loans on an asset and can only be repaid by free assets.

Interest amount

This is the total interest accrued on all of the loans for this asset. Interest is paid before the loan's principal.

Margin Ratio

The margin ratio is calculated as the value of assets / the total liabilities. The total liabilities is the borrowed principal amount and all interest owed. The exchange ensures that this ratio is greater than 1.1. If this is not the case, then the assets owned by the trader will be forced sold and the liabilities repaid. The trader must do everything possible to avoid liquidation.

Margin ratio limits

  • Margin trading limit: this is the minimum margin ratio at which no more borrowing will be allowed.
  • Margin call limit: this is the margin ratio at which the trader will be notified that their account is at risk of liquidation if the loans are not repaid or if more collateral is added.
  • Liquidation limit: below this margin ratio, the assets in the account will be automatically sold and interest and borrowed amount paid.


Liquidation will take place if the margin ratio drops below 1.1. This is an automatic process that is done by the exchange in which assets are sold to increase the margin ratio. Loans are repaid from the proceeds of sales and a fee is also taken by the exchange. In the case of Binance, the liquidation fee is 2% of the value of the assets sold. It is important to note that assets not associated with the trade may be sold to repay borrowed currencies. If the trader owns BTC and also has a 3x leverage long on LUNA, then some of the BTC will be sold if LUNA goes to zero.

Risk Associated with Margin Trading

It is important to understand the risks associated with margin trading, of which there are two main types: shorting risk and leverage risk.

Margin trading and shorting risk

The risk associated with short is that the asset price moves up without stopping. In the case of buying an asset, the price can only go to 0. If the price goes up, there is no limit. A short trade will be closed when the losses exceed the value of the collateral in the account. The position will be liquidated as a result and the loans on the shorted asset repaid.

Margin trading and leverage risk

If the trader has borrowed to buy an asset, then the margin account may be liquidated before the asset price has reached zero. In the case where leverage is 2x, if the value of the asset falls by about 50%, then the account will be liquidated. The maximum leverage allowed for cross margin on Binance is 3x. If a position is 3x leveraged, then it only takes a drop of about 33% for the account to be liquidated.

Loan and Repayment Refresh

When loans are placed, they are not immediately executed. They are similar to orders in that they are requests that can fail, if, for instance, the requested loan or repayment violates the margin ratios or available balances. The margin borrow repay logic returns a loan or repayment object that can be stored in the state and refreshed and queried later.

On the next execution after the loan or repayment is created, the status will change from “Pending” to “Confirmed” or “Failed”.

Python Code Samples for Your Margin Trading Bot

Now let's take a look at a few specific coding examples, which you can use with your margin bot.

One-Cancels-the-Other (OCO)

Short selling

Buying with leverage

Long-short QQE strategy

Calculate daily interest cost

Final Thoughts on Margin Trading Bots with Trality

Obviously, there is far more to say on the subject, but the above guide is meant to serve as an informative introduction to getting started with margin trading bots by using Python.

Looking to create your own trading algorithm using Python?

Check out the Trality's Python Bot Code Editor — a powerful browser-based tool designed for traders who want to build, backtest, optimize, and live trade with algorithmic trading bots. We offer the highest levels of flexibility and sophistication available in private trading. In fact, it’s the core of what we do at Trality.

Once you’re happy with your margin bot, the next step is to link it with the best crypto margin trading exchange. In terms of trading on a centralized exchange, a Binance trading bot will likely offer the best benefits to a broader range of traders, but there are excellent decentralized exchanges from which to choose if you decide to go that route.

Here at Trality HQ, we are constantly refining and optimizing our platform's tools to meet the needs of our users. If there are any specific code instances that you would like to see covered, then by all means get in touch!