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 cancelOrder(self, user_id: str, alive_order_index: int) -> str: dbo = DataBaseOperator() obj = dbo.searchOneWithTwoFields(AliveOrder, AliveOrder.user_id, user_id, AliveOrder.alive_order_index, alive_order_index) if not obj: return "您要删除的订单不存在" else: dbo.deleteWithTwoFields(AliveOrder, AliveOrder.user_id, user_id, AliveOrder.alive_order_index, alive_order_index) return "订单{}删除成功".format(obj.alive_order_index)
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()