Implement strategies and posting trades

This commit is contained in:
2022-10-27 18:08:40 +01:00
parent 7e4f3f52d1
commit 061c6f6ca7
32 changed files with 1060 additions and 178 deletions

View File

@@ -1,6 +1,8 @@
import re
import uuid
import orjson
from django.conf import settings
from django.contrib.auth.mixins import LoginRequiredMixin
from django.http import HttpResponse, HttpResponseBadRequest
from django.shortcuts import render
@@ -10,6 +12,7 @@ from rest_framework.views import APIView
from serde import ValidationError
from core.forms import HookForm
from core.lib import market
from core.lib.serde import drakdoo_s
from core.models import Callback, Hook
from core.util import logs
@@ -22,6 +25,18 @@ def get_hooks(user):
return hooks
def extract_price(message):
result = re.findall("\d+\.\d+", message) # noqa
if len(result) != 1:
log.error(f"Could not extract price from message: {message}")
return False
try:
log.debug(f"Extracted {result[0]} from '{message}'")
return float(result[0])
except ValueError:
return False
class HookAPI(APIView):
parser_classes = [JSONParser]
@@ -41,17 +56,31 @@ class HookAPI(APIView):
log.error(f"HookAPI POST: {e}")
return HttpResponseBadRequest(e)
price = extract_price(hook_resp.message)
if not price:
log.debug(f"Could not extract price from message: {hook_resp.message}")
return HttpResponseBadRequest("Could not extract price from message")
base = hook_resp.market.item
quote = hook_resp.market.currency
symbol = f"{base.upper()}/{quote.upper()}"
if symbol not in settings.ASSET_FILTER:
log.debug(f"Skipping {symbol} because it is not in the asset filter")
return HttpResponseBadRequest("Invalid symbol")
data = {
"title": hook_resp.title,
"message": hook_resp.message,
"period": hook_resp.period,
"timestamp_sent": hook_resp.timestamp.sent,
"sent": hook_resp.timestamp.sent,
"timestamp_trade": hook_resp.timestamp.trade,
"market_exchange": hook_resp.market.exchange,
"market_item": hook_resp.market.item,
"market_currency": hook_resp.market.currency,
"market_contract": hook_resp.market.contract,
"trade": hook_resp.market.exchange,
"base": hook_resp.market.item,
"quote": hook_resp.market.currency,
"symbol": symbol,
"contract": hook_resp.market.contract,
"price": price,
}
log.debug("HookAPI callback: data: %s", data)
# Try getting the hook
@@ -63,6 +92,7 @@ class HookAPI(APIView):
# Create the callback object
callback = Callback.objects.create(hook=hook, **data)
callback.save()
market.process_callback(callback)
# Bump received count
hook.received = hook.received + 1
hook.save()