Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
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
4 changes: 2 additions & 2 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
from history.tools import get_fee_amount, utc_to_mst_str
from history.models import Price, TradeRecommendation, PerformanceComp
import datetime

Expand Down Expand Up @@ -61,5 +61,5 @@ def handle(self, *args, **options):
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'))
created_on_str=utc_to_mst_str(tr_timerange_end))
pc.save()
10 changes: 4 additions & 6 deletions history/management/commands/pull_balance.py
Original file line number Diff line number Diff line change
@@ -1,6 +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.tools import (get_exchange_rate_to_btc, get_exchange_rate_btc_to_usd, get_deposit_balance,
utc_to_mst_str)
from history.models import Balance, Trade
import datetime
from django.db import transaction
Expand Down Expand Up @@ -40,13 +41,10 @@ def handle(self, *args, **options):
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.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=''):
# 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.created_on_str = utc_to_mst_str(tr.created_on)
tr.save()
5 changes: 2 additions & 3 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
from history.tools import get_utc_unixtime, utc_to_mst_str
from history.models import Deposit


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

Expand Down Expand Up @@ -25,5 +26,5 @@ 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.created_on_str = utc_to_mst_str(p.created_on)
p.save()
6 changes: 3 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, get_time, ClassifierTest
from history.tools import get_utc_unixtime, print_and_log
from history.models import Price, PredictionTest, Trade, TradeRecommendation, Balance, ClassifierTest
from history.tools import get_utc_unixtime, print_and_log, utc_to_mst_str
import datetime
import time
from history.poloniex import poloniex
Expand Down Expand Up @@ -192,7 +192,7 @@ def run_predictor(self, nn_index):
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')))
created_on_str=utc_to_mst_str(timezone.now()))
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),
),
]
21 changes: 6 additions & 15 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
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_mst_str(dt):
mst = pytz.timezone('MST')
local = timezone.localtime(dt, mst)
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
25 changes: 15 additions & 10 deletions history/views.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import pytz
from django.contrib.admin.views.decorators import staff_member_required
from history.models import (
PredictionTest, Price, Trade, Balance, TradeRecommendation, get_time, PerformanceComp,
PredictionTest, Price, Trade, Balance, TradeRecommendation, PerformanceComp,
ClassifierTest)
from django.shortcuts import render_to_response
from django.utils import timezone
Expand Down Expand Up @@ -383,8 +384,9 @@ def nn_chart_view(request):
trainer_last_seen = None
try:
last_pt = PredictionTest.objects.filter(type='mock').order_by('-created_on').first()
is_trainer_running = last_pt.created_on > (get_time() - datetime.timedelta(minutes=int(15)))
trainer_last_seen = (last_pt.created_on - datetime.timedelta(hours=int(7))).strftime('%a %H:%M')
fifteen_ago = timezone.now() - datetime.timedelta(minutes=15)
is_trainer_running = last_pt.created_on > fifteen_ago
trainer_last_seen = timezone.localtime(last_pt.created_on).strftime('%a %H:%M')
except Exception:
is_trainer_running = False

Expand All @@ -402,11 +404,11 @@ def nn_chart_view(request):
cht = get_line_chart(pts, symbol, parameter)
charts.append(cht)
options = []
chartnames.append("container"+str(i))
chartnames.append("container" + str(i))
metas.append({
'name': parameter,
'container_class': 'show',
'class': "container"+str(i),
'class': "container" + str(i),
'options': options,
})

Expand Down Expand Up @@ -473,8 +475,9 @@ def c_chart_view(request):
trainer_last_seen = None
try:
last_pt = ClassifierTest.objects.filter(type='mock').order_by('-created_on').first()
is_trainer_running = last_pt.created_on > (get_time() - datetime.timedelta(minutes=int(15)))
trainer_last_seen = (last_pt.created_on - datetime.timedelta(hours=int(7))).strftime('%a %H:%M')
fifteen_ago = timezone.now() - datetime.timedelta(minutes=15)
is_trainer_running = last_pt.created_on > fifteen_ago
trainer_last_seen = timezone.localtime(last_pt.created_on).strftime('%a %H:%M')
except Exception:
is_trainer_running = False

Expand Down Expand Up @@ -564,7 +567,8 @@ def profit_view(request):
# get data
data = {}
for t in Trade.objects.filter(symbol=symbol, status='fill').order_by('-created_on').all():
date = datetime.datetime.strftime(t.created_on-datetime.timedelta(hours=7), '%Y-%m-%d')
mst_dt = timezone.localtime(t.created_on, pytz.timezone('MST'))
date = datetime.datetime.strftime(mst_dt, '%Y-%m-%d')
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I had forgotten this one. It should be MST since it's in a view.

Copy link
Owner

Choose a reason for hiding this comment

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

MST was just what i used because thats where i live. might be worth making this a settings.DISPLAY_TIMEONE for others to configure.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

But then DBs would be inconsistent between each other.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Maybe it's best to leave it MST and look for a django-chartit alternative.

Copy link
Owner

Choose a reason for hiding this comment

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

But then DBs would be inconsistent between each other.

could be solved with a migration

Maybe it's best to leave it MST and look for a django-chartit alternative.

i dont feel strongly about this enoguh to hold up the PR. just a suggestion

Copy link
Contributor Author

@rubik rubik Apr 14, 2016

Choose a reason for hiding this comment

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

could be solved with a migration

Well in that case we would have to store the data in UTC, but at that point it would be useless. We could remove created_on_str and add an attribute on the fly. Or use a Python property. Is it possible? Currently I don't have data to create a chart and check myself. I only have the seed from #2.

Copy link
Owner

Choose a reason for hiding this comment

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

IIRC, django-chartit requires the data to be available within the queryset, so a python property will likely not work.

data is posted @ #2 (comment) not asking that you make the change, just telling you about this because now that you've made a PR you should at least have access to the data so you can trade :)

Copy link
Collaborator

@igorpejic igorpejic Apr 18, 2016

Choose a reason for hiding this comment

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

Can this line be:

dt = timezone.localtime(t.created_on, pytz.timezone(settings.DISPLAY_TZ))

?

Copy link
Owner

Choose a reason for hiding this comment

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

Yes I think it should be

if date not in data.keys():
data[date] = {'buyvol': [], 'sellvol': [], 'buy': [], 'sell': [], 'bal': 0.00}
data[date][t.type].append(t.price)
Expand Down Expand Up @@ -663,8 +667,9 @@ def optimize_view(request):

last_trade = TradeRecommendation.objects.order_by('-created_on').first()
if last_trade:
trader_last_seen = (last_trade.created_on - datetime.timedelta(hours=int(7))).strftime('%a %H:%M')
is_trader_running = last_trade.created_on > (get_time() - datetime.timedelta(minutes=int(15)))
fifteen_ago = timezone.now() - datetime.timedelta(minutes=15)
trader_last_seen = timezone.localtime(last_trade.created_on).strftime('%a %H:%M')
is_trader_running = last_trade.created_on > fifteen_ago
else:
trader_last_seen = None
is_trader_running = False
Expand Down