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
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))
async def searchOneStock(self, stock_name): dbo = DataBaseOperator() stock_id = dbo.searchOne(AllStock,AllStock.stock_name,stock_name).stock_id flushOneNow(stock_id) obj = dbo.searchOne(StockInformation, StockInformation.stock_name, stock_name) if obj: return str(obj) else: return "未找到股票"
async def addSelfStock(self, stock_name: str) -> str: dbo = DataBaseOperator() obj = dbo.searchOne(StockInformation, StockInformation.stock_name, stock_name) if obj: return "自选股已经被添加过\n" + + obj.stock_name else: stock_id = dbo.searchOne(AllStock, AllStock.stock_name, stock_name).stock_id new_obj = StockInformation(stock_id=stock_id, stock_name=stock_name, now_price=-1, flush_time=datetime.now(), up_down_rate=0) dbo.add(new_obj) stock_name = flushOneNow(stock_id) return "自选股添加成功\n" + str(stock_name)
async def searchOneStockWithID(self, stock_id): dbo = DataBaseOperator() obj = dbo.searchOne(StockInformation, StockInformation.stock_id, stock_id) if obj: if flushOneNow(stock_id): obj = dbo.searchOne(StockInformation, StockInformation.stock_id, stock_id) return str(obj) else: return "API查询失败,请稍后再次尝试" else: return "查询失败,请先添加自选股"
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))
async def addSelfStockWithID(self, stock_id: str) -> str: dbo = DataBaseOperator() obj = dbo.searchOne(StockInformation, StockInformation.stock_id, stock_id) if obj: return "自选股已经被添加过\n" + obj.stock_name else: try: df = ts.get_realtime_quotes(stock_id) stock_name = df['name'][0].replace(' ', '') now_price = float(df['price']) flush_time = datetime.strptime( df['date'][0] + " " + df['time'][0], '%Y-%m-%d %H:%M:%S') except: return "API缺少该股信息" try: up_down_rate = 100 * (float(df['price'][0]) - float( df['pre_close'][0])) / float(df['pre_close'][0]) except: up_down_rate = 0 new_obj = StockInformation(stock_id=stock_id, stock_name=stock_name, now_price=now_price, flush_time=flush_time, up_down_rate=up_down_rate) try: dbo.add(new_obj) except: pass # 下面,添加到all_stock中 new_stock = AllStock(stock_id=stock_id, stock_name=stock_name) dbo.add(new_stock) flushOneNow(stock_id) return "自选股添加成功\n" + str(stock_name)
async def searchOneStock(self, stock_name): dbo = DataBaseOperator() try: stock_id = dbo.searchOne(AllStock, AllStock.stock_name, stock_name).stock_id except: return "查询失败,API缺少该股信息" obj = dbo.searchOne(StockInformation, StockInformation.stock_name, stock_name) if obj: if flushOneNow(stock_id): return str(obj) else: return "API查询失败,请稍后再次尝试" else: return "查询失败,请先添加自选股"
async def register(self, user_id: str, user_name: str) -> str: dbo = DataBaseOperator() print(user_id, user_name) if dbo.searchOne(UserInformation, UserInformation.user_id, user_id): return "您已注册\n" + user_id + "\n" + user_name else: user = UserInformation(user_id=user_id, user_name=user_name, free_money_amount=begin_money, total_money_amount=begin_money) dbo.add(user) return "注册成功\n" + user_id + "\n" + user_name
async def deleteSelfStock(self, stock_name: str) -> str: dbo = DataBaseOperator() orders = dbo.searchAllWithField(AliveOrder, AliveOrder.stock_name, stock_name) holdings = dbo.searchAllWithField(UserHoldings, UserHoldings.stock_name, stock_name) if orders or holdings: return "已有订单或持仓,不能删除" else: if dbo.searchOne(StockInformation, StockInformation.stock_name, stock_name): dbo.delete(StockInformation, StockInformation.stock_name, stock_name) return stock_name + "删除成功" else: return "您要删除的自选股不存在,喂!不要乱玩这个功能!"
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
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()