请 [注册] 或 [登录]  | 返回主站

量化交易吧 /  量化平台 帖子:3353388 新帖:34

50ETF期权宽跨策略

汇市江湖百晓生发表于:5 月 10 日 07:11回复(1)

from PoboAPI import *
import datetime

做空50ETF期权波动率策略,卖宽跨式,比如你认为50ETF期权价格在较长时间里会在2~3区间运行,而不会向上或向下突破

short sugar price volatility, like you believe that sugar will range move in 2to 3,no break out in either way

开始时间,用于初始化一些参数

def OnStart(context) :
   g.a=0
   print "system starting..."

#设定一个全局变量品种g.code1 = "10001654.SHSE"g.code2 = "10001659.SHSE"#订阅实时数据,用于驱动OnQuote事件SubscribeQuote(g.code1)#订阅K线数据,用于驱动OnBar事件SubscribeBar(g.code1, BarType.Min)#登录交易账号,需在主页用户管理中设置账号,并把证券测试替换成您的账户名称context.myacc = Noneif context.accounts.has_key("章鱼哥") :print "登录交易账号[章鱼哥]"if context.accounts["章鱼哥"].Login() :
        context.myacc = context.accounts["章鱼哥"]

def TestSetFeeRate(context):
   if not context.myacc:
     return

fee = PBObj();
fee.OpenUnit = 2
fee.OpenRate = 0

fee.CloseUnit = 2
fee.CloseRate = 0    

fee.CloseTodayUnit = 2
fee.CloseTodayRate = 0    

fee.MiniFee = 0
context.myacc.SetFee(g.code1, fee) 
context.myacc.SetFee(g.code2, fee)

实时行情事件,当有新行情出现时调用该事件

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("1584最新价1: "   str(dyndata1.now))
    log.info("1628最新价2: "   str(dyndata2.now))#获取K线数据klinedata1 = GetHisData(g.code1, BarType.Min)
klinedata2 = GetHisData(g.code2, BarType.Min)#设置指定手续费率#SetFee(code, value)TestSetFeeRate(context)#打印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)

TestSetFeeRate(context)

#如果配置好交易账号了,可以根据条件下单,需把下面中的证券测试账号换成您设置的账号名称if len(klinedata1) > 1 and len(klinedata2) > 1 and lastsum>0.045 and   g.a ==0 and context.myacc and posmargin<100000 :# 两个期权价格之和大于200就同时卖出print "buy open the spread " str(lastsum)
    context.myacc.InsertOrder(g.code1, BSType.SellOpen, dyndata1.now, 3)
    context.myacc.InsertOrder(g.code2, BSType.SellOpen, dyndata2.now, 3)
    g.a =1if len(klinedata1) > 1 and len(klinedata2) > 1 and lastsum<0.0055 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, 3)
    context.myacc.InsertOrder(g.code2, BSType.BuyClose, dyndata2.now, 3)if len(klinedata1) > 1 and len(klinedata2) > 1 and lastsum>0.0950 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, 1)
    context.myacc.InsertOrder(g.code2, BSType.BuyClose, dyndata2.now, 1)

委托回报事件,当有委托回报时调用

def OnOrderChange(context, AccountName, order) :

#打印委托信息,id是编号,volume是数量,详细见API文档print "委托编号: "   order.id   "   账号名称: "   AccountNameprint "Vol: "   str(order.volume)   " Price: "   str(order.price)
 # coding:utf-8#!/usr/bin/env pythonfrom PoboAPI import *import datetime#做空50ETF期权波动率策略,卖宽跨式,比如你认为50ETF期权价格在较长时间里会在2~3区间运行,而不会向上或向下突破#short sugar price volatility, like you believe that sugar will range move in 2to 3,no break out in either way#开始时间,用于初始化一些参数
 def OnStart(context) :g.a=0print "system starting..."#设定一个全局变量品种g.code1 = "10001654.SHSE"g.code2 = "10001659.SHSE"#订阅实时数据,用于驱动OnQuote事件SubscribeQuote(g.code1)#订阅K线数据,用于驱动OnBar事件SubscribeBar(g.code1, BarType.Min)


     #登录交易账号,需在主页用户管理中设置账号,并把证券测试替换成您的账户名称context.myacc = Noneif context.accounts.has_key("章鱼哥") :print "登录交易账号[章鱼哥]"if context.accounts["章鱼哥"].Login() :context.myacc = context.accounts["章鱼哥"]
  
  def TestSetFeeRate(context):if not context.myacc:  returnfee = PBObj();fee.OpenUnit = 2fee.OpenRate = 0fee.CloseUnit = 2fee.CloseRate = 0    fee.CloseTodayUnit = 2fee.CloseTodayRate = 0    fee.MiniFee = 0context.myacc.SetFee(g.code1, fee) context.myacc.SetFee(g.code2, fee)#实时行情事件,当有新行情出现时调用该事件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.nownow2 = dyndata2.now#打印最新价log.info("1584最新价1: " + str(dyndata1.now))log.info("1628最新价2: " + str(dyndata2.now))#获取K线数据klinedata1 = GetHisData(g.code1, BarType.Min)klinedata2 = GetHisData(g.code2, BarType.Min)#设置指定手续费率#SetFee(code, value)TestSetFeeRate(context)#打印K线数据,如最新一根K线的收盘价if len(klinedata1) > 0 and len(klinedata2) > 0 :lastsum = klinedata1[-1].close + klinedata2[-1].close #期权价格之和log.info("最新价格和: "  + str(lastsum))bal = context.myacc.AccountBalanceposmargin=bal.FrozenMarginpos = context.myacc.GetPositions()    
    poslength=len(pos)print "持仓合约数: "+str(poslength)
   
   # TestSetFeeRate(context)
  
  #如果配置好交易账号了,可以根据条件下单,需把下面中的证券测试账号换成您设置的账号名称if len(klinedata1) > 1 and len(klinedata2) > 1 and lastsum>0.045 and   g.a ==0 and context.myacc and posmargin<100000 :# 两个期权价格之和大于200就同时卖出print "buy open the spread "+str(lastsum)context.myacc.InsertOrder(g.code1, BSType.SellOpen, dyndata1.now, 3)context.myacc.InsertOrder(g.code2, BSType.SellOpen, dyndata2.now, 3)g.a =1if len(klinedata1) > 1 and len(klinedata2) > 1 and lastsum<0.0055 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, 3)context.myacc.InsertOrder(g.code2, BSType.BuyClose, dyndata2.now, 3)if len(klinedata1) > 1 and len(klinedata2) > 1 and lastsum>0.0950 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, 1)context.myacc.InsertOrder(g.code2, BSType.BuyClose, dyndata2.now, 1)    #委托回报事件,当有委托回报时调用def OnOrderChange(context, AccountName, order) :#打印委托信息,id是编号,volume是数量,详细见API文档print "委托编号: " + order.id + "   账号名称: " + AccountNameprint "Vol: " + str(order.volume) + " Price: " + str(order.price)
  File "<ipython-input-1-8906500da5c5>", line 11    print "system starting..." ^SyntaxError: Missing parentheses in call to 'print'
 

全部回复

0/140

达人推荐

量化课程

    移动端课程