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

商品期货跨品种套利

Author: 雨幕(youquant), Date: 2022-10-26 14:05:09
Tags:

相关文章:https://www.youquant.com/digest-topic/9423


'''backtest
start: 2022-09-01 09:00:00
end: 2022-09-09 15:00:00
period: 1m
basePeriod: 1m
exchanges: [{"eid":"Futures_CTP","currency":"FUTURES"}]
mode: 1
'''

p = ext.NewPositionManager()
def onTick():
    global maxDiff, minDiff
    # 获取合约A行情数据
    infoA = exchange.SetContractType(symbolA)
    tickA = exchange.GetTicker()
    recordsA = exchange.GetRecords()
    
    # 获取合约B行情数据
    infoB = exchange.SetContractType(symbolB)
    tickB = exchange.GetTicker()
    recordsB = exchange.GetRecords()
    
    if not tickA or not tickB or not recordsA or not recordsB:
        return
    
    # 分析持仓
    pos = exchange.GetPosition()
    if pos is None:
        return 
    longPosOfSymbolA = p.GetPosition(symbolA, PD_LONG)
    shortPosOfSymbolA = p.GetPosition(symbolA, PD_SHORT)
    longPosOfSymbolB = p.GetPosition(symbolB, PD_LONG)
    shortPosOfSymbolB = p.GetPosition(symbolB, PD_SHORT)
    
    # 计算价差
    diff = tickA["Last"] - tickB["Last"]
    
    if not longPosOfSymbolA and not shortPosOfSymbolA and not longPosOfSymbolB and not shortPosOfSymbolB:
        if diff > maxDiff:
            # 空A合约,多B合约
            Log("空A合约:", symbolA, ",多B合约:", symbolB, ", diff:", diff, ", maxDiff:", maxDiff, "#FF0000")
            p.OpenShort(symbolA, 1)
            p.OpenLong(symbolB, 1)
        elif diff < minDiff:
            # 多A合约,空B合约
            Log("多A合约:", symbolA, ",空B合约:", symbolB, ", diff:", diff, ", minDiff:", minDiff, "#FF0000")
            p.OpenLong(symbolA, 1)
            p.OpenShort(symbolB, 1)
    
    if longPosOfSymbolA and shortPosOfSymbolB and not shortPosOfSymbolA and not longPosOfSymbolB:
        # 持有A多头、B空头
        if diff > (longPosOfSymbolA["Price"] - shortPosOfSymbolB["Price"]) + stopProfit:
            # 止盈
            Log("持有A多头、B空头,止盈。", "diff:", diff, "持有差价:", (longPosOfSymbolA["Price"] - shortPosOfSymbolB["Price"]))
            p.Cover(symbolA)
            p.Cover(symbolB)
        elif diff < (longPosOfSymbolA["Price"] - shortPosOfSymbolB["Price"]) - stopLoss:
            # 止损
            Log("持有A多头、B空头,止损。", "diff:", diff, "持有差价:", (longPosOfSymbolA["Price"] - shortPosOfSymbolB["Price"]))
            p.Cover(symbolA)
            p.Cover(symbolB)
    elif shortPosOfSymbolA and longPosOfSymbolB and not longPosOfSymbolA and not shortPosOfSymbolB:
        # 持有A空头、B多头
        if diff < (shortPosOfSymbolA["Price"] - longPosOfSymbolB["Price"]) - stopProfit:
            # 止盈
            Log("持有A空头、B多头,止盈。", "diff:", diff, "持有差价:", (shortPosOfSymbolA["Price"] - longPosOfSymbolB["Price"]))
            p.Cover(symbolA)
            p.Cover(symbolB)
        elif diff > (shortPosOfSymbolA["Price"] - longPosOfSymbolB["Price"]) + stopLoss:
            # 止损
            Log("持有A空头、B多头,止损。", "diff:", diff, "持有差价:", (shortPosOfSymbolA["Price"] - longPosOfSymbolB["Price"]))
            p.Cover(symbolA)
            p.Cover(symbolB)
    
    
    # 画图
    ext.PlotLine("差价", diff)

def main():
    while True:
        if exchange.IO("status"):
            onTick()
            LogStatus(_D(), "已连接")
        else:
            LogStatus(_D(), "未连接")
        Sleep(500)



更多内容