Ejemplo n.º 1
0
def get_current_trade_day(timing: datetime) -> None or date:
    current_string = str(timing.date())

    last_day = str((timing + timedelta(days=-1)).date())

    if timing.time() <= DAY_END and current_string in trade_dates:
        return current_string
    if timing.time() > DAY_END and current_string in trade_dates:
        index = trade_dates.index(current_string)
        return trade_dates[index + 1]

    if current_string not in trade_dates and last_day in trade_dates and timing.time() < NIGHT_END:
        """ 如果是周六凌晨2:30以前, 当前不为交易日 且前一日为交易日 返回下一个交易日"""
        index = trade_dates.index(last_day)
        return trade_dates[index + 1]
    return None
Ejemplo n.º 2
0
    def __call__(self, *args, **kwargs):
        """ 回测周期 """
        entity = args[0]
        # 日期不相等时, 更新前日结算价格
        if self.account.date is None:
            self.account.date = self.date
        if self.date is None:
            self.date = entity.datetime.date()
        if not self.auth_time(entity.datetime):
            return
        if entity.type == "bar" and entity.close_price <= 0:
            return
        elif entity.type == "tick" and entity.last_price <= 0:
            return

        self.data_type = entity.type
        # 回测的时候自动更新策略的日期
        try:
            seconds = (entity.datetime - self.datetime).seconds
            if seconds >= 60 * 60 * 4 and ((entity.datetime.hour >= 20) or (
                (14 <= self.datetime.hour <= 15) and entity.datetime.date() !=
                    self.datetime.date()) and entity.datetime.hour >= 8):  # 换天
                self.account.settle(entity.datetime.date())
                # 针对于账户的实现 我们需要将昨仓转换为今仓
                self.app.recorder.position_manager.covert_to_yesterday_holding(
                )
                for local_symbol, price in self.price_mapping.items():
                    self.pre_close_price[local_symbol] = price
                #  结算完触发初始化函数
                self.on_event(EVENT_INIT_FINISHED, True)
        except KeyError:
            pass
        except AttributeError:
            pass
        self.data_entity = entity
        self.change_month_record["".join(
            filter(str.isalpha,
                   entity.local_symbol.split(".")[0]))] = entity
        # 维护一个最新的价格
        self.price_mapping[self.data_entity.local_symbol] = self.data_entity.close_price if entity.type == "bar" \
            else self.data_entity.last_price
        if self.pre_close_price.get(self.data_entity.local_symbol) is None:
            self.pre_close_price[
                self.data_entity.
                local_symbol] = self.data_entity.last_price if entity.type == "tick" else self.data_entity.close_price
        self.datetime = entity.datetime
        self.match_deal()

        if entity.type == "tick":
            self.account.position_manager.update_tick(
                self.data_entity,
                self.pre_close_price[self.data_entity.local_symbol])
            self.app.recorder.position_manager.update_tick(
                self.data_entity,
                self.pre_close_price[self.data_entity.local_symbol])
            self.on_event(EVENT_TICK, TickData(**entity))

        if entity.type == "bar":
            self.account.position_manager.update_bar(
                self.data_entity,
                self.pre_close_price[self.data_entity.local_symbol])
            self.app.recorder.position_manager.update_bar(
                self.data_entity,
                self.pre_close_price[self.data_entity.local_symbol])
            self.on_event(EVENT_BAR, BarData(**entity))

        if entity.datetime.hour >= 21:
            """if hour > 21, switch to next trade day"""
            index = trade_dates.index(str(entity.datetime.date()))
            self.date = datetime.strptime(trade_dates[index + 1],
                                          "%Y-%m-%d").date()
        else:
            if str(entity.datetime.date()) not in trade_dates:
                last_day = entity.datetime + timedelta(days=-1)
                self.date = datetime.strptime(
                    trade_dates[trade_dates.index(str(last_day.date())) + 1],
                    "%Y-%m-%d").date()
            else:
                self.date = entity.datetime.date()
        # 穿过接口日期检查
        self.account.via_aisle()