Browse Source

Comissions support

master
Denis Tereshkin 8 years ago
parent
commit
20dc9c98e5
  1. 20
      migrations/0004_trade_commission.py
  2. 1
      models.py
  3. 3
      templates/dashboard/closed_trades.html
  4. 4
      templates/dashboard/performance.html
  5. 5
      templates/dashboard/trades.html
  6. 7
      tradesink.py
  7. 8
      views.py

20
migrations/0004_trade_commission.py

@ -0,0 +1,20 @@ @@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.6 on 2017-10-20 02:24
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('dashboard', '0003_closedtrade'),
]
operations = [
migrations.AddField(
model_name='trade',
name='commission',
field=models.DecimalField(decimal_places=10, default=0, max_digits=20),
),
]

1
models.py

@ -14,6 +14,7 @@ class Trade(models.Model): @@ -14,6 +14,7 @@ class Trade(models.Model):
signalId = models.CharField(max_length=64)
comment = models.CharField(max_length=256)
timestamp = models.DateTimeField()
commission = models.DecimalField(max_digits=20, decimal_places=10, default=0)
balanced = models.BooleanField(default=False)
class ClosedTrade(models.Model):

3
templates/dashboard/closed_trades.html

@ -2,6 +2,7 @@ @@ -2,6 +2,7 @@
{% load static %}
{% load bootstrap3 %}
{% load mathfilters %}
{% load humanize %}
{% block content %}
<script src="http://code.highcharts.com/highcharts.js"></script>
@ -37,7 +38,7 @@ @@ -37,7 +38,7 @@
<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.profit|floatformat:2 }} {{ closed_trade.profitCurrency}}</td>
<td>{{ closed_trade.strategyId }}</td>
</tr>
{% endfor %}

4
templates/dashboard/performance.html

@ -31,6 +31,10 @@ @@ -31,6 +31,10 @@
<td>PF</td>
<td> {{ results.total.pf|floatformat:2 }}</td>
</tr>
<tr>
<td>Total commission: </td>
<td> {{ results.total.commission|floatformat:2 }}</td>
</tr>
</table>
<script>

5
templates/dashboard/trades.html

@ -2,6 +2,7 @@ @@ -2,6 +2,7 @@
{% load static %}
{% load bootstrap3 %}
{% load mathfilters %}
{% load humanize %}
{% block content %}
<div class="panel-header">
@ -32,6 +33,7 @@ @@ -32,6 +33,7 @@
<td>Price</td>
<td>Quantity</td>
<td>Volume</td>
<td>Commission</td>
<td>Strategy ID</td>
<td>Signal ID</td>
<td> <a href="#new_trade_form" role="button" data-toggle="collapse">New trade...</a> </td>
@ -42,9 +44,10 @@ @@ -42,9 +44,10 @@
<td>{{ trade.account }}</td>
<td>{{ trade.security }}</td>
<td>{% if trade.quantity > 0 %} Buy {% else %} Sell {% endif %}</td>
<td>{{ trade.price }}</td>
<td>{{ trade.price|floatformat:4}}</td>
<td>{{ trade.quantity|abs }}</td>
<td>{{ trade.volume|stringformat:".3f"}} {{ trade.volumeCurrency }}</td>
<td>{{ trade.commission|floatformat:3 }}</td>
<td>{{ trade.strategyId }}</td>
<td>{{ trade.signalId }}</td>
<td><button type="button" class="btn btn-danger" onclick="if(window.confirm('Confirm deletion')) { document.location.href = '{% url 'delete_trade' trade_id=trade.pk %}'}; return false;">Delete</button></td>

7
tradesink.py

@ -22,9 +22,14 @@ def store_trade(j): @@ -22,9 +22,14 @@ def store_trade(j):
comment = j['order-comment']
except KeyError:
comment = ""
try:
commission = float(j['commission'])
except KeyError:
commission = ""
ts = parse_timestamp(j['execution-time'])
trade = Trade(account=j['account'], security=j['security'], price=float(j['price']), quantity=quantity, volume=float(j['volume']), volumeCurrency=j['volume-currency'], strategyId=j['strategy'],
signalId=j['signal-id'], comment=comment, timestamp=ts)
signalId=j['signal-id'], comment=comment, timestamp=ts, commission=commission)
trade.save()

8
views.py

@ -6,6 +6,7 @@ from django.urls import reverse @@ -6,6 +6,7 @@ from django.urls import reverse
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django.db import transaction
from django.db.models import Sum
from django.contrib.auth import authenticate, login, logout
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
@ -226,6 +227,7 @@ def aggregate_unbalanced_trades(): @@ -226,6 +227,7 @@ def aggregate_unbalanced_trades():
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]
balance_entry['commissions'] = trade.commission
else:
print('update entry: ', balance_key)
balance_entry['balance'] += trade.quantity
@ -233,10 +235,12 @@ def aggregate_unbalanced_trades(): @@ -233,10 +235,12 @@ def aggregate_unbalanced_trades():
balance_entry['ks'] += trade.volume / (trade.price * abs(trade.quantity))
balance_entry['ks'] /= 2
balance_entry['trade_ids'].append(trade.pk)
balance_entry['commissions'] += trade.commission
print('updated: ', balance_entry['balance'])
if balance_entry['balance'] == 0:
balance_entry['trade'].profit *= balance_entry['ks']
balance_entry['trade'].profit -= balance_entry['commissions']
balance_entry['trade'].exitTime = trade.timestamp
balanced_trades.append((balance_entry['trade'], balance_entry['trade_ids']))
balances[balance_key] = balance_entry
@ -323,6 +327,9 @@ def performance(request): @@ -323,6 +327,9 @@ def performance(request):
all_accounts.add(trade.account)
closed_trades = ClosedTrade.objects.exclude(account='demo').order_by('exitTime')
trades = Trade.objects.exclude(account='demo').order_by('timestamp')
trades = Trade.objects.exclude(account='demo').order_by('timestamp')
dates = []
columns = {}
for account in all_accounts:
@ -383,6 +390,7 @@ def performance(request): @@ -383,6 +390,7 @@ def performance(request):
results[trade.account]['loss'] -= trade.profit
results['total']['pf'] = results['total']['profit'] / results['total']['loss']
results['total']['commission'] = trades.aggregate(Sum('commission'))['commission__sum']
template = loader.get_template('dashboard/performance.html')
context = {

Loading…
Cancel
Save