CTP接口 自定义K线周期 多品种止盈止损 MACD策略 源码

在这里插入图片描述
在这里插入图片描述

###############################################################################################
def MACD策略(pDepthMarketData):
    InstrumentID = pDepthMarketData.InstrumentID   #合约代码
    Bid = pDepthMarketData.BidPrice1    #买价
    Ask = pDepthMarketData.AskPrice1    #卖价
    LastPrice = pDepthMarketData.LastPrice  #最新价
    
    数据 = Get_data(InstrumentID)  #读取 合约 内存K线 数据
    print("K线个数",len(数据[0]["data"]))  #读取K线 数据
    交易合约 = 数据[0]["coce"]      #读取 合约
    print("合约",交易合约)
    if len(数据[0]["data"]) <= 35:   # 小于35 条 退出 
        return                    # 退出 
        # 行情 = Get_kline(交易合约,'5m')  # 读取 备用数据
    else:
         行情 = 数据[0]["data"]      # 大于35 条 将数据赋值给   行情   这个变量
         # print("K线个数",len(数据[0]["data"]))
         # print("MACD策略监测",)
    指标 = TAInstance()             # 创建 指标库 对象

    # # KDJ = 指标.KDJ(行情) # 取KDJ指标数组
    # # K = KDJ[0]         # 获取K的值,返回一个数组
    # # D = KDJ[1]         # 获取D的值,返回一个数组
    # # J = KDJ[2]         # 获取J的值,返回一个数组
    MACD = 指标.MACD(行情) # 取MACD指标数组
    dif = MACD[0]        # 获取dif的值,返回一个数组
    dea = MACD[1]        # 获取dea的值,返回一个数组
    macd = MACD[2]       # 获取macd的值,返回一个数组
    close,High,low = 指标.tick(行情)      # 取收盘价数组 # 获取最新价格(卖价)
    # print("K线收盘价",close[-1])     # 取最新K线 收盘价
    # print("dif",dif[-10:])        # 显示10个 dif 元素
    # print("dea",dea[-10:])         # 显示10个 dea 元素
    # print("macd",macd[-10:])        # 显示10个 macd 元素
    Posion = GetPosition(交易合约)   # 通过  GetPosition() 函数 查询持仓情况 
    # 开多单
    if Posion["手数"] == 0 and dif[-1]>0 and dif[-2]<0 or Posion["手数"] == 0 and dif[-1]>dea[-1] and dif[-2] < dea[-2] and dea[-1] > 0:
        print("MACD策略开多Buy")
        最低价 = min(low[-10:])
        最高价 = max(High[-10:])
        Buy(交易合约, Ask, 1)      # 合约, 价格, 手数,
        买开(交易合约, Ask, 1, 最低价, Ask+(Ask-最低价)*3)      # 合约, 价格, 手数, 止损=None, 止盈=None   本地设置 止损 止盈 等用函数 
    
    # # 开空单
    if Posion["手数"] == 0 and dif[-1]<0 and dif[-2]>0 or Posion["手数"] == 0 and dif[-1]<dea[-1] and dif[-2] > dea[-2] and dea[-1] < 0:
        print("MACD策略开空Short")
        最低价 = min(low[-10:])
        最高价 = max(High[-10:])
        Short(交易合约, Bid,1)   # 合约, 价格, 手数
        卖开(交易合约, Bid, 1, 最高价, Bid-(最高价-Bid)*3)   # 合约, 价格, 手数, 止损=None, 止盈=None   本地设置 止损 止盈 等用函数 
    
    # # 平多单
    if Posion["方向"]=='Buy' and LastPrice < Posion["止损"] and Posion["止损"] != 0 or Posion["方向"]=='Buy' and LastPrice > Posion["止盈"] and Posion["止盈"] != 0 or Posion["方向"]=='Buy' and LastPrice > Posion["价格"] and dif[-1]<dea[-1] and dif[-2] > dea[-2]:   #Posion["方向"]=='Buy' and dif[-1]<dea[-1] and dif[-2] > dea[-2]
        print("MACD策略平多Sell")
        Sell(交易合约,Bid, 1)     # 合约, 价格, 手数
        买平(交易合约)
    
    # # 平空单
    if Posion["方向"]=='Sell' and LastPrice > Posion["止损"] and Posion["止损"] != 0 or Posion["方向"]=='Sell' and LastPrice < Posion["止盈"] and Posion["止盈"] != 0 or Posion["方向"]=='Sell' and LastPrice < Posion["价格"] and dif[-1]>dea[-1] and dif[-2] < dea[-2]: #Posion["方向"]=='Sell' and dif[-1]>dea[-1] and dif[-2] < dea[-2] 
        print("MACD策略平空Cover")
        Cover(交易合约,Ask, 1)    # 合约, 价格, 手数
        卖平(交易合约)
###############################################################################################

更多内容关注公众号:Ctp接口量化

猜你喜欢

转载自blog.csdn.net/qq_20575249/article/details/110739725