- 策略广场
- 跨期布林线对冲
跨期布林线对冲
Author:
yuzy, Date: 2022-11-17 11:35:32
Tags:
'''backtest
start: 2022-01-01 09:00:00
end: 2022-11-17 15:00:00
period: 1d
basePeriod: 1h
exchanges: [{"eid":"Futures_CTP","currency":"FUTURES"}]
'''
#商品期货跨品种套利研究策略
p = ext.NewPositionManager()
def onTick():
infoA = exchange.SetContractType(symbolA)
recordsA = exchange.GetRecords()
infoB = exchange.SetContractType(symbolB)
recordsB = exchange.GetRecords()
if not recordsA or not recordsB:
return
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)
# 计算差价K线
#判断如果两个合约的倒数第一根K线的时间不一致,就返回
#主要是为了确保后边用于计算两个合约价差准确,两者K线数据的时间必须要同步。
if recordsA[-1]["Time"] != recordsB[-1]["Time"]:
return
##因为两个合约的挂牌时间不一致,所以获取的K线数量会不一致,获取两者最小的长度
minL = min(len(recordsA), len(recordsB))
rA = recordsA.copy() #复制合约A的K线数据列表
rB = recordsB.copy()
rA.reverse() #对列表的元素进行反向排序,表示越是最新K线越靠前。
rB.reverse()
arrDiff = [] #定义价差空数组
for i in range(minL):#通过遍历,计算两者每根K线的收盘价差,并且在arrDiff列表末尾添加新的对象
arrDiff.append(rB[i]["Close"] - rA[i]["Close"])
arrDiff.reverse() #arrDiff列表反向排序
if len(arrDiff) < N:
return
# 计算布林指标
boll = TA.BOLL(arrDiff, N, P)
#调用画线类库的画BOLL指标线函数,画布林线指标图。(标签、数据、时间)
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])
if not LPsymbolA and not SPsymbolA and not LPsymbolB and not SPsymbolB:#首先无持仓情况下
if arrDiff[-1] > boll[0][-1]: #如果当前收盘价价差触及上轨,A买入开多,B卖出开空
Log("开仓 A买B卖", ",A最新价格:", recordsA[-1]["Close"], ",B最新价格:", recordsB[-1]["Close"], "#FF0000")
p.OpenLong(symbolA,1)
p.OpenShort(symbolB,1)
ext.PlotFlag(recordsA[-1]["Time"], "A买B卖", "正") #同时在boll指标图表画标记
elif arrDiff[-1] < boll[2][-1]: #如果当前收盘价触及下轨,A卖出开空,B买入开多
Log("开仓 A卖B买", ",A最新价格:", recordsA[-1]["Close"], ",B最新价格:", recordsB[-1]["Close"], "#FF0000")
p.OpenShort(symbolA,1)
p.OpenLong(symbolB,1)
ext.PlotFlag(recordsA[-1]["Time"], "A卖B买", "反")
#当持有A多头,B空头,且当前收盘价价差触及中轨,则A卖出平多,B买入平空
elif LPsymbolA and SPsymbolB and not SPsymbolA and not LPsymbolB and arrDiff[-1] < boll[1][-1]:
Log("平仓 A卖B买", ",A最新价格:", recordsA[-1]["Close"], ",B最新价格:", recordsB[-1]["Close"], "#FF0000")
p.Cover(symbolA)
p.Cover(symbolB)
ext.PlotFlag(recordsA[-1]["Time"], "A卖B买", "正平")
#当持有A空头,B多头,且当前收盘价价差触及中轨,则A买入平空,B卖出平多
elif SPsymbolA and LPsymbolB and not LPsymbolA and not SPsymbolB and arrDiff[-1] > boll[1][-1]:
Log("平仓 A买B卖", ",A最新价格:", recordsA[-1]["Close"], ",B最新价格:", recordsB[-1]["Close"], "#FF0000")
p.Cover(symbolA)
p.Cover(symbolB)
ext.PlotFlag(recordsA[-1]["Time"], "A买B卖", "反平")
def main():
while True:
if exchange.IO("status"):
onTick()
else:
LogStatus(_D(),"未连接")
Sleep(500)
template: strategy.tpl:40:21: executing "strategy.tpl" at <.api.GetStrategyListByName>: wrong number of args for GetStrategyListByName: want 7 got 6