做空白糖波动率策略范例Short Sugar Vola ,博易量化
# coding:utf-8
#!/usr/bin/env python
from PoboAPI import *
import datetime
#用poboquant python实现,在poboquant上运行,请关注A期客网www.a-qike.com ,更多程序化源码代下载。
#做空白糖期权波动率策略,卖宽跨式,比如你认为白糖价格在较长时间里会在5000~6000区间运行,而不会向上或向下突破
#short sugar price volatility, like you believe that sugar will range move in 5000 to 6000,no break out in either way
#开始时间,用于初始化一些参数
def OnStart(context) :
print "system starting..."
#设定一个全局变量品种
g.code1 = "SR901C6000.CZCE"
g.code2 = "SR901P5000.CZCE"
#订阅实时数据,用于驱动OnQuote事件
SubscribeQuote([g.code1,g.code2])
#订阅K线数据,用于驱动OnBar事件
SubscribeBar(g.code1, BarType.Day)
SubscribeBar(g.code2, BarType.Day)
#登录交易账号,需在主页用户管理中设置账号,并把证券测试替换成您的账户名称
context.myacc = None
if context.accounts.has_key("回测期货") :
print "登录交易账号[回测期货]"
if context.accounts["回测期货"].Login() :
context.myacc = context.accounts["回测期货"]
#实时行情事件,当有新行情出现时调用该事件
def OnQuote(context, code) :
#过滤掉不需要的行情通知
# if code != g.code1 :
# return
# if code != g.code2 :
# return
#获取最新行情
dyndata1 = GetQuote(g.code1)
dyndata2 = GetQuote(g.code2)
if dyndata1 and dyndata2 :
#.now指最新价,详细属性见API文档
now1 = dyndata1.now
now2 = dyndata2.now
#打印最新价
log.info("SR901C6000最新价1: " + str(dyndata1.now))
log.info("SR901P5000最新价2: " + str(dyndata2.now))
#获取K线数据
klinedata1 = GetHisData(g.code1, BarType.Day)
klinedata2 = GetHisData(g.code2, BarType.Day)
#打印K线数据,如最新一根K线的收盘价
if len(klinedata1) > 0 and len(klinedata2) > 0 :
lastsum = klinedata1[-1].close + klinedata2[-1].close #期权价格之和
log.info("最新价格和: " + str(lastsum))
bal = context.myacc.AccountBalance
posmargin=bal.FrozenMargin
pos = context.myacc.GetPositions()
poslength=len(pos)
print "持仓合约数: "+str(poslength)
#如果配置好交易账号了,可以根据条件下单,需把下面中的证券测试账号换成您设置的账号名称
if len(klinedata1) > 1 and len(klinedata2) > 1 and lastsum>200 and context.myacc and posmargin<100000 :
# 两个期权价格之和大于200就同时卖出
print "buy open the spread "+str(lastsum)
context.myacc.InsertOrder(g.code1, BSType.SellOpen, dyndata1.now, 10)
context.myacc.InsertOrder(g.code2, BSType.SellOpen, dyndata2.now, 10)
if len(klinedata1) > 1 and len(klinedata2) > 1 and lastsum<50 and poslength>0 and context.myacc :
# 两个期权价格之和小于50就同时买平
print "sell close the spread,take profit "+str(lastsum)
context.myacc.InsertOrder(g.code1, BSType.BuyClose, dyndata1.now, 10)
context.myacc.InsertOrder(g.code2, BSType.BuyClose, dyndata2.now, 10)
if len(klinedata1) > 1 and len(klinedata2) > 1 and lastsum>250 and poslength>0 and context.myacc :
# 两个期权价格之和大于250就同时买平,止损
print "sell close the spread,cut loss "+str(lastsum)
context.myacc.InsertOrder(g.code1, BSType.BuyClose, dyndata1.now, 10)
context.myacc.InsertOrder(g.code2, BSType.BuyClose, dyndata2.now, 10)
#委托回报事件,当有委托回报时调用
def OnOrderChange(context, AccountName, order) :
#打印委托信息,id是编号,volume是数量,详细见API文档
print "委托编号: " + order.id + " 账号名称: " + AccountName
print "Vol: " + str(order.volume) + " Price: " + str(order.price)
Click to rate this post!