Ejemplo n.º 1
0
 async def searchUserInformation(self, user_id: str) -> str:
     dbo = DataBaseOperator()
     user = dbo.searchOne(UserInformation, UserInformation.user_id, user_id)
     if not user:
         return "查询失败,您尚未注册"
     holdings = dbo.searchAllWithField(UserHoldings, UserHoldings.user_id,
                                       user_id)
     user.total_money_amount = 0
     for holding in holdings:
         try:
             price_now = dbo.searchOne(StockInformation,
                                       StockInformation.stock_name,
                                       holding.stock_name).now_price
             user.total_money_amount += holding.stock_amount * price_now
         except:
             return "查询个人信息失败,自选股被非法删除,请联系管理员"
     user.total_money_amount += user.free_money_amount
     dbo.update()
     rst = "您好,{},您当前的账户信息如下:\n".format(user_id)
     if type(user) == list:
         for item in user:
             rst += str(item) + "\n"
             rst += "-------------\n"
     else:
         rst += str(user)
     return rst
def flushStockInformation():
    dbo = DataBaseOperator()
    dbo.delete(StockInformation, StockInformation.stock_name, "API缺少该股信息")
    stock_list = dbo.searchAll(StockInformation)
    for stock in stock_list:
        try:
            df = ts.get_realtime_quotes(stock.stock_id)
            stock.stock_name = df['name'][0]
            stock.now_price = float(df['price'])
            stock.flush_time = datetime.strptime(
                df['date'][0] + " " + df['time'][0], '%Y-%m-%d %H:%M:%S')
            try:
                stock.up_down_rate = 100 * (float(df['price'][0]) - float(
                    df['pre_close'][0])) / float(df['pre_close'][0])
            except:
                stock.up_down_rate = 0
        except:
            stock.stock_name = "API缺少该股信息"
            stock.now_price = 9999
            stock.flush_time = datetime.now()
            stock.up_down_rate = 0
        dbo.update()
    flushAliveOrders()
    global timer
    timer = threading.Timer(global_flush_time * 60, flushStockInformation)
def flushOneNow(stock_id):
    dbo = DataBaseOperator()
    stock = dbo.searchOne(StockInformation, StockInformation.stock_id, stock_id)
    try:
        df = ts.get_realtime_quotes(stock.stock_id)
        stock.stock_name = df['name'][0].replace(' ', '')
        stock.now_price = float(df['price'])
        stock.flush_time = datetime.strptime(df['date'][0] + " " + df['time'][0], '%Y-%m-%d %H:%M:%S')
        try:
            stock.up_down_rate = 100 * (float(df['price'][0]) - float(df['pre_close'][0])) / float(
                df['pre_close'][0])
        except:
            stock.up_down_rate = 0
        dbo.update()
    except:
        return False
    return True
def flushOneNow(stock_id):
    dbo = DataBaseOperator()
    dbo.delete(StockInformation, StockInformation.stock_name, "API缺少该股信息")
    stock = dbo.searchOne(StockInformation, StockInformation.stock_id,
                          stock_id)
    try:
        df = ts.get_realtime_quotes(stock.stock_id)
        stock.stock_name = df['name'][0]
        stock.now_price = float(df['price'])
        stock.flush_time = datetime.strptime(
            df['date'][0] + " " + df['time'][0], '%Y-%m-%d %H:%M:%S')
        try:
            stock.up_down_rate = 100 * (float(df['price'][0]) - float(
                df['pre_close'][0])) / float(df['pre_close'][0])
        except:
            stock.up_down_rate = 0
    except:
        stock.stock_name = "API缺少该股信息"
        stock.now_price = 9999
        stock.flush_time = datetime.now()
        stock.up_down_rate = 0
    dbo.update()
    return stock.stock_name
Ejemplo n.º 5
0
def flushAliveOrders():
    dbo = DataBaseOperator()
    dbo.delete(AliveOrder, AliveOrder.is_alive, False)
    dbo.delete(UserHoldings, UserHoldings.stock_amount, 0)
    order_list = dbo.searchAll(AliveOrder)
    for order in order_list:
        # True是买入
        if order.buy_or_sell:
            stock = dbo.searchOne(StockInformation, StockInformation.stock_id,
                                  order.stock_id)
            print(stock.now_price, order.stock_price, stock.stock_name)
            if stock.now_price <= order.stock_price:
                # 买入
                user = dbo.searchOne(UserInformation, UserInformation.user_id,
                                     order.user_id)
                service_charge = order.order_money_amount * 0.0003
                if service_charge < 5:
                    service_charge = 5
                order.order_money_amount = order.stock_amount * stock.now_price
                if order.order_money_amount <= user.free_money_amount + service_charge:
                    user.free_money_amount -= (order.order_money_amount +
                                               service_charge)
                    order.is_alive = False
                    holdings = dbo.searchOneWithTwoFields(
                        UserHoldings, UserHoldings.user_id, order.user_id,
                        UserHoldings.stock_name, order.stock_name)
                    if holdings:
                        holdings.stock_amount += order.stock_amount
                        holdings.bought_price = order.stock_price
                        holdings.bought_total_price += order.order_money_amount
                    else:
                        holdings = UserHoldings(order.user_id,
                                                order.stock_name,
                                                order.stock_amount,
                                                stock.stock_price,
                                                order.order_money_amount,
                                                datetime.now())
                        dbo.add(holdings)
                dbo.update()
            else:
                continue
                # 卖出订单:
                #   查询表UserInformation的now_price,如果价格低于order的价格,continue
                #   否则,查询user_holdings的stock_amount字段,比较order的amount和holdings的amount,如果order的大,continue。
                #   否则,user的free_money_amount字段+=order的money_amount字段-手续费。holdings-=amount,order的is_alive设为false
        else:
            stock = dbo.searchOne(StockInformation, StockInformation.stock_id,
                                  order.stock_id)
            if stock.now_price > order.stock_price:
                holdings = dbo.searchOne(UserHoldings, UserHoldings.user_id,
                                         order.user_id)
                if holdings.stock_amount >= order.stock_amount:
                    service_charge = order.stock_amount * stock.now_price * 0.0013
                    if service_charge < 5:
                        service_charge = 5
                    user = dbo.searchOne(UserInformation,
                                         UserInformation.user_id,
                                         order.user_id)
                    user.free_money_amount += stock.now_price * order.stock_amount - service_charge
                    holdings.stock_amount -= order.stock_amount
                    order.is_alive = False
                else:
                    continue
            else:
                continue
            dbo.update()