Skip to content
Merged
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
27 changes: 27 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
sudo: required
services:
- docker

language: ruby

env:
DOCKER_COMPOSE_VERSION: 1.6.2

before_install:
- export DEBIAN_FRONTEND=noninteractive
- sudo apt-get update
- sudo apt-get -o Dpkg::Options::="--force-confnew" -y install docker-engine
- sudo rm /usr/local/bin/docker-compose
- curl -L https://github.com/docker/compose/releases/download/1.6.2/docker-compose-`uname -s`-`uname -m` > docker-compose
- chmod +x docker-compose
- sudo mv docker-compose /usr/local/bin
- sudo pip install flake8

script:
- flake8 .
- cp docker/env.example docker/env
- cp docker-compose.yml.example docker-compose.yml
- cp pypolo/local_settings.py.example pypolo/local_settings.py
- sed -i 's/POLONIEX_API_KEY=/POLONIEX_API_KEY=dummy/' docker/env
- sed -i 's/POLONIEX_API_SECRET=/POLONIEX_API_SECRET=dummy/' docker/env
- docker-compose build
2 changes: 1 addition & 1 deletion docker/create_admin.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env python
from django.contrib.auth.models import User
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "pypolo.settings")
from django.contrib.auth.models import User

username = os.environ.get("PYTRADER_USER", 'trader')
password = os.environ.get("PYTRADER_PASSWORD", 'trader')
Expand Down
49 changes: 31 additions & 18 deletions history/admin.py
Original file line number Diff line number Diff line change
@@ -1,40 +1,47 @@
from django.contrib import admin
from history.models import Price, PredictionTest, Trade, TradeRecommendation, Balance, PerformanceComp, Deposit, ClassifierTest
from history.models import (
Price, PredictionTest, Trade, TradeRecommendation, Balance, PerformanceComp,
Deposit, ClassifierTest
)


class BalanceAdmin(admin.ModelAdmin):
ordering = ['-id']
search_fields = ['symbol']
list_display = ['pk', 'created_on', 'symbol', 'coin_balance', 'btc_balance','usd_balance']
list_display = ['pk', 'created_on', 'symbol', 'coin_balance', 'btc_balance', 'usd_balance']

admin.site.register(Balance, BalanceAdmin)


class TradeAdmin(admin.ModelAdmin):
ordering = ['-created_on']
search_fields = ['type', 'symbol']
list_display = ['pk', 'price', 'status', 'created_on_str', 'symbol', 'type', 'amount']
readonly_fields = [ 'recommendation', 'algo' ]
readonly_fields = ['recommendation', 'algo']

def recommendation(self,obj):
def recommendation(self, obj):
trs = TradeRecommendation.objects.filter(trade=obj)
return ",".join([ "<a href='/admin/history/traderecommendation/{}'>Trade Rec {}</a>".format(tr.pk,tr.pk) for tr in trs ])
return ",".join(["<a href='/admin/history/traderecommendation/{}'>Trade Rec {}</a>".
format(tr.pk, tr.pk) for tr in trs])

recommendation.allow_tags = True

def algo(self,obj):
def algo(self, obj):
trs = TradeRecommendation.objects.filter(trade=obj)
html = ""
if trs.count:
tr = trs[0]
if tr.clf:
html += "<a href='/admin/history/classifiertest/{}'>{}</a>".format(tr.clf.pk,tr.clf)
html += "<a href='/admin/history/classifiertest/{}'>{}</a>".format(tr.clf.pk, tr.clf)
if tr.made_by:
html += "<a href='/admin/history/predictiontest/{}'>{}</a>".format(tr.made_by.pk,tr.made_by)
html += "<a href='/admin/history/predictiontest/{}'>{}</a>".format(tr.made_by.pk, tr.made_by)
return html

algo.allow_tags = True

admin.site.register(Trade, TradeAdmin)


class PriceAdmin(admin.ModelAdmin):
ordering = ['-id']
search_fields = ['price', 'symbol']
Expand All @@ -43,42 +50,48 @@ class PriceAdmin(admin.ModelAdmin):

admin.site.register(Price, PriceAdmin)


class PredictionTestAdmin(admin.ModelAdmin):
ordering = ['-id']
search_fields = ['symbol','output']
list_display = ['pk', 'type','symbol', 'created_on', 'percent_correct', 'profitloss', 'prediction_size']
search_fields = ['symbol', 'output']
list_display = ['pk', 'type', 'symbol', 'created_on', 'percent_correct', 'profitloss', 'prediction_size']

admin.site.register(PredictionTest, PredictionTestAdmin)


class PerformanceCompAdmin(admin.ModelAdmin):
ordering = ['-id']
search_fields = ['symbol']
list_display = ['pk', 'created_on','symbol', 'nn_rec', 'actual_movement', 'delta']
list_display = ['pk', 'created_on', 'symbol', 'nn_rec', 'actual_movement', 'delta']

admin.site.register(PerformanceComp, PerformanceCompAdmin)


class TradeRecommendationAdmin(admin.ModelAdmin):
ordering = ['-id']
search_fields = ['symbol','recommendation']
list_display = ['pk', 'created_on','symbol', 'recommendation', 'confidence']
search_fields = ['symbol', 'recommendation']
list_display = ['pk', 'created_on', 'symbol', 'recommendation', 'confidence']

admin.site.register(TradeRecommendation, TradeRecommendationAdmin)


class DepositAdmin(admin.ModelAdmin):
ordering = ['-id']
search_fields = ['symbol','amount','status']
list_display = ['pk', 'symbol','amount','status']
search_fields = ['symbol', 'amount', 'status']
list_display = ['pk', 'symbol', 'amount', 'status']

admin.site.register(Deposit, DepositAdmin)


class ClassifierTestAdmin(admin.ModelAdmin):
def view_link(obj):
return u"<a href='{}'>View</a>".format(obj.graph_url())
return u"<a href='{}'>View</a>".format(obj.graph_url())
view_link.short_description = ''
view_link.allow_tags = True

ordering = ['-id']
search_fields = ['symbol','output']
list_display = ['pk', 'type','symbol', 'name', 'created_on', 'percent_correct', 'score', 'prediction_size', view_link]
search_fields = ['symbol', 'output']
list_display = ['pk', 'type', 'symbol', 'name', 'created_on',
'percent_correct', 'score', 'prediction_size', view_link]

admin.site.register(ClassifierTest, ClassifierTestAdmin)
6 changes: 4 additions & 2 deletions history/management/commands/alert_fail_cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@ def handle(self, *args, **options):
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)))
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)))

if not is_trader_running:
self.alert_email("not is_trader_running")
Expand Down
24 changes: 15 additions & 9 deletions history/management/commands/compare_perf.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@

from django.core.management.base import BaseCommand
from django.conf import settings
from history.tools import get_fee_amount
from history.models import Price, TradeRecommendation, PerformanceComp
import datetime
from django.db import transaction


class Command(BaseCommand):
Expand All @@ -18,26 +16,34 @@ def handle(self, *args, **options):
ticker = 'BTC_ETH'

# get data
date_of_timerange_we_care_about_predictions_start = datetime.datetime.now() - datetime.timedelta(seconds=((granularity_mins) * 60 + (60 * (1 + buffer_between_prediction_and_this_script_mins))))
date_of_timerange_we_care_about_predictions_end = datetime.datetime.now() - datetime.timedelta(seconds=((granularity_mins) * 60))
tr_timerange_end = TradeRecommendation.objects.filter(symbol=ticker, created_on__gte=date_of_timerange_we_care_about_predictions_start, created_on__lte=date_of_timerange_we_care_about_predictions_end).order_by('-created_on').first().created_on
date_of_timerange_we_care_about_predictions_start = datetime.datetime.now() - datetime.timedelta(
seconds=((granularity_mins) * 60 + (60 * (1 + buffer_between_prediction_and_this_script_mins))))
date_of_timerange_we_care_about_predictions_end = datetime.datetime.now() - datetime.timedelta(
seconds=((granularity_mins) * 60))
tr_timerange_end = TradeRecommendation.objects.filter(
symbol=ticker, created_on__gte=date_of_timerange_we_care_about_predictions_start,
created_on__lte=date_of_timerange_we_care_about_predictions_end).order_by('-created_on').first().created_on
tr_timerange_start = tr_timerange_end - datetime.timedelta(seconds=120)
price_timerange_start = tr_timerange_end
price_timerange_end = tr_timerange_end + datetime.timedelta(seconds=(granularity_mins * 60))
trs = TradeRecommendation.objects.filter(created_on__gte=tr_timerange_start, created_on__lte=tr_timerange_end)
price_now = Price.objects.filter(symbol=ticker, created_on__lte=price_timerange_end).order_by('-created_on').first().price
price_then = Price.objects.filter(symbol=ticker, created_on__lte=price_timerange_start).order_by('-created_on').first().price
price_now = Price.objects.filter(symbol=ticker, created_on__lte=price_timerange_end
).order_by('-created_on').first().price
price_then = Price.objects.filter(symbol=ticker, created_on__lte=price_timerange_start
).order_by('-created_on').first().price

# nn attributes
pct_buy = round(1.0 * sum(tr.recommendation == 'BUY' for tr in trs) / len(trs), 2)
pct_sell = round(1.0 * sum(tr.recommendation == 'SELL' for tr in trs) / len(trs), 2)
pct_hold = round(1.0 * sum(tr.recommendation == 'HOLD' for tr in trs) / len(trs), 2)
price_diff = price_now - price_then
price_pct = price_diff / price_then
price_buy_hold_sell = 0 if abs(price_pct) < get_fee_amount() else (1 if price_pct > 0 else -1) # -1 = sell, 0 = hold, 1 = wait
# -1 = sell, 0 = hold, 1 = wait
price_buy_hold_sell = 0 if abs(price_pct) < get_fee_amount() else (1 if price_pct > 0 else -1)
avg_nn_rec = 1.0 * sum(tr.net_amount for tr in trs) / len(trs)
weighted_avg_nn_rec = 1.0 * sum(tr.net_amount * (tr.confidence / 100.0) for tr in trs) / len(trs)
directionally_same = ((avg_nn_rec > 0 and price_buy_hold_sell > 0) or (avg_nn_rec < 0 and price_buy_hold_sell < 0))
directionally_same = ((avg_nn_rec > 0 and price_buy_hold_sell > 0) or
(avg_nn_rec < 0 and price_buy_hold_sell < 0))
delta = abs(abs(avg_nn_rec) - abs(price_buy_hold_sell)) * (1 if directionally_same else -1)

pc = PerformanceComp(symbol=ticker,
Expand Down
28 changes: 12 additions & 16 deletions history/management/commands/pull_balance.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@

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 django.db import transaction
import time

import warnings
warnings.filterwarnings("ignore", category=DeprecationWarning)
Expand All @@ -16,17 +14,14 @@ class Command(BaseCommand):

help = 'pulls balances and stores them in a DB'


def handle(self, *args, **options):
from history.poloniex import poloniex
from history.models import Price
import time

#hit API
poo = poloniex(settings.API_KEY,settings.API_SECRET)
# hit API
poo = poloniex(settings.API_KEY, settings.API_SECRET)
balances = poo.returnBalances()

#record balances
# record balances
deposited_amount_btc, deposited_amount_usd = get_deposit_balance()
with transaction.atomic():
for ticker in balances:
Expand All @@ -35,22 +30,23 @@ def handle(self, *args, **options):

exchange_rate_coin_to_btc = get_exchange_rate_to_btc(ticker)
exchange_rate_btc_to_usd = get_exchange_rate_btc_to_usd()
exchange_rate_coin_to_usd = exchange_rate_btc_to_usd * exchange_rate_coin_to_btc
btc_val = exchange_rate_coin_to_btc * val
usd_val = exchange_rate_btc_to_usd * btc_val
b = Balance(symbol=ticker,coin_balance=val,btc_balance=btc_val,exchange_to_btc_rate=exchange_rate_coin_to_btc,usd_balance=usd_val,exchange_to_usd_rate=exchange_rate_coin_to_btc,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 = Balance(symbol=ticker, coin_balance=val, btc_balance=btc_val,
exchange_to_btc_rate=exchange_rate_coin_to_btc, usd_balance=usd_val,
exchange_to_usd_rate=exchange_rate_coin_to_btc,
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.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
# 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 = datetime.datetime.strftime(
tr.created_on - datetime.timedelta(hours=int(7)), '%Y-%m-%d %H:%M')
tr.save()



18 changes: 7 additions & 11 deletions history/management/commands/pull_deposits.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,25 @@

from django.core.management.base import BaseCommand
from django.conf import settings
import datetime
from django.db import transaction
import time
from history.tools import get_utc_unixtime
from history.models import Deposit


class Command(BaseCommand):

help = 'pulls balances and stores them in a DB'


def handle(self, *args, **options):
from history.poloniex import poloniex
from history.models import Price
import time

poo = poloniex(settings.API_KEY,settings.API_SECRET)
poo = poloniex(settings.API_KEY, settings.API_SECRET)
now = get_utc_unixtime()
r = poo.returnDepositHistory(0,now)
r = poo.returnDepositHistory(0, now)
deposits = r['deposits'] + r['withdrawals']
for d in deposits:
print(d)
currency = d['currency']
amount = float(d['amount']) * ( -1 if 'withdrawalNumber' in d.keys() else 1 )
amount = float(d['amount']) * (-1 if 'withdrawalNumber' in d.keys() else 1)
timestamp = d['timestamp']
txid = d['withdrawalNumber'] if 'withdrawalNumber' in d.keys() else d['txid']
status = d['status']
Expand All @@ -33,12 +28,13 @@ def handle(self, *args, **options):
d = Deposit.objects.get(txid=txid)
except:
d = Deposit()
d.symbol=currency
d.symbol = currency
d.amount = amount
d.txid = txid
d.type = 'deposit' if amount > 0 else 'withdrawal'
d.status = status
d.created_on = created_on
d.modified_on = 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 = datetime.datetime.strftime(
created_on - datetime.timedelta(hours=int(7)), '%Y-%m-%d %H:%M')
d.save()
5 changes: 2 additions & 3 deletions history/management/commands/pull_prices.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


class Command(BaseCommand):

help = 'pulls prices and stores them in a DB'
Expand All @@ -11,7 +11,7 @@ def handle(self, *args, **options):
from history.models import Price
import time

poo = poloniex(settings.API_KEY,settings.API_SECRET)
poo = poloniex(settings.API_KEY, settings.API_SECRET)
price = poo.returnTicker()

for ticker in price.keys():
Expand All @@ -27,4 +27,3 @@ def handle(self, *args, **options):
p.symbol = ticker
p.created_on_str = str(p.created_on)
p.save()

Loading