class Account(Document): """ 抢单用户的账号 """ class Meta: idf = IDFormatter('{user}_{exchange}_{login_name}') user = StringField(required=True) exchange = StringField(required=True) login_name = StringField(required=True) login_password = StringField(required=True) money_password = StringField() bank_password = StringField() collections = ListField(StringField, default=[]) position = ListField(EmbeddedField(MyPosition)) orders = ListField(EmbeddedField(MyOrder)) order_status = ListField(EmbeddedField(MyStatus)) money = FloatField() capital = FloatField() profit = FloatField() earned = FloatField() # 交易收入 lost = FloatField() # 交易亏损 @property def mobile(self): user = User.cached(60).query_one({'_id': self.user}) return user.mobile @property def username(self): user = User.cached(60).query_one({'_id': self.user}) return user.username
class Investor(Document): """ 投资人 """ class Meta: idf = IDFormatter('{id_number}') idx1 = Index(['user', 'order'], unique=True) user = StringField(required=True) # 用户 order = IntField(required=True) # 顺序 name = StringField(required=True) id_type = StringField(required=True) # 身份证/.. id_number = StringField(required=True) # 号码 id_front = BinaryField() # 照片 id_back = BinaryField() mobile = StringField(required=True) province = StringField(required=True) city = StringField(required=True) address = StringField(required=True) bank_accounts = ListField(EmbeddedField(BankAccount)) @classmethod def get_user_order(cls, user): i = cls.query_one({'user': user}, sort=[('order', -1)], limit=1) if not i: return 1 else: return i.order + 1 @classmethod def user_investors(cls, user): return list(cls.query({'user': user}, sort=[('order', 1)]))
class DailyTrading(Document): """ 当日账号汇总 """ class Meta: idf = IDFormatter('{account}_{date}') idx1 = Index(['account', 'date'], unique=True) date = DateTimeField(required=True) account = StringField(required=True) position = ListField(EmbeddedField(MyPosition)) orders = ListField(EmbeddedField(MyOrder)) order_status = ListField(EmbeddedField(MyStatus)) profit = FloatField() money = FloatField() capital = FloatField() earned = FloatField() # 交易收入 lost = FloatField() # 交易亏损
class Exchange(Document): """ 交易所 """ class Meta: idf = IDFormatter('{name}') name = StringField(required=True) num_users = IntField() num_accounts = IntField() position = ListField(EmbeddedField(MyPosition)) orders = ListField(EmbeddedField(MyOrder)) order_status = ListField(EmbeddedField(MyStatus)) money = FloatField() capital = FloatField() profit = FloatField() earned = FloatField() # 交易收入 lost = FloatField() # 交易亏损
class Status(Document): """ 当日挂单汇总 """ class Meta: idf = IDFormatter('{user_id}_{date}') idx1 = Index(['user_id', 'date'], unique=True) user_id = StringField(required=True) date = DateTimeField(required=True) status_list = ListField(EmbeddedField(MyStatus))
class Position(Document): """ 当日持仓汇总 """ class Meta: idf = IDFormatter('{user_id}_{date}') idx1 = Index(['user_id', 'date'], unique=True) user_id = StringField(required=True) date = DateTimeField(required=True) position_list = ListField(EmbeddedField(MyPosition))
class TradeAccount(Document): """ 交易账号 """ class Meta: idf = IDFormatter('{exchange}_{login_name}') idx1 = Index(['investor', 'exchange'], unique=True) idx2 = Index(['exchange', 'login_name'], unique=True) idx3 = Index('user') user = StringField(required=True) # 所属用户 investor = StringField(required=True) # 投资人 bank = StringField(required=True) # 工商银行/... exchange = StringField(required=True) # 交易所简称 login_name = StringField(required=True) # 账号 login_password = StringField(required=False) # 密码 money_password = StringField(required=False) # 资金密码 verify_message = StringField(default='请先更新状态', required=False) # 验证失败的原因 verified = BooleanField(default=False) # 验证通过 grab_buy_on = BooleanField(default=False) # 抢单买 grab_sell_on = BooleanField(default=False) # 抢单卖 position = ListField(EmbeddedField(MyPosition), default=[]) money = EmbeddedField(MyMoney, default={}) orders = ListField(EmbeddedField(Order), default=[]) order_status = ListField(EmbeddedField(OrderStatus), default=[]) @classmethod def user_accounts(cls, user): return list(cls.query({'user': user})) @property def investor_name(self): investors = list(Investor.cached(5).query({'user': self.user})) for i in investors: if i._id == self.investor: return i.name else: return '未找到' @property def money_position(self): return sum(p.price * p.quantity for p in self.position)
class User(Document): """ 抢单用户 """ class Meta: idf = IDFormatter('{mobile}') idx1 = Index('mobile', unique=True) idx2 = Index('username') mobile = StringField(required=True) username = StringField(required=True) password = StringField(required=True) owning = FloatField(default=0) # 欠款 paid = FloatField(default=0) # 已结清 num_exchanges = IntField() num_accounts = IntField() profit = FloatField() money = FloatField() capital = FloatField() earned = FloatField() # 交易收入 lost = FloatField() # 交易亏损 position = ListField(EmbeddedField(MyPosition)) orders = ListField(EmbeddedField(MyOrder)) order_status = ListField(EmbeddedField(MyStatus)) _is_admin = BooleanField() def get_id(self): return self._id def is_active(self): return True def is_anonymous(self): return False def is_authenticated(self): return True def is_admin(self): return self._is_admin
class Summary(Document): """ 各种组合的汇总信息 总资金/总持仓/总浮盈 """ class Meta: idf = IDFormatter('{exchange}_{user}_{collection}') idx1 = Index('exchange') idx2 = Index('user') idx3 = Index('collection') exchange = StringField(default='', required=True) user = StringField(default='', required=True) collection = StringField(default='', required=True) position = ListField(EmbeddedField(MyPosition)) orders = ListField(EmbeddedField(MyOrder)) order_status = ListField(EmbeddedField(MyStatus)) money = FloatField() capital = FloatField() profit = FloatField() earned = FloatField() # 交易收入 lost = FloatField() # 交易亏损