CTP期货接口python写多策略多品种多周期案例源码

"""
关注公众号: Ctp接口量化
​
"""

from API import *
class MACDStrategy(Strategy):
    def __init__(self):
        super().__init__()
        # self.symbol_lsit = ["UR301","rb2301","au2212","IF2211","IC2211","i2301","eb2211","MA301"]  #订阅合约
        self.symbol_lsit = ["i2301"]  #订阅合约
        self.BarType = BarType.Min5  #K线周期
        self.StrategyType = StrategyType.Bar  #策略类型  StrategyType.Renko   StrategyType.Bar   StrategyType.Tick
    def on_tick(self, tick=None):
        print(tick.InstrumentID,tick.LastPrice)  
    def on_bar(self, tick=None, Bar=None):
        symbol = tick.InstrumentID   #合约代码
        Bid = tick.BidPrice1    #买价
        Ask = tick.AskPrice1    #卖价
        LastPrice = tick.LastPrice  #最新价
        #print(symbol) #合约
        #print(tick.LastPrice) #合约tick.UpdateTime
        # print(tick.UpdateTime) #合约tick.UpdateTime
        # print(Bar[0]["symbol"]) #合约
        kline = Bar[0]["data"]    # K 线数据
        if len(kline) <= 35:   # 小于35 条 退出 
            return   
        # K,D,J  = self.KDJ(kline) # 取KDJ指标数组
        # UP,MB,DN  = self.BOLL(kline) # 取BOLL指标数组
        # EMA  = self.EMA(kline,60) # 取EMA指标数组
        # RSI  = self.RSI(kline) # 取RSI指标数组
        # MA1  = self.MA(kline,30) # 取MA指标数组Channels
        # MA2  = self.MA(kline,60) # 取MA指标数组Channels
        dif,dea,macd  = self.MACD(kline) # 取MACD指标数组
        close,High,low = self.tick(kline)      # 取收盘价数组 # 获取最新价格(卖价)
        # print(self.Get_Position(symbol))
        Pos = self.GetPosition(symbol)
        # print(Pos)
        # 开多单
        if Pos["Direction"]=="None" and dif[-1]>dea[-1] and dif[-2] < dea[-2] and dea[-1] > 0:
            print(symbol) #合约
            print("MACD策略开多")
            最低价 = min(low[-10:])
            止盈 = Ask + (Ask-最低价)*3
            # self.send(symbol, DirectionType.Buy, OffsetType.Open, Ask, 3, OrderType.Limit)  # # OrderType.FOK   OrderType.FAK   OrderType.Market
            self.send(symbol, DirectionType.Buy, OffsetType.Open, Ask, 3, OrderType.Limit,止损=最低价,止盈=止盈,移动止损=最低价)  # # OrderType.FOK   OrderType.FAK   OrderType.Market
        # # 开空单
        if Pos["Direction"]=="None" and dif[-1]<dea[-1] and dif[-2] > dea[-2] and dea[-1] < 0:
            print(symbol) #合约
            print("MACD策略开空")
            最高价 = max(High[-10:])
            止盈 = Bid - (最高价-Bid)*3
            # self.send(symbol, DirectionType.Sell, OffsetType.Open, Bid, 3, OrderType.Limit)   # # OffsetType.Open   OffsetType.Close   OffsetType.CloseToday  OffsetType.CloseYesterday
            self.send(symbol, DirectionType.Sell, OffsetType.Open, Bid, 3, OrderType.Limit,止损=最高价,止盈=止盈,移动止损=最高价)   # # OffsetType.Open   OffsetType.Close   OffsetType.CloseToday  OffsetType.CloseYesterday
        # # 平多单
        if Pos["Direction"]==DirectionType.Buy and dif[-1]<dea[-1] and dif[-2] > dea[-2]:
            print(symbol) #合约
            print("MACD策略平多单")
            self.send(symbol, DirectionType.Sell, OffsetType.Close, Bid, Pos['Volume'], OrderType.Limit)         
        # # 平空单        
        if Pos["Direction"]==DirectionType.Sell and dif[-1]>dea[-1] and dif[-2] < dea[-2]:
            print(symbol) #合约
            print("MACD策略平空单")
            self.send(symbol, DirectionType.Buy, OffsetType.Close, Ask, Pos['Volume'], OrderType.Limit)
        if 'kwargs' in Pos.keys():
            # print(Pos["kwargs"])
            if Pos["Direction"]==DirectionType.Buy and LastPrice<Pos["kwargs"]["止损"]:
                print(symbol) #合约
                print("MACD策略止损多单")
                self.send(symbol, DirectionType.Sell, OffsetType.Close, Bid, Pos['Volume'], OrderType.Limit)         
            # # 平空单        
            if Pos["Direction"]==DirectionType.Sell and LastPrice>Pos["kwargs"]["止损"]:
                print(symbol) #合约
                print("MACD策略止损空单")
                self.send(symbol, DirectionType.Buy, 
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值