Ejemplo n.º 1
0
class DemoCallback:
    def __init__(self):
        self.api = TqApi()
        self.quote = self.api.get_quote("SHFE.cu1805")

    def on_data_update(self):
        if self.quote.get("last_price", 0) > 1000:
            self.api.insert_order(symbol="SHFE.cu1805",
                                  direction="BUY",
                                  offset="OPEN",
                                  volume=1,
                                  limit_price=30000)

    def run(self):
        self.api.run(self.on_data_update)
Ejemplo n.º 2
0
class DemoTask:
    def __init__(self):
        self.api = TqApi()
        self.tm = TaskManager(self.api)

    def task_main(self):
        print("start")
        quote = self.api.get_quote("SHFE.cu1805")
        while True:
            wait_result = yield {
                "QUOTE_CHANGED": lambda: self.api.is_changing(quote),
                "TIMEOUT": 0.2,
            }
            if wait_result["QUOTE_CHANGED"]:
                print("Quote", quote)
            if wait_result["TIMEOUT"]:
                print("Timeout")

    def run(self):
        self.tm.start_task(self.task_main())
        self.api.run()
Ejemplo n.º 3
0
class DemoSpread:
    def __init__(self):
        self.api = TqApi()
        self.tm = TaskManager(self.api)

    def task_main(self):
        print ("start")
        symbol_a = "SHFE.cu1805"
        symbol_b = "SHFE.cu1806"
        quote_a = self.api.get_quote(symbol_a)
        quote_b = self.api.get_quote(symbol_b)
        buy_open_spread = 50000
        sell_close_spread = -70000
        max_volume = 5
        long_volume = 0
        while True:
            wait_result = yield {
                "BUY_OPEN": lambda: long_volume == 0 and quote_a["ask_price1"] - quote_b["bid_price1"] < buy_open_spread,
                "SELL_CLOSE": lambda: long_volume > 0 and quote_a["bid_price1"] - quote_b["ask_price1"] > sell_close_spread,
            }
            if wait_result["BUY_OPEN"]:
                task_a = self.tm.start_task(make_order_until_all_matched(self.api, symbol=symbol_a, direction="BUY", offset="OPEN", volume=max_volume))
                task_b = self.tm.start_task(make_order_until_all_matched(self.api, symbol=symbol_b, direction="SELL", offset="OPEN", volume=max_volume))
                long_volume = max_volume
            if wait_result["SELL_CLOSE"]:
                task_a = self.tm.start_task(make_order_until_all_matched(self.api, symbol=symbol_a, direction="SELL", offset="CLOSE", volume=max_volume))
                task_b = self.tm.start_task(make_order_until_all_matched(self.api, symbol=symbol_b, direction="BUY", offset="CLOSE", volume=max_volume))
                long_volume = 0
            wait_subtask_finish = yield {
                "ANY_TASK_ERROR": lambda: self.tm.get_error(task_a) or self.tm.get_error(task_b),
                "BOTH_TASK_FINSISH": lambda: self.tm.is_finish(task_a) and self.tm.is_finish(task_b),
            }
            if wait_subtask_finish["ANY_TASK_ERROR"]:
                break
        print ("finish")

    def run(self):
        self.tm.start_task(self.task_main())
        self.api.run()
Ejemplo n.º 4
0
class DemoMa:
    def __init__(self):
        self.api = TqApi()
        self.tm = TaskManager(self.api)

    def task_main(self):
        print("start")
        symbol = "SHFE.cu1805"
        kline_serial_5s = self.api.get_kline_serial(symbol, 5)
        kline_serial_1m = self.api.get_kline_serial(symbol, 60)
        while True:
            yield {
                "KLINE_DATA_UPDATED":
                lambda: self.api.is_changing(kline_serial_1m) or self.api.
                is_changing(kline_serial_5s),
            }
            # 计算最近3根5秒线均价
            average_price_15s = (kline_serial_5s[-1]["close"] +
                                 kline_serial_5s[-2]["close"] +
                                 kline_serial_5s[-3]["close"]) / 3
            # 计算最近30根1分钟线均价
            average_price_30m = sum(kline_serial_1m.close[-30:]) / 30
            # 如果条件符合
            print("average_price_15s", average_price_15s, "average_price_30m",
                  average_price_30m)
            if average_price_15s > average_price_30m:
                self.api.insert_order(symbol=symbol,
                                      direction="BUY",
                                      offset="OPEN",
                                      volume=1,
                                      limit_price=5000)
        print("finish")

    def run(self):
        self.tm.start_task(self.task_main())
        self.api.run()
Ejemplo n.º 5
0
class Y_Base:
    def __init__(self):
        self.api = TqApi()
        self.tm = TaskManager(self.api)        

    def task_main(self):
        """
        def test():
            max_volume = 1
            while True:
                wait_result = yield {
                    "BUY_OPEN": False,
                    "PRINT" : lambda: 1>0,
                    "SELL_CLOSE": False,
                }             
                
                if wait_result["BUY_OPEN"] :     
                    On_BUY(self, B_or_S, symbol, max_volume)                        
                            
                if wait_result["SELL_CLOSE"]:
                    #bors= "BUY" if self.b > price else "SELLL" 
                    On_CLOSE(self, B_or_S, symbol, max_volume)
                if wait_result["PRINT"]:  
                    pass
                
        """
        pass
            
    """
    def case_all(self, wait_result):
        if wait_result["BUY_OPEN"] :     
            self.On_BUY(B_or_S, symbol, max_volume)                        
                    
        if wait_result["SELL_CLOSE"]:
            #bors= "BUY" if self.b > price else "SELLL" 
            self.On_CLOSE(B_or_S, symbol, max_volume)
        if wait_result["PRINT"]:  
            pass       
    """ 
        
    def On_BUY(self, B_or_S, symbol, max_volume):
        #direction="BUY", offset="OPEN"
        if B_or_S == "BUY":
            task_a = self.tm.start_task(make_order_until_all_matched(self.api, symbol= symbol, direction="BUY", offset="OPEN", volume=max_volume))                                          
            #self.On_Open_Buy()     开多仓 
        else:
            task_b = self.tm.start_task(make_order_until_all_matched(self.api, symbol= symbol, direction="SELL", offset="OPEN", volume=max_volume))                             
            #self.On_Open_Sell()    开空仓 
        return task_a if   B_or_S == "BUY" else task_b 
        
    def On_CLOSE(self, B_or_S, symbol, max_volume): 
        """
        #direction="SELL", offset="CLOSE"
        BorS:
            "SELL"   多单持仓 (卖入平仓)
            "BUY"    空单持仓 (买入平仓) 
        """
        if B_or_S == "SELL":    
            """
                "SELL" 原持仓为多单,平仓   "BUY" 原持仓为空单, 平仓
            """
            task_a = self.tm.start_task(make_order_until_all_matched(self.api, symbol= symbol, direction="SELL", offset="CLOSE", volume=max_volume))                                                 
            #self.On_Close_Buy()                          
        else:
            #self.On_Close_Sell()    
            task_b = self.tm.start_task(make_order_until_all_matched(self.api, symbol= symbol, direction="BUY", offset="CLOSE", volume=max_volume))       
   
        return task_a if B_or_S == "SELL" else task_b 
    
    def max_min_ave_ks(self,symbol, m, n = 20):
        """
        Args:
            symbol (str): 指定合约代码.
                 m (int): K线数据周期,以秒为单位。
        Returns:
            Dict:{
                    min: n个数据的最小值
                    max: n个数据的最高值
                    ave: n个数据的平均值
                   }
                   
                   
        使用:
             def task_main(self):
                 quote_a = self.api.get_quote(self.symbol_a)  
                 dictk = self.max_min_ave_ks(self.symbol_a,60 * 60 * 24,20)     
                 while True:
                     wait_result = yield {
                         "BUY_OPEN": lambda: (long_volume == 0 and quote_a["ask_price1"] >= dictk['max']),                           
                           "PRINT" : lambda:  1>0,
                       "SELL_CLOSE": lambda: (long_volume > 0  and quote_a["ask_price1"] <= dictk['min']),
                                         }
                     if wait_result["PRINT"]:            
                         print(......)
                    
                     if wait_result["BUY_OPEN"] :                
                         print("开仓 ", quote_a["ask_price1"] )
                         task_a = self.On_BUY("BUY",self.symbol_a,max_volume)
                         
                     if wait_result["SELL_CLOSE"]:
                         print("平仓 ", quote_a["bid_price1"])
                         task_a = self.On_CLOSE("SELL",self.symbol_a,max_volume)                
                     ........   
        """
        kline_serial_1ks = self.api.get_kline_serial(self.symbol_a, m)
        return  { 'min' : min(kline_serial_1ks.low[-n:]),
                  'max' : max(kline_serial_1ks.high[-n:]),
                  'ave' : sum(kline_serial_1ks.close[-n:])/n,
                 }
    
    def run(self):                    
        self.tm.start_task(self.task_main())
        self.api.run()