TWS automatically logs out after about 11:45 PM. They recently added an automatic restart feature to the beta release but I have yet to get it working.
Since I am just paper trading to test algorithms I found an AutoHotKey script that will start/reopen TWS and login when the process disappears. I decided to write my own using Powershell so it works on all Windows systems without needing to install something.
Use task scheduler to start this script when you want it to open. I then start my custom algorithm process about 10 minutes later.
Make sure the trading mode is set to paper inside