Esempio n. 1
0
 def get_position(self, code: str = None) -> QA_Position:
     if code is None:
         return list(self.positions.values())[0]
     else:
         if code not in self.positions.keys():
             self.positions[code] = QA_Position(code=code)
         return self.positions[code]
Esempio n. 2
0
    def reload(self):
        message = self.db.account.find_one({
            'account_cookie': self.user_id,
            'password': self.password
        })

        time = datetime.datetime.now()
        # resume/settle

        if time.hour <= 15:
            self.trading_day = time.date()
        else:
            if time.weekday() in [0, 1, 2, 3]:
                self.trading_day = time.date() + datetime.timedelta(days=1)
            elif time.weekday() in [4, 5, 6]:
                self.trading_day = time.date() + datetime.timedelta(
                    days=(7 - time.weekday()))
        if message is not None:
            accpart = message.get('accounts')

            self.money = message.get('money')
            self.source_id = message.get('sourceid')

            self.pre_balance = accpart.get('pre_balance')
            self.deposit = accpart.get('deposit')
            self.withdraw = accpart.get('withdraw')
            self.withdrawQuota = accpart.get('WithdrawQuota')
            self.close_profit = accpart.get('close_profit')
            self.static_balance = accpart.get('static_balance')
            self.event = message.get('event')
            self.trades = message.get('trades')
            self.transfers = message.get('transfers')
            self.orders = message.get('orders')
            self.taskid = message.get('taskid', str(uuid.uuid4()))

            positions = message.get('positions')
            for position in positions.values():
                self.positions[position.get(
                    'instrument_id')] = QA_Position().loadfrommessage(position)

            for order in self.open_orders:
                self.log('try to deal {}'.format(order))
                self.make_deal(order)

            self.banks = message.get('banks')

            self.status = message.get('status')
            self.wsuri = message.get('wsuri')

            self.on_reload()

            if message.get('trading_day', '') == str(self.trading_day):
                # reload
                pass

            else:
                # settle
                self.settle()
Esempio n. 3
0
    def get_position(self, code: str = None) -> QA_Position:

        #exchange_id =  self.market_preset.get_exchange(code)
        if code is None:
            return list(self.positions.values())[0]
        else:
            code = self.format_code(code)
            if code not in self.positions.keys():
                pos = QA_Position(code=code)
                self.positions[code] = pos

            return self.positions[code]
Esempio n. 4
0
    def reload(self):
        if self.model.upper() == 'MANUAL':
            message = self.db.history.find_one(
                {'account_cookie': self.user_id, 'trading_day': {'$lte': self.trading_day}})
            if message is not None:
                accpart = message.get('accounts')

                self.money = message.get('money')
                self.source_id = message.get('sourceid')

                self.pre_balance = accpart.get('pre_balance')
                self.deposit = accpart.get('deposit')
                self.withdraw = accpart.get('withdraw')
                self.withdrawQuota = accpart.get('WithdrawQuota')
                self.close_profit = accpart.get('close_profit')
                self.static_balance = accpart.get('static_balance')
                self.event = message.get('event')
                self.trades = message.get('trades')
                self.transfers = message.get('transfers')
                self.orders = message.get('orders')
                self.taskid = message.get('taskid', str(uuid.uuid4()))

                positions = message.get('positions')
                for position in positions.values():
                    print(position)
                    p = QA_Position(
                    ).loadfrommessage(position)

                    self.positions[position.get('exchange_id')+'.'+position.get('instrument_id')] = QA_Position(
                    ).loadfrommessage(position)

                self.banks = message.get('banks')
                self.status = message.get('status')
                self.wsuri = message.get('wsuri')

                print("reload: {} {}".format(message.get(
                    'trading_day', ''), self.trading_day))
                if message.get('trading_day', '') == str(self.trading_day):
                    # reload
                    pass
                else:
                    # settle
                    # self.settle()
                    # 新的一天,先清空,但不需要保存到数据库,因为每次操作完都会 settle,到那时再保存到数据库
                    self.clear_for_newday()

        elif self.model.upper() in ['REAL', 'SIM']:
            message = self.db.account.find_one(
                {'account_cookie': self.user_id, 'password': self.password})

            time = datetime.datetime.now()
            # resume/settle

            if time.hour <= 15:
                self._trading_day = time.date()
            else:
                if time.weekday() in [0, 1, 2, 3]:
                    self._trading_day = time.date() + datetime.timedelta(days=1)
                elif time.weekday() in [4, 5, 6]:
                    self._trading_day = time.date() + datetime.timedelta(days=(7-time.weekday()))
            if message is not None:
                accpart = message.get('accounts')

                self.money = message.get('money')
                self.source_id = message.get('sourceid')

                self.pre_balance = accpart.get('pre_balance')
                self.deposit = accpart.get('deposit')
                self.withdraw = accpart.get('withdraw')
                self.withdrawQuota = accpart.get('WithdrawQuota')
                self.close_profit = accpart.get('close_profit')
                self.static_balance = accpart.get('static_balance')
                self.event = message.get('event')
                self.trades = message.get('trades')
                self.transfers = message.get('transfers')
                self.orders = message.get('orders')
                self.taskid = message.get('taskid', str(uuid.uuid4()))

                positions = message.get('positions')
                for position in positions.values():
                    p = QA_Position(
                    ).loadfrommessage(position)

                    self.positions[position.get('exchange_id')+'.'+position.get('instrument_id')] = QA_Position(
                    ).loadfrommessage(position)

                for order in self.open_orders:
                    self.log('try to deal {}'.format(order))
                    self.make_deal(order)

                self.banks = message.get('banks')

                self.status = message.get('status')
                self.wsuri = message.get('wsuri')

                self.on_reload()

                if message.get('trading_day', '') == str(self._trading_day):
                    # reload
                    pass

                else:
                    # settle
                    self.settle()