Repository: freqtrade/freqtrade

Issue:
NaN values on /status
<!-- 
Have you searched for similar issues before posting it?

If you have discovered a bug in the bot, please [search the issue tracker](https://github.com/freqtrade/freqtrade/issues?q=is%3Aissue). 
If it hasn't been reported, please create a new issue.

Please do not use bug reports to request new features.
-->

## Describe your environment

  * Operating system: Raspbian
  * Python Version: 3.9.9 (`python -V`)
  * CCXT version: 1.80.61 (`pip freeze | grep ccxt`)
  * Freqtrade Version: docker-44000ae0 (`freqtrade -V` or `docker-compose run --rm freqtrade -V` for Freqtrade running in docker)
  
Note: All issues other than enhancement requests will be closed without further comment if the above template is deleted or not filled out.

## Describe the problem:

*Explain the problem you have encountered*

### Steps to reproduce:

  1. Open FreqUI
  2. Go to Dashboard
  
### Observed Results:

Due to the API sending "NaN" as values, the frontend can't parse the JSON properly and instead is being converted into a string.



Discussion:
This will need more information (for example a screenshot on what's the end-result, as well as the API response you got).

Afaik, `/status` will only return NAN during an exchange downtime (we have an open issue for that already in #3221).
As such, i doubt this is a permanent problem (i'm not able to recreate it with the provided information, anyway).
it looks like this:
![image](https://user-images.githubusercontent.com/11251134/165245577-e012d2f4-a125-4866-a6b6-3aaf9d659c1a.png)

The telegram bot works fine but the FreqUI isn't.
please open the chrome dev tools and copy/paste the response from the API request to status.

It's unclear to me which field would be nan - and as said before - i'm currently unable to recreate this - as it's working for me.

```
[
    {
        "trade_id": 45,
        "pair": "LIT/USDT",
        "base_currency": "LIT",
        "quote_currency": "USDT",
        "is_open": true,
        "is_short": false,
        "exchange": "binance",
        "amount": 9.34078323,
        "amount_requested": 9.55921677,
        "stake_amount": 19.61551282,
        "strategy": "NostalgiaForInfinityX",
        "buy_tag": "14 ",
        "enter_tag": "14 ",
        "timeframe": 5,
        "fee_open": 0.001,
        "fee_open_cost": 0.0095,
        "fee_open_currency": "LIT",
        "fee_close": 0.001,
        "fee_close_cost": null,
        "fee_close_currency": null,
        "open_date": "2022-04-02 14:03:05",
        "open_timestamp": 1648908185936,
        "open_rate": 2.052,
        "open_rate_requested": 2.052,
        "open_trade_value": 19.18645447,
        "close_date": null,
        "close_timestamp": null,
        "close_rate": null,
        "close_rate_requested": null,
        "close_profit": null,
        "close_profit_pct": null,
        "close_profit_abs": null,
        "profit_ratio": -0.34147432,
        "profit_pct": -34.15,
        "profit_abs": -6.5516814,
        "profit_fiat": -6.5496634821288,
        "sell_reason": null,
        "exit_reason": null,
        "exit_order_status": null,
        "stop_loss_abs": 0.020520000000000017,
        "stop_loss_ratio": -0.99,
        "stop_loss_pct": -99.0,
        "stoploss_order_id": null,
        "stoploss_last_update": "2022-04-02 14:03:17",
        "stoploss_last_update_timestamp": 1648908197285,
        "initial_stop_loss_abs": 0.020520000000000017,
        "initial_stop_loss_ratio": -0.99,
        "initial_stop_loss_pct": -99.0,
        "min_rate": 1.205,
        "max_rate": 2.073,
        "open_order_id": null,
        "orders": [
            {
                "pair": "LIT/USDT",
                "order_id": "301108014",
                "status": "closed",
                "remaining": 0.0,
                "amount": 9.5,
                "safe_price": 2.052,
                "cost": 19.494,
                "filled": 9.5,
                "ft_order_side": "buy",
                "order_type": "limit",
                "is_open": false,
                "order_timestamp": 1648908185877,
                "order_filled_timestamp": 1648908195461
            }
        ],
        "leverage": 1.0,
        "interest_rate": 0.0,
        "funding_fees": 0.0,
        "trading_mode": null,
        "stoploss_current_dist": -1.33348,
        "stoploss_current_dist_pct": -98.48,
        "stoploss_current_dist_ratio": -0.9848449,
        "stoploss_entry_dist": -18.99497327,
        "stoploss_entry_dist_ratio": -0.99001998,
        "current_profit": -0.34147432,
        "current_profit_abs": -6.5516814,
        "current_profit_pct": -34.15,
        "current_rate": 1.354,
        "open_order": null
    },
    {
        "trade_id": 52,
        "pair": "ZIL/USDT",
        "base_currency": "ZIL",
        "quote_currency": "USDT",
        "is_open": true,
        "is_short": false,
        "exchange": "binance",
        "amount": 126.92179117,
        "amount_requested": 127.27820883,
        "stake_amount": 18.40061065,
        "strategy": "NostalgiaForInfinityX",
        "buy_tag": "10 ",
        "enter_tag": "10 ",
        "timeframe": 5,
        "fee_open": 0.001,
        "fee_open_cost": 0.1272,
        "fee_open_currency": "ZIL",
        "fee_close": 0.001,
        "fee_close_cost": null,
        "fee_close_currency": null,
        "open_date": "2022-04-04 16:10:07",
        "open_timestamp": 1649088607659,
        "open_rate": 0.14457,
        "open_rate_requested": 0.14457,
        "open_trade_value": 18.36743243,
        "close_date": null,
        "close_timestamp": null,
        "close_rate": null,
        "close_rate_requested": null,
        "close_profit": null,
        "close_profit_pct": null,
        "close_profit_abs": null,
        "profit_ratio": -0.28558327,
        "profit_pct": -28.56,
        "profit_abs": -5.2454314,
        "profit_fiat": -5.2438158071288,
        "sell_reason": null,
        "exit_reason": null,
        "exit_order_status": null,
        "stop_loss_abs": 0.0014457000000000014,
        "stop_loss_ratio": -0.99,
        "stop_loss_pct": -99.0,
        "stoploss_order_id": null,
        "stoploss_last_update": "2022-04-04 16:13:00",
        "stoploss_last_update_timestamp": 1649088780662,
        "initial_stop_loss_abs": 0.0014457000000000014,
        "initial_stop_loss_ratio": -0.99,
        "initial_stop_loss_pct": -99.0,
        "min_rate": 0.10091,
        "max_rate": 0.14523,
        "open_order_id": null,
        "orders": [
            {
                "pair": "ZIL/USDT",
                "order_id": "742171477",
                "status": "closed",
                "remaining": 0.0,
                "amount": 127.2,
                "safe_price": 0.14457,
                "cost": 18.389304,
                "filled": 127.2,
                "ft_order_side": "buy",
                "order_type": "limit",
                "is_open": false,
                "order_timestamp": 1649088607600,
                "order_filled_timestamp": 1649088778666
            }
        ],
        "leverage": 1.0,
        "interest_rate": 0.0,
        "funding_fees": 0.0,
        "trading_mode": null,
        "stoploss_current_dist": -0.10204429999999999,
        "stoploss_current_dist_pct": -98.6,
        "stoploss_current_dist_ratio": -0.98603053,
        "stoploss_entry_dist": -18.18412509,
        "stoploss_entry_dist_ratio": -0.99001998,
        "current_profit": -0.28558327,
        "current_profit_abs": -5.2454314,
        "current_profit_pct": -28.56,
        "current_rate": 0.10349,
        "open_order": null
    },
    {
        "trade_id": 54,
        "pair": "GXS/USDT",
        "base_currency": "GXS",
        "quote_currency": "USDT",
        "is_open": true,
        "is_short": false,
        "exchange": "binance",
        "amount": 6.58984928,
        "amount_requested": 7.40315072,
        "stake_amount": 18.39238766,
        "strategy": "NostalgiaForInfinityX",
        "buy_tag": "11 ",
        "enter_tag": "11 ",
        "timeframe": 5,
        "fee_open": 0.001,
        "fee_open_cost": 0.007,
        "fee_open_currency": "GXS",
        "fee_close": 0.001,
        "fee_close_cost": null,
        "fee_close_currency": null,
        "open_date": "2022-04-04 22:36:39",
        "open_timestamp": 1649111799674,
        "open_rate": 2.4844,
        "open_rate_requested": 2.4844,
        "open_trade_value": 16.38819337,
        "close_date": null,
        "close_timestamp": null,
        "close_rate": null,
        "close_rate_requested": null,
        "close_profit": null,
        "close_profit_pct": null,
        "close_profit_abs": null,
        "profit_ratio": NaN,
        "profit_pct": NaN,
        "profit_abs": NaN,
        "profit_fiat": NaN,
        "sell_reason": null,
        "exit_reason": null,
        "exit_order_status": null,
        "stop_loss_abs": 0.024844000000000022,
        "stop_loss_ratio": -0.99,
        "stop_loss_pct": -99.0,
        "stoploss_order_id": null,
        "stoploss_last_update": "2022-04-04 22:37:53",
        "stoploss_last_update_timestamp": 1649111873824,
        "initial_stop_loss_abs": 0.024844000000000022,
        "initial_stop_loss_ratio": -0.99,
        "initial_stop_loss_pct": -99.0,
        "min_rate": 1.5421,
        "max_rate": 2.5291,
        "open_order_id": null,
        "orders": [
            {
                "pair": "GXS/USDT",
                "order_id": "233326027",
                "status": "closed",
                "remaining": 0.0,
                "amount": 7.0,
                "safe_price": 2.4844,
                "cost": 17.3908,
                "filled": 7.0,
                "ft_order_side": "buy",
                "order_type": "limit",
                "is_open": false,
                "order_timestamp": 1649111799614,
                "order_filled_timestamp": 1649111871871
            }
        ],
        "leverage": 1.0,
        "interest_rate": 0.0,
        "funding_fees": 0.0,
        "trading_mode": null,
        "stoploss_current_dist": NaN,
        "stoploss_current_dist_pct": NaN,
        "stoploss_current_dist_ratio": NaN,
        "stoploss_entry_dist": -16.22463887,
        "stoploss_entry_dist_ratio": -0.99001998,
        "current_profit": NaN,
        "current_profit_abs": NaN,
        "current_profit_pct": NaN,
        "current_rate": NaN,
        "open_order": null
    },
    {
        "trade_id": 57,
        "pair": "KEY/USDT",
        "base_currency": "KEY",
        "quote_currency": "USDT",
        "is_open": true,
        "is_short": false,
        "exchange": "binance",
        "amount": 1793.30695487,
        "amount_requested": 1796.89704513,
        "stake_amount": 18.52780543,
        "strategy": "NostalgiaForInfinityX",
        "buy_tag": "5 8 ",
        "enter_tag": "5 8 ",
        "timeframe": 5,
        "fee_open": 0.001,
        "fee_open_cost": 1.796,
        "fee_open_currency": "KEY",
        "fee_close": 0.001,
        "fee_close_cost": null,
        "fee_close_currency": null,
        "open_date": "2022-04-05 15:37:46",
        "open_timestamp": 1649173066286,
        "open_rate": 0.010311,
        "open_rate_requested": 0.010311,
        "open_trade_value": 18.5092788,
        "close_date": null,
        "close_timestamp": null,
        "close_rate": null,
        "close_rate_requested": null,
        "close_profit": null,
        "close_profit_pct": null,
        "close_profit_abs": null,
        "profit_ratio": -0.19732028,
        "profit_pct": -19.73,
        "profit_abs": -3.65225612,
        "profit_fiat": -3.6511312251150403,
        "sell_reason": null,
        "exit_reason": null,
        "exit_order_status": null,
        "stop_loss_abs": 0.0001031100000000001,
        "stop_loss_ratio": -0.99,
        "stop_loss_pct": -99.0,
        "stoploss_order_id": null,
        "stoploss_last_update": "2022-04-05 15:38:06",
        "stoploss_last_update_timestamp": 1649173086820,
        "initial_stop_loss_abs": 0.0001031100000000001,
        "initial_stop_loss_ratio": -0.99,
        "initial_stop_loss_pct": -99.0,
        "min_rate": 0.007656,
        "max_rate": 0.01075,
        "open_order_id": null,
        "orders": [
            {
                "pair": "KEY/USDT",
                "order_id": "233187774",
                "status": "closed",
                "remaining": 0.0,
                "amount": 1796.0,
                "safe_price": 0.010311,
                "cost": 18.518556,
                "filled": 1796.0,
                "ft_order_side": "buy",
                "order_type": "limit",
                "is_open": false,
                "order_timestamp": 1649173066228,
                "order_filled_timestamp": 1649173084609
            }
        ],
        "leverage": 1.0,
        "interest_rate": 0.0,
        "funding_fees": 0.0,
        "trading_mode": null,
        "stoploss_current_dist": -0.00818989,
        "stoploss_current_dist_pct": -98.76,
        "stoploss_current_dist_ratio": -0.98756662,
        "stoploss_entry_dist": -18.32455583,
        "stoploss_entry_dist_ratio": -0.99001998,
        "current_profit": -0.19732028,
        "current_profit_abs": -3.65225612,
        "current_profit_pct": -19.73,
        "current_rate": 0.008293,
        "open_order": null
    },
    {
        "trade_id": 68,
        "pair": "DF/USDT",
        "base_currency": "DF",
        "quote_currency": "USDT",
        "is_open": true,
        "is_short": false,
        "exchange": "binance",
        "amount": 418.0,
        "amount_requested": 224.51641925,
        "stake_amount": 33.7062,
        "strategy": "NostalgiaForInfinityX",
        "buy_tag": "5 ",
        "enter_tag": "5 ",
        "timeframe": 5,
        "fee_open": 0.001,
        "fee_open_cost": 0.0337062,
        "fee_open_currency": "DF",
        "fee_close": 0.001,
        "fee_close_cost": null,
        "fee_close_currency": null,
        "open_date": "2022-04-15 13:52:30",
        "open_timestamp": 1650030750314,
        "open_rate": 0.08063684210526315,
        "open_rate_requested": 0.0771,
        "open_trade_value": 33.7399062,
        "close_date": null,
        "close_timestamp": null,
        "close_rate": null,
        "close_rate_requested": null,
        "close_profit": null,
        "close_profit_pct": null,
        "close_profit_abs": null,
        "profit_ratio": -0.11879309,
        "profit_pct": -11.88,
        "profit_abs": -4.0080678,
        "profit_fiat": -4.0068333151176,
        "sell_reason": null,
        "exit_reason": null,
        "exit_order_status": null,
        "stop_loss_abs": 0.0008370000000000007,
        "stop_loss_ratio": -0.99,
        "stop_loss_pct": -99.0,
        "stoploss_order_id": null,
        "stoploss_last_update": "2022-04-15 13:53:35",
        "stoploss_last_update_timestamp": 1650030815649,
        "initial_stop_loss_abs": 0.0008370000000000007,
        "initial_stop_loss_ratio": -0.99,
        "initial_stop_loss_pct": -99.0,
        "min_rate": 0.0695,
        "max_rate": 0.0858,
        "open_order_id": null,
        "orders": [
            {
                "pair": "DF/USDT",
                "order_id": "20012989",
                "status": "closed",
                "remaining": 0.0,
                "amount": 224.0,
                "safe_price": 0.0837,
                "cost": 18.7488,
                "filled": 224.0,
                "ft_order_side": "buy",
                "order_type": "limit",
                "is_open": false,
                "order_timestamp": 1650030750256,
                "order_filled_timestamp": 1650030813406
            },
            {
                "pair": "DF/USDT",
                "order_id": "20185179",
                "status": "closed",
                "remaining": 0.0,
                "amount": 194.0,
                "safe_price": 0.0771,
                "cost": 14.9574,
                "filled": 194.0,
                "ft_order_side": "buy",
                "order_type": "limit",
                "is_open": false,
                "order_timestamp": 1650082417886,
                "order_filled_timestamp": 1650082692955
            }
        ],
        "leverage": 1.0,
        "interest_rate": 0.0,
        "funding_fees": 0.0,
        "trading_mode": null,
        "stoploss_current_dist": -0.070363,
        "stoploss_current_dist_pct": -98.82,
        "stoploss_current_dist_ratio": -0.98824438,
        "stoploss_entry_dist": -33.39039007,
        "stoploss_entry_dist_ratio": -0.98964087,
        "current_profit": -0.11879309,
        "current_profit_abs": -4.0080678,
        "current_profit_pct": -11.88,
        "current_rate": 0.0712,
        "open_order": null
    }
]
```
There is no "NaN" value in the response.

What's the UI version you're using? i suspect you'll simply have to update the UI to the latest version to get this running again.
These are the NaN values in the provided response:
```
        "profit_ratio": NaN,
        "profit_pct": NaN,
        "profit_abs": NaN,
        "profit_fiat": NaN,

        "current_profit": NaN,
        "current_profit_abs": NaN,
        "current_profit_pct": NaN,


        "stoploss_current_dist": NaN,
        "stoploss_current_dist_pct": NaN,
        "stoploss_current_dist_ratio": NaN,
```

I'm running the latest FreqUI.

This just happened one day and since then stopped "working".
sorry - browser search didn't find them for some reason.

this seems to impact only one pair (GXS/USDT) - are you sure this pair is showing up in telegram correctly?

The pair has been [delisted](https://www.binance.com/en/support/announcement/bdfb52830f2f4bfcba496f3f92876b5c) - and replaced with another Pair. 
While that shouldn't break the UI per se - it makes it quite unlikely that it'll be correct/ok in telegram.
So the proper action here will most likely be to fix the trade (either deleting that trade, or switching the currency to the "new" currency. I'm however not sure if it's been a 1:1 conversion - so make sure you get the new currency amounts right (assuming this is a real trade, not dry-run).

Faking a trade for GSX i can see that it's also not working with telegram. As such - this stems from an invalid state in freqtrade (open trade on invalid pair).

![image](https://user-images.githubusercontent.com/5024695/165355598-8465ede1-1a62-4046-b450-a8fd9325a27c.png)


Your logs will probably be filled with "outdated history for pair GXS/USDT ... as well as "can't determine price".
you'll see that once you've fixed this particular problem, the visualization problem will disappear as well.


Resolving patch:
diff --git a/freqtrade/rpc/api_server/webserver.py b/freqtrade/rpc/api_server/webserver.py
--- a/freqtrade/rpc/api_server/webserver.py
+++ b/freqtrade/rpc/api_server/webserver.py
@@ -2,7 +2,7 @@
 from ipaddress import IPv4Address
 from typing import Any, Dict
 
-import rapidjson
+import orjson
 import uvicorn
 from fastapi import Depends, FastAPI
 from fastapi.middleware.cors import CORSMiddleware
@@ -24,7 +24,7 @@ def render(self, content: Any) -> bytes:
         Use rapidjson for responses
         Handles NaN and Inf / -Inf in a javascript way by default.
         """
-        return rapidjson.dumps(content).encode("utf-8")
+        return orjson.dumps(content, option=orjson.OPT_SERIALIZE_NUMPY)
 
 
 class ApiServer(RPCHandler):
diff --git a/setup.py b/setup.py
--- a/setup.py
+++ b/setup.py
@@ -57,6 +57,7 @@
         'pycoingecko',
         'py_find_1st',
         'python-rapidjson',
+        'orjson',
         'sdnotify',
         'colorama',
         'jinja2',


Test patch:
diff --git a/tests/rpc/test_rpc_apiserver.py b/tests/rpc/test_rpc_apiserver.py
--- a/tests/rpc/test_rpc_apiserver.py
+++ b/tests/rpc/test_rpc_apiserver.py
@@ -13,7 +13,6 @@
 from fastapi import FastAPI
 from fastapi.exceptions import HTTPException
 from fastapi.testclient import TestClient
-from numpy import isnan
 from requests.auth import _basic_auth_str
 
 from freqtrade.__init__ import __version__
@@ -985,7 +984,7 @@ def test_api_status(botclient, mocker, ticker, fee, markets, is_short,
     assert_response(rc)
     resp_values = rc.json()
     assert len(resp_values) == 4
-    assert isnan(resp_values[0]['profit_abs'])
+    assert resp_values[0]['profit_abs'] is None
 
 
 def test_api_version(botclient):