How to make a cryptocurrency trading bot- Part 2

Risk management. Why you have to avoid liquidation on BitMex. .

Strategst
Coinmonks
Published in
5 min readJul 25, 2020

--

Quick caveats:

  • Not trading advice.
  • Some others know bot-building/Pine/etc. better than I.
  • I personally have no affiliation with TradingView, Alertatron, exchanges, or any mentioned services.
  • Most products I’m showing here I use for several years already.
  • You can help this project by registering with the referral links in the post.

Ask everything about bots in My Own Crypto Bot discussions

Part zero, why I am doing this

Part One, how to generate signals with open source strategy on TradingView.

After following the steps from Part 1, now we have two alerts for buying and selling Bitcoin.

First, let’s return to the strategy version of our script and look at what risk management we have here.

Add this updated version of the strategy, as the old version will not work with this tutorial.

Go to strategy Settings — Properties — List of Trades

As we saw in Part 1, we are testing high leverage strategy with a simple risk management system:

1. The initial capital of $14552.5, equals 1 BTC in dollars

2. Risking 3% of capital in every trade

3. Same Position Size of 1 BTC in every trade

4. Standard Stop loss of 3%.

5. Here we won’t use take profits, as in this trend-following strategy the foremost thing is to ride the whole trend wave, and to avoid early exiting.

For precise backtesting make sure that your Initial Capital and Order Size are in proportion with Bitcoin price in the first trade

For testing this strategy with other amount of Initial Capital (supposed you don’t have 1 whole BTC), you have to change your Initial Capital value and Order Size accordingly. So for example, if you want to test your strategy with 0.3 BTC, you have to set your Initial Capital to $4365.75 (Value of 0.3 on 2018/01) and Order Size of 0.3.

Testing TradingView strategy with 0.3 BTC

We can return to Overview and see we’ve changed that net profit and drawdown numbers.

We choose this specific risk management system for its simplicity. You can see that the standard Order Size (or Position Size) here is 100% of our Initial Capital, but we risk only 3% of it. That means we use the leverage of x33 to trade this position.

How we made these leverage calculations?

We used a free AntiLiquidation Tool for this.

There are few detailed guides for this tool, but here we will briefly describe how it works.

Assuming that we have 1 BTC that costs $10000. Our allocated capital for one trade is 3%. So, I am in fact we are able to risk only $300 in real dollars. Our stop loss is also 3%. To calculate our Position Size, we have to divide our Risk Amount by Distance to Stop (credits to for this formula).

So, our position will be $300/0.03 = +10,000.

If it all sounds confusing to you, happy to say that you don’t have to make all these calculations for every trade, we will code this system in automation service in the next chapter.

But now, for checking our calculation logic we will use Antiliquidation Tool that can calculate all of it for us. Input your Risk Amount and your Initial Capital from strategy settings (denominated in Bitcoin) and leave “Distance to stop” as 3% of BTC price — we have hardcoded this stoploss inside the strategy. In the green window on the right, you will have Results that show your leverage and position size.

Let’s calculate one more example for calculating trading position with 0.3 BTC and risking 3% of capital en every trade.

Here we put Bitcoin price on the start of our backtesting in “Bitcoin Price”, our “Capital”, “Risk amount” of 3%, and Distance to stop as 3% of Bitcoin price, it will be $436. In “Result” we have $4367 as our Position Size and leverage of x33.

But here is one more thing.

There is no KYC on crypto exchanges such as BitMex or ByBit, exchange owners doesn’t know who you are, so they can’t credit you with money. That means you can’t owe them anything in the case you get liquidated on your trade.

Instead, BitMex proposed another thing. In every position they will reserve some percentage of your real dollars Risk amount, to have some insurance in the case they couldn’t sell your position before you go bankrupt.

That means, when you trade with high leverage and tight liquidation price, the exchange has to sell your position real quick not to draw you into debt. As the debt here is impossible, they will start to sell your position some dollars before the price hits your bankruptcy level.

So, for example, when you trade with x100 leverage and BTC is $10,000, you have 1% or $9900 as your bankruptcy price, but BitMex will start to sell your position at just about $9950, giving you 0.5% of exchange margin, to be sure that they will be able to sell your position before BTC price will hit $9900.

What if they sell your position quickly on $9949 level? The exchange will liquidate you and take your $49 dollars to their Insurance Fund. BitMex Insurance Fund numbers normally increasing, but in times of real big moves, it helps the exchange to stay liquid.

In this part, we were calculating our position and real leverage keeping one thing in mind: not to feed exchanges insurance fund. How to do this? Always keep your liquidation price below your stop loss in longs and above stop loss in shorts.

In the next chapter, we will set BitMex leverage to x10 to keep liquidation price low and will trade our strategy at x33 real leverage using our own calculations.

Then I will show you how to automate the risk management system and execute it on the exchange, so we can wrap up our bot and start automatic crypto trading.

See you on My Own Crypto Bot discussions!

Get Best Software Deals Directly In Your Inbox

--

--