121 views
0

import datetime
import pandas as pd
  # 长假前一个交易日无夜盘的日期,可以从上期所等网站查找
workday_nonight = [(2020,12,31), (2021,2,10), (2021,4,2), (2021,4,30), (2021,6,11), (2021,9,18), (2021,9,30), (2021,12,31)]
# 长假后第一个交易日
first_trade_day = [(2021,1,4), (2021,2,18), (2021,4,6), (2021,5,7), (2021,6,15), (2021,9,22), (2021,10,8)]
 # 策略前初始化时判断是否长假后第一个交易日
def is_first_tradeday():
    for ymd in first_trade_day:
        y, m, d = ymd[0], ymd[1], ymd[2]
        today = datetime.datetime.today()
        y1, m1, d1 = today.year, today.month, today.day
        if y == y1 and m == m1 and d == d1:
            return True
                      break
    return False
 def is_tqdata_wrong(kline): # 天勤数据是否有多余夜盘
    dt = kline.datetime.values
    mask = dt == 0 # 全False的数组
    for ymd in workday_nonight:
        y, m, d = ymd[0], ymd[1], ymd[2]
        tstamp = datetime.datetime(y, m, d).timestamp()
        night_start = (tstamp + 3600*15) * 1e+9 # 当天下午15点
        night_end = (tstamp + 3600*21) * 1e+9 # 第二天早晨9点
        mask = (mask | ((night_start < dt) & (dt < night_end)))
    if len(mask[mask]) > 0:
        return True, mask
    else:
        return False, mask
 # 取得kline后判断数据是否有多余夜盘数据
if is_first_tradeday():
    is_wrong, mask = is_tqdata_wrong(kline)
    if is_wrong: # 确定有多余夜盘数据
        kline_right = kline[~mask] # 去除错误数据
                 # 而后使用kline_right进行参数计算
        pass

Posted new comment

厉害,学习了