欢迎光临
专业期货策略平台

豆粕期权价差套利代码范例详解

# coding:utf-8
#!/usr/bin/env python

from PoboAPI import *
import datetime
#用poboquant python实现,在poboquant上运行,请关注A期客网,下载更多量化代码 www.a-qike.com
#回测的时候最好自己选好期权还在交易的日期段,否则回测会不能进行 pick the date range the options in trading or back test will hault
#这是豆粕1901 看涨3000和看涨3050的价差套利,在豆粕牛市中,看涨期权价差也会拉大
#A spread trade strategy of DCE soybean meal 1901 call 3000 and 3050
#开始时间,用于初始化一些参数
def OnStart(context) :
    print "system starting..."
    #设定一个全局变量品种
    g.code1 = "m1901-C-3000.DCE"
    g.code2= "m1901-C-3050.DCE"
    #订阅实时数据,用于驱动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("m1901-C-3000最新价1: " + str(dyndata1.now))
        log.info("m1901-C-3050最新价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 :
        lastspread = klinedata1[-1].close - klinedata2[-1].close #期权价差
        log.info("最新价差: "  + str(lastspread))
    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 lastspread<5 and context.myacc and posmargin<100000 :
        # 两个期权价差小于50就买入价差,开仓策略,open position
        print "buy open the spread "+str(lastspread)
        context.myacc.InsertOrder(g.code1, BSType.BuyOpen, dyndata1.now, 10)
        context.myacc.InsertOrder(g.code2, BSType.SellOpen, dyndata2.now, 10)
        
        
    if len(klinedata1) > 1 and len(klinedata2) > 1 and lastspread>60 and poslength>0 and context.myacc  :
        # 两个期权价差大于60就卖平价差,平仓,close positions
        print "sell close the spread,take profit "+str(lastspread)
        context.myacc.InsertOrder(g.code1, BSType.SellClose, dyndata1.now, 10)
        context.myacc.InsertOrder(g.code2, BSType.BuyClose, dyndata2.now, 10)
    
    if len(klinedata1) > 1 and len(klinedata2) > 1 and lastspread<2 and poslength>0 and context.myacc  :
        # 两个期权价差小于2就卖平价差,止损,cut loss
        print "sell close the spread,cut loss "+str(lastspread)
        context.myacc.InsertOrder(g.code1, BSType.SellClose, 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)
0 0 vote
Article Rating
Click to rate this post!
[Total: 0 Average: 0]
赞(0) 打赏
未经允许不得转载:A期客 » 豆粕期权价差套利代码范例详解
订阅
提醒
guest
0 评论
Inline Feedbacks
View all comments

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏

0
Would love your thoughts, please comment.x
()
x