Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use UTC datetimes everywhere #69

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
12 changes: 6 additions & 6 deletions history/management/commands/alert_fail_cases.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import datetime
from django.utils import timezone
from django.conf import settings
from django.core.management.base import BaseCommand
from history.models import PredictionTest, TradeRecommendation, get_time
from history.models import PredictionTest, TradeRecommendation


class Command(BaseCommand):
Expand Down Expand Up @@ -35,11 +36,10 @@ def handle(self, *args, **options):
print(last_pt.created_on)
print(last_trade.created_on)

# 7 hours thing is a hack for MST vs UTC timezone issues
is_trader_running = last_trade.created_on > (
get_time() - datetime.timedelta(hours=int(7)) - datetime.timedelta(minutes=int(15)))
is_trainer_running = last_pt.created_on > (get_time() - datetime.timedelta(hours=int(7)) -
datetime.timedelta(minutes=int(15)))
# Since USE_TZ=True, created_on is already UTC, like timezone.now()
fifteen_ago = timezone.now() - datetime.timedelta(minutes=15)
is_trader_running = last_trade.created_on > fifteen_ago
is_trainer_running = last_pt.created_on > fifteen_ago

if not is_trader_running:
self.alert_email("not is_trader_running")
Expand Down
3 changes: 1 addition & 2 deletions history/management/commands/compare_perf.py
Original file line number Diff line number Diff line change
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=(tr_timerange_end - datetime.timedelta(hours=7)).strftime('%Y-%m-%d %H:%M'))
directionally_same_int=1 if directionally_same else 0)
pc.save()
15 changes: 1 addition & 14 deletions history/management/commands/pull_balance.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
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
from history.models import Balance, Trade
import datetime
from history.models import Balance
from django.db import transaction

import warnings
Expand Down Expand Up @@ -38,15 +37,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'):
# django timezone stuff , FML
b.date_str = datetime.datetime.strftime(b.created_on - datetime.timedelta(hours=int(7)), '%Y-%m-%d %H:%M')
b.save()

# normalize trade recommendations too. merp
for tr in Trade.objects.filter(created_on_str=''):
# django timezone stuff , FML
tr.created_on_str = datetime.datetime.strftime(
tr.created_on - datetime.timedelta(hours=int(7)), '%Y-%m-%d %H:%M')
tr.save()
2 changes: 0 additions & 2 deletions history/management/commands/pull_deposits.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,4 @@ def handle(self, *args, **options):
d.status = status
d.created_on = created_on
d.modified_on = created_on
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line has no effect since, auto_now_add is used for modified_on?

Copy link
Contributor Author

@rubik rubik Apr 18, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! We are in the situation described here:
#75 (comment)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think @owocki intention was to capture the time of creation on the poloniex server with this.
A proposal could be we add deposit_created_on to Deposit model and use created_on for the creation of the model in our database.
In short:
deposit_created_on - time of creation at poloniex server
created_on - time of saving in our database

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems reasonable. The only downside of this approach is that we will have to remember to use deposit_created_on instead of created_on.

d.created_on_str = datetime.datetime.strftime(
created_on - datetime.timedelta(hours=int(7)), '%Y-%m-%d %H:%M')
d.save()
1 change: 0 additions & 1 deletion history/management/commands/pull_prices.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,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 = 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,5 +1,5 @@
from django.core.management.base import BaseCommand
from history.models import Price, PredictionTest, Trade, TradeRecommendation, Balance, get_time, ClassifierTest
from history.models import Price, PredictionTest, Trade, TradeRecommendation, Balance, ClassifierTest
from history.tools import get_utc_unixtime, print_and_log
import datetime
import time
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=str(get_time().strftime('%Y-%m-%d %H:%M')))
net_amount=-1 if recommend == 'SELL' else (1 if recommend == 'BUY' else 0))
tr.save()
self.trs[nn_index] = tr
return recommend
Expand Down
94 changes: 94 additions & 0 deletions history/migrations/0004_auto_20160409_1213.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations


class Migration(migrations.Migration):

dependencies = [
('history', '0003_auto_20160330_1920'),
]

operations = [
migrations.AlterField(
model_name='balance',
name='created_on',
field=models.DateTimeField(auto_now_add=True, db_index=True),
),
migrations.AlterField(
model_name='balance',
name='modified_on',
field=models.DateTimeField(auto_now=True),
),
migrations.AlterField(
model_name='classifiertest',
name='created_on',
field=models.DateTimeField(auto_now_add=True),
),
migrations.AlterField(
model_name='classifiertest',
name='modified_on',
field=models.DateTimeField(auto_now=True),
),
migrations.AlterField(
model_name='deposit',
name='created_on',
field=models.DateTimeField(auto_now_add=True, db_index=True),
),
migrations.AlterField(
model_name='deposit',
name='modified_on',
field=models.DateTimeField(auto_now=True),
),
migrations.AlterField(
model_name='performancecomp',
name='created_on',
field=models.DateTimeField(auto_now_add=True, db_index=True),
),
migrations.AlterField(
model_name='performancecomp',
name='modified_on',
field=models.DateTimeField(auto_now=True),
),
migrations.AlterField(
model_name='predictiontest',
name='created_on',
field=models.DateTimeField(auto_now_add=True),
),
migrations.AlterField(
model_name='predictiontest',
name='modified_on',
field=models.DateTimeField(auto_now=True),
),
migrations.AlterField(
model_name='price',
name='created_on',
field=models.DateTimeField(auto_now_add=True, db_index=True),
),
migrations.AlterField(
model_name='price',
name='modified_on',
field=models.DateTimeField(auto_now=True),
),
migrations.AlterField(
model_name='trade',
name='created_on',
field=models.DateTimeField(auto_now_add=True, db_index=True),
),
migrations.AlterField(
model_name='trade',
name='modified_on',
field=models.DateTimeField(auto_now=True),
),
migrations.AlterField(
model_name='traderecommendation',
name='created_on',
field=models.DateTimeField(auto_now_add=True, db_index=True),
),
migrations.AlterField(
model_name='traderecommendation',
name='modified_on',
field=models.DateTimeField(auto_now=True),
),
]
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 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',
),
]
27 changes: 6 additions & 21 deletions history/models.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
from __future__ import unicode_literals
from django.utils import timezone
import datetime
from pybrain.datasets import SupervisedDataSet
from pybrain.tools.shortcuts import buildNetwork
from pybrain.supervised.trainers import BackpropTrainer
from django.db import models
from history.tools import create_sample_row, get_fee_amount
from django.utils.timezone import localtime
from django.conf import settings
from django.utils import timezone
from django.core.urlresolvers import reverse
import cgi
import time
Expand All @@ -33,12 +32,12 @@


def get_time():
return localtime(timezone.now())
return timezone.localtime(timezone.now())


class TimeStampedModel(models.Model):
created_on = models.DateTimeField(null=False, default=get_time, db_index=True)
modified_on = models.DateTimeField(null=False, default=get_time)
created_on = models.DateTimeField(null=False, auto_now_add=True, db_index=True)
modified_on = models.DateTimeField(null=False, auto_now=True)

def get_readonly_fields(self, request, obj=None):
return [f.name for f in self._meta.get_fields()]
Expand All @@ -49,10 +48,6 @@ def has_add_permission(self, request, obj=None):
def has_delete_permission(self, request, obj=None):
return False

def save(self, *args, **kwargs):
self.modified_on = get_time()
return super(TimeStampedModel, self).save(*args, **kwargs)

class Meta:
abstract = True

Expand All @@ -62,8 +57,8 @@ def url_to_edit_object(self):


class AbstractedTesterClass(models.Model):
created_on = models.DateTimeField(null=False, default=get_time)
modified_on = models.DateTimeField(null=False, default=get_time)
created_on = models.DateTimeField(null=False, auto_now_add=True)
modified_on = models.DateTimeField(null=False, auto_now=True)

def get_readonly_fields(self, request, obj=None):
return [f.name for f in self._meta.get_fields()]
Expand All @@ -74,10 +69,6 @@ def has_add_permission(self, request, obj=None):
def has_delete_permission(self, request, obj=None):
return False

def save(self, *args, **kwargs):
self.modified_on = get_time()
return super(AbstractedTesterClass, self).save(*args, **kwargs)

class Meta:
abstract = True

Expand Down Expand Up @@ -131,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 @@ -143,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 @@ -192,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 @@ -204,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 @@ -233,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
11 changes: 8 additions & 3 deletions history/tools.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import time
import pytz
import datetime
from django.conf import settings
from django.utils import timezone
from django.core.exceptions import ImproperlyConfigured


Expand All @@ -11,14 +13,17 @@ def print_and_log(log_string):


def get_utc_unixtime():
import time
import datetime

d = datetime.datetime.now()
unixtime = time.mktime(d.timetuple())
return int(unixtime)


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')


def create_sample_row(data, i, size):
sample = ()
for k in range(0, size):
Expand Down
Loading