资源加载中... loading...

复合品种对冲版

Author: 量价时空, Date: 2020-08-15 11:42:34
Tags: 对冲套利C++


void main() {

    Sleep(10000);

    Log(exchange.GetAccount());

    while (1) {
         int chi1=0;
         int chi2=0;
         int pl1=0;
         int pl2=0;
         int m1=0;
         int m2=0;
        
            exchange.SetContractType(Symbol1);
            auto ticker111 = exchange.GetTicker();
          


        if (exchange.IO("status") == 1 && ticker111.Last > 500 ) {
           
              auto position = exchange.GetPosition();
              if(position.size() > 0){
              for( int u=0; u < (int)position.size(); u++  ){
            
              if( position[position.size()-u].ContractType == Symbol1){
               chi1=1;
               pl1=position.size()-u;   
              }   
               if( position[position.size()-u].ContractType == Symbol2){
               chi2=1;
               pl2=position.size()-u;   
              }       
             
            }
            }else if( !(position.size() > 0)){
            chi1=-1;
            chi2=-1;    
         
            }
              if(chi1 != 1){ chi1=-1;}
              if(chi2 != 1){ chi2=-1;}
            
            
             Sleep(1000);
            
            

                 auto t1 = 0;
                 auto x1 = 0;
            
            exchange.SetContractType(Symbol1);
            auto ticker11 = exchange.GetTicker();
            exchange.SetContractType(Symbol21);
            auto ticker21 = exchange.GetTicker();

            int y1 = ticker11.Last - ticker21.Last;
            int w1 = ((int)(y1 - k1)) / (int) v1;
            int a11 = (int)(ticker11.Buy - ticker21.Sell - k1) % (int) v1;
            int b11= (int)(ticker11.Buy - ticker21.Sell - k1) / (int) v1;
            int a21= (int)(ticker11.Sell - ticker21.Buy - k1) % (int) v1;
            int b21 = (int)(ticker11.Sell - ticker21.Buy - k1) / (int) v1;
            if (b11 < 0) {
                b11 = -b11;
            }
            if (b21 < 0) {
                b21 = -b21;
            }
            if (y1 - k1 == 0) {
                t1 = 0;
            }


            if (y1 - k1 < 0) {
                w1 = -w1;
                t1 = -1;
            } else if (y1 - k1 > 0) {
                t1 = 1;
            }

            if (chi1 > 0) {
                if(position[pl1].ContractType == Symbol1){
                m1 = position[pl1].Amount/(int)bei1;
                
                }
            } 
                
            if (chi1 == -1) {
                    if ( t1 > 0 && b11 >= 1) {
                        exchange.SetContractType(Symbol1);
                        exchange.SetDirection("sell");
                        exchange.Sell(ticker11.Last - 10, bei1);

                        exchange.SetContractType(Symbol21);
                        exchange.SetDirection("buy");
                        exchange.Buy(ticker21.Last + 10, bei1);
                        Sleep(1000);
                        continue;
                    } else if (t1 < 0 && b21 >= 1) {

                        exchange.SetContractType(Symbol1);
                        exchange.SetDirection("buy");
                        exchange.Buy(ticker11.Last + 10, bei1);

                        exchange.SetContractType(Symbol21);
                        exchange.SetDirection("sell");
                        exchange.Sell(ticker21.Last - 10, bei1);

                        Sleep(1000);
                        continue;
                    }
                }

            
           if (chi1 > 0) {
                Log(position[pl1].ContractType, Symbol1);
            }

            if (position[pl1].ContractType == Symbol1) {
                if (position[pl1].Type == 0 || position[pl1].Type == 2) {
                    x1 = 1;
                } else if (position[pl1].Type == 1 || position[pl1].Type == 3) {
                    x1 = -1;
                }
            }

         

            if (chi1 > 0) {
                if ((ticker11.Buy - ticker21.Sell - k1) > 0 && x1 > 0) {
                    if (position[pl1].Type < 2) {
                        exchange.SetContractType(Symbol21);
                        exchange.SetDirection("closesell_today");
                        exchange.Buy(ticker21.Last + 10, bei1);

                        exchange.SetContractType(Symbol1);
                        exchange.SetDirection("closebuy_today");
                       exchange.Sell(ticker11.Last - 10, bei1);
                        Sleep(1000);
                        continue;
                    }
                    if (position[pl1].Type > 1) {
                        exchange.SetContractType(Symbol21);
                        exchange.SetDirection("closesell");
                       exchange.Buy(ticker21.Last + 10, bei1);

                        exchange.SetContractType(Symbol1);
                        exchange.SetDirection("closebuy");
                      exchange.Sell(ticker11.Last - 10, bei1);
                        Sleep(1000);
                        continue;
                    }
                 
                } else if ((ticker11.Sell - ticker21.Buy - k1) < 0 && x1 < 0) {
                    if (position[pl1].Type < 2) {
                        exchange.SetContractType(Symbol1);
                        exchange.SetDirection("closesell_today");
                        auto id1 = exchange.Buy(ticker11.Last + 10, bei1);

                        exchange.SetContractType(Symbol21);
                        exchange.SetDirection("closebuy_today");
                        auto id2 = exchange.Sell(ticker21.Last - 10, bei1);
                        Sleep(1000);
                        continue;
                    }
                    if (position[pl1].Type > 1) {
                        exchange.SetContractType(Symbol1);
                        exchange.SetDirection("closesell");
                       exchange.Buy(ticker11.Last + 10, bei1);

                        exchange.SetContractType(Symbol21);
                        exchange.SetDirection("closebuy");
                        exchange.Sell(ticker21.Last - 10, bei1);
                        Sleep(1000);
                        continue;
                    }
                   
                }

            }

          Log("M1=", m1, "  x1=", x1, "  w1=", w1, "  t1=", t1, " a11=", a11, " a21=", a21, " b11=", b11, " b21=", b21);
            
            
              if (m1 > 3) { //大于三的平仓
                     int u11=m1-3;
                     int h11=0;
                    if(ticker11.Buy - ticker21.Sell > k1){
                     if(u11==1){h11=15;}
                     if(u11==2){h11=15+6;}
                     if(u11==3){h11=15+6+7;}
                     if(u11==4){h11=15+6+7+8;}  
                     if(u11==5){h11=15+6+7+8+9;} 
                     if(u11==6){h11=15+6+7+8+9+10;} 
                     if(u11==7){h11=15+6+7+8+9+10+11;}   
                     if(u11==8){h11=15+6+7+8+9+10+11+12;}
                     if(u11==9){h11=15+6+7+8+9+10+11+12+13;}   
                     if(u11==10){h11=15+6+7+8+9+10+11+12+13+14;}   
                     if(u11==11){h11=15+6+7+8+9+10+11+12+13+14+15;}  
                        
                     }
                      if(ticker11.Sell - ticker21.Buy < k1){
                     if(u11==1){h11=15;}
                     if(u11==2){h11=15+6;}
                     if(u11==3){h11=15+6+7;}
                     if(u11==4){h11=15+6+7+8;}      
                     if(u11==5){h11=15+6+7+8+9;} 
                     if(u11==6){h11=15+6+7+8+9+10;} 
                     if(u11==7){h11=15+6+7+8+9+10+11;}   
                     if(u11==8){h11=15+6+7+8+9+10+11+12;}
                     if(u11==9){h11=15+6+7+8+9+10+11+12+13;}   
                     if(u11==10){h11=15+6+7+8+9+10+11+12+13+14;}   
                     if(u11==11){h11=15+6+7+8+9+10+11+12+13+14+15;}      
                          
                          h11=-h11;
                     }     
                        Log( "  u11=", u11, "  h11=", h11);
                if ((ticker11.Buy - ticker21.Sell - k1 - h11) >= 0 && x1 > 0  && h11 !=0) {
                    if (position[pl1].Type < 2) {
                        exchange.SetContractType(Symbol21);
                        exchange.SetDirection("closesell_today");
                        exchange.Buy(ticker21.Last + 10, bei1);

                        exchange.SetContractType(Symbol1);
                        exchange.SetDirection("closebuy_today");
                        exchange.Sell(ticker11.Last - 10, bei1);
                        Sleep(1000);
                        continue;
                    }
                    if (position[pl1].Type > 1) {
                        exchange.SetContractType(Symbol21);
                        exchange.SetDirection("closesell");
                        exchange.Buy(ticker21.Last + 10, bei1);

                        exchange.SetContractType(Symbol1);
                        exchange.SetDirection("closebuy");
                        exchange.Sell(ticker11.Last - 10, bei1);
                        Sleep(1000);
                        continue;
                    }
                   
                    
                } else if ((ticker11.Sell - ticker21.Buy - k1 -h11) <= 0 && x1 < 0 && h11 !=0) {
                    if (position[pl1].Type < 2) {
                        exchange.SetContractType(Symbol1);
                        exchange.SetDirection("closesell_today");
                        exchange.Buy(ticker11.Last + 10, bei1);

                        exchange.SetContractType(Symbol21);
                        exchange.SetDirection("closebuy_today");
                        exchange.Sell(ticker21.Last - 10, bei1);
                        Sleep(1000);
                        continue;
                    }
                    if (position[pl1].Type > 1) {
                        exchange.SetContractType(Symbol1);
                        exchange.SetDirection("closesell");
                        exchange.Buy(ticker11.Last + 10, bei1);

                        exchange.SetContractType(Symbol21);
                        exchange.SetDirection("closebuy");
                        exchange.Sell(ticker21.Last - 10, bei1);
                        Sleep(1000);
                        continue;
                    }
                   
                }

            } 
            
            
            
            
            
            if (m1 < 4  &&  m1 >1 && m1 > w1) {   //小于4的平仓


                if (x1 > 0 && t1 < 0 && ((a11 == 0 && m1 > b11) || m1 > b11 + 1)) {
                    if (position[pl1].Type < 2) {
                        exchange.SetContractType(Symbol21);
                        exchange.SetDirection("closesell_today");
                       exchange.Buy(ticker21.Last + 10, bei1);

                        exchange.SetContractType(Symbol1);
                        exchange.SetDirection("closebuy_today");
                        exchange.Sell(ticker11.Last - 10, bei1);
                        Sleep(1000);
                        continue;
                    }
                    if (position[pl1].Type > 1) {
                        exchange.SetContractType(Symbol21);
                        exchange.SetDirection("closesell");
                       exchange.Buy(ticker21.Last + 10, bei1);

                        exchange.SetContractType(Symbol1);
                        exchange.SetDirection("closebuy");
                         exchange.Sell(ticker11.Last - 10, bei1);
                        Sleep(1000);
                        continue;
                    }

                
                }

                if (x1 < 0 && t1 > 0 && ((a21 == 0 && m1 > b21) || m1 > b21 + 1)) {
                    if (position[pl1].Type < 2) {
                        exchange.SetContractType(Symbol1);
                        exchange.SetDirection("closesell_today");
                        exchange.Buy(ticker11.Last + 10, bei1);

                        exchange.SetContractType(Symbol21);
                        exchange.SetDirection("closebuy_today");
                        exchange.Sell(ticker21.Last - 10, bei1);
                        Sleep(1000);
                        continue;
                    }
                    if (position[pl1].Type > 1) {
                        exchange.SetContractType(Symbol1);
                        exchange.SetDirection("closesell");
                       exchange.Buy(ticker11.Last + 10, bei1);

                        exchange.SetContractType(Symbol21);
                        exchange.SetDirection("closebuy");
                        exchange.Sell(ticker21.Last - 10, bei1);
                        Sleep(1000);
                        continue;
                    }

                   
                }

            } 
            
            if ( m1 < 8 && w1 > m1 && m1>0) {
                if(m1<3){   //m<4
                if (t1 > 0 && b11 > m1) {
                    exchange.SetContractType(Symbol1);
                    exchange.SetDirection("sell");
                    exchange.Sell(ticker11.Last - 10, bei1);

                    exchange.SetContractType(Symbol21);
                    exchange.SetDirection("buy");
                    exchange.Buy(ticker21.Last + 10, bei1);
                    Sleep(1000);
                    continue;
                } else if (t1 < 0 && b21 > m1) {

                    exchange.SetContractType(Symbol1);
                    exchange.SetDirection("buy");
                    exchange.Buy(ticker11.Last + 10, bei1);

                    exchange.SetContractType(Symbol21);
                    exchange.SetDirection("sell");
                    exchange.Sell(ticker21.Last - 10, bei1);
                    Sleep(1000);
                    continue;
                }
                }
                     if(m1>2){
                     auto u1=m1-3;
                     auto h1=0;
                     if(ticker11.Buy - ticker21.Sell > k1){
                     if(u1==0){h1=15;}
                     if(u1==1){h1=15+6;}
                     if(u1==2){h1=15+6+7;}
                     if(u1==3){h1=15+6+7+8;}  
                     if(u1==4){h1=15+6+7+8+9;} 
                     if(u1==5){h1=15+6+7+8+9+10;} 
                     if(u1==6){h1=15+6+7+8+9+10+11;}   
                     if(u1==7){h1=15+6+7+8+9+10+11+12;}
                     if(u1==8){h1=15+6+7+8+9+10+11+12+13;}    
                     if(u1==9){h1=15+6+7+8+9+10+11+12+13+14;}    
                     if(u1==10){h1=15+6+7+8+9+10+11+12+13+14+15;}    
                         
                     }
                      if(ticker11.Sell - ticker21.Buy < k1){
                     if(u1==0){h1=15;}
                     if(u1==1){h1=15+6;}
                     if(u1==2){h1=15+6+7;}
                     if(u1==3){h1=15+6+7+8;}  
                     if(u1==4){h1=15+6+7+8+9;} 
                     if(u1==5){h1=15+6+7+8+9+10;} 
                     if(u1==6){h1=15+6+7+8+9+10+11;}   
                     if(u1==7){h1=15+6+7+8+9+10+11+12;}
                     if(u1==8){h1=15+6+7+8+9+10+11+12+13;}    
                     if(u1==9){h1=15+6+7+8+9+10+11+12+13+14;}    
                     if(u1==10){h1=15+6+7+8+9+10+11+12+13+14+15;}         
                          h1=-h1;
                     }     
                          
                     int b11 = (int)(ticker11.Buy - ticker21.Sell - k1-h1) / (int) (v1+u1+1);
                     int b21 = (int)(ticker11.Sell - ticker21.Buy - k1-h1) / (int) (v1+u1+1);
                if (b11 < 0) {
                    b11 = -b11;
                }
                if (b21 < 0) {
                    b21= -b21;
                } 
                if (t1 > 0 && b11 >= 1) {
                    exchange.SetContractType(Symbol1);
                    exchange.SetDirection("sell");
                    exchange.Sell(ticker11.Last - 10, bei1);

                    exchange.SetContractType(Symbol21);
                    exchange.SetDirection("buy");
                    exchange.Buy(ticker21.Last + 10, bei1);
                    Sleep(1000);
                    continue;
                } else if (t1 < 0 && b21 >= 1) {

                    exchange.SetContractType(Symbol1);
                    exchange.SetDirection("buy");
                    exchange.Buy(ticker11.Last + 10, bei1);

                    exchange.SetContractType(Symbol21);
                    exchange.SetDirection("sell");
                    exchange.Sell(ticker21.Last - 10, bei1);
                    Sleep(1000);
                    continue;
                }
                  
                Log("M11=", m1, "  x1=", x1, "  w1=", w1, "  t1=", t1, "  u1=", u1, "  h1=", h1, " b1=", b11, " b2=", b21);    
 
                      }
            }
 //     第二回合     第二回合
        
                    Sleep(1000);
                   Log("第二回合");    
            
          
              auto t2 = 0;
              auto x2 = 0;
            exchange.SetContractType(Symbol2);
            auto ticker12 = exchange.GetTicker();
            exchange.SetContractType(Symbol22);
            auto ticker22 = exchange.GetTicker();

            auto y2 = ticker12.Last - ticker22.Last;
            auto w2 = ((int)(y2 - k2)) / (int) v2;
            int a12 = (int)(ticker12.Buy - ticker22.Sell - k2) % (int) v2;
            int b12= (int)(ticker12.Buy - ticker22.Sell - k2) / (int) v2;
            int a22= (int)(ticker12.Sell - ticker22.Buy - k2) % (int) v2;
            int b22 = (int)(ticker12.Sell - ticker22.Buy - k2) / (int) v2;
            if (b12 < 0) {
                b12 = -b12;
            }
            if (b22 < 0) {
                b22 = -b22;
            }
            if (y2 - k2 == 0) {
                t2 = 0;
            }


            if (y2 - k2 < 0) {
                w2 = -w2;
                t2 = -1;
            } else if (y2 - k2 > 0) {
                t2 = 1;
            }

  
            
         //Log( "  w=", w, "  t=", t," a1=",a1," a2=",a2," b1=",b1," b2=",b2);
            if (chi2 > 0) {
                if(position[pl2].ContractType == Symbol2){
                m2 = position[pl2].Amount/(int)bei2;
           
                }
            } else {
                
                if (chi2 < 0) {
                    if (t2 > 0 && b12 >= 1) {
                        exchange.SetContractType(Symbol2);
                        exchange.SetDirection("sell");
                        exchange.Sell(ticker12.Last - 10, bei2);

                        exchange.SetContractType(Symbol22);
                        exchange.SetDirection("buy");
                        exchange.Buy(ticker22.Last + 10, bei2);
                        Sleep(1000);
                        continue;
                    } else if (t2 < 0 && b22 >= 1) {

                        exchange.SetContractType(Symbol2);
                        exchange.SetDirection("buy");
                        exchange.Buy(ticker12.Last + 10, bei2);

                        exchange.SetContractType(Symbol22);
                        exchange.SetDirection("sell");
                        exchange.Sell(ticker22.Last - 10, bei2);

                        Sleep(1000);
                        continue;
                    }
                 
                    continue;

                }

            }
           if (chi2>0) {
                Log(position[pl2].ContractType, Symbol2);
            }

            if (position[pl2].ContractType == Symbol2) {
                if (position[pl2].Type == 0 || position[pl2].Type == 2) {
                    x2 = 1;
                } else if (position[pl2].Type == 1 || position[pl2].Type == 3) {
                    x2 = -1;
                }
            }

         

            if (chi2 > 0) {
                if ((ticker12.Buy - ticker22.Sell - k2) > 0 && x2 > 0) {
                    if (position[pl2].Type < 2) {
                        exchange.SetContractType(Symbol22);
                        exchange.SetDirection("closesell_today");
                        exchange.Buy(ticker22.Last + 10, bei2);

                        exchange.SetContractType(Symbol1);
                        exchange.SetDirection("closebuy_today");
                       exchange.Sell(ticker12.Last - 10, bei2);
                        Sleep(1000);
                        continue;
                    }
                    if (position[pl2].Type > 1) {
                        exchange.SetContractType(Symbol22);
                        exchange.SetDirection("closesell");
                       exchange.Buy(ticker22.Last + 10, bei2);

                        exchange.SetContractType(Symbol2);
                        exchange.SetDirection("closebuy");
                      exchange.Sell(ticker12.Last - 10, bei2);
                        Sleep(1000);
                        continue;
                    }
                 
                } else if ((ticker12.Sell - ticker22.Buy - k2) < 0 && x2 < 0) {
                    if (position[pl2].Type < 2) {
                        exchange.SetContractType(Symbol2);
                        exchange.SetDirection("closesell_today");
                        auto id1 = exchange.Buy(ticker12.Last + 10, bei2);

                        exchange.SetContractType(Symbol22);
                        exchange.SetDirection("closebuy_today");
                        auto id2 = exchange.Sell(ticker22.Last - 10, bei2);
                        Sleep(1000);
                        continue;
                    }
                    if (position[pl2].Type > 1) {
                        exchange.SetContractType(Symbol2);
                        exchange.SetDirection("closesell");
                       exchange.Buy(ticker12.Last + 10, bei2);

                        exchange.SetContractType(Symbol22);
                        exchange.SetDirection("closebuy");
                        exchange.Sell(ticker22.Last - 10, bei2);
                        Sleep(1000);
                        continue;
                    }
                   
                }

            }

          Log("M1=", m2, "  x1=", x2, "  w1=", w2, "  t1=", t2, " a11=", a12, " a21=", a22, " b11=", b12, " b21=", b22);
            
            
              if (m2 > 3) { //大于三的平仓
                     int u12=m2-3;
                     int h12=0;
                    if(ticker12.Buy - ticker22.Sell > k2){
                     if(u12==1){h12=15;}
                     if(u12==2){h12=15+6;}
                     if(u12==3){h12=15+6+7;}
                     if(u12==4){h12=15+6+7+8;}  
                     if(u12==5){h12=15+6+7+8+9;} 
                     if(u12==6){h12=15+6+7+8+9+10;} 
                     if(u12==7){h12=15+6+7+8+9+10+11;}   
                     if(u12==8){h12=15+6+7+8+9+10+11+12;}
                     if(u12==9){h12=15+6+7+8+9+10+11+12+13;}   
                     if(u12==10){h12=15+6+7+8+9+10+11+12+13+14;}   
                     if(u12==11){h12=15+6+7+8+9+10+11+12+13+14+15;}  
                        
                     }
                      if(ticker12.Sell - ticker22.Buy < k2){
                     if(u12==1){h12=15;}
                     if(u12==2){h12=15+6;}
                     if(u12==3){h12=15+6+7;}
                     if(u12==4){h12=15+6+7+8;}      
                     if(u12==5){h12=15+6+7+8+9;} 
                     if(u12==6){h12=15+6+7+8+9+10;} 
                     if(u12==7){h12=15+6+7+8+9+10+11;}   
                     if(u12==8){h12=15+6+7+8+9+10+11+12;}
                     if(u12==9){h12=15+6+7+8+9+10+11+12+13;}   
                     if(u12==10){h12=15+6+7+8+9+10+11+12+13+14;}   
                     if(u12==11){h12=15+6+7+8+9+10+11+12+13+14+15;}      
                          
                          h12=-h12;
                     }     
                        Log( "  u11=", u12, "  h11=", h12);
                if ((ticker12.Buy - ticker22.Sell - k2 - h12) >= 0 && x2 > 0  && h12 !=0) {
                    if (position[pl2].Type < 2) {
                        exchange.SetContractType(Symbol22);
                        exchange.SetDirection("closesell_today");
                        exchange.Buy(ticker22.Last + 10, bei2);

                        exchange.SetContractType(Symbol2);
                        exchange.SetDirection("closebuy_today");
                        exchange.Sell(ticker12.Last - 10, bei2);
                        Sleep(1000);
                        continue;
                    }
                    if (position[pl2].Type > 1) {
                        exchange.SetContractType(Symbol22);
                        exchange.SetDirection("closesell");
                        exchange.Buy(ticker22.Last + 10, bei2);

                        exchange.SetContractType(Symbol2);
                        exchange.SetDirection("closebuy");
                        exchange.Sell(ticker12.Last - 10, bei2);
                        Sleep(1000);
                        continue;
                    }
                   
                    
                } else if ((ticker12.Sell - ticker22.Buy - k2 -h12) <= 0 && x2 < 0 && h12 !=0) {
                    if (position[pl2].Type < 2) {
                        exchange.SetContractType(Symbol2);
                        exchange.SetDirection("closesell_today");
                        exchange.Buy(ticker12.Last + 10, bei2);

                        exchange.SetContractType(Symbol22);
                        exchange.SetDirection("closebuy_today");
                        exchange.Sell(ticker22.Last - 10, bei2);
                        Sleep(1000);
                        continue;
                    }
                    if (position[pl2].Type > 1) {
                        exchange.SetContractType(Symbol2);
                        exchange.SetDirection("closesell");
                        exchange.Buy(ticker12.Last + 10, bei2);

                        exchange.SetContractType(Symbol22);
                        exchange.SetDirection("closebuy");
                        exchange.Sell(ticker22.Last - 10, bei2);
                        Sleep(1000);
                        continue;
                    }
                   
                }

            } 
            
            
            
            
            
            if (m2 < 4  &&  m2 >1 && m2 > w2) {   //小于4的平仓


                if (x2 > 0 && t2 < 0 && ((a12 == 0 && m2 > b12) || m2 > b12 + 1)) {
                    if (position[pl2].Type < 2) {
                        exchange.SetContractType(Symbol22);
                        exchange.SetDirection("closesell_today");
                       exchange.Buy(ticker22.Last + 10, bei2);

                        exchange.SetContractType(Symbol2);
                        exchange.SetDirection("closebuy_today");
                        exchange.Sell(ticker12.Last - 10, bei2);
                        Sleep(1000);
                        continue;
                    }
                    if (position[pl2].Type > 1) {
                        exchange.SetContractType(Symbol22);
                        exchange.SetDirection("closesell");
                       exchange.Buy(ticker22.Last + 10, bei2);

                        exchange.SetContractType(Symbol2);
                        exchange.SetDirection("closebuy");
                         exchange.Sell(ticker12.Last - 10, bei2);
                        Sleep(1000);
                        continue;
                    }

                
                }

                if (x2 < 0 && t2 > 0 && ((a22 == 0 && m2 > b22) || m2 > b22 + 1)) {
                    if (position[pl2].Type < 2) {
                        exchange.SetContractType(Symbol2);
                        exchange.SetDirection("closesell_today");
                        exchange.Buy(ticker12.Last + 10, bei2);

                        exchange.SetContractType(Symbol22);
                        exchange.SetDirection("closebuy_today");
                        exchange.Sell(ticker22.Last - 10, bei2);
                        Sleep(1000);
                        continue;
                    }
                    if (position[pl2].Type > 1) {
                        exchange.SetContractType(Symbol2);
                        exchange.SetDirection("closesell");
                       exchange.Buy(ticker12.Last + 10, bei2);

                        exchange.SetContractType(Symbol22);
                        exchange.SetDirection("closebuy");
                        exchange.Sell(ticker22.Last - 10, bei2);
                        Sleep(1000);
                        continue;
                    }

                   
                }

            } 
            
            if ( m2 < 8 && w2 > m2 && m2>0) {
                if(m2<3){   //m<4
                if (t2 > 0 && b12 > m2) {
                    exchange.SetContractType(Symbol2);
                    exchange.SetDirection("sell");
                    exchange.Sell(ticker12.Last - 10, bei1);

                    exchange.SetContractType(Symbol22);
                    exchange.SetDirection("buy");
                    exchange.Buy(ticker22.Last + 10, bei2);
                    Sleep(1000);
                    continue;
                } else if (t2 < 0 && b22 > m2) {

                    exchange.SetContractType(Symbol2);
                    exchange.SetDirection("buy");
                    exchange.Buy(ticker12.Last + 10, bei2);

                    exchange.SetContractType(Symbol22);
                    exchange.SetDirection("sell");
                    exchange.Sell(ticker22.Last - 10, bei2);
                    Sleep(1000);
                    continue;
                }
                }
                     if(m2>2){
                     auto u2=m2-3;
                     auto h2=0;
                     if(ticker12.Buy - ticker22.Sell > k2){
                     if(u2==0){h2=15;}
                     if(u2==1){h2=15+6;}
                     if(u2==2){h2=15+6+7;}
                     if(u2==3){h2=15+6+7+8;}  
                     if(u2==4){h2=15+6+7+8+9;} 
                     if(u2==5){h2=15+6+7+8+9+10;} 
                     if(u2==6){h2=15+6+7+8+9+10+11;}   
                     if(u2==7){h2=15+6+7+8+9+10+11+12;}
                     if(u2==8){h2=15+6+7+8+9+10+11+12+13;}    
                     if(u2==9){h2=15+6+7+8+9+10+11+12+13+14;}    
                     if(u2==10){h2=15+6+7+8+9+10+11+12+13+14+15;}    
                         
                     }
                      if(ticker12.Sell - ticker22.Buy < k2){
                     if(u2==0){h2=15;}
                     if(u2==1){h2=15+6;}
                     if(u2==2){h2=15+6+7;}
                     if(u2==3){h2=15+6+7+8;}  
                     if(u2==4){h2=15+6+7+8+9;} 
                     if(u2==5){h2=15+6+7+8+9+10;} 
                     if(u2==6){h2=15+6+7+8+9+10+11;}   
                     if(u2==7){h2=15+6+7+8+9+10+11+12;}
                     if(u2==8){h2=15+6+7+8+9+10+11+12+13;}    
                     if(u2==9){h2=15+6+7+8+9+10+11+12+13+14;}    
                     if(u2==10){h2=15+6+7+8+9+10+11+12+13+14+15;}         
                          h2=-h2;
                     }     
                          
                     int b12 = (int)(ticker12.Buy - ticker22.Sell - k2-h2) / (int) (v2+u2+1);
                     int b22 = (int)(ticker12.Sell - ticker22.Buy - k2-h2) / (int) (v2+u2+1);
                if (b12 < 0) {
                    b12 = -b12;
                }
                if (b22 < 0) {
                    b22= -b22;
                } 
                if (t2 > 0 && b12 >= 1) {
                    exchange.SetContractType(Symbol2);
                    exchange.SetDirection("sell");
                    exchange.Sell(ticker12.Last - 10, bei2);

                    exchange.SetContractType(Symbol22);
                    exchange.SetDirection("buy");
                    exchange.Buy(ticker22.Last + 10, bei2);
                    Sleep(1000);
                    continue;
                } else if (t2 < 0 && b22 >= 1) {

                    exchange.SetContractType(Symbol2);
                    exchange.SetDirection("buy");
                    exchange.Buy(ticker12.Last + 10, bei2);

                    exchange.SetContractType(Symbol22);
                    exchange.SetDirection("sell");
                    exchange.Sell(ticker22.Last - 10, bei2);
                    Sleep(1000);
                    continue;
                }
                  
                Log("M11=", m2, "  x1=", x2, "  w1=", w2, "  t1=", t2, "  u1=", u2, "  h1=", h2, " b1=", b12, " b2=", b22);    
 
                      }
            }       
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        } else {
            LogStatus(_D(), "未连接CTP !");
            Sleep(1000);
        }

    }
}

相关内容

更多内容