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 searchStock(self):
     dbo = DataBaseOperator()
     objs = dbo.searchAll(StockInformation)
     rst = "股票名 价格 涨跌幅 更新时间\n"
     for obj in objs:
         rst += str(obj) + "\n--------\n"
     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)
Ejemplo n.º 4
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.º 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()