Skip to content

Commit

Permalink
Merge pull request #3 from adolfojmnz/dev
Browse files Browse the repository at this point in the history
Fix Bugs On Metrics Endpoints
  • Loading branch information
adolfojmnz authored Nov 25, 2023
2 parents 5b6beac + bb3912c commit aba896c
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 30 deletions.
44 changes: 22 additions & 22 deletions trades/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ class MetricsSummarySerializer(TradeMetricsMixin, Serializer):
average_holding_time = SerializerMethodField()
average_position_volume = SerializerMethodField()

def __init__(self, queryset=None, *args, **kwargs):
self.queryset = queryset or Trade.objects.all()
def __init__(self, queryset, *args, **kwargs):
self.queryset = queryset
return super().__init__(*args, **kwargs)

def get_net_profit(self, *args, **kwargs):
Expand Down Expand Up @@ -107,7 +107,7 @@ def get_average_holding_time(self, *args, **kwargs):

def get_average_position_volume(self, *args, **kwargs):
results = self.queryset.aggregate(Avg("volume"))["volume__avg"]
return round(results, 2)
return round(results or 0, 2)

class Meta:
fields = [
Expand Down Expand Up @@ -137,8 +137,8 @@ class ProfitAndLossSerializer(Serializer):
average_profit = SerializerMethodField()
average_loss = SerializerMethodField()

def __init__(self, queryset=None, *args, **kwargs):
self.queryset = queryset or Trade.objects.all()
def __init__(self, queryset, *args, **kwargs):
self.queryset = queryset
return super().__init__(*args, **kwargs)

def get_net_profit(self, *args, **kwargs):
Expand Down Expand Up @@ -212,8 +212,8 @@ class TotalTradesSerializer(TradeMetricsMixin, Serializer):
total_long_positions = SerializerMethodField()
total_short_positions = SerializerMethodField()

def __init__(self, queryset=None, *args, **kwargs):
self.queryset = queryset or Trade.objects.all()
def __init__(self, queryset, *args, **kwargs):
self.queryset = queryset
return super().__init__(*args, **kwargs)

def get_total_trades(self, *args, **kwargs):
Expand Down Expand Up @@ -248,8 +248,8 @@ class HoldingTimeSerializer(TradeMetricsMixin, Serializer):
average_holding_time_per_long_position = SerializerMethodField()
average_holding_time_per_short_position = SerializerMethodField()

def __init__(self, queryset=None, *args, **kwargs):
self.queryset = queryset or Trade.objects.all()
def __init__(self, queryset, *args, **kwargs):
self.queryset = queryset
return super().__init__(*args, **kwargs)

def get_average_holding_time(self, *args, **kwargs):
Expand Down Expand Up @@ -314,65 +314,65 @@ class PositionVolumeSerializer(TradeMetricsMixin, Serializer):
average_position_volume_per_winning_trade = SerializerMethodField()
average_position_volume_per_losing_trade = SerializerMethodField()

def __init__(self, queryset=None, *args, **kwargs):
self.queryset = queryset or Trade.objects.all()
def __init__(self, queryset, *args, **kwargs):
self.queryset = queryset
return super().__init__(*args, **kwargs)

def get_min_position_volume(self, *args, **kwargs):
return self.queryset.aggregate(Min("volume"))["volume__min"]
return self.queryset.aggregate(Min("volume"))["volume__min"] or 0

def get_max_position_volume(self, *args, **kwargs):
return self.queryset.aggregate(Max("volume"))["volume__max"]
return self.queryset.aggregate(Max("volume"))["volume__max"] or 0

def get_min_position_volume_per_winning_trade(self, *args, **kwargs):
return self.queryset.filter(pnl__gt=0).aggregate(
Min("volume")
)["volume__min"]
)["volume__min"] or 0

def get_min_position_volume_per_losing_trade(self, *args, **kwargs):
return self.queryset.filter(pnl__lt=0).aggregate(
Min("volume")
)["volume__min"]
)["volume__min"] or 0

def get_max_position_volume_per_winning_trade(self, *args, **kwargs):
return self.queryset.filter(pnl__gt=0).aggregate(
Max("volume")
)["volume__max"]
)["volume__max"] or 0

def get_max_position_volume_per_losing_trade(self, *args, **kwargs):
return self.queryset.filter(pnl__lt=0).aggregate(
Max("volume")
)["volume__max"]
)["volume__max"] or 0

def get_average_position_volume(self, *args, **kwargs):
results = self.queryset.aggregate(
Avg("volume")
)["volume__avg"]
return round(results, 2)
return round(results or 0, 2)

def get_average_position_volume_per_winning_trade(self, *args, **kwargs):
results = self.queryset.filter(pnl__gt=0).aggregate(Avg("volume"))[
"volume__avg"
]
return round(results, 2)
return round(results or 0, 2)

def get_average_position_volume_per_losing_trade(self, *args, **kwargs):
results = self.queryset.filter(pnl__lt=0).aggregate(Avg("volume"))[
"volume__avg"
]
return round(results, 2)
return round(results or 0, 2)

def get_average_position_volume_per_long_position(self, *args, **kwargs):
results = self.queryset.filter(type="L").aggregate(
Avg("volume")
)["volume__avg"]
return round(results, 2)
return round(results or 0, 2)

def get_average_position_volume_per_short_position(self, *args, **kwargs):
results = self.queryset.filter(type="S").aggregate(
Avg("volume")
)["volume__avg"]
return round(results, 2)
return round(results or 0, 2)

class Meta:
fields = [
Expand Down
6 changes: 4 additions & 2 deletions trades/view_mixins.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from rest_framework import status
from rest_framework.response import Response
from rest_framework.generics import GenericAPIView
from rest_framework.permissions import IsAuthenticated

from trades.models import Trade
Expand All @@ -19,17 +20,18 @@ def get_queryset(self):
)


class MetricsViewMixin:
class MetricsViewMixin(GenericAPIView):
model = Trade
queryset = model.objects.all()
permission_classes = [IsAuthenticated]
filter_backends = [TradeFilterBackend]
filterset_class = TradeFilterSet

def get_queryset(self):
return self.queryset.filter(
queryset = super().get_queryset().filter(
user=self.request.user
)
return self.filter_queryset(queryset)

def get(self, request, *args, **kwargs):
serializer = self.serializer_class(
Expand Down
11 changes: 5 additions & 6 deletions trades/views.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from rest_framework.views import APIView
from rest_framework.generics import (
ListCreateAPIView,
RetrieveUpdateDestroyAPIView,
Expand All @@ -23,21 +22,21 @@ class TradeDetailView(TradeViewMixin, RetrieveUpdateDestroyAPIView):
serializer_class = TradeSerializer


class MetricsSummaryView(MetricsViewMixin, APIView):
class MetricsSummaryView(MetricsViewMixin):
serializer_class = MetricsSummarySerializer


class ProfitAndLossView(MetricsViewMixin, APIView):
class ProfitAndLossView(MetricsViewMixin):
serializer_class = ProfitAndLossSerializer


class TotalTradesView(MetricsViewMixin, APIView):
class TotalTradesView(MetricsViewMixin):
serializer_class = TotalTradesSerializer


class HoldingTimeView(MetricsViewMixin, APIView):
class HoldingTimeView(MetricsViewMixin):
serializer_class = HoldingTimeSerializer


class PositionVolumeView(MetricsViewMixin, APIView):
class PositionVolumeView(MetricsViewMixin):
serializer_class = PositionVolumeSerializer

0 comments on commit aba896c

Please sign in to comment.