def _check_force_close(self, event):
    contract_list = list(event.bar_dict.keys())
    for contract in contract_list:
        event.bar_dict[contract].force_close = False
    cur_time = event.calendar_dt
    force_close = False
    for ft in self._force_close_time:
        start_time = cur_time.replace(hour=ft['start']['hour'], minute=ft['start']['minute'])
        end_time = cur_time.replace(hour=ft['end']['hour'], minute=ft['end']['minute'])
        if (start_time <= cur_time <= end_time):
            force_close = True
            break
    if force_close:
        contract_list = list(event.bar_dict.keys())
        for contract in contract_list:
            long_positions = get_position(contract, POSITION_DIRECTION.LONG)
            short_positions = get_position(contract, POSITION_DIRECTION.SHORT)
            if ((long_positions.quantity == 0) and (short_positions.quantity == 0)):
                continue
            event.bar_dict[contract].force_close = True
            if (not self._log_dir):
                continue
            if (not self._log_file[contract]):
                path = os.path.join(self._log_dir, (contract + '_force_close.csv'))
                self._log_file[contract] = open(path, 'w')
            msg = ('%s,%s' % (str(cur_time), 'FORCE_CLOSE'))
            self._log_file[contract].write((msg + '\n'))
    return
def _check_force_close(self, event):
    contract_list = list(event.bar_dict.keys())
    for contract in contract_list:
        event.bar_dict[contract].force_close = False
    cur_time = event.calendar_dt
    force_close = False
    for ft in self._force_close_time:
        start_time = cur_time.replace(hour=ft['start']['hour'], minute=ft['start']['minute'])
        end_time = cur_time.replace(hour=ft['end']['hour'], minute=ft['end']['minute'])
        if (start_time <= cur_time <= end_time):
            force_close = True
            break
    if force_close:
        contract_list = list(event.bar_dict.keys())
        for contract in contract_list:
            long_positions = get_position(contract, POSITION_DIRECTION.LONG)
            short_positions = get_position(contract, POSITION_DIRECTION.SHORT)
            if ((long_positions.quantity == 0) and (short_positions.quantity == 0)):
                continue
            event.bar_dict[contract].force_close = True
            if (not self._log_dir):
                continue
            if (not self._log_file[contract]):
                path = os.path.join(self._log_dir, (contract + '_force_close.csv'))
                self._log_file[contract] = open(path, 'w')
            msg = ('%s,%s' % (str(cur_time), 'FORCE_CLOSE'))
            self._log_file[contract].write((msg + '\n'))
    return