Late sharing: Bitcoin high-frequency robot with 5% returns everyday in 2014
Tags: bitcoin, robot, fmzquant, cryptocurrency
Published: 2024-05-17 01:26:34

### Introduction of the strategy

The strategy was shared at:
https://www.fmz.com/strategy/1088
The strategy is my main strategy since I started with digital currency. After continuous improvement and modification, it has become more complicated, but the main idea has not changed. The shared version is the original version without obvious bugs. It is the simplest and clearest. There is no position management. Every transaction is full, and there is no restart, but it is enough to explain the problem.
The strategy ran from August 2014 to the beginning of this year, when the exchange charges. During the period, the operation was quite good, and the time of loss was very little. The capital has increased from 200 yuan to 80 Bitcoin. The specific process can be seen in [The way of automated transaction of virtual currency](https://blog.sina.com.cn/s/blog_8e6ab2610102v6sq.html) series of articles in [Xiaocao's Sina blog](https://blog.sina.com.cn/u/2389357153).
The following figure is the return curve of the OKcoin platform that I specifically counted. The initial capital is 1000 yuan. You can see that the initial capital has increased steadily. The middle line is that my strategy has stopped. Later, because the strategy has been changed to a currency-earning strategy, the returns in RMB fluctuates sharply. The specific process is described in the article of the two-year summary of strategy trading.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oyqmc1tcbct9dgqbg66s.png)

The following chart shows the curve of total assets converted to currency:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eyivo0rky1qfcrldea1a.png)

### Why share this strategy?

1. After the exchange charged, almost all high-frequency strategies were killed, and mine is no exception. But changing the strategy may still work, you can study it.
2. I haven't shared anything for a long time. I have long wanted to write this article.
3. Communicate and learn with you all.

### Principle of strategy

The principle of this strategy is very simple. It can be understood as a quasi-high frequency market making strategy. You may want to hit people after reading it, can it make money?! At that time, almost everyone could write it. I didn't expect it to be so effective at the beginning. It can be seen that we should pay attention to practice as soon as we have ideas in mind. In 2014, when Bitcoin robots first emerged, it was too easy to write money-making strategies.
Like all high-frequency strategies, this strategy is also based on orderbook. The figure below shows the order distribution of a typical Bitcoin exchange:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a7ns52vhww4ody6m40ce.png)

We can see the buying order on the left, showing the number of orders at different prices, and on the right side is the selling order. It can be imagined that if someone wants to buy Bitcoin, if he doesn't want to pend the order and wait, he can choose to take the order only. If he has a large number of orders, it will cause a large number of transactions to sell the order and list, which will impact the price. However, this impact will not continue. Some people want to take the order and sell, and the price will probably recover in a very short time. Conversely, it is similar to understanding that someone wants to sell coins.
Take the pending order in the figure as an example. If you want to buy 5 coins directly, the price will reach 10377. At this time, if someone wants to sell 5 coins directly, the price will reach 10348. The price difference is the profit margin. The strategy will pend an order at a price slightly lower than 10377, such as 10376.99, and buy at a price slightly higher than 10348, such as 10348.01. This is because if the situation just happened, it will earn the difference obviously. Although it will not be so perfect every time, the chances of making money are actually incredibly high given the probabilities.
Explain the specific operation with the parameters of the current strategy. This parameter is of course not available, just for illustration. It will look up for a price with an accumulated amount of 8 coins, here is 10377, then the selling price at this time is the price minus 0.01 (the amount can be random). Similarly, it will look down for an accumulated amount of 8 coins, here is 10348, then the selling price at this time is 10348.01, and the difference between the buying and selling price at this time is 10376.99-10348.01=28.98, which is greater than the preset price difference of 1.5, so it will pend an order to wait for the transaction with these two prices, if the price difference is less than 1.5, it will also find a price to pend an order, such as the opening price plus or minus 10, and wait for pick up (it is more appropriate to continue to look down the depth of the follow-through).
In addition, it is noted that this strategy is only related to the current deep pending orders and does not care about the historical market and its own historical transaction. The strategy also has no concept of single loss. In fact, the winning rate of a single transaction is very high.

### Further explanation

- 1. What should I do if I run out of money or currency?
This situation is very common when I have less money. Most of the time, I only pend one side of the order, but it is not a big problem. In fact, we can add the logic of balancing currency and money, but it is inevitable to lose in the process of balance. After all, every transaction is a matter of probability. I choose to wait for the transaction on one side. Of course, this also wastes the transaction opportunity on the other side.
- 2. How are positions managed?
At the beginning, they were all in full position to buy and sell. Later, they were divided into different groups according to different parameters, and they would not be completely closed at one time.
- 3. Is there stop-loss?
The strategy has a complete logic of buying and selling orders. I think there is no need to stop loss (which can be discussed), and there is also the preference of probability. The transaction is an opportunity, and the stop loss is a pity.
- 4. How to adjust to the strategy of earning currency?
At this time, the parameters are symmetrical, that is, the cumulative selling orders of 8 coins up and the cumulative buying orders of 8 coins down are slightly unbalanced. For example, the cumulative selling orders of 15 coins up make the selling opportunities more difficult, and there is a greater chance that they will be returned at a lower price, which will make currency, and in turn make money. In fact, the early strategy is so effective that both currency and money are increased.
- 5. How to deal with floating losses?
Of course, there will be losses in a single transaction, such as the rise of the currency price after selling and the fall of the currency price after buying. Such floating losses do not need to be dealt with, because the transactions are frequent, and it is normal for thousands of times every day. Floating losses are normal, as long as the probability of profit is greater.
- 6. How to prevent black swans?
Bitcoin has a lot of Black Swan time, sometimes it just goes down all the way, and there is no chance to sell it. This situation should not be too worried, because the Black Swan time often brings high volatility, and the strategy makes exactly this part of the money, and the loss can also be earned back quickly.

### Code explanation

The complete code can be seen in my strategy sharing at www.fmz.com. Here, only the core logic functions are explained. Without any changes, the simulation bot that comes with botvs actually works perfectly. This is a strategy more than three years ago, and the platform still supports it now. It is very moving.
First of all, to obtain the bid-ask price function GetPrice(), you need to obtain the order depth information. Note that the order depth information length of different platforms is different, and even if all orders are traversed, there is still no required quantity (this situation will be caused by many 0.01 grid orders in the later stage). The call is GetPrice ('Buy'), which is to obtain the buying price.
```
function GetPrice(Type) {
   //_C() is the fault-tolerant function of the platform
    var depth=_C(exchange.GetDepth);
    var amountBids=0;
    var amountAsks=0;
    //Calculate the buy price and get the cumulative depth to a preset price
    if(Type=="Buy"){
       for(var i=0;i<20;i++){
           amountBids+=depth.Bids[i].Amount;
           //The parameter floatamountbuy is the preset accumulated depth
           if (amountBids>floatamountbuy){
               //Add 0.01 to make the order in the front
              return depth.Bids[i].Price+0.01;}
        }
    }
    //Calculate the selling price similarly
    if(Type=="Sell"){
       for(var j=0; j<20; j++){
    	   amountAsks+=depth.Asks[j].Amount;
            if (amountAsks>floatamountsell){
            return depth.Asks[j].Price-0.01;}
        }
    }
    //After traversing the full depth but still not meeting the demand, a price is returned to avoid bugs
    return depth.Asks[0].Price
}
```
The main function of each loop is onTick(). The loop time set here is 3.5s. Each loop will cancel the original order and re-pend the order. The simpler it is, the less it will encounter a bug.
```
function onTick() {
    var buyPrice = GetPrice("Buy");
    var sellPrice= GetPrice("Sell");
    //diffprice is the preset spread, if the bid/ask spread is less than the preset spread, it will pend a relatively deeper price.
    if ((sellPrice - buyPrice) <= diffprice){
            buyPrice-=10;
            sellPrice+=10;}
    //Cancel all the original orders. In fact, the new price is often the same as the price of the order. At this time, it is not necessary to cancel.
    CancelPendingOrders() 
    //Get account information to determine how much money and how many currencies are currently in the account.
    var account=_C(exchange.GetAccount);
    //The amount of Bitcoins that can be bought, _N() is the precision function of the platform.
    var amountBuy = _N((account.Balance / buyPrice-0.1),2); 
    //The amount of Bitcoin that can be sold, note that there is no position limit, buy and sell as much as you can, as I had very little money at the time.
    var amountSell = _N((account.Stocks),2); 
    if (amountSell > 0.02) {
        exchange.Sell(sellPrice,amountSell);}
    if (amountBuy > 0.02) {
        exchange.Buy(buyPrice, amountBuy);}
    //Sleep and enter the next loop
    Sleep(sleeptime);
}
```
### Ending

The whole program is only over 40 lines, which seems very simple, but it also took me more than a week at that time, which was on the botvs platform. The biggest advantage is that it started early. In 2014, the market was dominated by moving bricks, and the high frequency strategy of grid and inventory grabbing was not too many, which made the strategy like a fish in water. Later, the competition became increasingly fierce, and I had more money and faced many challenges. I had to make major changes every other time to deal with it, but it was generally smooth. Under the condition that the trading platform does not charge, it is a paradise for programmed trading. Because retail investors tend to operate if there is not charge fees, it provides opportunity for high-frequency and arbitrage strategies. All of this basically ends with the frequent two-way fees of 0.1-0.2%. It is not only the problem of being charged, but also the decline of the overall market activity.
However, there is still a lot of room for high-frequency quantitative strategies.

From: https://blog.mathquant.com/2023/01/17/late-sharing-bitcoin-high-frequency-robot-with-5-returns-everyday-in-2014.html