MORITZ PUTZHAMMER
22 November 2022 • 10 min read
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.
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).
Essentially, there are three main use cases for a margin trading bot.
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.
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.
Short selling using borrowing can be better understood by looking at the cash flow that occurs during a short sale.
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.
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.
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 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.
This is the total owned and can be used to repay loans or buy assets, or allocated for limit orders.
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.
This is the total of all the loans on an asset and can only be repaid by free assets.
This is the total interest accrued on all of the loans for this asset. Interest is paid before the loan's principal.
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.
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.
It is important to understand the risks associated with margin trading, of which there are two main types: shorting risk and leverage 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.
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.
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”.
Now let's take a look at a few specific coding examples, which you can use with your margin bot.
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.
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!