V1.0_OKex contract for the bulls and bears

Author: Quantification of district classes, Date: 2019-10-23 09:12:08
Tags: OKEX

In the previous article, we discussed that the small sales strategy can achieve an annualized gain of 130% in certain cases. However, this is in certain cases, the monthly average line of the commodity, which is up in the long run, and after a long time. If in the short three months of entering the market, it is in a falling market, it can be hit significantly. The main reason for this is that the strategy does not introduce a do-it-yourself mechanism. As in the A-share market, you can only do more, and when it goes down, you can only do it empty or passively do it. Today's strategy is the bull bear small sales department strategy. The main idea is that the bull market comes, do more, while raising the expectation of fair prices; the bear market comes, do nothing, while lowering the expectation of fair prices. The bull market comes, high and low. This strategy applies to ETH, where two pits need to be pointed out: 1, this strategy is based on ETH, ETH in the OKex futures, is a 10 piece; the friend who needs BTC, please modify the division yourself; 2, after the contract is placed, if the same multiple orders, merge into one position, so the position length is up to 2, which is also where the zone manager caused the repeated questioning at the time, and it is better to finally solve it. After registering, you will be able to sign up for the program. Search IoT blockchain to connect to the author's block


/*backtest
start: 2019-01-01 00:00:00
end: 2019-10-10 00:00:00
period: 1d
exchanges: [{"eid":"OKEX","currency":"ETH_USDT","stocks":0}]
args: [["OpMode",1,10989],["MaxAmount",1,10989],["TradeFee",0.001,10989]]
*/
//注册币乎后https://m.bihu.com/signup?i=1ewtKO&s=4&c=4
//搜索 物联网区块链 可以联系到作者区班主
var status = 10; //10表示猴市初始化,11表示猴市继续;20表示牛市初始化,21表示牛市继续;30表示熊市初始化,31表示熊市继续
var dhigh;
var dlow;
var mlow;
var mhigh;
    
var operPrice;
function monkeyOper(){
   var i;
   var position;
   var account = _C(exchange.GetAccount);
   var ticker = _C(exchange.GetTicker);
   var nowPrice=ticker.Sell;
   var pAmount;
    
  /* if(status==10){ //进入猴市初始化,设定公允价格
       operPrice=mlow+mhigh;
   }else{ 
       if(nowPrice<operPrice*0.97){ //值得买
            //买平所有空单
            position = _C(exchange.GetPosition);
            for (i = 0; i < position.length; i++) {
               if(position[i].Type==PD_SHORT){ //买平空单
                 exchange.SetDirection("closesell"); 
                 exchange.Buy(nowPrice,position[i].Amount);
               }else{
                 pAmount=position[i].Amount;
               }
           }
          
           if(pAmount*10<account.Stocks*nowPrice){ //最多持半仓
              exchange.Buy(nowPrice,Math.floor(nowPrice*account.Stocks*0.1/10)); //尝试做多
              Log("猴市买入",account.Stocks*0.1);
              operPrice=nowPrice;
           } 
       }else if(nowPrice>operPrice*1.03){ //值得卖
            //卖平所有多单
            position = _C(exchange.GetPosition);
            for (i = 0; i < position.length; i++) {
               if(position[i].Type==PD_LONG){ //卖平多单
                 exchange.SetDirection("closebuy"); 
                 exchange.Sell(nowPrice,position[i].Amount);
               }else{
                 pAmount=position[i].Amount;
               }
           }
          
           if(pAmount*10<account.Stocks*nowPrice){ //最多持半仓
              exchange.Sell(nowPrice,Math.floor(nowPrice*account.Stocks*0.1/10)); //尝试做多
              Log("猴市卖出",account.Stocks*0.1);
              operPrice=nowPrice;
           } 
       }    
   }*/
}

function bullOper(){
   //去掉所有挂的空仓
   var orders = _C(exchange.GetOrders);
   var account = _C(exchange.GetAccount);
  
   for (var i = 0; i < orders.length; i++) {
       var order=orders[i];
       if(order.type==1){  //空单
           exchange.CancelOrder(order.Id);
           Log("清空单");
       }
   }
   
   var ticker = _C(exchange.GetTicker);
   var nowPrice=ticker.Sell;
   //买平所有空单
   var position = _C(exchange.GetPosition);
   var pAmount=0;
   for (i = 0; i < position.length; i++) {
       if(position[i].Type==PD_SHORT){ //买平空单,注意成交方向相反
           exchange.SetDirection("closesell"); 
           exchange.Buy(nowPrice,position[i].Amount);
       }else{
           pAmount=position[i].Amount;
       }
   }
   
   if(status==20){  //当前价格做多
       exchange.SetDirection("buy"); 
       if(pAmount*10<account.Stocks*nowPrice){ //最多持半仓
          exchange.Buy(nowPrice,Math.floor(nowPrice*account.Stocks*0.2/10)); //尝试做多
          Log("初始买入",account.Stocks*0.1);
       } 
       operPrice=nowPrice;
   }else if(nowPrice<operPrice*0.97){  //最多挂两单,大力做多
       exchange.SetDirection("buy");
       if(pAmount*10<account.Stocks*nowPrice){ //最多持半仓
          exchange.Buy(nowPrice,Math.floor(nowPrice*account.Stocks*0.3/10));
          Log("加大买入",account.Stocks*0.3);
       }
       operPrice=nowPrice;
   }
}

function bearOper(){
   //去掉所有挂的多仓
   var orders = _C(exchange.GetOrders);
   var account = _C(exchange.GetAccount);
  
   for (var i = 0; i < orders.length; i++) {
       var order=orders[i];
       if(order.type==0){  //多单
           exchange.CancelOrder(order.Id);
           Log("清多单");
       }
   }
   
   var ticker = _C(exchange.GetTicker);
   var nowPrice=ticker.Sell;
   //买平所有多单
   var position = _C(exchange.GetPosition);
   var pAmount=0;
   for (i = 0; i < position.length; i++) {
       if(position[i].Type==PD_LONG){ //卖平多单
           exchange.SetDirection("closebuy"); 
           exchange.Sell(nowPrice,position[i].Amount);
       }else{
           pAmount=position[i].Amount;
       }
   }
   
   if(status==30){  //当前价格做空
       exchange.SetDirection("sell"); 
       if(pAmount*10<account.Stocks*nowPrice){ //最多持半仓
          exchange.Sell(nowPrice,Math.floor(nowPrice*account.Stocks*0.2/10)); //尝试做空
          Log("初始卖出",account.Stocks*0.1);
       } 
       operPrice=nowPrice;
   }else if(nowPrice>operPrice*1.03){  //最多挂两单,大力做多
       exchange.SetDirection("sell");
       if(pAmount*10<account.Stocks*nowPrice){ //最多持半仓
          exchange.Sell(nowPrice,Math.floor(nowPrice*account.Stocks*0.3/10));
          Log("加大卖出",account.Stocks*0.3);
       }
       operPrice=nowPrice;
   }
}

function oper(){
    var ticker = _C(exchange.GetTicker);
    var nowPrice=ticker.Sell;
    
    var drecords = exchange.GetRecords(PERIOD_D1);
    var mrecords = exchange.GetRecords(PERIOD_M30);
    //日线5天内的高低点(不包含当前Bar)
    dhigh=TA.Highest(drecords, dnum, 'High');
    dlow=TA.Lowest(drecords, dnum, 'Low');
       
    //30分钟线10个周期内的高低点(不包含当前Bar)
    mhigh=TA.Highest(mrecords, mnum, 'High');
    mlow=TA.Lowest(mrecords, mnum, 'Low');
    
    if(mhigh>dhigh&&mlow<dlow){ //如果分钟高低点都突破了日线高低点,看重心在哪,决定是什么市
        if((mhigh+mlow)<(dhigh+dlow)*0.97){
            if(status==30){
              status=31;
            }else{
              status=30; 
            }
            bearOper();
        }else if((mhigh+mlow)>(dhigh+dlow)*1.03){
            if(status==20){
              status=21;
            }else{
              status=20; 
            }
            bullOper();
        }else{
            if(status==10){
              status=11;
            }else{
              status=10;
            }
            monkeyOper();
        }
    }else if(mhigh>dhigh){ //分钟低点突破日高点,牛开始
        if(status==20){
           status=21;
        }else{
           status=20; 
        }
        bullOper();
    }else if(mlow<dlow){  //分钟低点跌破日低点,熊开始
        if(status==30){
           status=31;
        }else{
           status=30;
        }
        bearOper();
    }else{  //没有方向,猴市
        if(status==10){
           status=11;
        }else{
           status=10;
        }
        monkeyOper();
    }
}

function main() {
    var initAccount = _C(exchange.GetAccount);
    Log(initAccount);
    exchange.SetContractType("quarter")    // 举例设置为OKEX期货当周合约
    exchange.SetMarginLevel(5);              // 设置杠杆为5倍
    while (true) {
        oper();
        Sleep(Interval*1000);
    }
}

Related

More

Zayne@tomjava Is this fair price calculation sure to work? minimum price plus maximum price not divided by 2?