diff --git a/apps/transactions/forms.py b/apps/transactions/forms.py index 8fdabad..3da3c0a 100644 --- a/apps/transactions/forms.py +++ b/apps/transactions/forms.py @@ -24,3 +24,13 @@ class TransactionForm(forms.ModelForm): 'fiat_currency': forms.Select(choices = fiat_currencies), 'crypto_currency': forms.Select(choices = crypto_currencies), } + + +class ImportForm(forms.Form): + api_key = forms.CharField(label='API Key', widget=forms.PasswordInput, max_length=100) + api_secret = forms.CharField(label='API Secret', widget=forms.PasswordInput, max_length=100) + YEAR_CHOICES = [] + now = datetime.datetime.now() + for y in range(2000,now.year+1): + YEAR_CHOICES.append(y) + start_date = forms.DateField(label='Start Date', widget=forms.SelectDateWidget(years=YEAR_CHOICES), initial=datetime.datetime(year=now.year, month=1, day=1)) diff --git a/apps/transactions/importbtcde.py b/apps/transactions/importbtcde.py new file mode 100644 index 0000000..39ac107 --- /dev/null +++ b/apps/transactions/importbtcde.py @@ -0,0 +1,55 @@ +from django.db import connection +from apps.transactions.models import Transaction +from decimal import Decimal +import datetime +import btcde + +class ImportBtcDe: + def Import(self, apiKey, apiSecret, StartDate, Owner): + + conn = btcde.Connection(apiKey, apiSecret) + page=1 + while True: + response = conn.showMyTrades(date_start=StartDate.isoformat()+"T00:00:00+00:00", state=1,page=page) + trades = response.get('trades') + if not trades: + raise Exception(('%s' % (response,))) + raise Exception(('%s %s %s ' % (apiKey,apiSecret, StartDate.isoformat()))) + break + for trade in trades: + + # TODO: check if this trade_id does not exist in the database for this user + with connection.cursor() as cursor: + sql = """SELECT trade_id FROM `transaction` WHERE trade_id=%s and owner_id=%s""" + cursor.execute(sql, [trade['trade_id'], Owner.id]) + if cursor.rowcount > 0: + continue + + # import the trade + t = Transaction() + t.trade_id = trade['trade_id'] + supported_cryptos = ['BTC', 'BCH', 'ETH'] + for c in supported_cryptos: + if trade['trading_pair'].upper().startswith(c): + t.crypto_currency = c + supported_fiat = ['USD', 'EUR'] + for f in supported_fiat: + if trade['trading_pair'].upper().endswith(f): + t.fiat_currency = f + t.owner = Owner + t.amount_before_fee = Decimal(trade['amount']) + t.amount_after_fee = Decimal(trade['amount']) - Decimal(trade['fee_currency']) + t.amount = Decimal(trade['volume']) + if trade['type'] == 'sell': + t.amount -= Decimal(trade['fee_eur']) + t.amount_before_fee *= -1 + t.amount_after_fee *= -1 + t.amount *= -1 + t.exchange_rate = trade['price'] + t.date_valid = trade['created_at'] + t.save() + + if page >= response.get('page')['last']: + break + page += 1 + diff --git a/apps/transactions/migrations/0002_transaction_trade_id.py b/apps/transactions/migrations/0002_transaction_trade_id.py new file mode 100644 index 0000000..563b0cd --- /dev/null +++ b/apps/transactions/migrations/0002_transaction_trade_id.py @@ -0,0 +1,18 @@ +# Generated by Django 3.1.5 on 2021-02-13 21:59 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('transactions', '0001_initial'), + ] + + operations = [ + migrations.AddField( + model_name='transaction', + name='trade_id', + field=models.CharField(default='MANUAL', max_length=20), + ), + ] diff --git a/apps/transactions/models.py b/apps/transactions/models.py index f34f74e..d4a82d5 100644 --- a/apps/transactions/models.py +++ b/apps/transactions/models.py @@ -3,6 +3,7 @@ from django.contrib.auth.models import User # Create your models here. class Transaction(models.Model): + trade_id = models.CharField(max_length=20, default='MANUAL') crypto_currency = models.CharField(max_length=10) owner = models.ForeignKey(User, on_delete=models.CASCADE) amount_before_fee = models.DecimalField(max_digits=24, decimal_places=10) diff --git a/apps/transactions/templates/add.html b/apps/transactions/templates/add.html index 2285d80..faeb569 100644 --- a/apps/transactions/templates/add.html +++ b/apps/transactions/templates/add.html @@ -28,6 +28,13 @@ +