资源加载中... loading...

商品期货跨品种套利

Author: yuzy, Date: 2022-11-22 15:49:21
Tags:

跨品种对冲套利策略 指利用两种不同但相关性较强的商品期货或者同产业链商品之间进行套利,根据均值回归来抓取两者短期内不合理的价差(或者价比), 同时买入等量的低估合约和卖出等量的高估合约,当两者的价差(或者价比)收缩或扩大至一定程度时,同时平仓了结,盈亏相抵后,获取了额外收益。

策略逻辑 此次我们选择关联性较强的焦炭和焦煤2个期货品种。当两者价差出现了不合理变动时,就入场进行对冲套利。 价差 = 焦炭价格 - 焦煤价格

开仓逻辑:
当价差大于价差上限时,就做空焦炭,做多焦煤,进行正套;
当价差小于价差下限时 , 就做多焦炭,做空焦煤,进行反套。

止盈止损逻辑:
正套:当实时价差小于建仓价差一个止盈距离,平仓止盈。
正套:当实时价差大于建仓价差一个止损距离,平仓止损。
反套:当实时价差大于建仓价差一个止盈距离,平仓止盈。
反套:当实时价差小于建仓价差一个止损距离,平仓止损。

'''backtest
start: 2022-10-01 00:00:00
end: 2022-11-30 23:59:00
period: 1d
basePeriod: 1h
exchanges: [{"eid":"Futures_CTP","currency":"FUTURES"}]
'''


p = ext.NewPositionManager()
def onTick():
    _C(exchange.SetContractType,symbolA)
    tickA = _C(exchange.GetTicker)
    _C(exchange.SetContractType,symbolB)
    tickB = _C(exchange.GetTicker)
    pos = exchange.GetPosition()
    if pos is None:
        return
    LPsymbolA = p.GetPosition(symbolA,PD_LONG)
    SPsymbolA = p.GetPosition(symbolA,PD_SHORT)
    LPsymbolB = p.GetPosition(symbolB,PD_LONG)
    SPsymbolB = p.GetPosition(symbolB,PD_SHORT)
    diff = tickA["Last"] - tickB["Last"]
    if not LPsymbolA and not SPsymbolA and not LPsymbolB and not SPsymbolB :
        if diff > maxDiff :
            Log("空A合约:",symbolA," 多B合约:",symbolB," diff:",diff," maxDiff:",maxDiff,"#FF0000")
            p.OpenShort(symbolA , 1)
            p.OpenLong(symbolB , 1)
        elif diff < minDiff :
            Log("多A合约:",symbolA," 空B合约:",symbolB," diff:",diff," minDiff:",minDiff,"#FF0000")
            p.OpenLong(symbolA , 1)
            p.OpenShort(symbolB , 1)
    if LPsymbolA and SPsymbolB and not SPsymbolA and not LPsymbolB :
        if diff > (LPsymbolA["Price"] - SPsymbolB["Price"]) + stopProfit :
            Log("持有A多头,B空头,止盈。"," diff:",diff," 持有价差:",(LPsymbolA["Price"] - SPsymbolB["Price"]))
            p.Cover(symbolA)
            p.Cover(symbolB)
        elif diff < (LPsymbolA["Price"] - SPsymbolB["Price"]) - stopLoss :
            Log("持有A多头,B空头,止损。"," diff:",diff," 持有价差:",(LPsymbolA["Price"] - SPsymbolB["Price"]))
            p.Cover(symbolA)
            p.Cover(symbolB)
    elif SPsymbolA and LPsymbolB and not LPsymbolA and not SPsymbolB :
        if diff < (SPsymbolA["Price"] - LPsymbolB["Price"]) - stopProfit :
            Log("持有A空头,B多头,止盈。"," diff:",diff," 持有价差:",(SPsymbolA["Price"] - LPsymbolB["Price"]))
            p.Cover(symbolA)
            p.Cover(symbolB)
        elif diff > (SPsymbolA["Price"] - LPsymbolB["Price"]) + stopLoss :
            Log("持有A空头,B多头,止损。"," diff:",diff," 持有价差:",(SPsymbolA["Price"] - LPsymbolB["Price"]))
            p.Cover(symbolA)
            p.Cover(symbolB)
    ext.PlotLine("价差:",diff)




def main():
    while True:
        if exchange.IO("status"):
            onTick()
        else:
            LogStatus(_D(),"未连接")
        Sleep(1000)
template: strategy.tpl:40:21: executing "strategy.tpl" at <.api.GetStrategyListByName>: wrong number of args for GetStrategyListByName: want 7 got 6