-
Notifications
You must be signed in to change notification settings - Fork 218
/
LLDPE-PVC价差 LLDPE - PVC Spread Strategy
79 lines (67 loc) · 3 KB
/
LLDPE-PVC价差 LLDPE - PVC Spread Strategy
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
# coding:utf-8
#!/usr/bin/env python
from PoboAPI import *
import datetime
#用poboquant python实现,在poboquant上运行,如果有问题 可加群 726895887 咨询
#开始时间,用于初始化一些参数
def OnStart(context) :
print "I\'m starting..."
#设定一个全局变量品种
g.code1 = "l1905.DCE"
g.code2="v1905.DCE"
#订阅实时数据,用于驱动OnQuote事件
SubscribeQuote([g.code1,g.code2])
#订阅K线数据,用于驱动OnBar事件
#SubscribeBar(g.code, BarType.Min)
#登录交易账号,需在主页用户管理中设置账号,并把证券测试替换成您的账户名称
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 or code!=g.code2 :
# return
#获取最新行情
dyndatal = GetQuote(g.code1)
dyndatav = GetQuote(g.code2)
#if dyndata :
#.now指最新价,详细属性见API文档
# now1 = dyndata.now
#打印最新价
# log.info("最新价: " + str(dyndata.now))
#获取K线数据
g.code1 = "l1905.DCE"
g.code2="v1905.DCE"
klinedata3l = GetHisData(g.code1, BarType.Min3)
klinedata1l = GetHisData(g.code1, BarType.Min)
klinedata3v = GetHisData(g.code2, BarType.Min3)
klinedata1v = GetHisData(g.code2, BarType.Min)
spread3=klinedata3l[-2].low-klinedata3v[-2].low
print "spread3 is " +str(spread3)
spread1=klinedata1l[-1].low-klinedata1v[-1].low
print "spread1 is " +str(spread1)
#打印K线数据,如最新一根K线的最高价
#if len(klinedata3) > 0 and len(klinedata1)> 0:
# lasthigh3 = klinedata3[-1].high
# lasthigh1 = klinedata1[-1].high
# log.info("最新3分钟K线的最高价: " + str(lasthigh3))
# log.info("最新1分钟K线的最高价: " + str(lasthigh1))
#如果配置好交易账号了,可以根据条件下单,需把下面中的证券测试账号换成您设置的账号名称
if spread3-50>=spread1 and context.myacc :
# 当日收盘价小于昨收则买入
print "满足价差开仓条件......."
context.myacc.InsertOrder(g.code1, BSType.BuyOpen, dyndatal.now, 50)
context.myacc.InsertOrder(g.code2, BSType.SellOpen, dyndatav.now, 50)
elif spread3+10<=spread1 and context.myacc :
# 当日收盘价大于昨收则卖出
print "满足价差平仓条件......."
context.myacc.InsertOrder(g.code1, BSType.SellClose, dyndatal.now, 50)
context.myacc.InsertOrder(g.code2, BSType.BuyClose, dyndatav.now, 50)
#委托回报事件,当有委托回报时调用
def OnOrderChange(context, AccountName, order) :
#打印委托信息,id是编号,volume是数量,详细见API文档
print "委托编号: " + order.id + " 账号名称: " + AccountName
print "Vol: " + str(order.volume) + " Price: " + str(order.price)