Repository: freqtrade/freqtrade

Issue:
API Server under 2021.8
<!-- 
Have you searched for similar issues before posting it?
Did you have a VERY good look at the [documentation](https://www.freqtrade.io/en/latest/) and are sure that the question is not explained there

Please do not use the question template to report bugs or to request new features.
-->

## Describe your environment

  * Operating system: Windows Server 2019
  * Python Version: Miniconda 3
  * CCXT version: 1.55.56_____ (`pip freeze | grep ccxt`)
  * Freqtrade Version: 2021.8 (`freqtrade -V` or `docker-compose run --rm freqtrade -V` for Freqtrade running in docker)
  
## Your question
This might be a bug, I post it as question, since I am nor sure for 100%.

(OS and Miniconda configuration works fine for Freqtrade since 2020.12)
Trading works fine under Telegram with current version.
With current version and activated API Server, the system remains idle and does not begin to trade. Play button in GUI is pushed.  Even no trades visible in DB, opened in a SQlite explorer.
API Server web GUI works excellent.
(Trading with API Server works fine under 2021.7)

*Ask the question you have not been able to find an answer in our [Documentation](https://www.freqtrade.io/en/latest/)*



Discussion:
the API is not relevant to trading, but is there for informal purposes (while it's possible to stop/start trading, it's not required nor necessary for the bot to work).
Please make sure that you've updated your dependencies along with freqtrade.

Also - we'll need logs - without it, there's nothing we can do. If you're using a configuration generated in 2020, it is possible that you're having some messages / incompatibilities in your configuration.
The log will show that.
Also, freqtrade works and trades fine for me on the latest version - so i'd think it's not a general problem - maybe simply a inactive phase from your strategy.
I thank you for your reply. This bot is a fresh 2021.8 installation in dryrun mode. I have multiple bots with different strategies under 2021.8 via API server. None of them starts to trade. To be sure, that the reason is not simply trend related, I started a strategy, which is also active on telegram. Both of the bots are dryrunning on binance. The telegram bot has multiple buy / sell transactions per day. The file with the log is the API server version with no trades: I copied and pasted the content from http://192.168.144.39:11016/logs. Tell me, if you need more time range. By the way, this strategy is the known strategy Quickie from berlinguyinca. This one has always a lot of sell / buy transactions on binance during the day, why it is a good test for us.
[log.txt](https://github.com/freqtrade/freqtrade/files/7085902/log.txt), 
This is only the access log (which is strange in it's own right - as that's disabled by default).

It's missing everything else ... how exactly did you start the bot?
I have set verbosity to info.
I always start all bots similar to the printscreen. This works fine since months for telegram and api. Dry and live run.
Do you want me to activate --logfile FILE in the command line and let it run for some hours?

![Screenshot_1](https://user-images.githubusercontent.com/34372534/131561343-de3c93cb-fa31-46c2-8015-d579a57254a8.png)

as mentioned above - the api (so all the log that you provided) does is pointless.

Best start the bot and have a very close look at the log entries (best also disable api log or disable auto-reloads in the UI to avoid cluttering the log again).


I have at these points.
Currently I started a test, with telegram AND API server set to false.
No other changes.
Some minutes after the start:
2021-09-01 13:53:49,827 - freqtrade.persistence.models - INFO - MARKET_BUY has been fulfilled for Trade(id=None, pair=YFII/USDT, amount=0.00390000, open_rate=6303.00000000, open_since=2021-09-01 11:53:49).

. . .so there is an issue in my case with the API server.
That means the bot did buy something.

The API server is not tied into the bot loop - so it can't influence the bot behaviour (unless you run /forcebuy / forcesell).
I think you're misinterpreting the information provided here - all this shows is that today your strategy provided a buy signal.

that has nothing to do with the API server - but solely with your strategy.
I am absolutely at your side, since I am also programming: Financial transactions won't have anything to do with the graphical part. That's why I don't understand, why setting API to false, enables trading. This seems to be impossible. I continue my tests, and let you know more asap.
Can confirm, after upgrading to 2021.8 the run loop doesn't seem to actually run with both API and Telegram enabled. Disabling the API 'wakes' everything back up.

For example, there is no 'Bot heartbeat' messages with both enabled.
so far, noone was able to provide me with logs of the bot's startup.
The log provided above is the api access log - which is NOT relevant itself.

It's also important to mention if you're using the docker image, or a native installation. (in case of native installation - please always make sure to also update dependencies!).


Resolving patch:
diff --git a/freqtrade/rpc/api_server/uvicorn_threaded.py b/freqtrade/rpc/api_server/uvicorn_threaded.py
--- a/freqtrade/rpc/api_server/uvicorn_threaded.py
+++ b/freqtrade/rpc/api_server/uvicorn_threaded.py
@@ -5,6 +5,20 @@
 import uvicorn
 
 
+def asyncio_setup() -> None:  # pragma: no cover
+    # Set eventloop for win32 setups
+    # Reverts a change done in uvicorn 0.15.0 - which now sets the eventloop
+    # via policy.
+    import sys
+
+    if sys.version_info >= (3, 8) and sys.platform == "win32":
+        import asyncio
+        import selectors
+        selector = selectors.SelectSelector()
+        loop = asyncio.SelectorEventLoop(selector)
+        asyncio.set_event_loop(loop)
+
+
 class UvicornServer(uvicorn.Server):
     """
     Multithreaded server - as found in https://github.com/encode/uvicorn/issues/742
@@ -28,7 +42,7 @@ def run(self, sockets=None):
         try:
             import uvloop  # noqa
         except ImportError:  # pragma: no cover
-            from uvicorn.loops.asyncio import asyncio_setup
+
             asyncio_setup()
         else:
             asyncio.set_event_loop(uvloop.new_event_loop())


Test patch: