Browse Source

Performance: added weekly, monthly view

master
Denis Tereshkin 9 years ago
parent
commit
ce6e4df1af
  1. 5
      templates/dashboard/performance.html
  2. 31
      views.py

5
templates/dashboard/performance.html

@ -5,6 +5,11 @@ @@ -5,6 +5,11 @@
{% load humanize %}
{% block content %}
<ul class="nav nav-pills">
<li role="presentation"{% if timeframe|length == 0 or timeframe == 'daily' %} class="active"{% endif %}><a href="{% url 'performance' %}?timeframe=daily">Daily</a></li>
<li role="presentation"{% if timeframe == 'weekly' %} class="active"{% endif %}><a href="{% url 'performance' %}?timeframe=weekly">Weekly</a></li>
<li role="presentation"{% if timeframe == 'monthly' %} class="active"{% endif %}><a href="{% url 'performance' %}?timeframe=monthly">Monthly</a></li>
</ul>
<script src="http://code.highcharts.com/highcharts.js"></script>
<div id="profits-container" style="width:100%; height:400px;">
</div>

31
views.py

@ -14,6 +14,7 @@ from .forms import NewTradeForm, ClosedTradeFilterForm, LoginForm @@ -14,6 +14,7 @@ from .forms import NewTradeForm, ClosedTradeFilterForm, LoginForm
import redis
import json
import datetime
import calendar
def login_view(request):
if request.method == 'POST':
@ -300,6 +301,8 @@ def performance(request): @@ -300,6 +301,8 @@ def performance(request):
columns = {}
for account in all_accounts:
columns[account] = []
timeframe = request.GET.get('timeframe')
if timeframe == 'daily':
prev_day = None
for trade in closed_trades:
if prev_day != trade.exitTime.date():
@ -308,6 +311,31 @@ def performance(request): @@ -308,6 +311,31 @@ def performance(request):
for account in all_accounts:
columns[account].append(0)
columns[trade.account][-1] += trade.profit
elif timeframe == 'weekly':
epoch = datetime.date(1970, 1, 1)
prev_week = None
for trade in closed_trades:
this_week = (trade.exitTime.date() - epoch).days // 7
if prev_week != this_week:
prev_week = this_week
week_end = epoch + datetime.timedelta(weeks=prev_week + 1)
dates.append(week_end)
for account in all_accounts:
columns[account].append(0)
columns[trade.account][-1] += trade.profit
elif timeframe == 'monthly':
epoch = datetime.date(1970, 1, 1)
prev_month = None
for trade in closed_trades:
this_month = trade.exitTime.date().month
if prev_month != this_month:
prev_month = this_month
this_date = trade.exitTime.date()
month_end = datetime.date(this_date.year, this_date.month, calendar.monthrange(this_date.year, this_date.month)[1])
dates.append(month_end)
for account in all_accounts:
columns[account].append(0)
columns[trade.account][-1] += trade.profit
results = { 'total' : { 'pnl' : 0, 'profit' : 0, 'loss' : 0 } }
@ -333,6 +361,7 @@ def performance(request): @@ -333,6 +361,7 @@ def performance(request):
'user' : request.user,
'dates' : dates,
'columns' : columns,
'results' : results
'results' : results,
'timeframe' : timeframe
}
return HttpResponse(template.render(context, request))

Loading…
Cancel
Save