Template 3.2: Digital currency trading class library (integrated Cash, futures support OKCoin futures/BitVC)

Author: The Little Dream, Created: 2017-01-04 19:00:10, Updated: 2017-10-11 10:27:01

Template 3.2: Digital currency trading class library (integrated Cash, futures support OKCoin futures/BitVC)


Chapter 3.1 shows a spot trading template, which greatly simplifies the difficulty of writing a spot strategy. However, the processing of futures trading is very different from spot, so the integration of futures trading functions on the basis of the spot template is now open.

img

This is the first time that a video has been released on Strategy Square:

img

  • In stock:

    The cash aspect is the same as the previous liquidity-dependent cryptocurrency.

  • The future:

    Parameters are:img

    img

    The strategy comes with a test code:

    function main() {
      if (exchange.GetName() === 'Futures_OKCoin') {
          var info = exchange.SetContractType("this_week");
          Log("info 返回值:", info);
          Log("当前持仓信息", exchange.GetPosition(), _C(exchange.GetTicker));
          var depth = exchange.GetDepth();
          var p = $.NewPositionManager();
          p.OpenShort("this_week", 10, depth.Bids[0].Price - 2);
          Log(exchange.GetPosition());
          Sleep(500 * 1000);
          depth = exchange.GetDepth();
          var ret = p.Cover("this_week", depth.Bids[0].Price + 2, 5);
          Log("cover ret:", ret);
          //LogProfit(p.Profit());
          Log(exchange.GetPosition());
          Log("-----------------------------测试分割线----------------------------------------");
          var depth = exchange.GetDepth();
          p.OpenLong("this_week", 20, depth.Bids[0].Price + 2);
          Log(exchange.GetPosition());
          Sleep(500 * 1000);
          depth = exchange.GetDepth();
          var ret = p.Cover("this_week", depth.Bids[0].Price - 2, 10, PD_LONG);
          Log("cover ret:", ret);
          Log(exchange.GetPosition());
          Log("-----------------------------测试分割线----------------------------------------");
          var ret = p.Cover("this_week", depth.Bids[0].Price - 3, 10, PD_LONG);
          Log("cover ret:", ret);
          var ret = p.Cover("this_week", depth.Bids[0].Price + 3, 5, PD_SHORT);
          Log("cover ret:", ret);
          Log(exchange.GetPosition());
      } else if (exchange.GetName() === 'Futures_BitVC') {
          var info = exchange.SetContractType("week");
          Log("info 返回值:", info);
          Log("当前持仓信息", exchange.GetPosition(), _C(exchange.GetTicker));
          var depth = exchange.GetDepth();
          var p = $.NewPositionManager();
          p.OpenLong("week", 500, depth.Bids[0].Price + 2);
          Log(exchange.GetPosition());
          Sleep(500 * 1000);
          depth = exchange.GetDepth();
          var ret = p.Cover("week", depth.Bids[0].Price - 2, 500);
          Log("cover ret:", ret);
          Log(exchange.GetPosition());
          Log("-----------------------------测试分割线----------------------------------------");
          var info = exchange.SetContractType("week");
          Log("info 返回值:", info);
          Log("当前持仓信息", exchange.GetPosition(), _C(exchange.GetTicker));
          var depth = exchange.GetDepth();
          p.OpenShort("week", 600, depth.Bids[0].Price - 2);
          Log(exchange.GetPosition());
          Sleep(500 * 1000);
          depth = exchange.GetDepth();
          var ret = p.Cover("week", depth.Bids[0].Price - 2, 500, PD_SHORT);
          Log("cover ret:", ret);
          Log(exchange.GetPosition());
          Log("-----------------------------测试分割线----------------------------------------");
          var ret = p.Cover("week", depth.Bids[0].Price + 3, 100, PD_SHORT);
          Log("cover ret:", ret);
          //p.Cover("week", depth.Asks[0].Price - 3, 300, PD_LONG);
          Log(exchange.GetPosition());
      } else if(exchange.GetName() === 'huobi' || exchange.GetName() === 'OKCoin'){
          Log($.GetAccount());
          Log($.Buy(0.5));
          Log($.Sell(0.5));
          exchange.Buy(1000, 3);
          $.CancelPendingOrders(exchanges[0]);
          Log($.Cross(30, 7));
          Log($.Cross([1,2,3,2.8,3.5], [3,1.9,2,5,0.6]));
      }
    }
    

    Used for:img

Test code in the policy (select reference template)

img

The test strategy:

function main(){
    var p = $.NewPositionManager();
    var i = 0;
    exchanges[0].SetContractType("this_week");
    var isFirst = true;
    var ret = null;
    while(true){
        var depth = _C(exchanges[0].GetDepth);
        var positions = _C(exchanges[0].GetPosition);
        var len = positions.length;
        if(isFirst === true && i % 3 === 0 && len === 0){
            ret = p.OpenLong("this_week", 1 + (i % 3) + (i % 2), depth.Asks[0].Price);
            isFirst = false;
        }else if(isFirst === false){
            ret = p.OpenShort("this_week", 1 + (i % 3) + (i % 2), depth.Bids[0].Price);
            isFirst = true;
        }else{
            for(var j = 0 ; j < len; j++){
                if(positions[j].Type === PD_LONG){
                    ret = p.Cover("this_week", depth.Bids[0].Price - 2, positions[j].Amount, PD_LONG);
                }else if(positions[j].Type === PD_SHORT){
                    ret = p.Cover("this_week", depth.Asks[0].Price + 2, positions[j].Amount, PD_SHORT);    
                }
                Log("ret:", ret);
            }
        }
        Log("ret", ret, "---------------------#FF0000");
        i++;
        Sleep(1000 * 60 * 15);
    }
}

Please contact the author if you have any questions, bugs, and thank you very much!


More

yundiWhy can't I find the JS's cryptocurrency trading library, the policy doesn't show up in it

I'm not a bad uncle.What is the price of $.Buy ((0.5)?

outlawjkDoes the python version also support OKEX futures?

simple-chunWhat does ret stand for? What is the abbreviation of the word in English? RECOVER T?

simple-chunWhere is the PY version?

yhfggI'm used to Python, is there a Python version?

The Little DreamThe trading directory of the digital currency futures is unofficial, so it is cancelled publicly because the exchange's holding interface is often delayed, which can lead to repeated orders.

yundiHi, this is a spot and commodity futures, I asked in the group, the JS digital currency futures library is gone because of a bug.

The Little Dream/upload/asset/16c4dcc69723e302152c.png This is a list of all the different ways Upload/asset/16c4dcc69723e302152c.png is credited in the database. In this position

The Little DreamThe price of the rivals at the time, plus a little slippage.

The Little DreamThis is currently not written, but there is a Python template for commodity futures, my JS is actually written with the structure of commodity futures, so I can refer to it.

outlawjkI'm not going to use jS much, referring to the Python template for the OKEX futures?

The Little DreamPython has no futures feature. No transplants

The Little Dreamreturn means return value, generally used to temporarily store a function that returns value.

simple-chunThank you. Thank you.

The Little Dreamhttps://www.botvs.com/strategy/21104, which may be slightly different from the version of JS, is a port of JS.

The Little DreamThere is a version of python, but it is less used, so it is better to test it.