Skip to content

Latest commit

 

History

History
46 lines (35 loc) · 1.86 KB

gpt1.md

File metadata and controls

46 lines (35 loc) · 1.86 KB

To get the last trade that the logged-in user made in the given broker within the api_risk function, you need to modify the existing code. Here's how you can achieve this:

def api_risk(req):
    if req.method == "GET":
        user = None
        if req.user.is_authenticated:
            user = User.objects.get(username=req.user)
        
        broker_name = req.GET.get("broker")
        sym_name = req.GET.get("symbol")
        
        broker: Broker = Broker.objects.get(name=broker_name)
        ub: UserBroker = UserBroker.objects.get(user=user, broker=broker)
        sym: Symbol = Symbol.objects.get(broker=broker, name=sym_name)
        
        # Get the last trade for the user and symbol
        trade: Trade = Trade.objects.filter(
            user=user,
            symbol=sym
        ).order_by('-id').first()
        
        if trade:
            ic(trade.risk)
            return JsonResponse({"last_trade": trade.id}, safe=False)
        else:
            return JsonResponse({"message": "No trades found"}, status=404, safe=False)

This modified version does the following:

  1. It filters the Trade objects for the specific user and symbol.
  2. It orders the results by -id (descending order) to get the most recent trade first.
  3. It uses .first() to get only the last trade.
  4. If a trade is found, it returns the trade ID along with the risk value.
  5. If no trades are found, it returns a 404 error.

Make sure to add the necessary imports at the top of your file:

from django.db.models import Prefetch

Also, ensure that your Trade model has a foreign key relationship with both User and Symbol. If it doesn't, you'll need to adjust the query accordingly.

This approach will give you the last trade made by the authenticated user for the specified broker and symbol. You can then use this information as needed in your API response.