天天看點

用ctp接口實作磚型圖政策交易

#磚型圖資料用畫圖工具顯示出來的效果

用ctp接口實作磚型圖政策交易

#CTP接口接收行情後合成的資料源

用ctp接口實作磚型圖政策交易

#自動化交易政策源碼

from CTP import *
class 磚型圖政策(Strategy):
    def __init__(self):
        super().__init__()
        self.symbol_lsit = ["IC2212","au2302","i2301","ni2301","IF2212"]  #訂閱合約
        # self.symbol_lsit = ["ni2301"]  #訂閱合約
        self.Renko_Tick = 10  #磚型幅度 合約最小變動價倍數  
        self.StrategyType = StrategyType.Renko  #政策類型  StrategyType.Renko   StrategyType.Bar   StrategyType.Tick
    def on_bar(self, tick=None, Bar=None):
        symbol = tick.InstrumentID   #合約代碼
        Bid = tick.BidPrice1    #買價
        Ask = tick.AskPrice1    #賣價
        LastPrice = tick.LastPrice  #最新價
        Pos = self.Get_Position(symbol) # 擷取全部持倉
        #print(Pos)
        kline = Bar[0]["data"]    # K 線資料
        if len(kline) < 2:   # 小于2條 退出 
            return 
        print(symbol,kline[-1])   
        if Pos["Direction"]=="None" and kline[-1]["Renko"]=="▲" and kline[-2]["Renko"]=="▲": # 如果是空倉 連續兩個上漲磚就開多單
            self.send(symbol, DirectionType.Buy, OffsetType.Open, Ask, 3, OrderType.Limit)    #開多  # # OrderType.FOK   OrderType.FAK   OrderType.Market
        if Pos["Direction"]=="None" and kline[-1]["Renko"]=="▽" and kline[-2]["Renko"]=="▽":  # 如果是空倉 連續兩個下跌磚就開空單
            self.send(symbol, DirectionType.Sell, OffsetType.Open, Bid, 3, OrderType.Limit) # 開空
        if Pos["Direction"]=='Long' and kline[-1]["Renko"]=="▽" and kline[-2]["Renko"]=="▲":     # 如果有多單 最新磚下跌 上一磚是上漲 平多單開空單
            print("磚型圖政策平多單")
            if Pos["ExchangeID"]=='SHFE' and Pos["Volume"]==Pos["yd_amount"]: # 如果是上期所 且是昨倉 用平昨 平多單
                self.send(symbol, DirectionType.Sell, OffsetType.CloseYesterday, Bid, Pos['Volume'], OrderType.Limit) #用平昨
                self.send(symbol, DirectionType.Sell, OffsetType.Open, Bid, 3, OrderType.Limit) #開空                
            elif Pos["ExchangeID"]=='SHFE' and Pos["Volume"]==Pos["td_amount"]: # 如果是上期所 且是今倉 用平今 平多單
                self.send(symbol, DirectionType.Sell, OffsetType.CloseToday, Bid, Pos['Volume'], OrderType.Limit) 用平今
                self.send(symbol, DirectionType.Sell, OffsetType.Open, Bid, 3, OrderType.Limit) #開空                
            else:
                self.send(symbol, DirectionType.Sell, OffsetType.Close, Bid, Pos['Volume'], OrderType.Limit) #用普通平倉
                self.send(symbol, DirectionType.Sell, OffsetType.Open, Bid, 3, OrderType.Limit) #開空                
        if Pos["Direction"]=='Short' and kline[-1]["Renko"]=="▲" and kline[-2]["Renko"]=="▽":    # 如果有空單 最新磚上漲 上一磚是下跌 平空單開多單
            print("磚型圖政策平空單")
            if Pos["ExchangeID"]=='SHFE' and Pos["Volume"]==Pos["yd_amount"]: # 如果是上期所 且是昨倉 用平昨 平空單
                self.send(symbol, DirectionType.Buy, OffsetType.CloseYesterday, Ask, Pos['Volume'], OrderType.Limit) #用平昨
                self.send(symbol, DirectionType.Buy, OffsetType.Open, Ask, 3, OrderType.Limit) #開多                
            elif Pos["ExchangeID"]=='SHFE' and Pos["Volume"]==Pos["td_amount"]: # 如果是上期所 且是今倉 用平今 平空單
                self.send(symbol, DirectionType.Buy, OffsetType.CloseToday, Ask, Pos['Volume'], OrderType.Limit) #用平今
                self.send(symbol, DirectionType.Buy, OffsetType.Open, Ask, 3, OrderType.Limit) #開多                
            else:
                self.send(symbol, DirectionType.Buy, OffsetType.Close, Ask, Pos['Volume'], OrderType.Limit) #用普通平倉
                self.send(symbol, DirectionType.Buy, OffsetType.Open, Ask, 3, OrderType.Limit) #開多                

Config = {'brokerid':'9999', 'userid':'123456', 'password':'*******', 'appid':'simnow_client_test', 'auth_code':'0000000000000000', 'product_info':'python dll', 'td_address':'tcp://180.168.146.187:10130', 'md_address':'tcp://180.168.146.187:10131'}
if __name__ == '__main__':    
    t = CtpGateway()
    t.add_Strategy(磚型圖政策())
    t.add_Config(Config)
    t.Start()