Loading ...

大小周期跃迁合约_2019年化10倍_区班量化_开源策略

Author: 区班量化, Date: 2020-06-18 17:58:25
Tags: Bollinger

  • 华尔街有句名言:“一个好的操盘手是一个没有观点的操盘手。”这句话的意思是说:一个真正成功的投资者在投资过程中不事先假定股市应该朝哪个方向走,也就是不做预测,而是让股市告诉他股市会走到何处,他只是对股市的走势作出反应而已,他不必设法证明自己的观点是正确的。我们的结论是,你不必去解释股市过去的为什么,也不必要预测股市将来会是什么,但是你必须知道你现在该干什么!– 周期跃迁的策略就是体现以上思路的最好框架。

  • 具体说来,我们把每个周期的K线与布林线的关系罗列出来,一共有8种状态,三个周期就有888=512种状态,这512种状态足以应付所有可能出现的盘面。以上做法时候于8进制,如果可以的话,我们再把状态细分成101010=1000种状态。我们把大周期状态放在百位,中周期状态放在十位,小周期放到个位,这样就形成了0-999有明确编号的状态顺序,我们把0状态归为最强态,999状态归为最弱态。这样做的好处是,状态跃迁和价格对应起来了。如果状态数字跳动越大,说明遇到了巨大的上涨或者下跌。

  • 接下来,我们可以统计一下,各个状态在一定周期后,多空的指数比和数学期望,由此就排序出来了,最佳的进场点位、出场点位和出场周期、胜率。这个框架搭建好后,还可以结合各种数据因子,也可以观察各种数据因子对状态的刺激作用,也就是说,大数据和人工智能算法的功能就可以初步具备了。

  • 以下是回测效果 img img

  • 注意:1、该策略针对2019年进行了优化,已经过拟合;如果实盘,还需要进行参数优化;

  •  2、以往的成绩和回测并不代表未来表现,如果实盘,请在有经验的朋友指导下完成
    
  •  3、有缘的朋友可以在此基础上进一步修改,如果有所收益,请来信告诉区班。
    
  •  4、切不可用于割韭菜和当付费策略销售,否则区班保留追诉的权利。
    
  •  5、支持FMZ,是做量化最好的平台
    
  •  6、欢迎围观实盘 https://www.fmz.com/strategy/209340     
    

币乎文章链接

联系方式:区班量化 email:tomjava@163.com 策略咨询/定制,请加QQ:279689894 申请加好友请填写fmz 合约帝实盘 img


/*backtest
start: 2019-01-01 00:00:00
end: 2019-10-10 00:00:00
period: 1d
basePeriod: 1h
exchanges: [{"eid":"Futures_OKCoin","currency":"ETH_USD","stocks":2}]
*/
//注册币乎后https://m.bihu.com/signup?i=1ewtKO&s=4&c=4
//搜索 物联网区块链 可以联系到作者区班主
//区班量化 -- 定制化策略服务、数字货币api代托管服务、FMZ策略销售 -- 邮箱tomjava@163.com
//注意:1、该策略针对2019年进行了优化,已经过拟合;如果实盘,还需要进行参数优化;
//     2、以往的成绩和回测并不代表未来表现,如果实盘,请在有经验的朋友指导下完成
//     3、有缘的朋友可以在此基础上进一步修改,如果有所收益,请来信告诉区班。
//     4、切不可用于割韭菜和当付费策略销售,否则区班保留追诉的权利。
//     5、支持FMZ,是做量化最好的平台
var operPrice;
var markTime=0;

var bigRate=0; //大周期状态
var smallRate=0;//小周期状态
var proArray=[[2.99657,-7.37215],[3.64668,-6.72027],[4.60098,-6.62712],[4.43731,-4.61083],[4.0472,-3.99329],[4.13526,-2.87032],[4.14059,-2.9638],[2.84087,-2.96401],[5.83542,-1.60364],[6.25204,-0.223156],[7.41218,-0.137121],[6.96753,-0.353675],[2.52459,-3.38017],[2.16413,-6.57087],[2.59576,-4.9082],[2.02171,-4.56818],[1.31829,-4.87325],[1.22335,-4.60537],[1.1446,-3.32129],[1.66059,-5.43336],[1.50949,-8.83255],[3.00573,-2.33071],[5.12309,-1.11303],[6.05146,-1.83565],[3.84156,-2.52345],[3.79669,-0.353105],[5.11526,-3.48674],[3.10505,-3.25298],[2.20919,-2.11508],[1.77129,-2.51125],[2.84276,-1.3671],[2.82776,-1.47372],[2.53952,-2.82061],[2.68369,-3.95974],[2.55339,-3.83088],[2.80135,-4.49936],[2.93106,-2.60239],[2.76122,-2.78447],[3.05025,-2.26087],[2.85011,-2.42152],[3.37308,-2.57224],[3.51145,-2.80128],[3.57301,-2.83873],[3.16392,-2.47698],[2.9446,-1.85426],[3.17552,-1.67138],[4.16394,-1.57979],[5.58505,-1.3254],[2.51827,-2.06503],[2.16579,-2.36479],[1.9847,-2.65326],[2.11293,-2.68681],[2.04331,-2.60146],[1.98932,-2.95501],[2.05944,-3.04417],[1.95637,-2.74058],[2.13725,-2.54146],[2.48996,-2.54522],[2.88275,-2.249],[2.82241,-2.30341],[2.50266,-3.62789],[2.25634,-5.14906],[2.47522,-3.67291],[2.64692,-1.96422],[2.15417,-1.89567],[2.01916,-2.05823],[2.09824,-2.22042],[2.39092,-2.10961],[2.5381,-1.89547],[3.41646,-1.78455],[2.67657,-1.46138],[2.3007,-1.55649],[1.79344,-5.06731],[1.78934,-3.52562],[2.14424,-6.6736],[2.53494,-3.35928],[2.48096,-1.76187],[1.66729,-1.43395],[1.87608,-1.80415],[2.28437,-1.87001],[2.9459,-1.64984],[2.01116,-1.93997],[3.84472,-1.0525],[3.03797,-1.18691],[2.75211,-5.10342],[2.29134,-3.74946],[2.40516,-3.1264],[2.56881,-3.34411],[2.58037,-2.44943],[2.36739,-2.56054],[2.26939,-2.67091],[2.39893,-2.43438],[2.72282,-2.63999],[2.90529,-2.63259],[2.78186,-2.5928],[2.56782,-2.73012],[4.10042,-3.1606],[4.59101,-3.53242],[3.65983,-3.03906],[3.82614,-2.81485],[3.87051,-2.26924],[3.74936,-2.35774],[4.09307,-2.53924],[4.10991,-2.8146],[3.87239,-2.83617],[3.56228,-3.3444],[3.17848,-2.30792],[3.28287,-3.32179],[6.20355,-1.07094],[3.30721,-2.16401],[6.58902,-1.11087],[3.18174,-2.708],[4.29393,-1.67499],[2.34116,-2.23752],[3.32551,-1.89667],[2.13403,-2.18003],[2.86717,-4.23512],[5.44095,-3.78374],[5.44268,-4.56077],[4.74622,-3.75955],[7.48381,-1.69228],[3.69667,-2.87117],[3.77418,-3.15169],[3.63942,-2.90889],[4.99195,-3.90082],[4.17369,-3.2744],[5.79762,-3.23169],[3.96437,-3.71588],[3.17114,-6.08863],[4.18639,-2.66454],[4.23371,-2.81303],[5.72963,-2.01454],[6.90345,-2.57512],[6.83582,-2.63675],[4.40922,-4.57948],[2.75029,-3.02772],[4.25895,-4.11628],[5.62286,-4.77658],[4.84151,-3.97468],[4.67309,-4.78398],[6.97272,-3.77173],[8.26254,-4.1681],[7.94011,-3.96833],[6.77912,-3.27668]];

//是否已经平仓
var enter_short=0;
var enter_long=0;

//0小幅震荡 1大幅震荡 2小幅上涨 3大幅上涨 -1小幅下跌 -2大幅下跌
var status=0;
var beforeLevel=65;
var beforeStatus=0;

//多空交战锁,可以锁12小时,在12小时内在交易价格2%幅度范围内高抛低吸
var fightLock=-1;
//之前的买卖价格
var fightPrice=-1;
//胶着标志
var fightFlag=0;

//买入满格,满仓后锁住,只有卖出才解锁
var buyLock=0;
function myBuy(rate){  
   if(buyLock==1){
       return;
   }
    
   enter_long=0; //只平一次仓
   var orders = _C(exchange.GetOrders);
   var account = _C(exchange.GetAccount);
   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){ //买平空单,注意成交方向相反
           if(enter_short==0&&fightLock<0){ //多空交战时不平仓
              Log("买平空单");
              enter_short=1;
              exchange.SetDirection("closesell"); 
              exchange.Buy(nowPrice,position[i].Amount);
           }
       }else{
           pAmount=position[i].Amount; //当前持有的多仓
       }
   }
   
   //以下开始买入,cashRatio不能低于20%
   var allAmount=pAmount*10/5+account.Stocks*nowPrice; //计算出总金额
   var allStock=allAmount/nowPrice;  //估算一个总货币数
   var cashRatio=account.Stocks*nowPrice*100/allAmount;
   var objid=0;
   var order;
   exchange.SetDirection("buy"); 
 
      if(rate==2&&cashRatio-4>=78){ //买入1份
           objid=exchange.Buy(nowPrice,Math.floor(nowPrice*allStock*0.2/10));
           if(objid){
              order = exchange.GetOrder(objid);
              Log("cashRatio",cashRatio-4,"建仓买入",allStock*0.2," 现币",account.Stocks," Price:", order.Price, " Amount:", order.Amount);
              isClean=0;
              upwardPrice=nowPrice;
              upwardTime=ticker.Time;
              upwardRate=bigRate;
           }else{
              Log("未成建仓买入",allStock*0.2," 现币",account.Stocks);
           }
      }else if(cashRatio-2>=78){
           objid=exchange.Buy(nowPrice,Math.floor(nowPrice*allStock*0.1/10));
           if(objid){
              order = exchange.GetOrder(objid);
              Log("cashRatio",cashRatio-2,"建仓买入",allStock*0.1," 现币",account.Stocks," Price:", order.Price, " Amount:", order.Amount);
              isClean=0;
              upwardPrice=nowPrice;
              upwardTime=ticker.Time;
              upwardRate=bigRate;
           }else{
              Log("未成功建仓买入",allStock*0.1," 现币",account.Stocks);
           }
      }else{
           buyLock=1;
      }
}

function mySell(rate,isAdd){
   enter_short=0;
   var orders = _C(exchange.GetOrders);
   var account = _C(exchange.GetAccount);
   var ticker = _C(exchange.GetTicker);
   var nowPrice=ticker.Sell;
   
   //买平所有多单
   var position = _C(exchange.GetPosition);
   var pAmount=0;
   
   buyLock=0;
   for (i = 0; i < position.length; i++) {
       if(position[i].Type==PD_LONG){ //卖平多单
           if(enter_long==0){
              exchange.SetDirection("closebuy"); 
              if(isAdd==0){
                 if(fightLock<0){ //多空交战时不平仓
                    Log("卖平多单");
                    enter_long=1;
                    exchange.Sell(nowPrice,position[i].Amount);
                 }
              }else{
                 Log("清空多单但不做空");
                 enter_long=1;
                 exchange.Sell(nowPrice,position[i].Amount);
                 return;
              }
           }
       }else{
           pAmount=position[i].Amount;
       }
   }
   
   //以下开始买入,cashRatio不能低于20%
   var allAmount=pAmount*10/5+account.Stocks*nowPrice; //计算出总金额
   var allStock=allAmount/nowPrice;  //估算一个总货币数
   var cashRatio=account.Stocks*nowPrice*100/allAmount;
   var objid=0;
   exchange.SetDirection("sell"); 
   
      if(rate==2&&cashRatio-4>=78){
           objid=exchange.Sell(nowPrice,Math.floor(nowPrice*allStock*0.2/10));
           if(objid){
              Log("建仓卖出",allStock*0.2," 现金比例",cashRatio-4," 开单价格",nowPrice);
           }else{
              Log("失败建仓卖出",allStock*0.2," 现金比例",cashRatio-4," 开单价格",nowPrice);
           }
      }else if(cashRatio-2>=78){ //卖出1份
           objid=exchange.Sell(nowPrice,Math.floor(nowPrice*allStock*0.1/10));
           if(objid){
              Log("建仓卖出",allStock*0.1," 现金比例",cashRatio-2," 开单价格",nowPrice);
           }else{
              Log("失败建仓卖出",allStock*0.1," 现金比例",cashRatio-2," 开单价格",nowPrice);
           }
      }
}

function getLevel(f){
	var level=5;
	if(f>=110){
		level=11;
	}else if(f>100){
		level=10;
	}else if(f>90){
        level=9;   
	}else if(f>50){
        level=8;   
	}else if(f>10){
		level=7;
	}else if(f>0){
        level=6;   
	}else if(f>-10){
        level=5;   
	}else if(f>-50){
        level=4;   
	}else if(f>-90){
		level=3;
	}else if(f>-100){
        level=2;   
	}else if(f>-110){
		level=1;
	}else if(f<=-110){
        level=0;
	}

	return level;
}

function oper(){
    var ticker = _C(exchange.GetTicker);
    var nowPrice=ticker.Sell;
   
    var h1records = exchange.GetRecords(PERIOD_H1);
    var h1boll;var h1upLine;var h1midLine;var h1downLine;
    var h1bw;
    if(h1records && h1records.length > 20) {
        h1boll = TA.BOLL(h1records, 20, 2);
        h1upLine = h1boll[0][h1records.length-1];
        h1midLine = h1boll[1][h1records.length-1];
        h1downLine = h1boll[2][h1records.length-1];
        h1bw=(h1upLine-h1midLine);
    }
    
    var drecords = exchange.GetRecords(PERIOD_D1);
    var dboll;var dupLine;var dmidLine;var ddownLine;
    var dbw;
    if(drecords && drecords.length > 20) {
        dboll = TA.BOLL(drecords, 20, 2);
        dupLine = dboll[0][drecords.length-1];
        dmidLine = dboll[1][drecords.length-1];
        ddownLine = dboll[2][drecords.length-1];
        dbw=(dupLine-dmidLine);
    }
    
    if(ticker.Time-markTime<15*60*1000){ //只有满足15分钟间隔,才允许判断状态
        return;
    }else{
        markTime=ticker.Time;
    }
    
    var level=0;
    var pro=0;
    if(h1records && h1records.length > 20 && drecords && drecords.length > 20) {
        bigRate=(nowPrice-dmidLine)*100/dbw;
        smallRate=(nowPrice-h1midLine)*100/h1bw;
        level=getLevel(bigRate)*12+getLevel(smallRate);
        //Log("b",bigRate统计法_大小周期跃迁合约V1.06_遇到回撤3%就清空_0101_1214_年化296_回撤35%_交易3892," s",smallRate," level",level);
        pro=proArray[level][0]+proArray[level][1];
       
        if(fightLock<0){
          if(pro>2){
             status=3;
             if(level!=beforeLevel&&beforeStatus<0){ //如果是从空方来
                Log("b",bigRate," s",smallRate," l",level," bl",getLevel(bigRate)," sl",getLevel(smallRate)," price",nowPrice," 多空大战 空转大多",beforeLevel);
                fightLock=ticker.Time;
                fightPrice=nowPrice;
                fightFlag=0;
             }else{
                Log("b",bigRate," s",smallRate," l",level," bl",getLevel(bigRate)," sl",getLevel(smallRate)," price",nowPrice," 高位恐慌买入");
                myBuy(2,0);
             }
          }else if(pro>1){
              status=2;
              if(level!=beforeLevel&&beforeStatus<0){ //如果是从空方来
                Log("b",bigRate," s",smallRate," l",level," bl",getLevel(bigRate)," sl",getLevel(smallRate)," price",nowPrice," 多空大战 空转小多 beforeL",beforeLevel);
                fightLock=ticker.Time;
                fightPrice=nowPrice;
                fightFlag=0;
              }else{
                Log("b",bigRate," s",smallRate," l",level," bl",getLevel(bigRate)," sl",getLevel(smallRate)," price",nowPrice,"买一份");
                myBuy(1,0);
              }  
         }else if(pro<-1){
              status=-1;
              if(level!=beforeLevel&&beforeStatus>0){ //如果是从多方来
                Log("b",bigRate," s",smallRate," l",level," bl",getLevel(bigRate)," sl",getLevel(smallRate)," price",nowPrice," 多空大战 多转小空 beforeL",beforeLevel);
                fightLock=ticker.Time;
                fightPrice=nowPrice;
                fightFlag=0;
              }else{
                Log("b",bigRate," s",smallRate," l",level," bl",getLevel(bigRate)," sl",getLevel(smallRate)," price",nowPrice,"卖一份");
                mySell(1,0);
              }
          }else if(pro<-2){
              status=-2;
              if(level!=beforeLevel&&beforeStatus>0){ //如果是从多方来
                Log("b",bigRate," s",smallRate," l",level," bl",getLevel(bigRate)," sl",getLevel(smallRate)," price",nowPrice," 多空大战 多转大空 beforeL",beforeLevel);
                fightLock=ticker.Time;
                fightPrice=nowPrice;
                fightFlag=0;
              }else{
                Log("b",bigRate," s",smallRate," l",level," bl",getLevel(bigRate)," sl",getLevel(smallRate)," price",nowPrice,"低位恐慌抛售");
                mySell(2,0);
              }
           }else{
              var avgpro=(proArray[level][0]-proArray[level][1])/2.0;
              if(drecords[drecords.length-1].High>nowPrice*1.03&&getLevel(bigRate)==3){ //如果当日回撤太多,是在下跌途中,清空多仓
                  mySell(0,1);
              }else if(getLevel(bigRate)==7){
                      fightFlag=1; //多胶着
                      Log("b",bigRate," s",smallRate," l",level," bl",getLevel(bigRate)," sl",getLevel(smallRate)," price",nowPrice," 多胶着 beforeL",beforeLevel);
                      fightLock=ticker.Time;
                      fightPrice=nowPrice;
              }else if(getLevel(bigRate)==4){ //空胶着
                      fightFlag=-1;
                      Log("b",bigRate," s",smallRate," l",level," bl",getLevel(bigRate)," sl",getLevel(smallRate)," price",nowPrice," 空胶着 beforeL",beforeLevel);
                      fightLock=ticker.Time;
                      fightPrice=nowPrice;
              }
              status=0;
           }
        }else{ //进入高抛低吸状态,每2%为一个网格
            if(nowPrice>fightPrice*1.02){
                Log("网格交易抛一份 now",nowPrice," fight",fightPrice);
                mySell(2,0);
                fightPrice=nowPrice;
            }else if(nowPrice<fightPrice*0.98){
                Log("网格交易买一份 now",nowPrice," fight",fightPrice);
                myBuy(2,0);
                fightPrice=nowPrice;
            }
        }
        
        if(getLevel(bigRate)!=7&&getLevel(bigRate)!=8&&fightFlag==1){ //如果跌落到中部以下,多空交战立即解锁
                Log("布林中线上方离开震荡区域 立即解锁网格","b",getLevel(bigRate)," s",getLevel(smallRate));
                fightLock=-1;
                fightFlag=0;
        }else if(getLevel(bigRate)!=4&&getLevel(bigRate)!=5&&fightFlag==-1){ //如果上涨到中部以上,多空交战立即解锁
                Log("布林中线下方离开震荡区域 立即解锁网格","b",getLevel(bigRate)," s",getLevel(smallRate));
                fightLock=-1;
                fightFlag=0;
        }
            
        if(fightLock>0&&ticker.Time-fightLock>12*60*60*1000){ //12小时后,自动解锁
            Log("多空大战 网格解锁");
            fightLock=-1;
        }
        
        beforeLevel=level;
        beforeStatus=status;
    }
}

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

yuyuyu_______ 6666666

小正 有微信可以加嗎??

bjnbjnbjn 直接使用这个策略可以吗? 在哪里设置杠杆倍数?

13737166766 例如:var proArray=[........],代表什么?

13737166766 如果多点策略注解就好了。有部分看不懂

小草 手动点赞

区班量化 免费版只是给大家研究,参数还要优化才能应对不同情况,请参考实盘

区班量化 不是所有币都可以,需要优化

区班量化 可以看我的实盘里有联系方式

bjnbjnbjn 我直接复制策略用机器人跑 是不能的吗 是还需要修改参数吗? 之前都是用别人的策略直接跑 没有修改过这些参数 有点不太懂 哈哈

13737166766 能具体点吗?指明一个方向也好!任何币种都能用这组参数吗?

区班量化 是统计出来的量化参数

区班量化 请仔细阅读文章最后注意的第2点