diff --git a/api/utils.py b/api/utils.py index 4f747657..e138d322 100644 --- a/api/utils.py +++ b/api/utils.py @@ -3,6 +3,8 @@ import requests, ring, os from decouple import config import numpy as np +from api.models import Order + market_cache = {} @ring.dict(market_cache, expire=3) #keeps in cache for 3 seconds @@ -49,7 +51,6 @@ def get_exchange_rates(currencies): return median_rates.tolist() lnd_v_cache = {} - @ring.dict(lnd_v_cache, expire=3600) #keeps in cache for 3600 seconds def get_lnd_version(): @@ -59,7 +60,6 @@ def get_lnd_version(): return lnd_version robosats_commit_cache = {} - @ring.dict(robosats_commit_cache, expire=3600) def get_commit_robosats(): @@ -67,4 +67,22 @@ def get_commit_robosats(): lnd_version = stream.read() return lnd_version + +premium_percentile = {} +@ring.dict(premium_percentile, expire=300) +def compute_premium_percentile(order): + + queryset = Order.objects.filter(currency=order.currency, status=Order.Status.PUB) + + print(len(queryset)) + if len(queryset) <= 1: + return 0.5 + + order_rate = float(order.last_satoshis) / float(order.amount) + rates = [] + for similar_order in queryset: + rates.append(float(similar_order.last_satoshis) / float(similar_order.amount)) + + rates = np.array(rates) + return round(np.sum(rates < order_rate) / len(rates),2) diff --git a/api/views.py b/api/views.py index d1fb2427..6e529a40 100644 --- a/api/views.py +++ b/api/views.py @@ -11,7 +11,7 @@ from django.contrib.auth.models import User from .serializers import ListOrderSerializer, MakeOrderSerializer, UpdateOrderSerializer from .models import LNPayment, MarketTick, Order, Currency from .logics import Logics -from .utils import get_lnd_version, get_commit_robosats +from .utils import get_lnd_version, get_commit_robosats, compute_premium_percentile from .nick_generator.nick_generator import NickGenerator from robohash import Robohash @@ -125,7 +125,7 @@ class OrderView(viewsets.ViewSet): # 3. c) If maker and Public, add num robots in book, premium percentile and num similar orders. if data['is_maker'] and order.status == Order.Status.PUB: data['robots_in_book'] = None # TODO - data['premium_percentile'] = None # TODO + data['premium_percentile'] = compute_premium_percentile(order) data['num_similar_orders'] = len(Order.objects.filter(currency=order.currency, status=Order.Status.PUB)) # 4) Non participants can view details (but only if PUB) @@ -326,9 +326,9 @@ class UserView(APIView): return Response(context, status.HTTP_400_BAD_REQUEST) # Does not allow this 'mistake' if the last login was sometime ago (5 minutes) - if request.user.last_login < timezone.now() - timedelta(minutes=5): - context['bad_request'] = f'You are already logged in as {request.user}' - return Response(context, status.HTTP_400_BAD_REQUEST) + # if request.user.last_login < timezone.now() - timedelta(minutes=5): + # context['bad_request'] = f'You are already logged in as {request.user}' + # return Response(context, status.HTTP_400_BAD_REQUEST) token = request.GET.get(self.lookup_url_kwarg) diff --git a/frontend/src/components/TradeBox.js b/frontend/src/components/TradeBox.js index 1d3c6e72..d0bfab8d 100644 --- a/frontend/src/components/TradeBox.js +++ b/frontend/src/components/TradeBox.js @@ -256,7 +256,7 @@ export default class TradeBox extends Component { - + @@ -272,7 +272,8 @@ export default class TradeBox extends Component { - +