Loading ...

均线趋势

Author: 量价时空, Date: 2020-07-10 22:35:13
Tags: C++ MA Trend


/*backtest
start: 2020-09-14 09:00:00
end: 2020-09-19 15:00:00
period: 1m
basePeriod: 1m
exchanges: [{"eid":"Futures_CTP","currency":"FUTURES","fee":[0.00025,0.00025]}]
args: [["li",600],["zhi",400]]
*/

void main() {
    Sleep(10000);
    Log(exchange.GetAccount());
     auto  chi=0;
    int nv1=1;
    int p1=3;
    auto jia=0;
   auto position1 = exchange.GetPosition();
    while(nv1){
        auto error1 = GetLastError();
       if(error1 == "Futures_OP 3"){
           Sleep(1000);
           position1 = exchange.GetPosition();
           continue;
    }
    
            nv1=0;
        }  
     if(position1.size() > 0){
              for( int u1=0; u1 <= (int)position1.size(); u1++  ){
              if( position1[position1.size()-u1].ContractType == Symbol){
                  if(position1[position1.size()-u1].Type == 0){p1=0;}
                  if(position1[position1.size()-u1].Type == 1){p1=1;}
                  chi=1;
          }
              }          
                  }
    
    
    while (1) {
        if (exchange.IO("status") == 1) {
            
            exchange.SetContractType(Symbol);

            auto r = exchange.GetRecords(zhouqi);
            auto ma = TA.MA(r, duan);
            auto a = ma[ma.size() - 1];
            auto b = ma[ma.size() - 3];
        
            auto ma1 = TA.MA(r, chang);
            auto c = ma1[ma1.size() - 1];
            auto d = ma1[ma1.size() - 3];   

            auto ticker = exchange.GetTicker();

            //开仓
            if (chi == 0) {
            if(c-a < k &&  c>a  && d>b){
                        exchange.SetContractType(Symbol);
                        exchange.SetDirection("buy");
                        auto id=exchange.Buy(ticker.Last + 300, 1);
                              chi=1;   
                 continue;
            }
            if( a-c < k &&  a>c && d<b ){
                       exchange.SetContractType(Symbol);
                       exchange.SetDirection("sell");
                        auto id=exchange.Sell(ticker.Last - 300, 1);
                             chi=1;    
                 continue;
                }
            }
        if(chi==1 && jia<300) {
           auto position1 = exchange.GetPosition();
           if(position1.size() > 0){
              for( int u1=0; u1 <= (int)position1.size(); u1++  ){
              if( position1[position1.size()-u1].ContractType == Symbol){
                  if(position1[position1.size()-u1].Type == 0){p1=0;}
                  if(position1[position1.size()-u1].Type == 1){p1=1;}
                  chi=1;
                  jia=position1[position1.size()-u1].Price;
          }
              }          
                  }
                continue;
        }       
                
           if (chi == 1 && jia > 300) { //平仓
               
            if( p1==0 && ( ticker.Last - jia > li || jia - ticker.Last >zhi ) ){ 
                     exchange.SetContractType(Symbol);
                       exchange.SetDirection("closebuy_today");
                        auto id=exchange.Sell(ticker.Last - 300, 1);
                             chi=0;   
                             jia=0;
                continue;
            }
           if( p1==1  && ( ticker.Last - jia > zhi || jia - ticker.Last >li )){ 
                 exchange.SetContractType(Symbol);
                       exchange.SetDirection("closesell_today");
                        auto id=exchange.Buy(ticker.Last + 300, 1);
                             chi=0;   
                             jia=0;
                continue;
            }    
            }
            
            
        } else {
            LogStatus(_D(), "未连接CTP !");
            Sleep(1000);
        }
      
    }
}

Related

More