Repository: freqtrade/freqtrade
Pull request: Support all types of pairs for /balance
## Summary
Some pairs are not available as <pair>/BTC - but BTC/<pair>. These pairs are often stake-currencies (like PAX).
we used to have a hardcoded list of these pairs, however using the exchanges "markets" for this is way more flexible, and should eliminate this bug for many other currencies (USDC, ...) too.

Closes #1981


Problem statement:
Pax balance not detected
Version 2019.5
Bot won't detect pax base, will update to newest to see if fixed. Will also assumes that this is the same issue for other stable coins that are not tether 

Last version of CCTX
Running 2019.6
Exchange Binance
Edited to add more information about the issue

Hints from issue discussion:
This is lacking quite some information.

What exchange are you using?
what version of ccxt?

Edit: did you get any message while starting the bot?
> This is lacking quite some information.
> 
> What exchange are you using?
> what version of ccxt?
> 
> Edit: did you get any message while starting the bot?

No, no specific message.
So what's the behaviour you observed? "does not detect" is very generic ... 
I also did not get the issue...
Basically when you click Balance it says I have PAX < 1, and is not included in the overall calculation of my balance.
This means that it does recognize that you have PAX in your account - however it's < 1$, which is excluded due to "binance dust", which has the potential to show a very large amount of coins with almost 0 balance.

The limit is 0.0001 BTC - which is ~0.91$ at this moment (according to google - but it'll fluctuate a lot ).
Even if you have this balance, it'll be below every limit you could use as stake-currency due to binance having min_notational / lotsize.
@xmatthias I'm aware, I was holding around 1500 PAX so that was not the case.
without the related logs i doubt we have a chance to recreate this problem (also, i don't currently hold PAX ...)
It's simply to re-create this issue, you simply buy PAX and query for balance, i can do one, give me a few minutes

I suspect that other stable pairs do give the same issues, so testing for TUSD, USDC and so on is also trivial as they should be worth 1$ USD

There's nothing on the logs 
@xmatthias 
````
2019-07-03 01:34:26,838 - freqtrade.freqtradebot - INFO - Found no buy signals for whitelisted currencies. Trying again..
2019-07-03 01:34:31,840 - freqtrade.freqtradebot - INFO - Found no buy signals for whitelisted currencies. Trying again..
2019-07-03 01:34:36,844 - freqtrade.freqtradebot - INFO - Found no buy signals for whitelisted currencies. Trying again..
2019-07-03 01:34:41,844 - freqtrade.freqtradebot - INFO - Found no buy signals for whitelisted currencies. Trying again..
2019-07-03 01:34:46,847 - freqtrade.freqtradebot - INFO - Found no buy signals for whitelisted currencies. Trying again..
2019-07-03 01:34:51,853 - freqtrade.freqtradebot - INFO - Found no buy signals for whitelisted currencies. Trying again..
2019-07-03 01:34:56,854 - freqtrade.freqtradebot - INFO - Found no buy signals for whitelisted currencies. Trying again..
2019-07-03 01:34:57,793 - freqtrade.rpc.telegram - INFO - Executing handler: _balance for chat_id: 146554112
2019-07-03 01:35:01,859 - freqtrade.freqtradebot - INFO - Found no buy signals for whitelisted currencies. Trying again..
2019-07-03 01:35:05,848 - freqtrade.rpc.telegram - INFO - Executing handler: _balance for chat_id: 146554112
2019-07-03 01:35:05,878 - freqtrade.exchange.exchange - INFO - returning cached ticker-data for ETH/BTC
2019-07-03 01:35:05,879 - freqtrade.exchange.exchange - INFO - returning cached ticker-data for BNB/BTC
2019-07-03 01:35:05,879 - freqtrade.exchange.exchange - INFO - returning cached ticker-data for BTC/USDT
2019-07-03 01:35:05,879 - freqtrade.exchange.exchange - INFO - returning cached ticker-data for OMG/BTC
2019-07-03 01:35:05,879 - freqtrade.exchange.exchange - INFO - returning cached ticker-data for ONG/BTC
2019-07-03 01:35:05,880 - freqtrade.exchange.exchange - INFO - returning cached ticker-data for PAX/BTC
2019-07-03 01:35:05,880 - freqtrade.exchange.exchange - INFO - returning cached ticker-data for FTM/BTC
2019-07-03 01:35:06,863 - freqtrade.freqtradebot - INFO - Found no buy signals for whitelisted currencies. Trying again..
2019-07-03 01:35:11,869 - freqtrade.freqtradebot - INFO - Found no buy signals for whitelisted currencies. Trying again..
2019-07-03 01:35:16,868 - freqtrade.freqtradebot - INFO - Found no buy signals for whitelisted currencies. Trying again..
```
`

What's the configuration you use for this? (mainly interrested in stake_currency)
Do other currencies you have in your account show up fine?

how exactly is the telegram output? (does it include `PAX: not showing <1$ amount`)?

Commit message: Fix #1981 - Detect reverted currency pairs

Patch to review:
diff --git a/freqtrade/exchange/exchange.py b/freqtrade/exchange/exchange.py
index c3171b96111..536a707cd69 100644
--- a/freqtrade/exchange/exchange.py
+++ b/freqtrade/exchange/exchange.py
@@ -270,6 +270,15 @@ def validate_pairs(self, pairs: List[str]) -> None:
                     f'Pair {pair} is not available on {self.name}. '
                     f'Please remove {pair} from your whitelist.')
 
+    def get_valid_pair_combination(self, paira, pairb) -> str:
+        """
+        Get valid combination of paira and pairb by trying both combinations.
+        """
+        for pair in [f"{paira}/{pairb}", f"{pairb}/{paira}"]:
+            if pair in self._api.markets and self._api.markets[pair].get('active'):
+                return pair
+        raise DependencyException(f"Could not combine {paira} and {pairb} to get a valid pair.")
+
     def validate_timeframes(self, timeframe: List[str]) -> None:
         """
         Checks if ticker interval from config is a supported timeframe on the exchange
@@ -501,7 +510,7 @@ def get_tickers(self) -> Dict:
     def get_ticker(self, pair: str, refresh: Optional[bool] = True) -> dict:
         if refresh or pair not in self._cached_ticker.keys():
             try:
-                if pair not in self._api.markets:
+                if pair not in self._api.markets or not self._api.markets[pair].get('active'):
                     raise DependencyException(f"Pair {pair} not available")
                 data = self._api.fetch_ticker(pair)
                 try:
diff --git a/freqtrade/rpc/rpc.py b/freqtrade/rpc/rpc.py
index f5adffc65be..f77e0eddb80 100644
--- a/freqtrade/rpc/rpc.py
+++ b/freqtrade/rpc/rpc.py
@@ -281,10 +281,11 @@ def _rpc_balance(self, fiat_display_currency: str) -> Dict:
                 rate = 1.0
             else:
                 try:
-                    if coin in('USDT', 'USD', 'EUR'):
-                        rate = 1.0 / self._freqtrade.get_sell_rate('BTC/' + coin, False)
+                    pair = self._freqtrade.exchange.get_valid_pair_combination(coin, "BTC")
+                    if pair.startswith("BTC"):
+                        rate = 1.0 / self._freqtrade.get_sell_rate(pair, False)
                     else:
-                        rate = self._freqtrade.get_sell_rate(coin + '/BTC', False)
+                        rate = self._freqtrade.get_sell_rate(pair, False)
                 except (TemporaryError, DependencyException):
                     logger.warning(f" Could not get rate for pair {coin}.")
                     continue


Code review comments:
diff_hunk: @@ -270,6 +270,15 @@ def validate_pairs(self, pairs: List[str]) -> None:
                     f'Pair {pair} is not available on {self.name}. '
                     f'Please remove {pair} from your whitelist.')
 
+    def get_valid_pair_combination(self, paira, pairb) -> str:
path: freqtrade/exchange/exchange.py
text: @user1:
parameters are not the pairs, these are currencies (or symbols), the names of the parameters are confusing

@author:
Great catch! :+1: