Ejemplo n.º 1
0
 async def buyStock(self, user_id: str, stock_name: str, stock_amount: int, stock_price: float) -> str:
     if self.outTime():
         return "不在交易时间内"
     dbo = DataBaseOperator()
     if not dbo.searchOne(StockInformation, StockInformation.stock_name, stock_name):
         return "订单失败,请先添加自选股"
     else:
         aoi = dbo.searchAll(AliveOrder)
         if aoi:
             alive_order_index = aoi[-1].alive_order_index + 1
         else:
             alive_order_index = 1
         obj = dbo.searchOne(StockInformation, StockInformation.stock_name, stock_name)
         stock_id = obj.stock_id
         order_money_amount = stock_amount * stock_price
         free_money_amount = dbo.searchOne(UserInformation, UserInformation.user_id, user_id).free_money_amount
         if free_money_amount < order_money_amount:
             return "购买订单创建失败,可支配金额不足"
         else:
             alive_order = AliveOrder(user_id=user_id, alive_order_index=alive_order_index,
                                      alive_order_time=datetime.now(),
                                      buy_or_sell=True, stock_id=stock_id,
                                      stock_name=stock_name, stock_price=stock_price,
                                      stock_amount=stock_amount,
                                      order_money_amount=order_money_amount)
             dbo.add(alive_order)
             flushAliveOrders()
             return "购买订单创建成功\n股票:{}\n股数:{}\n预定价格:{}\n总价格:{}\n".format(stock_name, str(stock_amount),
                                                                       str(stock_price),
                                                                       str(order_money_amount))
Ejemplo n.º 2
0
    async def sellStock(self, user_id: str, stock_name: str, stock_amount: int, stock_price: float) -> str:
        if self.outTime():
            return "不在交易时间内"
        dbo = DataBaseOperator()
        stock_amount_obj = dbo.searchOneWithTwoFields(UserHoldings, UserHoldings.stock_name, stock_name,
                                                      UserHoldings.user_id, user_id)
        if not stock_amount_obj:
            return "卖出订单创建失败,您没有购买该支股票"
        else:
            now = datetime.now().strftime('%Y-%m-%d')
            bought_time = stock_amount_obj.bought_time.strftime('%Y-%m-%d')
            if now == bought_time:
                return "卖出订单创建失败,您必须在购买成功后的T+1日才能卖出"
            if stock_amount > stock_amount_obj.stock_amount:
                return "卖出订单创建失败,您没有足够的持仓,当前持仓为:" + str(stock_amount_obj.stock_amount)
            else:
                aoi = dbo.searchAll(AliveOrder)
                if aoi:
                    alive_order_index = aoi[-1].alive_order_index + 1
                else:
                    alive_order_index = 1
                stock_id = dbo.searchOne(StockInformation, StockInformation.stock_name, stock_name).stock_id
                alive_order = AliveOrder(user_id=user_id, alive_order_index=alive_order_index,
                                         alive_order_time=datetime.now(),
                                         buy_or_sell=False, stock_id=stock_id,
                                         stock_name=stock_name, stock_price=stock_price,
                                         stock_amount=stock_amount,
                                         order_money_amount=stock_amount * stock_price)

                dbo.add(alive_order)
                flushAliveOrders()
                return "卖出订单创建成功\n股票:{}\n股数:{}\n预定价格:{}\n总价格:{}\n".format(stock_name, str(stock_amount),
                                                                          str(stock_price),
                                                                          str(stock_amount * stock_price))
Ejemplo n.º 3
0
    async def searchAliveOrders(self, user_id: str) -> str:
        dbo = DataBaseOperator()
        dbo.delete(AliveOrder, AliveOrder.is_alive, False)
        flushAliveOrders()
        aliveorders = dbo.searchAllWithField(AliveOrder, AliveOrder.user_id, user_id)
        if not aliveorders:
            return "您当前没有有效订单"
        else:
            rst = "您好,{},您当前的订单情况是:\n".format(user_id)
            if type(aliveorders) == list:
                for item in aliveorders:
                    rst += str(item) + "\n"
                    rst += "-------------\n"
            else:
                rst += str(aliveorders)

            return rst