炒单!炒单!!!!

Author: 量价时空, Date: 2020-09-17 12:31:44
Tags: C++


void main() {

    Sleep(10000);

    Log(exchange.GetAccount());
     huan:
    while (1) {
        int chi = 0;
        auto wei = 0;
        exchange.IO("mode", 1);
        exchange.SetContractType(Symbol);
        auto ticker11 = exchange.GetTicker();



        if (exchange.IO("status") == 1 && ticker11.Sell > 500) {
            
               auto orders = exchange.GetOrders();   //撤挂单
                if (orders.size() > 0) {
                    for (int u = 0; u <= (int)orders.size(); u++)
                        exchange.CancelOrder(orders[u].Id);
                }
            int nv=1;
             auto ticker1 = exchange.GetTicker();
             auto position = exchange.GetPosition();
            while (nv) {
                auto error = GetLastError();
                if (error == "Futures_OP 3") {
                    Sleep(1000);
                    position = exchange.GetPosition();
                    continue;
                }
                nv = 0;
            }

            if (position.size() > 0) {//判断持仓
                for (int u1 = 0; u1 <= (int) position.size(); u1++) {
                    if (position[position.size() - u1].ContractType == Symbol) {

                        if (position[position.size() - u1].Type == 0) {
                            chi = chi + 1;
                            wei = position.size() - u1;
                        }
                        if (position[position.size() - u1].Type == 1) {
                            chi = chi + 1;
                            wei = position.size() - u1;
                        }
                    }
                }
            }
             
            
            

            if (chi == 1) {//平余仓             
                if (position[wei].Type == 0) {
                    exchange.SetContractType(Symbol);
                    exchange.SetDirection("closebuy_today");
                    exchange.Sell((int)(ticker1.Last * 0.995), 1);

                }
                if (position[wei].Type == 1) {
                    exchange.SetContractType(Symbol);
                    exchange.SetDirection("closesell_today");
                    exchange.Buy((int)(ticker1.Last * 1.005), 1);
                }

            }
            
            
            exchange.SetContractType(Symbol);  //获取盘口
            auto depth = exchange.GetDepth();
            auto s1_m = depth.Asks[0].Amount;
            auto b1_m = depth.Bids[0].Amount;
            auto s1_p = depth.Asks[0].Price;
            auto b1_p = depth.Bids[0].Price;

            if (s1_p > b1_p) {

                if (chi == 0) {
                    Sleep(1000);
                    while (1) {
                        exchange.SetContractType(Symbol);
                        auto depth = exchange.GetDepth();
                        auto s1_m = depth.Asks[0].Amount;
                        auto b1_m = depth.Bids[0].Amount;
                        auto s1_p = depth.Asks[0].Price;
                        auto b1_p = depth.Bids[0].Price;
                        auto mz1=0;
                        auto bz1=0;
                            for(int h=0; h < 5; h++) 
                          {
                             mz1=mz1 + depth.Asks[h].Amount;
                             bz1=bz1 + depth.Bids[h].Amount;
                               }
                        
                        
                        
                         if (s1_m  < b1_m * 0.7 && mz1 > 0.8*bz1){
                            exchange.SetContractType(Symbol);
                            exchange.SetDirection("sell");
                            auto id = exchange.Sell(s1_p + 1, 1);
                            Sleep(1000);
                          while (1) {
                                auto order = exchange.GetOrder(id);
                                if (order.Status == 0) {
                                    auto ticker = exchange.GetTicker();
                                    if (ticker.Last < s1_p - 4) {
                                        exchange.CancelOrder(id);
                                        break;
                                    }}
                                    if (order.Status == 1) {
                                        exchange.SetContractType(Symbol);
                                        exchange.SetDirection("closesell_today");
                                        auto id1 = exchange.Buy(s1_p - 2, 1);
                                          while(1){
                                            auto  ticker = exchange.GetTicker();
                                          auto order = exchange.GetOrder(id1);
                                          if (order.Status == 1){ goto huan;}
                                          if(order.Status == 0) { 
                                          if( ticker.Sell > s1_p + 2 ){
                                          exchange.SetContractType(Symbol);
                                          exchange.SetDirection("buy");
                                          exchange.Buy(s1_p + 15, 1);                                                       
                                          goto huan;
                                          }                                          
                                          }      
                                          
                                    }
                                }






                            }
                        
                            
                            
                            
                        
                        }//卖完成

                        
                        
                        if(s1_m * 0.7 > b1_m  &&  mz1*0.8 < bz1 ) {
                            exchange.SetContractType(Symbol);
                            exchange.SetDirection("buy");
                            auto id = exchange.Buy(b1_p - 1, 1);
                            Sleep(1000);
                            while (1) {
                                auto order = exchange.GetOrder(id);
                                if (order.Status == 0) {
                                    auto ticker = exchange.GetTicker();
                                    if (ticker.Last > b1_p + 4) {
                                        exchange.CancelOrder(id);
                                        break;
                                    }}
                                    if (order.Status == 1) {
                                        exchange.SetContractType(Symbol);
                                        exchange.SetDirection("closebuy_today");
                                        auto id1 = exchange.Sell(b1_p + 2, 1);
                                          while(1){
                                          auto ticker = exchange.GetTicker();
                                          auto order = exchange.GetOrder(id1);
                                          if (order.Status == 1){ goto huan;}
                                          if(order.Status == 0) { 
                                          if( ticker.Buy < b1_p-2 ){
                                          exchange.SetContractType(Symbol);
                                          exchange.SetDirection("sell");
                                          exchange.Sell(b1_p - 15, 1);                                                       
                                          goto huan;
                                          }  }      
                                          
                                             }
                                






                            }
                        }


                    }//完成买
                    }
                }

                if (chi == 2) {
                  Sleep(1000);
                    while (1) {
                        exchange.SetContractType(Symbol);
                        auto depth = exchange.GetDepth();
                        auto s1_m = depth.Asks[0].Amount;
                        auto b1_m = depth.Bids[0].Amount;
                        auto s1_p = depth.Asks[0].Price;
                        auto b1_p = depth.Bids[0].Price;
                          auto mz1=0;
                          auto bz1=0;
                            for(int h=0; h < 5; h++) 
                          {
                             mz1=mz1 + depth.Asks[h].Amount;
                             bz1=bz1 + depth.Bids[h].Amount;
                               }
                        
                        
                     if (s1_m  < b1_m * 0.7 && mz1 > bz1*0.8 ){
                            exchange.SetContractType(Symbol);
                            exchange.SetDirection("closebuy_today");
                            auto id = exchange.Sell(s1_p + 1, 1);
                            Sleep(1000);
                          while (1) {
                                auto order = exchange.GetOrder(id);
                                if (order.Status == 0) {
                                    auto ticker = exchange.GetTicker();
                                    if (ticker.Last < s1_p - 4) {
                                        exchange.CancelOrder(id);
                                        break;
                                    }}
                                    if (order.Status == 1) {
                                        exchange.SetContractType(Symbol);
                                        exchange.SetDirection("closesell_today");
                                        auto id1 = exchange.Buy(s1_p - 2, 1);
                                          while(1){
                                            auto  ticker = exchange.GetTicker();
                                          auto order = exchange.GetOrder(id1);
                                          if (order.Status == 1){ goto huan;}
                                          if(order.Status == 0) { 
                                          if( ticker.Sell > s1_p + 2 ){
                                          exchange.SetContractType(Symbol);
                                          exchange.SetDirection("buy");
                                          exchange.Buy(s1_p + 15, 1);                                                       
                                          goto huan;
                                          }                                          
                                          }      
                                          
                                    }
                                }






                            }
                        
                            
                            
                            
                        
                        }//卖完成
                
                  if(s1_m * 0.7 > b1_m && mz1*0.8 < bz1) {
                            exchange.SetContractType(Symbol);
                            exchange.SetDirection("closesell_today");
                            auto id = exchange.Buy(b1_p - 1, 1);
                            Sleep(1000);
                            while (1) {
                                auto order = exchange.GetOrder(id);
                                if (order.Status == 0) {
                                    auto ticker = exchange.GetTicker();
                                    if (ticker.Last > b1_p + 4) {
                                        exchange.CancelOrder(id);
                                        break;
                                    }}
                                    if (order.Status == 1) {
                                        exchange.SetContractType(Symbol);
                                        exchange.SetDirection("closebuy_today");
                                        auto id1 = exchange.Sell(b1_p + 2, 1);
                                          while(1){
                                          auto ticker = exchange.GetTicker();
                                          auto order = exchange.GetOrder(id1);
                                          if (order.Status == 1){ goto huan;}
                                          if(order.Status == 0) { 
                                          if( ticker.Buy < b1_p-2 ){
                                          exchange.SetContractType(Symbol);
                                          exchange.SetDirection("sell");
                                          exchange.Sell(b1_p - 15, 1);                                                       
                                          goto huan;
                                          }  }      
                                          
                                             }
                                






                            }
                        }


                    }//完成买
                
                
                
                
                
                
                
                    }
                
                
                
                
                }










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

    }
}

相关内容

更多内容