The resource loading... loading...

Bitcoin high-frequency strategy robot from 2014

Author: The grass, Date: 2014-10-21 22:58:44
Tags: High-frequency

The strategy is presented.

This is the address of the policy sharing:https://www.fmz.com/strategy/1088This strategy has been my main strategy since I started working on virtual currencies, and after constant improvement and modification, it has become much more complex, but the main idea has not changed. This version shared is the original version without obvious bugs, the simplest and clearest, no position management, every transaction is full, no card reset after death, etc., but it is also enough to explain the problem. The strategy ran from August 2014 until the exchange charged fees earlier this year. It was a good run, with little time to lose. The funds ran from the initial $200 to $80 bitcoin.The Sina blog of the little grassIn theHow to automate virtual currency transactionsThis is a series of articles.

Why Share This Strategy

1. The exchange has killed almost all high-frequency strategies, including mine, after charging the transaction fee. 2. I haven't shared anything in a long time, I wanted to write this article for a long time. 3. Share and learn together.

The Principles of Strategy

This strategy is extremely simple, it can be understood as a quasi-high-frequency market strategy, you may want to beat someone after you see it, it can make money, almost anyone can write it. I did not expect it to be so effective at the beginning, it is obvious that there is an idea in my mind to rush to practice, not to say there must be surprises. As with all high-frequency strategies, this one is based on an order book, and the chart below is a typical Bitcoin exchange's order distribution.imgYou can see on the left side is the purchase order, showing the number of pending orders at different prices, on the right side is the sale order. You can imagine if a person wants to buy bitcoin, if he does not want to wait for the pending order, he can only choose to buy the order, if his order is more, it will make a lot of transactions in the pending order, causing a shock to the price, but this shock is generally continuous, and someone does not want to eat the order, the price is likely to recover in a very short time, and on the other hand understands that someone wants to sell the coin similarly. For example, if someone were to directly buy 5 coins, the price would reach 10,377, while if someone were to directly sell 5 coins, the price would reach 10,348, which is the profit space. The strategy would be to list a price slightly below 10,377, such as 10,376.99, and buy at a price slightly above 10,348, such as 10,348.01, which would obviously be the difference if this had just happened. To explain the specific operation with the parameters of the strategy now, this parameter can not be used, of course, only as an illustration. It will look up the price of the cumulative number of pending orders of 8 coins, here is 10377, so the selling price at this time is this price minus 0.01 (minus how much can be random), and the same way to look down the cumulative order hanging at 8 coins, here is 10348, so the selling price at this time is 10348.01, at this time the difference in the purchase price is 10376.99-10348.01 = 28.98, greater than the price difference predicted by the strategy of 1.5, so that if the two prices pending orders are delivered, the price difference is less than 1.5, also find a price for the pending order, such as the discount price of 10, plus the waiting hole.

Further information

  1. What do you do with no money or coins? This situation is very common when I have less money, most of the time only hanging on one side of the note, but it is not a big problem. In fact, it can be added to the logic of monetary balance, but in the process of balancing, inevitably there are losses, after all, each transaction is a speculation of probability, I choose to remain unilaterally waiting for the transaction, of course this also wastes the opportunity of the other side.
  2. How is the position managed? At the beginning, there are full stocks bought and sold, then divided into different groups according to different parameters, not completely completed at once.
  3. Is there no stopping? The strategy has a complete logic of buying and selling lists, I think there is no need to stop losses (it can be discussed), there is also the prospect of probability, the transaction is an opportunity, the stop loss is unfortunate.
  4. How can the strategy be adapted to the coin? The parameters at this time are symmetrical, i.e. the cumulative sale order of up to 8 coins, the cumulative payment of down to 8 coins, slightly unbalanced, such as the up to 15 coins cumulative sale order, making it more difficult to sell the coin, there is a greater chance that the coin will come back at a lower price, so the coin will be minted, in turn, make money.

Code explained

The full code can be found on my policy share at www.fmz.com, where only the core logic functions are explained. It is very impressive that without any changes, the analogue disk of the botvs carrier works perfectly, which is a strategy that was supported more than 3 years ago and is still supported today. The first is to get the buy/sell price function GetPrice (), which needs to get order depth information, be aware that order depth information lengths vary between different platforms, and that even if all orders are traveled, there is still no required amount (which causes this situation in many later 0.01 grid listings), the call is GetPrice (Buy) is to get the buy price.

function GetPrice(Type) {
   //_C()是平台的容错函数
    var depth=_C(exchange.GetDepth);
    var amountBids=0;
    var amountAsks=0;
    //计算买价,获取累计深度达到预设的价格
    if(Type=="Buy"){
       for(var i=0;i<20;i++){
           amountBids+=depth.Bids[i].Amount;
           //参数floatamountbuy是预设的累计深度
           if (amountBids>floatamountbuy){
               //稍微加0.01,使得订单排在前面
              return depth.Bids[i].Price+0.01;}
        }
    }
    //同理计算卖价
    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;}
        }
    }
    //遍历了全部深度仍未满足需求,就返回一个价格,以免出现bug
    return depth.Asks[0].Price
}

The main function of each loop is onTick (), where the cycle time is set to 3.5s, with each cycle canceling the original single and re-hanging the list, the simpler the less bugs will occur.

function onTick() {
    var buyPrice = GetPrice("Buy");
    var sellPrice= GetPrice("Sell");
    //diffprice是预设差价,买卖价差如果小于预设差价,就会挂一个相对更深的价格
    if ((sellPrice - buyPrice) <= diffprice){
            buyPrice-=10;
            sellPrice+=10;}
    //把原有的单子全部撤销,实际上经常出现新的价格和已挂单价格相同的情况,此时不需要撤销
    CancelPendingOrders() 
    //获取账户信息,确定目前账户存在多少钱和多少币
    var account=_C(exchange.GetAccount);
    //可买的比特币量,_N()是平台的精度函数
    var amountBuy = _N((account.Balance / buyPrice-0.1),2); 
    //可卖的比特币量,注意到没有仓位的限制,有多少就买卖多少,因为我当时的钱很少
    var amountSell = _N((account.Stocks),2); 
    if (amountSell > 0.02) {
        exchange.Sell(sellPrice,amountSell);}
    if (amountBuy > 0.02) {
        exchange.Buy(buyPrice, amountBuy);}
    //休眠,进入下一轮循环
    Sleep(sleeptime);
}

The tail

The whole process was also more than 40 lines, it seemed very simple, but at the time it took me more than a week, this was still the case on the botvs platform. The biggest advantage was to start early, in 2014, the market was dominated by moving, the grid and the high frequency of the grab plate was not much, making the strategy like fish get water, then the competition inevitably became more and more fierce, my money was also more and more, the challenges faced a lot, every once in a while to make major changes to deal with, but overall it was going well. But there's plenty of room for a quantification strategy that doesn't require high frequency.


/*
就是我刚开始编写机器人的源代码,几乎没有改动,参数也是原来的参数。这个版本的程序有许多
需要改进的地方,但即使如此,它也当时表现除了惊人的盈利能力,在我本金不多时,不加杠杆平
均每天盈利在5%左右。当然无论从哪一方面,它都不适应今天的市场。
我同时也发了一篇文章在社区,大家可以看看。
by 小草
*/

//稍微改了一下,用了平台的容错函数_C(),和精度函数_N().
//取消全部订单
function CancelPendingOrders() {
    var orders = _C(exchange.GetOrders);
    for (var j = 0; j < orders.length; j++) {
          exchange.CancelOrder(orders[j].Id, orders[j]);}
}

//计算将要下单的价格
function GetPrice(Type,depth) {
    var amountBids=0;
    var amountAsks=0;
    //计算买价,获取累计深度达到预设的价格
    if(Type=="Buy"){
       for(var i=0;i<20;i++){
           amountBids+=depth.Bids[i].Amount;
           //floatamountbuy就是预设的累计买单深度
           if (amountBids>floatamountbuy){
               //稍微加0.01,使得订单排在前面
              return depth.Bids[i].Price+0.01;}
        }
    }
    //同理计算卖价
    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;}
        }
    }
    //遍历了全部深度仍未满足需求,就返回一个价格,以免出现bug
    return depth.Asks[0].Price
}
 
function onTick() {
    var depth=_C(exchange.GetDepth);
    var buyPrice = GetPrice("Buy",depth);
    var sellPrice= GetPrice("Sell",depth);
    //买卖价差如果小于预设值diffprice,就会挂一个相对更深的价格
    if ((sellPrice - buyPrice) <= diffprice){
            buyPrice-=10;
            sellPrice+=10;}
    //把原有的单子全部撤销,实际上经常出现新的价格和已挂单价格相同的情况,此时不需要撤销
    CancelPendingOrders() 
    //获取账户信息,确定目前账户存在多少钱和多少币
    var account=_C(exchange.GetAccount);
    //可买的比特币量
    var amountBuy = _N((account.Balance / buyPrice-0.1),2); 
    //可卖的比特币量,注意到没有仓位的限制,有多少就买卖多少,因为我当时的钱很少
    var amountSell = _N((account.Stocks),2); 
    if (amountSell > 0.02) {
        exchange.Sell(sellPrice,amountSell);}
    if (amountBuy > 0.02) {
        exchange.Buy(buyPrice, amountBuy);}
    //休眠,进入下一轮循环
    Sleep(sleeptime);
}
    
function main() {
    while (true) {
        onTick();
    }
}

Related

More

guohwaNow, some exchanges seem to have some of the coins free of transaction fees.

a624587332Bitcoin's BTC spot transaction is free of transaction fees and can be done again?

abcdsfasdfhzwI'm looking at the program and I'm looking at the buy code and I'm looking at the buy code and I'm looking at the buy code and I'm looking at the buy code and I'm looking at the buy code and I'm looking at the buy code and I'm looking at the buy code and I'm looking at the buy code and I'm looking at the buy code.

tiemuerAsk why it's not working now? What other useful strategies are there now?

bijiasuoMake a mark, and expect an improved version.

qinergThank you so much for sharing, it was very helpful. I look forward to more of this profound exchange of ideas. Thank you, thank you!

LogaLet's get it on.