Browse Source

Closed trades index

master
Denis Tereshkin 9 years ago
parent
commit
b467466d7d
  1. BIN
      static/images/down-arrow-2.png
  2. BIN
      static/images/up-arrow-7.png
  3. 1
      templates/dashboard/base.html
  4. 10
      templates/dashboard/closed_trades.html
  5. 1
      urls.py
  6. 59
      views.py

BIN
static/images/down-arrow-2.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.5 KiB

BIN
static/images/up-arrow-7.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.9 KiB

1
templates/dashboard/base.html

@ -17,6 +17,7 @@ @@ -17,6 +17,7 @@
<ul class="nav navbar-nav">
<li><a href="{% url 'overview' %}">Overview</a></li>
<li><a href="{% url 'trades_index' %}">Trades</a></li>
<li><a href="{% url 'closed_trades_index' %}">Closed</a></li>
</ul>
</nav>

10
templates/dashboard/closed_trades.html

@ -1,4 +1,5 @@ @@ -1,4 +1,5 @@
{% extends "dashboard/base.html" %}
{% load static %}
{% load bootstrap3 %}
{% load mathfilters %}
@ -14,6 +15,15 @@ @@ -14,6 +15,15 @@
<td>Strategy ID</td>
</tr>
{% for closed_trade in closed_trades %}
<tr class="{% if closed_trade.profit > 0 %}success{% else %}danger{% endif %}">
<td style="width: 32px;">{% if closed_trade.direction == "long" %}<img src="{% static "images/up-arrow-7.png" %}" class="img-responsive"/> {% else %}<img src="{% static "images/down-arrow-2.png" %}" class="img-responsive"/> {% endif %}</td>
<td>{{ closed_trade.account }}</td>
<td>{{ closed_trade.security }}</td>
<td>{{ closed_trade.entryTime }}</td>
<td>{{ closed_trade.exitTime }}</td>
<td>{{ closed_trade.profit }} {{ closed_trade.profitCurrency}}</td>
<td>{{ closed_trade.strategyId }}</td>
</tr>
{% endfor %}
</table>
{% endblock %}

1
urls.py

@ -10,4 +10,5 @@ urlpatterns = [ @@ -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<trade_id>[^/]+)$', views.delete_trade, name='delete_trade'),
url(r'^closed_trades/$', views.closed_trades_index, name='closed_trades_index'),
]

59
views.py

@ -4,8 +4,9 @@ from django.template import loader @@ -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): @@ -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))

Loading…
Cancel
Save