diff --git a/history/admin.py b/history/admin.py index d227c90..b9f2419 100644 --- a/history/admin.py +++ b/history/admin.py @@ -16,7 +16,7 @@ class BalanceAdmin(admin.ModelAdmin): class TradeAdmin(admin.ModelAdmin): ordering = ['-created_on'] search_fields = ['type', 'symbol'] - list_display = ['pk', 'price', 'status', 'created_on_str', 'symbol', 'type', 'amount'] + list_display = ['pk', 'price', 'status', 'symbol', 'type', 'amount'] readonly_fields = ['recommendation', 'algo'] def recommendation(self, obj): diff --git a/history/management/commands/compare_perf.py b/history/management/commands/compare_perf.py index a97d2fa..d85b197 100644 --- a/history/management/commands/compare_perf.py +++ b/history/management/commands/compare_perf.py @@ -1,6 +1,6 @@ from django.core.management.base import BaseCommand from django.conf import settings -from history.tools import get_fee_amount, utc_to_mst_str +from history.tools import get_fee_amount from history.models import Price, TradeRecommendation, PerformanceComp import datetime @@ -60,6 +60,5 @@ def handle(self, *args, **options): rec_count=trs.count(), weighted_avg_nn_rec=weighted_avg_nn_rec, directionally_same=directionally_same, - directionally_same_int=1 if directionally_same else 0, - created_on_str=utc_to_mst_str(tr_timerange_end)) + directionally_same_int=1 if directionally_same else 0) pc.save() diff --git a/history/management/commands/pull_balance.py b/history/management/commands/pull_balance.py index 8c4d488..e413c4a 100644 --- a/history/management/commands/pull_balance.py +++ b/history/management/commands/pull_balance.py @@ -1,7 +1,6 @@ from django.core.management.base import BaseCommand from django.conf import settings -from history.tools import (get_exchange_rate_to_btc, get_exchange_rate_btc_to_usd, get_deposit_balance, - utc_to_mst_str) +from history.tools import get_exchange_rate_to_btc, get_exchange_rate_btc_to_usd, get_deposit_balance from history.models import Balance, Trade import datetime from django.db import transaction @@ -39,12 +38,3 @@ def handle(self, *args, **options): deposited_amount_btc=deposited_amount_btc if ticker == 'BTC' else 0.00, deposited_amount_usd=deposited_amount_usd if ticker == 'BTC' else 0.00) b.save() - - for b in Balance.objects.filter(date_str='0'): - b.date_str = utc_to_mst_str(b.created_on) - b.save() - - # normalize trade recommendations too. merp - for tr in Trade.objects.filter(created_on_str=''): - tr.created_on_str = utc_to_mst_str(tr.created_on) - tr.save() diff --git a/history/management/commands/pull_deposits.py b/history/management/commands/pull_deposits.py index e123fba..6878dcb 100644 --- a/history/management/commands/pull_deposits.py +++ b/history/management/commands/pull_deposits.py @@ -1,7 +1,7 @@ from django.core.management.base import BaseCommand from django.conf import settings import datetime -from history.tools import get_utc_unixtime, utc_to_mst_str +from history.tools import get_utc_unixtime from history.models import Deposit @@ -35,5 +35,4 @@ def handle(self, *args, **options): d.status = status d.created_on = created_on d.modified_on = created_on - d.created_on_str = utc_to_mst_str(created_on) d.save() diff --git a/history/management/commands/pull_prices.py b/history/management/commands/pull_prices.py index fb4b8db..107c3bc 100644 --- a/history/management/commands/pull_prices.py +++ b/history/management/commands/pull_prices.py @@ -1,4 +1,3 @@ -from history.tools import utc_to_mst_str from django.core.management.base import BaseCommand from django.conf import settings @@ -26,5 +25,4 @@ def handle(self, *args, **options): p.lowestask = price[ticker]['lowestAsk'] p.highestbid = price[ticker]['highestBid'] p.symbol = ticker - p.created_on_str = utc_to_mst_str(p.created_on) p.save() diff --git a/history/management/commands/trade.py b/history/management/commands/trade.py index a769c47..8c4ba2d 100644 --- a/history/management/commands/trade.py +++ b/history/management/commands/trade.py @@ -1,6 +1,6 @@ from django.core.management.base import BaseCommand from history.models import Price, PredictionTest, Trade, TradeRecommendation, Balance, ClassifierTest -from history.tools import get_utc_unixtime, print_and_log, utc_to_mst_str +from history.tools import get_utc_unixtime, print_and_log import datetime import time from history.poloniex import poloniex @@ -191,8 +191,7 @@ def run_predictor(self, nn_index): clf=clf, confidence=confidence, recommendation=recommend, - net_amount=-1 if recommend == 'SELL' else (1 if recommend == 'BUY' else 0), - created_on_str=utc_to_mst_str(timezone.now())) + net_amount=-1 if recommend == 'SELL' else (1 if recommend == 'BUY' else 0)) tr.save() self.trs[nn_index] = tr return recommend diff --git a/history/migrations/0005_auto_20160415_0555.py b/history/migrations/0005_auto_20160415_0555.py new file mode 100644 index 0000000..7448781 --- /dev/null +++ b/history/migrations/0005_auto_20160415_0555.py @@ -0,0 +1,38 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import models, migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('history', '0004_auto_20160409_1213'), + ] + + operations = [ + migrations.RemoveField( + model_name='balance', + name='date_str', + ), + migrations.RemoveField( + model_name='deposit', + name='created_on_str', + ), + migrations.RemoveField( + model_name='performancecomp', + name='created_on_str', + ), + migrations.RemoveField( + model_name='price', + name='created_on_str', + ), + migrations.RemoveField( + model_name='trade', + name='created_on_str', + ), + migrations.RemoveField( + model_name='traderecommendation', + name='created_on_str', + ), + ] diff --git a/history/models.py b/history/models.py index 742cdf1..e37db3e 100644 --- a/history/models.py +++ b/history/models.py @@ -122,7 +122,6 @@ class Deposit(TimeStampedModel): type = models.CharField(max_length=10) txid = models.CharField(max_length=500, default='') status = models.CharField(max_length=100, default='none') - created_on_str = models.CharField(max_length=50, default='') class Trade(TimeStampedModel): @@ -134,7 +133,6 @@ class Trade(TimeStampedModel): orderNumber = models.CharField(max_length=50, default='') status = models.CharField(max_length=10, default='none') net_amount = models.FloatField(null=True) - created_on_str = models.CharField(max_length=50, default='') fee_amount = models.FloatField(null=True) btc_amount = models.FloatField(null=True) usd_amount = models.FloatField(null=True) @@ -183,7 +181,6 @@ class Price(TimeStampedModel): volume = models.FloatField(null=True) lowestask = models.FloatField(null=True) highestbid = models.FloatField(null=True) - created_on_str = models.CharField(max_length=50, default='') class Balance(TimeStampedModel): @@ -195,7 +192,6 @@ class Balance(TimeStampedModel): exchange_to_usd_rate = models.FloatField(null=True) deposited_amount_usd = models.FloatField(default=0.00) deposited_amount_btc = models.FloatField(default=0.00) - date_str = models.CharField(max_length=20, default='0', db_index=True) class PerformanceComp(TimeStampedModel): @@ -203,7 +199,6 @@ class PerformanceComp(TimeStampedModel): nn_rec = models.FloatField() actual_movement = models.FloatField() delta = models.FloatField() - created_on_str = models.CharField(max_length=30) directionally_same = models.BooleanField(default=False) directionally_same_int = models.IntegerField(default=0) weighted_avg_nn_rec = models.FloatField(default=0) @@ -224,7 +219,6 @@ class TradeRecommendation(TimeStampedModel): made_on = models.TextField(max_length=30) recommendation = models.CharField(max_length=30) confidence = models.FloatField() - created_on_str = models.CharField(max_length=30, default='') net_amount = models.FloatField(default=0) trade = models.ForeignKey('Trade', null=True, db_index=True) diff --git a/history/tools.py b/history/tools.py index 4ce5a70..8b05d7a 100644 --- a/history/tools.py +++ b/history/tools.py @@ -18,9 +18,9 @@ def get_utc_unixtime(): return int(unixtime) -def utc_to_mst_str(dt): - mst = pytz.timezone('MST') - local = timezone.localtime(dt, mst) +def utc_to_tz_str(dt): + tzname = getattr(settings, 'DISPLAY_TZ', 'UTC') + local = timezone.localtime(dt, pytz.timezone(tzname)) return datetime.datetime.strftime(local, '%Y-%m-%d %H:%M') diff --git a/history/views.py b/history/views.py index 9441374..3973f30 100644 --- a/history/views.py +++ b/history/views.py @@ -9,7 +9,7 @@ from django.db.models import Avg, Max, Min, Sum, Count # Create your views here. from chartit import DataPool, Chart, PivotDataPool, PivotChart -from history.tools import median_value, get_cost_basis +from history.tools import median_value, get_cost_basis, utc_to_tz_str from django.conf import settings @@ -114,7 +114,7 @@ def get_balance_breakdown_chart(bs, denom, symbol, start_time): series=[ {'options': { 'source': bs.filter(created_on__gte=start_time).order_by('-created_on').all(), - 'categories': 'date_str', + 'categories': 'date', 'legend_by': 'symbol'}, 'terms': { 'total_value': Sum(denom)}}]) @@ -148,7 +148,7 @@ def get_balance_chart(bs, denom, symbol, start_time): series=[ {'options': { 'source': bs.filter(created_on__gte=start_time).order_by('-created_on').all(), - 'categories': 'date_str' + 'categories': 'date' }, 'terms': { 'total_value': Sum(denom), 'total_invested': Sum(dep_amount_fieldname), @@ -177,17 +177,17 @@ def get_balance_chart(bs, denom, symbol, start_time): def get_trade_chart(bs, denom, symbol, start_time): if settings.MAKE_TRADES: - trades = Trade.objects.exclude(created_on_str="").filter( + trades = Trade.objects.filter( symbol=symbol, created_on__gte=start_time).filter(status__in=['fill', 'open', 'error']).order_by('id') else: - trades = Trade.objects.exclude(created_on_str="").filter( + trades = Trade.objects.filter( symbol=symbol, created_on__gte=start_time).order_by('id') ds = PivotDataPool( series=[ {'options': { 'source': trades, - 'categories': 'created_on_str', + 'categories': 'created_on', 'legend_by': 'status'}, 'terms': { 'total_value': Sum('net_amount')}}]) @@ -217,17 +217,17 @@ def get_trade_chart(bs, denom, symbol, start_time): def get_trade_profitability_chart(bs, denom, symbol, start_time): if settings.MAKE_TRADES: - trades = Trade.objects.exclude(created_on_str="").filter( + trades = Trade.objects.filter( symbol=symbol, created_on__gte=start_time).filter(status__in=['fill', 'open', 'error']).order_by('id') else: - trades = Trade.objects.exclude(created_on_str="").filter( + trades = Trade.objects.filter( symbol=symbol, created_on__gte=start_time).order_by('id') ds = PivotDataPool( series=[ {'options': { 'source': trades, - 'categories': 'created_on_str', + 'categories': 'created_on', 'legend_by': 'status'}, 'terms': { 'total_value': Sum('btc_net_profit')}}]) @@ -267,8 +267,15 @@ def get_performance_comps_chart(bs, denom, symbol, start_time): series=[ {'options': { 'source': pcs}, - 'terms': ['created_on_str', 'delta', 'actual_movement', 'nn_rec', - 'pct_buy', 'pct_sell', 'weighted_avg_nn_rec']} + 'terms': [ + ('created_on', utc_to_tz_str), + 'delta', + 'actual_movement', + 'nn_rec', + 'pct_buy', + 'pct_sell', + 'weighted_avg_nn_rec' + ]} ]) cht = Chart( @@ -278,8 +285,8 @@ def get_performance_comps_chart(bs, denom, symbol, start_time): 'type': 'line', 'stacking': False}, 'terms': { - 'created_on_str': ['delta', 'actual_movement', 'nn_rec', - 'pct_buy', 'pct_sell', 'weighted_avg_nn_rec'] + 'created_on': ['delta', 'actual_movement', 'nn_rec', + 'pct_buy', 'pct_sell', 'weighted_avg_nn_rec'] }}], chart_options={ 'title': { @@ -304,7 +311,7 @@ def get_directional_change_chart(bs, denom, symbol, start_time): series=[ {'options': { 'source': pcs, - 'categories': 'created_on_str' + 'categories': 'created_on', }, 'terms': { 'total_value': Sum('directionally_same_int')}}]) @@ -333,8 +340,8 @@ def get_ticker_price(bs, denom, symbol, start_time): p = Price.objects.none() for minute in [0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55]: - p = p | Price.objects.exclude(created_on_str="").filter(symbol=symbol, created_on__gte=start_time, - created_on__minute=minute) + p = p | Price.objects.filter(symbol=symbol, created_on__gte=start_time, + created_on__minute=minute) p = p.order_by('created_on') ds = DataPool( @@ -342,7 +349,7 @@ def get_ticker_price(bs, denom, symbol, start_time): {'options': { 'source': p}, 'terms': [ - 'created_on_str', + ('created_on', utc_to_tz_str), 'price']} ]) @@ -353,13 +360,14 @@ def get_ticker_price(bs, denom, symbol, start_time): 'type': 'line', 'stacking': False}, 'terms': { - 'created_on_str': [ + 'created_on': [ 'price'] }}], chart_options={ 'title': { 'text': 'Price Over Time {}'.format(symbol)}, 'xAxis': { + 'type': 'datetime', 'title': { 'text': 'Time'}}}) return cht diff --git a/pypolo/local_settings.py.example b/pypolo/local_settings.py.example index 970e73b..bfa7d20 100644 --- a/pypolo/local_settings.py.example +++ b/pypolo/local_settings.py.example @@ -3,6 +3,7 @@ import os BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) MAKE_TRADES = False +DISPLAY_TZ = 'UTC' API_KEY = os.environ['POLONIEX_API_KEY'] API_SECRET = os.environ['POLONIEX_API_SECRET'] @@ -14,7 +15,7 @@ DATABASES = { 'USER': os.environ['POSTGRES_USER'], 'PASSWORD': os.environ['POSTGRES_PASSWORD'], 'HOST': 'db', - 'PORT': '', + 'PORT': '', 'ATOMIC_REQUESTS': True, }, }