[原创] Python版商品期货跨期布林对冲策略 (教学)

之前写的跨期策略,是需要手动输入开仓对冲差价、平仓对冲差价的。对于差价判断比较主观。本次文章,我们一起把之前的对冲策略魔改成使用BOLL指标进行对冲开平仓操作的策略。

class Hedge:
    '对冲控制类'
    def __init__(self, q, e, initAccount, symbolA, symbolB, maPeriod, atrRatio, opAmount):
        self.q = q 
        self.initAccount = initAccount
        self.status = 0
        self.symbolA = symbolA
        self.symbolB = symbolB
        self.e = e
        self.isBusy = False 
        
        self.maPeriod = maPeriod
        self.atrRatio = atrRatio
        self.opAmount = opAmount
        self.records = []
        self.preBarTime = 0
        
    def poll(self):
        if (self.isBusy or not exchange.IO("status")) or not ext.IsTrading(self.symbolA):
            Sleep(1000)
            return 

        insDetailA = exchange.SetContractType(self.symbolA)
        if not insDetailA:
            return 

        recordsA = exchange.GetRecords()
        if not recordsA:
            return 

        insDetailB = exchange.SetContractType(self.symbolB)
        if not insDetailB:
            return 

        recordsB = exchange.GetRecords()
        if not recordsB:
            return 

        # 计算差价K线
        if recordsA[-1]["Time"] != recordsB[-1]["Time"]:
            return 

        minL = min(len(recordsA), len(recordsB))
        rA = recordsA.copy()
        rB = recordsB.copy()

        rA.reverse()
        rB.reverse()
        count = 0
        
        arrDiff = []
        for i in range(minL):
            arrDiff.append(rB[i]["Close"] - rA[i]["Close"])
        arrDiff.reverse()
        if len(arrDiff) < self.maPeriod:
            return 

        # 计算布林指标
        boll = TA.BOLL(arrDiff, self.maPeriod, self.atrRatio)

        ext.PlotLine("上轨", boll[0][-2], recordsA[-2]["Time"])
        ext.PlotLine("中轨", boll[1][-2], recordsA[-2]["Time"])
        ext.PlotLine("下轨", boll[2][-2], recordsA[-2]["Time"])
        ext.PlotLine("收盘价差价", arrDiff[-2], recordsA[-2]["Time"])

        LogStatus(_D(), "上轨:", boll[0][-1], "\n", "中轨:", boll[1][-1], "\n", "下轨:", boll[2][-1], "\n", "当前收盘差价:", arrDiff[-1])
        
        action = 0
        # 信号触发
        if self.status == 0:
            if arrDiff[-1] > boll[0][-1]:
                Log("开仓 A买B卖", ",A最新价格:", recordsA[-1]["Close"], ",B最新价格:", recordsB[-1]["Close"], "#FF0000")
                action = 2
                # 加入图表标记
                ext.PlotFlag(recordsA[-1]["Time"], "A买B卖", "正")
            elif arrDiff[-1] < boll[2][-1]:
                Log("开仓 A卖B买", ",A最新价格:", recordsA[-1]["Close"], ",B最新价格:", recordsB[-1]["Close"], "#FF0000")
                action = 1
                # 加入图表标记
                ext.PlotFlag(recordsA[-1]["Time"], "A卖B买", "反")
        elif self.status == 1 and arrDiff[-1] > boll[1][-1]:
            Log("平仓 A买B卖", ",A最新价格:", recordsA[-1]["Close"], ",B最新价格:", recordsB[-1]["Close"], "#FF0000")
            action = 2
            # 加入图表标记
            ext.PlotFlag(recordsA[-1]["Time"], "A买B卖", "反平")
        elif self.status == 2 and arrDiff[-1] < boll[1][-1]:
            Log("平仓 A卖B买", ",A最新价格:", recordsA[-1]["Close"], ",B最新价格:", recordsB[-1]["Close"], "#FF0000")
            action = 1 
            # 加入图表标记
            ext.PlotFlag(recordsA[-1]["Time"], "A卖B买", "正平")


        # 执行具体指令
        if action == 0:
            return 
        
        self.isBusy = True
        tasks = []
        if action == 1:
            tasks.append([self.symbolA, "sell" if self.status == 0 else "closebuy"])
            tasks.append([self.symbolB, "buy" if self.status == 0 else "closesell"])
        elif action == 2:
            tasks.append([self.symbolA, "buy" if self.status == 0 else "closesell"])
            tasks.append([self.symbolB, "sell" if self.status == 0 else "closebuy"])

        def callBack(task, ret):
            def callBack(task, ret):
                self.isBusy = False
                if task["action"] == "sell":
                    self.status = 2
                elif task["action"] == "buy":
                    self.status = 1
                else:
                    self.status = 0
                    account = _C(exchange.GetAccount)
                    LogProfit(account["Balance"] - self.initAccount["Balance"], account)
            self.q.pushTask(self.e, tasks[1][0], tasks[1][1], self.opAmount, callBack)

        self.q.pushTask(self.e, tasks[0][0], tasks[0][1], self.opAmount, callBack)



def main():
    SetErrorFilter("ready|login|timeout")
    Log("正在与交易服务器连接...")
    while not exchange.IO("status"):
        Sleep(1000)

    Log("与交易服务器连接成功")
    initAccount = _C(exchange.GetAccount)
    Log(initAccount)

    def callBack(task, ret):
        Log(task["desc"], "成功" if ret else "失败")

    q = ext.NewTaskQueue(callBack)
    p = ext.NewPositionManager()
    if CoverAll:
        Log("开始平掉所有残余仓位...")
        p.CoverAll()
        Log("操作完成")

    t = Hedge(q, exchange, initAccount, SA, SB, MAPeriod, ATRRatio, OpAmount)
    while True:
        q.poll()
        t.poll()

策略参数设置:

策略整体框架和Python版商品期货跨期对冲策略 (教学)基本一样,只是增加了对应的BOLL指标参数,策略运行时,获取两个合约的K线数据,然后计算差价,计算出一个差价数组,用作TA.BOLL函数的数据,计算布林线,当差价超过布林线上轨时正对冲,触碰下轨时反对冲。持仓时触碰布林中线平仓。

回测运行:

策略主要用于教学、学习参考。
完整策略:https://www.fmz.com/strategy/213826

免责声明:信息仅供参考,不构成投资及交易建议。投资者据此操作,风险自担。
如果觉得文章对你有用,请随意赞赏收藏
相关推荐
相关下载
登录后评论
Copyright © 2019 宽客在线