コード例 #1
0
    def load_account(self) -> AccountStats:
        records = AccountStats.query_data(
            filters=[AccountStats.trader_name == self.trader_name],
            order=AccountStats.timestamp.desc(),
            limit=1,
            return_type='domain')
        if not records:
            return self.account
        latest_record: AccountStats = records[0]

        # create new orm object from latest record
        account_dict = account_stats_schema.dump(latest_record)
        del account_dict['id']
        del account_dict['positions']
        account = AccountStats()
        fill_domain_from_dict(account, account_dict)

        positions: List[Position] = []
        for position_domain in latest_record.positions:
            position_dict = position_schema.dump(position_domain)
            self.logger.debug('current position:{}'.format(position_dict))
            del position_dict['id']
            del position_dict['account_stats']
            position = Position()
            fill_domain_from_dict(position, position_dict)
            positions.append(position)

        account.positions = positions

        return account
コード例 #2
0
ファイル: account.py プロジェクト: wisfern/zvt
    def init_account(self) -> AccountStats:
        trader_info = get_trader_info(session=self.session,
                                      trader_name=self.trader_name,
                                      return_type='domain',
                                      limit=1)

        if trader_info:
            self.logger.warning(
                "trader:{} has run before,old result would be deleted".format(
                    self.trader_name))
            self.session.query(TraderInfo).filter(
                TraderInfo.trader_name == self.trader_name).delete()
            self.session.query(AccountStats).filter(
                AccountStats.trader_name == self.trader_name).delete()
            self.session.query(Position).filter(
                Position.trader_name == self.trader_name).delete()
            self.session.query(Order).filter(
                Order.trader_name == self.trader_name).delete()
            self.session.commit()

        return AccountStats(entity_id=f'trader_zvt_{self.trader_name}',
                            timestamp=self.start_timestamp,
                            trader_name=self.trader_name,
                            cash=self.base_capital,
                            all_value=self.base_capital,
                            value=0,
                            closing=False)
コード例 #3
0
    def __init__(self,
                 entity_schema: EntityMixin,
                 trader_name,
                 timestamp,
                 provider=None,
                 level=IntervalLevel.LEVEL_1DAY,
                 base_capital=1000000,
                 buy_cost=0.001,
                 sell_cost=0.001,
                 slippage=0.001):
        self.entity_schema = entity_schema
        self.base_capital = base_capital
        self.buy_cost = buy_cost
        self.sell_cost = sell_cost
        self.slippage = slippage
        self.trader_name = trader_name

        self.session = get_db_session('zvt', data_schema=TraderInfo)
        self.provider = provider
        self.level = level
        self.start_timestamp = timestamp

        account = get_trader_info(session=self.session,
                                  trader_name=self.trader_name,
                                  return_type='domain',
                                  limit=1)

        if account:
            self.logger.warning(
                "trader:{} has run before,old result would be deleted".format(
                    trader_name))
            self.session.query(TraderInfo).filter(
                TraderInfo.trader_name == self.trader_name).delete()
            self.session.query(AccountStats).filter(
                AccountStats.trader_name == self.trader_name).delete()
            self.session.query(Position).filter(
                Position.trader_name == self.trader_name).delete()
            self.session.query(Order).filter(
                Order.trader_name == self.trader_name).delete()
            self.session.commit()

        account = AccountStats(entity_id=f'trader_zvt_{self.trader_name}',
                               trader_name=self.trader_name,
                               cash=self.base_capital,
                               all_value=self.base_capital,
                               value=0,
                               closing=False,
                               timestamp=timestamp)
        self.latest_account = account_stats_schema.dump(account)
コード例 #4
0
ファイル: account.py プロジェクト: zvtvz/zvt
    def init_account(self) -> AccountStats:
        # 清除历史数据
        if not self.keep_history:
            self.clear_account()

        # 读取之前保存的账户
        if self.keep_history:
            self.account = self.load_account()
            if self.account:
                return self.account

        # init trader info
        entity_type = self.entity_schema.__name__.lower()
        sim_account = TraderInfo(
            id=self.trader_name,
            entity_id=f"trader_zvt_{self.trader_name}",
            timestamp=self.start_timestamp,
            trader_name=self.trader_name,
            entity_type=entity_type,
            start_timestamp=self.start_timestamp,
            provider=self.provider,
            level=self.level.value,
            real_time=self.real_time,
            kdata_use_begin_time=self.kdata_use_begin_time,
            kdata_adjust_type=self.adjust_type.value,
        )
        self.session.add(sim_account)
        self.session.commit()

        return AccountStats(
            entity_id=f"trader_zvt_{self.trader_name}",
            timestamp=self.start_timestamp,
            trader_name=self.trader_name,
            cash=self.base_capital,
            input_money=self.base_capital,
            all_value=self.base_capital,
            value=0,
            closing=False,
        )
コード例 #5
0
    def persist_account(self, timestamp):
        """
        save the account to db,we do this after closing time every day

        :param timestamp:
        :type timestamp:
        """
        the_id = '{}_{}'.format(self.trader_name,
                                to_time_str(timestamp, TIME_FORMAT_ISO8601))
        positions = []
        for position in self.latest_account['positions']:
            position_domain = Position()
            fill_domain_from_dict(position_domain, position, None)

            position_domain.id = '{}_{}_{}'.format(
                self.trader_name, position['entity_id'],
                to_time_str(timestamp, TIME_FORMAT_ISO8601))
            position_domain.timestamp = to_pd_timestamp(timestamp)
            position_domain.account_stats_id = the_id

            positions.append(position_domain)

        account_domain = AccountStats(
            id=the_id,
            entity_id=f'trader_zvt_{self.trader_name}',
            trader_name=self.trader_name,
            cash=self.latest_account['cash'],
            positions=positions,
            all_value=self.latest_account['all_value'],
            value=self.latest_account['value'],
            timestamp=to_pd_timestamp(self.latest_account['timestamp']))

        self.logger.info('persist_account:{}'.format(
            account_stats_schema.dump(account_domain)))

        self.session.add(account_domain)
        self.session.commit()