vn.py官网
开源量化社区
Member
avatar
加入于:
帖子: 73
声望: 4

1.vnpy.app新建user_tools文件夹trading_hour.py放入user_tools
trading_hour.py如下:

from datetime import datetime,time,timedelta
import json

class TRADINGHOUR(object):
PATH = "***.vntrader/trading_hour.json" #PATH填trading_hour.json在电脑的绝对路径,trading_hour.json放入在vntrader目录下,如:c:/users/电脑名/vntrader/trading_hour.json
time_switch = 0
start_time = ""
end_time = ""

def get_trading_time(self,symbol):   #读取存入json中的标的开收盘时间
    with open(self.PATH,"r",encoding="utf_8") as f:            
        trading_hour = json.load(f)
    for key_symbol in trading_hour: 
        if key_symbol == symbol:                
            start = trading_hour[symbol][0]
            end = trading_hour[symbol][1]
            break
    self.start_time = datetime.strptime(start,"%H:%M:%S")   #datetime对象,后面比较时候要转换成time对象
    self.end_time = datetime.strptime(end,"%H:%M:%S")
    return self.start_time,self.end_time

def day_night_switch(self):             #time_switch = 0 白盘标的,1为夜盘0:00前收盘的标的(如:rb),2为0:00后收盘的标的(如:ag,cu)
    if self.start_time.time() < time(hour=20,minute=0):
        self.time_switch = 0

    elif self.end_time.time() > time(hour=3,minute=0):
        self.time_switch = 1

    else:
        self.time_switch = 2 
    return self.time_switch

def trading_period(self,bar):   #判断标的日内交易时间

        DAY_START = time(hour=9,minute=0)
        DAY_END = time(hour=14,minute=57)

        if self.day_night_switch() == 0:                
            return bar.datetime.time() < (self.end_time + timedelta(minutes=-3)).time()

        elif self.day_night_switch() == 1:
            return bar.datetime.time() >= self.start_time.time() and bar.datetime.time() < (self.end_time + timedelta(minutes=-3)).time() \
                or (bar.datetime.time() >= DAY_START and bar.datetime.time() < DAY_END )
        else:
            return bar.datetime.time() >= self.start_time.time() or bar.datetime.time() < (self.end_time + timedelta(minutes=-3)).time() \
                or (bar.datetime.time() >= DAY_START and bar.datetime.time() < DAY_END )

2.trading_hour.json如下: #记录了期货白盘期货标的开收盘时间,夜盘标的夜盘开盘以及夜盘收盘时间,遇到特殊日子(比如疫情期间,ag没有夜盘了,那时间要修正“AG”:["9:30:00","15:00:00"]),直接过来修改开收盘时间即可
{
"IF":["9:30:00","15:00:00"],
"IC":["9:30:00","15:00:00"],
"IH":["9:30:00","15:00:00"],
"T":["9:30:00","15:15:00"],
"AU":["21:00:00","2:30:00"],
"AG":["21:00:00","2:30:00"],
"CU":["21:00:00","1:00:00"],
"AL":["21:00:00","1:00:00"],
"ZN":["21:00:00","1:00:00"],
"PB":["21:00:00","1:00:00"],
"NI":["21:00:00","1:00:00"],
"SN":["21:00:00","1:00:00"],
"RB":["21:00:00","23:00:00"],
"I":["21:00:00","23:00:00"],
"HC":["21:00:00","23:00:00"],
"SS":["21:00:00","1:00:00"],
"SF":["9:00:00","15:00:00"],
"SM":["9:00:00","15:00:00"],
"JM":["21:00:00","23:00:00"],
"J":["21:00:00","23:00:00"],
"ZC":["21:00:00","23:00:00"],
"FG":["21:00:00","23:00:00"],
"SP":["21:00:00","23:00:00"],
"FU":["21:00:00","23:00:00"],
"LU":["21:00:00","23:00:00"],
"SC":["21:00:00","2:30:00"],
"BU":["21:00:00","23:00:00"],
"PG":["21:00:00","23:00:00"],
"RU":["21:00:00","23:00:00"],
"NR":["21:00:00","23:00:00"],
"L":["21:00:00","23:00:00"],
"TA":["21:00:00","23:00:00"],
"V":["21:00:00","23:00:00"],
"EG":["21:00:00","23:00:00"],
"MA":["21:00:00","23:00:00"],
"PP":["21:00:00","23:00:00"],
"EB":["21:00:00","23:00:00"],
"UR":["9:00:00","15:00:00"],
"SA":["21:00:00","23:00:00"],
"C":["21:00:00","23:00:00"],
"A":["21:00:00","23:00:00"],
"CS":["21:00:00","23:00:00"],
"B":["21:00:00","23:00:00"],
"M":["21:00:00","23:00:00"],
"Y":["21:00:00","23:00:00"],
"RM":["21:00:00","23:00:00"],
"OI":["21:00:00","23:00:00"],
"P":["21:00:00","23:00:00"],
"CF":["21:00:00","23:00:00"],
"SR":["21:00:00","23:00:00"],
"JD":["9:00:00","15:00:00"],
"AP":["9:00:00","15:00:00"],
"CJ":["9:00:00","15:00:00"]
}

3.在自己的策略中import TRADINHOUR from vnpy.app.user_tools.trading_hour import TRADINGHOUR 同时import re
def --init--():加入
self.tradingtime = TRADINGHOUR()
self.symbol = "".join(re.findall(r"\D+",self.get_data()["vt_symbol"].split(".")[0])).upper() #获取标的代码
self.start_time,self.end_time = self.tradingtime.get_trading_time(self.symbol)
这些完成后,就可以在你的on_bar函数里面调用self.tradingtime.trading_period(bar)获取标的日内交易时段了
例如:def on_bar(self, bar: BarData):
### ###
if self.tradingtime.trading_period(bar): #获取当下交易标的日内交易时段,平仓时间设置为收盘前3min

        if self.pos == 0:
                 ###****
        if self.pos > 0: 
               ### ****
        if self.pos < 0:
                ###*****

else: #收盘平仓

            if self.pos > 0:
                   self.sell(bar.close_price *0.99, abs(self.pos))
            elif self.pos < 0:
                   self.cover(bar.close_price*1.01, abs(self.pos))
Administrator
avatar
加入于:
帖子: 4749
声望: 278

挺实用的,给你加个精华

Member
avatar
加入于:
帖子: 6
声望: 0

学习受教,记录备用

© 2015-2019 上海韦纳软件科技有限公司
备案服务号:沪ICP备18006526号-3

沪公网安备 31011502017034号