Skip to content

Commit

Permalink
Remove date_str and created_on_str from the models and the views
Browse files Browse the repository at this point in the history
  • Loading branch information
rubik committed Apr 15, 2016
1 parent e4b9a88 commit 6db44c8
Show file tree
Hide file tree
Showing 11 changed files with 76 additions and 50 deletions.
2 changes: 1 addition & 1 deletion history/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
5 changes: 2 additions & 3 deletions history/management/commands/compare_perf.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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()
12 changes: 1 addition & 11 deletions history/management/commands/pull_balance.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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()
3 changes: 1 addition & 2 deletions history/management/commands/pull_deposits.py
Original file line number Diff line number Diff line change
@@ -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


Expand Down Expand Up @@ -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()
2 changes: 0 additions & 2 deletions history/management/commands/pull_prices.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from history.tools import utc_to_mst_str
from django.core.management.base import BaseCommand
from django.conf import settings

Expand Down Expand Up @@ -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()
5 changes: 2 additions & 3 deletions history/management/commands/trade.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down
38 changes: 38 additions & 0 deletions history/migrations/0005_auto_20160415_0555.py
Original file line number Diff line number Diff line change
@@ -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',
),
]
6 changes: 0 additions & 6 deletions history/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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)
Expand Down Expand Up @@ -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):
Expand All @@ -195,15 +192,13 @@ 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):
symbol = models.CharField(max_length=30)
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)
Expand All @@ -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)

Expand Down
6 changes: 3 additions & 3 deletions history/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')


Expand Down
44 changes: 26 additions & 18 deletions history/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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)}}])
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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')}}])
Expand Down Expand Up @@ -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')}}])
Expand Down Expand Up @@ -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(
Expand All @@ -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': {
Expand All @@ -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')}}])
Expand Down Expand Up @@ -333,16 +340,16 @@ 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(
series=[
{'options': {
'source': p},
'terms': [
'created_on_str',
('created_on', utc_to_tz_str),
'price']}
])

Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion pypolo/local_settings.py.example
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand All @@ -14,7 +15,7 @@ DATABASES = {
'USER': os.environ['POSTGRES_USER'],
'PASSWORD': os.environ['POSTGRES_PASSWORD'],
'HOST': 'db',
'PORT': '',
'PORT': '',
'ATOMIC_REQUESTS': True,
},
}
Expand Down

0 comments on commit 6db44c8

Please sign in to comment.