diff --git a/static/images/down-arrow-2.png b/static/images/down-arrow-2.png new file mode 100644 index 0000000..33a16f2 Binary files /dev/null and b/static/images/down-arrow-2.png differ diff --git a/static/images/up-arrow-7.png b/static/images/up-arrow-7.png new file mode 100644 index 0000000..abc7a96 Binary files /dev/null and b/static/images/up-arrow-7.png differ diff --git a/templates/dashboard/base.html b/templates/dashboard/base.html index 6418644..c044548 100644 --- a/templates/dashboard/base.html +++ b/templates/dashboard/base.html @@ -17,6 +17,7 @@ diff --git a/templates/dashboard/closed_trades.html b/templates/dashboard/closed_trades.html index 3a839dd..4d91306 100644 --- a/templates/dashboard/closed_trades.html +++ b/templates/dashboard/closed_trades.html @@ -1,4 +1,5 @@ {% extends "dashboard/base.html" %} +{% load static %} {% load bootstrap3 %} {% load mathfilters %} @@ -14,6 +15,15 @@ Strategy ID {% for closed_trade in closed_trades %} + + {% if closed_trade.direction == "long" %} {% else %} {% endif %} + {{ closed_trade.account }} + {{ closed_trade.security }} + {{ closed_trade.entryTime }} + {{ closed_trade.exitTime }} + {{ closed_trade.profit }} {{ closed_trade.profitCurrency}} + {{ closed_trade.strategyId }} + {% endfor %} {% endblock %} diff --git a/urls.py b/urls.py index 858005e..e9b8610 100644 --- a/urls.py +++ b/urls.py @@ -10,4 +10,5 @@ urlpatterns = [ url(r'^trades/$', views.trades_index, name='trades_index'), url(r'^add_trade/$', views.add_trade, name='add_trade'), url(r'^delete_trade/(?P[^/]+)$', views.delete_trade, name='delete_trade'), + url(r'^closed_trades/$', views.closed_trades_index, name='closed_trades_index'), ] diff --git a/views.py b/views.py index 0306ead..a76b5c4 100644 --- a/views.py +++ b/views.py @@ -4,8 +4,9 @@ from django.template import loader from django.shortcuts import render, get_object_or_404 from django.urls import reverse from django.contrib import messages +from django.db import transaction -from .models import RobotInstance, Trade +from .models import RobotInstance, Trade, ClosedTrade from .forms import NewTradeForm import redis import json @@ -109,3 +110,59 @@ def add_trade(request): return HttpResponse(template.render(context, request)) raise Http404("Invalid method") +@transaction.atomic +def aggregate_unbalanced_trades(): + unbalanced_trades = Trade.objects.filter(balanced=False).order_by('timestamp') + balanced_trades = [] + balances = {} + for trade in unbalanced_trades: + balance_key = '/'.join([trade.account, trade.security, trade.strategyId]) + try: + balance_entry = balances[balance_key] + except KeyError: + balance_entry = { 'balance' : 0} + + print('ts:', trade.timestamp) + if balance_entry['balance'] == 0: + print('new entry: ', balance_key) + balance_entry['balance'] = trade.quantity + direction = '' + if trade.quantity > 0: + direction='long' + else: + direction='short' + balance_entry['trade'] = ClosedTrade(account=trade.account, security=trade.security, entryTime=trade.timestamp, profitCurrency=trade.volumeCurrency, + profit=(-trade.price * trade.quantity), strategyId=trade.strategyId, direction=direction) + balance_entry['ks'] = trade.volume / (trade.price * abs(trade.quantity)) + balance_entry['trade_ids'] = [trade.pk] + else: + print('update entry: ', balance_key) + balance_entry['balance'] += trade.quantity + balance_entry['trade'].profit += -trade.price * trade.quantity + balance_entry['ks'] += trade.volume / (trade.price * abs(trade.quantity)) + balance_entry['ks'] /= 2 + balance_entry['trade_ids'].append(trade.pk) + + print('updated: ', balance_entry['balance']) + if balance_entry['balance'] == 0: + balance_entry['trade'].profit *= balance_entry['ks'] + balance_entry['trade'].exitTime = trade.timestamp + balanced_trades.append((balance_entry['trade'], balance_entry['trade_ids'])) + balances[balance_key] = balance_entry + + for trade, trade_ids in balanced_trades: + trade.save() + for trade_id in trade_ids: + tr = Trade.objects.get(pk=trade_id) + tr.balanced = True + tr.save() + + +def closed_trades_index(request): + aggregate_unbalanced_trades() + closed_trades = ClosedTrade.objects.all() + template = loader.get_template('dashboard/closed_trades.html') + context = { + 'closed_trades' : closed_trades + } + return HttpResponse(template.render(context, request))