Repository: freqtrade/freqtrade

Issue:
Kraken: EUR zero balance, BTC unknown format 
## Step 1: Have you search for this issue before posting it?
Yes, I am done my research. I found what I believe was a similar issue that was resolved but has not resurfaced --> #1840  


## Step 2: Describe your environment

  * Operating system: Ubuntu 19.04
  * Python Version: Python 3.7.3 
  * CCXT version: ccxt==1.18.1085 
  * Branch: Master
  * Last Commit ID: a7e45c5a732c60e2815bfd7bd3f2cb4cb24faf56 

 
  
## Step 3: Describe the problem:
I am wanting to use Kraken as my exchange. when using BTC I receive the below error:

 **File "/home/sgarc90/freqtrade/freqtrade/rpc/telegram.py", line 52, in wrapper
    return command_handler(self, *args, **kwargs)
  File "/home/sgarc90/freqtrade/freqtrade/rpc/telegram.py", line 336, in _balance
    "\t`Est. BTC: {est_btc: .8f}`\n".format(**currency)**
--------------------------
When I change to EUR. a zero balance is returned.

WARNING - Unable to create trade: Available balance(0 EUR) is lower than stake amount(19 EUR)
*Explain the problem you have encountered*

### Steps to reproduce:
once you start up freqtrade using Kraken. from within telegram send the balance button and watch the screen is a short time the telegram request will error out.

I just happened to see the EUR error appear on the screen when freqtrade tried to place an order
 
  



Discussion:
The problem here is the return from ccxt - which does only contain "total" - but not "available", nor "free" per pair.

i know this worked before since I tested kraken in the past. 

Maybe it's simply a bug in ccxt, or  the exchange did change how / what they return.

In the latter case, this will land kraken on the blocked list, since freqtrade cannot operate if it does not know how much free balance is available.


Related ccxt issue: https://github.com/ccxt/ccxt/issues/5776

Resolving patch:
diff --git a/freqtrade/exchange/exchange.py b/freqtrade/exchange/exchange.py
--- a/freqtrade/exchange/exchange.py
+++ b/freqtrade/exchange/exchange.py
@@ -30,6 +30,9 @@
     "bitmex": "Various reasons",
     "bitstamp": "Does not provide history. "
                 "Details in https://github.com/freqtrade/freqtrade/issues/1983",
+    "kraken": "TEMPORARY: Balance does not report free balance, so freqtrade will not know "
+              "if enough balance is available."
+              "Details in https://github.com/freqtrade/freqtrade/issues/1687#issuecomment-528509266"
     }
 
 
diff --git a/freqtrade/rpc/rpc.py b/freqtrade/rpc/rpc.py
--- a/freqtrade/rpc/rpc.py
+++ b/freqtrade/rpc/rpc.py
@@ -294,9 +294,9 @@ def _rpc_balance(self, fiat_display_currency: str) -> Dict:
             total = total + est_btc
             output.append({
                 'currency': coin,
-                'available': balance['free'],
-                'balance': balance['total'],
-                'pending': balance['used'],
+                'free': balance['free'] if balance['free'] is not None else 0,
+                'balance': balance['total'] if balance['total'] is not None else 0,
+                'used': balance['used'] if balance['used'] is not None else 0,
                 'est_btc': est_btc,
             })
         if total == 0.0:
diff --git a/freqtrade/rpc/telegram.py b/freqtrade/rpc/telegram.py
--- a/freqtrade/rpc/telegram.py
+++ b/freqtrade/rpc/telegram.py
@@ -328,9 +328,9 @@ def _balance(self, update: Update, context: CallbackContext) -> None:
             for currency in result['currencies']:
                 if currency['est_btc'] > 0.0001:
                     curr_output = "*{currency}:*\n" \
-                            "\t`Available: {available: .8f}`\n" \
+                            "\t`Available: {free: .8f}`\n" \
                             "\t`Balance: {balance: .8f}`\n" \
-                            "\t`Pending: {pending: .8f}`\n" \
+                            "\t`Pending: {used: .8f}`\n" \
                             "\t`Est. BTC: {est_btc: .8f}`\n".format(**currency)
                 else:
                     curr_output = "*{currency}:* not showing <1$ amount \n".format(**currency)


Test patch:
diff --git a/freqtrade/tests/rpc/test_rpc.py b/freqtrade/tests/rpc/test_rpc.py
--- a/freqtrade/tests/rpc/test_rpc.py
+++ b/freqtrade/tests/rpc/test_rpc.py
@@ -363,9 +363,9 @@ def test_rpc_balance_handle_error(default_conf, mocker):
     assert 'USD' == result['symbol']
     assert result['currencies'] == [{
         'currency': 'BTC',
-        'available': 10.0,
+        'free': 10.0,
         'balance': 12.0,
-        'pending': 2.0,
+        'used': 2.0,
         'est_btc': 12.0,
     }]
     assert result['total'] == 12.0
@@ -417,22 +417,22 @@ def test_rpc_balance_handle(default_conf, mocker):
     assert 'USD' == result['symbol']
     assert result['currencies'] == [
         {'currency': 'BTC',
-            'available': 10.0,
+            'free': 10.0,
             'balance': 12.0,
-            'pending': 2.0,
+            'used': 2.0,
             'est_btc': 12.0,
          },
-        {'available': 1.0,
+        {'free': 1.0,
          'balance': 5.0,
          'currency': 'ETH',
          'est_btc': 0.05,
-         'pending': 4.0
+         'used': 4.0
          },
-        {'available': 5.0,
+        {'free': 5.0,
          'balance': 10.0,
          'currency': 'PAX',
          'est_btc': 0.1,
-         'pending': 5.0}
+         'used': 5.0}
     ]
     assert result['total'] == 12.15
 
diff --git a/freqtrade/tests/rpc/test_rpc_apiserver.py b/freqtrade/tests/rpc/test_rpc_apiserver.py
--- a/freqtrade/tests/rpc/test_rpc_apiserver.py
+++ b/freqtrade/tests/rpc/test_rpc_apiserver.py
@@ -251,9 +251,9 @@ def mock_ticker(symbol, refresh):
     assert len(rc.json["currencies"]) == 5
     assert rc.json['currencies'][0] == {
         'currency': 'BTC',
-        'available': 12.0,
+        'free': 12.0,
         'balance': 12.0,
-        'pending': 0.0,
+        'used': 0.0,
         'est_btc': 12.0,
     }
 
diff --git a/freqtrade/tests/rpc/test_rpc_telegram.py b/freqtrade/tests/rpc/test_rpc_telegram.py
--- a/freqtrade/tests/rpc/test_rpc_telegram.py
+++ b/freqtrade/tests/rpc/test_rpc_telegram.py
@@ -577,8 +577,8 @@ def test_balance_handle_too_large_response(default_conf, update, mocker) -> None
         curr = choice(ascii_uppercase) + choice(ascii_uppercase) + choice(ascii_uppercase)
         balances.append({
             'currency': curr,
-            'available': 1.0,
-            'pending': 0.5,
+            'free': 1.0,
+            'used': 0.5,
             'balance': i,
             'est_btc': 1
         })
diff --git a/freqtrade/tests/test_configuration.py b/freqtrade/tests/test_configuration.py
--- a/freqtrade/tests/test_configuration.py
+++ b/freqtrade/tests/test_configuration.py
@@ -494,7 +494,7 @@ def test_check_exchange(default_conf, caplog) -> None:
     caplog.clear()
 
     # Test an available exchange, supported by ccxt
-    default_conf.get('exchange').update({'name': 'kraken'})
+    default_conf.get('exchange').update({'name': 'huobipro'})
     assert check_exchange(default_conf)
     assert log_has_re(r"Exchange .* is supported by ccxt and .* not officially supported "
                       r"by the Freqtrade development team\. .*", caplog)