-
Notifications
You must be signed in to change notification settings - Fork 218
/
豆粕期权soymeal covered-call例子
81 lines (70 loc) · 3.57 KB
/
豆粕期权soymeal covered-call例子
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# coding:utf-8
#!/usr/bin/env python
from PoboAPI import *
import datetime
import numpy as np
#简单的例子,并不挣钱,细节读者自己添加 an example of soybean meal covered-call strategy,could get some problem of price data gap as
#soybean meal options got quite thin trading volume 读者可以自己写一些算法避免期权历史价格稀疏的问题
#在用户设置里查一下自己的初始测试账户名称,是不是“回测期货”,不是的话改成你自己的测试期货账户名称即可
#分钟或日线级别策略 back test on minute level
#读者可以自己补充换月的功能,否则只在豆粕1805期权的存续周期内回测
#开始时间,用于初始化一些参数
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(["m1805.DCE","m1805-C-3000.DCE"])
g.code1="m1805.DCE"
g.code2="m1805-C-3000.DCE"
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)
#dyndatam1901 = GetQuote(g.code1)
#dyndatam1901c3000 = GetQuote(g.code2)
leg1=GetQuote(g.code1).clear
print "leg1 price "+str(leg1)
leg2=GetQuote(g.code2).clear
print "leg2 price "+str(leg2)
timerecorder=GetQuote(g.code1).time
if leg2==0:
print "option price error"
premiumratio=float(leg2/leg1)
print "premium ratio "+str(premiumratio)+" "+str(leg2)+" "+str(leg1)
print "at time ",timerecorder
if premiumratio<=0.001 and len(pos)==0 : # if soybean meal options too cheap,then buy opt short fut
print "to buy Spread...."
context.myacc.InsertOrder(g.code2, BSType.BuyOpen, leg2, 1)
context.myacc.InsertOrder(g.code1, BSType.SellOpen, leg1, 1)
print "positions bought ",timerecorder
bal=context.accounts["回测期货"].AccountBalance.AssetsBalance #返回成交后账户权益
#api参考https://quant.pobo.net.cn/quant/doc?name=api#%E6%8C%81%E4%BB%93%E5%AF%B9%E8%B1%A1position%E7%94%A8%E6%B3%95
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 premiumratio>0.01 and len(pos)>0:
print "to sell Spread..."
context.myacc.InsertOrder(g.code1, BSType.BuyClose, leg1, 1)
context.myacc.InsertOrder(g.code2, BSType.SellClose, leg2, 1)
print "positions sold ",timerecorder
bal=context.accounts["回测期货"].AccountBalance.AssetsBalance #返回成交后账户权益
print "账户金额 :"+str(bal)
pos = context.accounts["回测期货"].GetPositions()
print "len(pos) str in trade "+str(len(pos))