1 Star 0 Fork 0

adam01/poboquant

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
国债期货套利Bond Fut Spread Trade 3.21 KB
一键复制 编辑 原始数据 按行查看 历史
# coding:utf-8
#!/usr/bin/env python
from PoboAPI import *
import datetime
import numpy as np
#用poboquant python实现,在poboquant上运行,如果有问题 可加群 726895887 咨询
#债券期货价差策略,10年期国债与5年期国债价差 spread trade on 10y and 5y bond futures
def OnStart(context) :
print "I\'m starting..."
context.myacc = None
if context.accounts.has_key("回测期货"):
print "登录交易账号[回测期货]"
if context.accounts["回测期货"].Login() :
context.myacc = context.accounts["回测期货"]
SubscribeQuote(["T1812.CFFEX","TF1812.CFFEX"])
g.code1="T1812.CFFEX"
g.code2="TF1812.CFFEX"
def OnOrderChange(context, AccountName, order) :
Test = context.accounts["回测期货"].GetOrder(order.id)
print str(Test.volume)
def OnQuote(context,code) :
option=PBObj()
#option.buysellflag='0'
pos = context.accounts["回测期货"].GetPositions()
#print "positions ",pos
#print "len(pos) ",len(pos)
leg1 = GetQuote(g.code1)
leg2 = GetQuote(g.code2)
leg1now=leg1.now
leg2now=leg2.now
spreadnow=leg1now-leg2now
print "spread "+str(spreadnow) # 最新价差
if spreadnow<=0.5 and len(pos)==0 : #10债和5债价差小于0.5就买10债卖5债
print "to buy Spread....要开仓"
context.myacc.InsertOrder(g.code1, BSType.BuyOpen, leg1now, 10)
context.myacc.InsertOrder(g.code2, BSType.SellOpen, leg2now, 10)
print "positions bought"
bal=context.accounts["回测期货"].AccountBalance.AssetsBalance #返回成交后账户权益
print "账户金额 :"+str(bal)
#orders = context.accounts["回测期货"].GetOrder(order.id)
#print str(orders.volume)
#OnOrderChange(context, "回测期货", order)
TradeDetails = context.accounts["回测期货"].GetTradeDetails()
print TradeDetails
#print "len(pos) in trade ",len(pos)
pos = context.accounts["回测期货"].GetPositions()
print "len(pos) str in trade "+str(len(pos))
if spreadnow>1 and len(pos)>0: #有持仓且最新价差大于1就卖出止盈
print "to sell Spread..."
context.myacc.InsertOrder(g.code1, BSType.SellClose, leg1now, 10)
context.myacc.InsertOrder(g.code2, BSType.BuyClose, leg2now, 10)
print "positions sold,已经获利平仓"
bal=context.accounts["回测期货"].AccountBalance.AssetsBalance #返回成交后账户权益
print "账户金额 :"+str(bal)
pos = context.accounts["回测期货"].GetPositions()
print "len(pos) str in trade "+str(len(pos))
if spreadnow<=0.3 and len(pos)>0: #有持仓且最新价差小于0.3就卖出止损
print "to sell Spread..."
context.myacc.InsertOrder(g.code1, BSType.SellClose, leg1now, 10)
context.myacc.InsertOrder(g.code2, BSType.BuyClose, leg2now, 10)
print "positions sold 已经止损"
bal=context.accounts["回测期货"].AccountBalance.AssetsBalance #返回成交后账户权益
print "账户金额 :"+str(bal)
pos = context.accounts["回测期货"].GetPositions()
print "len(pos) str in trade "+str(len(pos))
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/adam01/poboquant.git
git@gitee.com:adam01/poboquant.git
adam01
poboquant
poboquant
master

搜索帮助