Example #1
0
    def on_trading_open(self, timestamp):
        self.logger.info('on_trading_open:{}'.format(timestamp))
        if is_same_date(timestamp, self.start_timestamp):
            return
        # get the account for trading at the date
        accounts = get_account(session=self.session,
                               trader_name=self.trader_name,
                               return_type='domain',
                               end_timestamp=to_time_str(timestamp),
                               limit=1,
                               order=SimAccount.timestamp.desc())
        if accounts:
            account = accounts[0]
        else:
            return

        positions = []
        # FIXME:dump all directly
        for position_domain in account.positions:
            position_dict = position_schema.dump(position_domain).data
            self.logger.info('current position:{}'.format(position_dict))
            del position_dict['sim_account']
            positions.append(position_dict)

        self.latest_account = sim_account_schema.dump(account).data
        self.latest_account['positions'] = positions
        self.logger.info('on_trading_open:{},latest_account:{}'.format(
            timestamp, self.latest_account))
Example #2
0
    def __init__(self, trader_name,
                 timestamp,
                 provider=Provider.NETEASE,
                 level=TradingLevel.LEVEL_1DAY,
                 base_capital=1000000,
                 buy_cost=0.001,
                 sell_cost=0.001,
                 slippage=0.001):

        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', StoreCategory.business)
        self.provider = provider
        self.level = level
        self.start_timestamp = timestamp

        account = get_account(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(SimAccount).filter(SimAccount.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 = SimAccount(trader_name=self.trader_name, cash=self.base_capital,
                             positions=[], all_value=self.base_capital, value=0, closing=False,
                             timestamp=timestamp)
        self.latest_account = sim_account_schema.dump(account).data
Example #3
0
    def get_account_at_time(self, timestamp):
        """

        :param timestamp:
        :type timestamp:
        :return:
        :rtype:SimAccount
        """
        return get_account(session=self.session, trader_name=self.trader_name, return_type='domain',
                           end_timestamp=timestamp, limit=1)[0]