Пример #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
    def generate_domain(self, security_item, original_data):
        """
        generate the data_schema instance using security_item and original_data,the original_data should be from record

        :param security_item:
        :param original_data:
        """
        the_id = self.generate_domain_id(security_item, original_data)

        items = get_data(data_schema=self.data_schema, session=self.session, provider=self.provider,
                         security_id=security_item.id,
                         filters=[self.data_schema.id == the_id],
                         return_type='domain')

        if items and not self.force_update:
            self.logger.info('ignore the data {}:{} saved before'.format(self.data_schema, the_id))
            return None

        if not items:
            timestamp_str = original_data[self.get_timestamp_field()]
            timestamp = None
            try:
                timestamp = to_pd_timestamp(timestamp_str)
            except Exception as e:
                self.logger.exception(e)

            domain_item = self.data_schema(id=the_id,
                                           code=security_item.code,
                                           security_id=security_item.id,
                                           timestamp=timestamp)
        else:
            domain_item = items[0]

        fill_domain_from_dict(domain_item, original_data, self.get_data_map())
        return domain_item
Пример #3
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['security_id'],
                                                   to_time_str(timestamp, TIME_FORMAT_ISO8601))
            position_domain.timestamp = to_pd_timestamp(timestamp)
            position_domain.sim_account_id = the_id

            positions.append(position_domain)

        account_domain = SimAccount(id=the_id, 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(sim_account_schema.dump(account_domain).data))

        self.session.add(account_domain)
        self.session.commit()
Пример #4
0
    def generate_domain(self, entity, original_data):
        """
        generate the data_schema instance using entity and original_data,the original_data is from record result

        :param entity:
        :param original_data:
        """

        got_new_data = False

        #: if the domain is directly generated in record method, we just return it
        if isinstance(original_data, self.data_schema):
            got_new_data = True
            return got_new_data, original_data

        the_id = self.generate_domain_id(entity, original_data)

        #: optional way
        #: item = self.session.query(self.data_schema).get(the_id)

        items = get_data(
            data_schema=self.data_schema,
            session=self.session,
            provider=self.provider,
            entity_id=entity.id,
            filters=[self.data_schema.id == the_id],
            return_type="domain",
        )

        if items and not self.force_update:
            self.logger.info("ignore the data {}:{} saved before".format(
                self.data_schema, the_id))
            return got_new_data, None

        if not items:
            timestamp_str = original_data[self.get_original_time_field()]
            timestamp = None
            try:
                timestamp = to_pd_timestamp(timestamp_str)
            except Exception as e:
                self.logger.exception(e)

            if "name" in get_schema_columns(self.data_schema):
                domain_item = self.data_schema(id=the_id,
                                               code=entity.code,
                                               name=entity.name,
                                               entity_id=entity.id,
                                               timestamp=timestamp)
            else:
                domain_item = self.data_schema(id=the_id,
                                               code=entity.code,
                                               entity_id=entity.id,
                                               timestamp=timestamp)
            got_new_data = True
        else:
            domain_item = items[0]

        fill_domain_from_dict(domain_item, original_data, self.get_data_map())
        return got_new_data, domain_item