Loading ...

波动方差

Author: 量价时空, Date: 2020-09-19 18:25:12
Tags:


/*backtest
start: 2020-09-16 00:00:00
end: 2020-09-18 00:00:00
period: 1m
basePeriod: 1m
exchanges: [{"eid":"Futures_CTP","currency":"FUTURES","fee":[0.001,0.001]}]
mode: 1
args: [["zhi",1.8],["ua",120],["ua2",40]]
*/


void main() {

    Sleep(3*100*60);

    Log(exchange.GetAccount());
    
    while (1) {
       

         huan:
        if (exchange.IO("status") == 1 ) {          
   
             exchange.SetContractType(Symbol);             
             auto r = exchange.GetRecords(zhouqi);
            //计算方差
            int zj;
            auto leijia=0;
            auto leijia_l=0;
            auto leijia_f=0;
            auto ju=0;
            int f=0;
            for(int i=1 ; i <= 100;i++){
                if(r[r.size()-i].Close>1000 && r[r.size()-i].Volume >0){
            leijia=leijia + r[r.size()-i].Close * r[r.size()-i].Volume;
            leijia_l= leijia_l + r[r.size()-i].Volume; 
                }
            }
            ju=leijia/leijia_l;
            for(int i=1; i<= 100 ;i++ ) {
                
               if(r[r.size()-i].Close>1000 && r[r.size()-i].Volume >0){     
                leijia_f=leijia_f+r[r.size()-i].Volume*(r[r.size()-i].Close - ju)*(r[r.size()-i].Close - ju);      
               }
            }
            f= (int)sqrt(leijia_f/leijia_l) ;
            f=f/10;
            auto yu=f%10;
            if(yu>3){f=f*10+10;}else{f=f*10;}
            zj=(int)(ju/10);
            zj=zj*10;
           // Log("f",f, "leijai" ,leijia," leijia_l",leijia_l ," zj",zj);
            
            
             auto ticker = exchange.GetTicker();
            
            
            if(ticker.Last > zj && ticker.Last < (k*f +zj) && f<ua  && f>ua2){
             exchange.SetContractType(Symbol);
             exchange.SetDirection("sell");
            auto id=exchange.Sell( zj + k*f , 1);
              while(1) {
              Sleep(1200);
                   auto order = exchange.GetOrder(id);
                   auto ticker = exchange.GetTicker();
                  
                  if(order.Status == 1){
                   exchange.SetContractType(Symbol);
                   exchange.SetDirection("closesell_today");
                   auto id2=exchange.Buy( zj , 1);
                      while(1){
                      Sleep(1100);
                      auto order2 = exchange.GetOrder(id2);
                      auto ticker = exchange.GetTicker();
                     if( order2.Status == 1 ){
                      goto huan; 
                     }    
                    if(ticker.Last >= zj + k*f*zhi ) {
                    auto id22= exchange.CancelOrder(id2);
                    while(1){
                      if(id22 == true){
                    break;                      
                      }
                        Sleep(1000);
                       id22= exchange.CancelOrder(id2); 
                    } 
                   exchange.SetContractType(Symbol);
                   exchange.SetDirection("closesell_today");
                   exchange.Buy( zj+1000 , 1);
                   goto huan;
                      }
                  }}
                      
                if(ticker.Last < zj){
                auto id1= exchange.CancelOrder(id);
                    while(1){
                      if(id1 == true){
                      goto huan;                      
                      }
                     Sleep(1000);
                    id1= exchange.CancelOrder(id); 
                    }
                }  }  }//多向开仓
            
            if(ticker.Last < zj && ticker.Last > (k*f - zj) && f<ua && f>ua2){
             exchange.SetContractType(Symbol);
             exchange.SetDirection("buy");
            auto id9=exchange.Buy( zj - k*f , 1);
              while(1) {
              Sleep(1200);
                   auto order9 = exchange.GetOrder(id9);
                   auto ticker9 = exchange.GetTicker();
                  
                  if(order9.Status == 1){
                   exchange.SetContractType(Symbol);
                   exchange.SetDirection("closebuy_today");
                   auto id29=exchange.Sell( zj , 1);
                      while(1){
                      Sleep(1100);
                      auto order29 = exchange.GetOrder(id29);
                      auto ticker9 = exchange.GetTicker();
                     if( order29.Status == 1 ){
                      goto huan; 
                     }    
                    if(ticker9.Last <= zj - k*f*zhi ) {
                    auto id229= exchange.CancelOrder(id29);
                    while(1){
                      if(id229 == true){
                    break;                      
                      }
                        Sleep(1000);
                       id229= exchange.CancelOrder(id29); 
                    } 
                   exchange.SetContractType(Symbol);
                   exchange.SetDirection("closebuy_today");
                   exchange.Sell( zj - 1000 , 1);
                   goto huan;
                      }
                  }}
                      
                if(ticker9.Last > zj){
                auto id19= exchange.CancelOrder(id9);
                    while(1){
                      if(id19 == true){
                      goto huan;                      
                      }
                     Sleep(1000);
                    id19= exchange.CancelOrder(id9); 
                    }
                }  }  }//空向开仓            
            
            
            
            

        } else {
            LogStatus(_D(), "未连接CTP !");
            Sleep(1000);
        }

    }
}

More