Loading ...

均线趋势

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


void main() {
    Sleep(10000);
    Log(exchange.GetAccount());
    auto id=0;
    while (1) {
        if (exchange.IO("status") == 1) {
            exchange.SetContractType(Symbol);

            auto r = exchange.GetRecords(60);
            auto ma = TA.MA(r, 20);
            auto a = ma[ma.size() - 1];
            auto b = ma[ma.size() - 3];
            auto r1 = exchange.GetRecords(60);
            auto ma1 = TA.MA(r1, 120);
            auto c = ma1[ma1.size() - 1];
            auto d = ma1[ma1.size() - 3];

            // Log(ma[ma.size() - 1]);

            auto ticker = exchange.GetTicker();
            auto position = exchange.GetPosition();
             auto orders = exchange.GetOrders();
            if (position.size() > 0 && !(orders.size() > 0)) {
                if (!position[0].Type) {
                   // if (position[0].Profit > 2000) {

                        exchange.SetContractType(Symbol);
                        exchange.SetDirection("closebuy_today");
                        exchange.Sell(orders[0].Price + 50, 1);
                   /* } else if (a < c && b > d ) {
                        exchange.SetContractType(Symbol);
                        exchange.SetDirection("closebuy_today");
                        exchange.Sell(ticker.Buy, 1);
                         Log("pingcang");
                        exchange.SetContractType(Symbol);
                        exchange.SetDirection("sell");
                        exchange.Sell(ticker.Buy, 1);
                       continue;
                    }*/

                }
                if (position[0].Type) {
                   // if (position[0].Profit > 2000) {
                        exchange.SetContractType(Symbol);
                        exchange.SetDirection("closesell_today");
                        exchange.Buy(orders[0].Price - 50, 1);
                  
                    
                    /*  } else if (a > c && b < d) {
                        exchange.SetContractType(Symbol);
                        exchange.SetDirection("closesell_today");
                        exchange.Buy(ticker.Sell, 1);
                         Log("pingcang");
                        exchange.SetContractType(Symbol);
                        exchange.SetDirection("buy");
                        exchange.Buy(ticker.Sell, 1);
                          continue;
                    }*/

                }



            } else if( !(orders.size() > 0)) {
                auto position1 = exchange.GetPosition();
                if (!(position1.size() > 0)){
                if (a < c && b > d) {
                    exchange.SetContractType(Symbol);
                    exchange.SetDirection("sell");
                   id=exchange.Sell(ticker.Sell - 1000, 1);
                    continue;
                } else if (a > c && b < d) {
                    exchange.SetContractType(Symbol);
                    exchange.SetDirection("buy");
                    id=exchange.Buy(ticker.Buy + 1000, 1);
                    continue;
                }
                }
            }else if (orders.size() > 0 && position[0].Profit < -30){
                 exchange.SetContractType(Symbol);
                
               auto orders = exchange.GetOrders();
                if(orders[0].Type == 0  ) {
                exchange.CancelOrder(id);
                    Sleep(100);
                    exchange.SetContractType(Symbol);
                       exchange.SetDirection("closebuy_today");
                        exchange.Sell(ticker.Buy-1000, 1);
                } else if(orders[0].Type == 1 ){
                exchange.CancelOrder(id);
                    Sleep(100);
                    exchange.SetContractType(Symbol);
                    exchange.SetDirection("closesell_today");
                    exchange.Buy(orders[0].Price + 1000, 1);
                }
            
            }
        } else {
            LogStatus(_D(), "未连接CTP !");
            Sleep(1000);
        }
      
    }
}

Related

More