amor71 / LiuAlgoTrader

Framework for algorithmic trading
MIT License
789 stars 131 forks source link

Unable to launch the backtester_ui due to Streamlit error #186

Closed akgpt13 closed 3 years ago

akgpt13 commented 3 years ago

After setting up the tool and sourcing the env_vars.sh as source env_vars.sh, the next step as per the documentation is:

streamlit run https://raw.github.com/amor71/LiuAlgoTrader/master/analysis/backtester_ui.py

Below is the output of the above command:

  You can now view your Streamlit app in your browser.

  Local URL: http://localhost:8501
  Network URL: http://172.16.17.5:8501

2021-05-31 12:40:05.617 Compute Engine Metadata server unavailable onattempt 1 of 3. Reason: timed out
2021-05-31 12:40:05.685 Compute Engine Metadata server unavailable onattempt 2 of 3. Reason: [Errno 113] No route to host
2021-05-31 12:40:08.688 Compute Engine Metadata server unavailable onattempt 3 of 3. Reason: timed out
2021-05-31 12:40:08.688 Authentication failed using Compute Engine authentication due to unavailable metadata server.

Going to the web browser, below is the error on the webpage:

Below is the error that I receive: RuntimeError: There is no current event loop in thread 'ScriptRunner.scriptThread'.

Traceback:

File "/home/metacu/pythons/liu_env/lib/python3.8/site-packages/streamlit/script_runner.py", line 338, in _run_script
    exec(code, module.__dict__)
File "/tmp/tmp8h71d3lx/backtester_ui.py", line 17, in <module>
    from liualgotrader.analytics.analysis import (calc_batch_revenue, count_trades,
File "/home/metacu/pythons/liu_env/lib/python3.8/site-packages/liualgotrader/analytics/analysis.py", line 10, in <module>
    from liualgotrader.common.data_loader import DataLoader  # type: ignore
File "/home/metacu/pythons/liu_env/lib/python3.8/site-packages/liualgotrader/common/data_loader.py", line 21, in <module>
    nest_asyncio.apply()
File "/home/metacu/pythons/liu_env/lib/python3.8/site-packages/nest_asyncio.py", line 12, in apply
    loop = loop or asyncio.get_event_loop()
File "/usr/lib/python3.8/asyncio/events.py", line 639, in get_event_loop
    raise RuntimeError('There is no current event loop in thread %r.'

Screenshot: liu_algo

amor71 commented 3 years ago

Thanks for reporting the issue. I pushed a fix, kindly re-try and let me know if the issue resolved or not. Sorry about that.

akgpt13 commented 3 years ago

Thanks for the quick fix. I now have a different error, shown below.

Terminal Log:

  You can now view your Streamlit app in your browser.

  Local URL: http://localhost:8501
  Network URL: http://172.16.17.6:8501

2021-05-31 12:04:52.616 Failed to extract font properties from /usr/share/fonts/truetype/noto/NotoColorEmoji.ttf: In FT2Font: Can not load face.  Unknown file format.
2021-05-31 12:04:52.785 generated new fontManager
2021-05-31 12:04:56.070 Compute Engine Metadata server unavailable onattempt 1 of 3. Reason: timed out
2021-05-31 12:04:56.128 Compute Engine Metadata server unavailable onattempt 2 of 3. Reason: [Errno 113] No route to host
2021-05-31 12:04:59.130 Compute Engine Metadata server unavailable onattempt 3 of 3. Reason: timed out
2021-05-31 12:04:59.130 Authentication failed using Compute Engine authentication due to unavailable metadata server.

Browser error

And in the browser at the URL http://localhost:8501/, the error shown is :

ModuleNotFoundError: No module named 'liualgotrader.trading'
Traceback:

File "/home/metacu/anaconda3/envs/quant1/lib/python3.9/site-packages/streamlit/script_runner.py", line 338, in _run_script
    exec(code, module.__dict__)
File "/tmp/tmp4tgngtnf/backtester_ui.py", line 20, in <module>
    from liualgotrader.analytics.analysis import (calc_batch_revenue, count_trades,
File "/home/metacu/anaconda3/envs/quant1/lib/python3.9/site-packages/liualgotrader/analytics/analysis.py", line 16, in <module>
    from liualgotrader.trading.trader_factory import trader_factory
akgpt13 commented 3 years ago

Adding to the previous info, the command pip install liualgotrader executes successfully.

amor71 commented 3 years ago

Thank you for your email. I don't think font error is material, re: the error you see in the browser, I suspect it's related to your python-path. I see that you're using Anaconda (which I do not), I will try to replicate this locally. In the meantime, I suggest you add the path explicitly. Will get back to you shortly.

On Mon, May 31, 2021 at 10:08 PM akgpt13 @.***> wrote:

Thanks for the quick fix. I now have a different error, shown below. Terminal Log:

You can now view your Streamlit app in your browser.

Local URL: http://localhost:8501 Network URL: http://172.16.17.6:8501

2021-05-31 12:04:52.616 Failed to extract font properties from /usr/share/fonts/truetype/noto/NotoColorEmoji.ttf: In FT2Font: Can not load face. Unknown file format. 2021-05-31 12:04:52.785 generated new fontManager 2021-05-31 12:04:56.070 Compute Engine Metadata server unavailable onattempt 1 of 3. Reason: timed out 2021-05-31 12:04:56.128 Compute Engine Metadata server unavailable onattempt 2 of 3. Reason: [Errno 113] No route to host 2021-05-31 12:04:59.130 Compute Engine Metadata server unavailable onattempt 3 of 3. Reason: timed out 2021-05-31 12:04:59.130 Authentication failed using Compute Engine authentication due to unavailable metadata server.

Browser error

And in the browser at the URL http://localhost:8501/, the error shown is :

ModuleNotFoundError: No module named 'liualgotrader.trading' Traceback:

File "/home/metacu/anaconda3/envs/quant1/lib/python3.9/site-packages/streamlit/script_runner.py", line 338, in _run_script exec(code, module.dict) File "/tmp/tmp4tgngtnf/backtester_ui.py", line 20, in from liualgotrader.analytics.analysis import (calc_batch_revenue, count_trades, File "/home/metacu/anaconda3/envs/quant1/lib/python3.9/site-packages/liualgotrader/analytics/analysis.py", line 16, in from liualgotrader.trading.trader_factory import trader_factory

— You are receiving this because you were assigned. Reply to this email directly, view it on GitHub https://github.com/amor71/LiuAlgoTrader/issues/186#issuecomment-851640697, or unsubscribe https://github.com/notifications/unsubscribe-auth/ALTIHBAEW62UD2EKCJRH5C3TQPNBVANCNFSM4525VQKQ .

amor71 commented 3 years ago

I looked at this a bit more, but I may need your assistance to resolve it. It looks like an issue related to the Anaconda environment. I would be happy to add details on how to resolve the documentation (and add you as a contributor) if you can figure this out. Alternatively, I could suggest creating a plan vanilla virtual env for this ('python -m venv

") and installing LiuAlgoTrader in it. LMK, if that helps? On Mon, May 31, 2021 at 10:08 PM akgpt13 ***@***.***> wrote: > Thanks for the quick fix. I now have a different error, shown below. > Terminal Log: > > You can now view your Streamlit app in your browser. > > Local URL: http://localhost:8501 > Network URL: http://172.16.17.6:8501 > > 2021-05-31 12:04:52.616 Failed to extract font properties from /usr/share/fonts/truetype/noto/NotoColorEmoji.ttf: In FT2Font: Can not load face. Unknown file format. > 2021-05-31 12:04:52.785 generated new fontManager > 2021-05-31 12:04:56.070 Compute Engine Metadata server unavailable onattempt 1 of 3. Reason: timed out > 2021-05-31 12:04:56.128 Compute Engine Metadata server unavailable onattempt 2 of 3. Reason: [Errno 113] No route to host > 2021-05-31 12:04:59.130 Compute Engine Metadata server unavailable onattempt 3 of 3. Reason: timed out > 2021-05-31 12:04:59.130 Authentication failed using Compute Engine authentication due to unavailable metadata server. > > Browser error > > And in the browser at the URL http://localhost:8501/, the error shown is : > > ModuleNotFoundError: No module named 'liualgotrader.trading' > Traceback: > > File "/home/metacu/anaconda3/envs/quant1/lib/python3.9/site-packages/streamlit/script_runner.py", line 338, in _run_script > exec(code, module.__dict__) > File "/tmp/tmp4tgngtnf/backtester_ui.py", line 20, in > from liualgotrader.analytics.analysis import (calc_batch_revenue, count_trades, > File "/home/metacu/anaconda3/envs/quant1/lib/python3.9/site-packages/liualgotrader/analytics/analysis.py", line 16, in > from liualgotrader.trading.trader_factory import trader_factory > > — > You are receiving this because you were assigned. > Reply to this email directly, view it on GitHub > , > or unsubscribe > > . >
akgpt13 commented 3 years ago

Sure, I'm still trying to figure out. I tried with the python virtual environments and ended up spending several hours trying to get it to work on a plain vanilla Ubuntu Mate 20.04 set up from scratch. I think the documentation/wiki needs more improvement for setup so that it is easy to get started for anyone and I would be happy to contribute once I figure it out.

Below is the issue I'm currently stuck at with the venv style setup.

(env) metacu@ubuntu:~/liu/LiuAlgoTrader-0.3.0/liu_samples$ streamlit run https://raw.github.com/amor71/LiuAlgoTrader/master/analysis/backtester_ui.py
Traceback (most recent call last):
  File "/usr/local/bin/streamlit", line 33, in <module>
    sys.exit(load_entry_point('streamlit==0.82.0', 'console_scripts', 'streamlit')())
  File "/usr/local/bin/streamlit", line 25, in importlib_load_entry_point
    return next(matches).load()
  File "/usr/lib/python3.9/importlib/metadata.py", line 77, in load
    module = import_module(match.group('module'))
  File "/usr/lib/python3.9/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 972, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 855, in exec_module
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  File "/usr/local/lib/python3.9/dist-packages/streamlit-0.82.0-py3.9.egg/streamlit/__init__.py", line 76, in <module>
    from streamlit.delta_generator import DeltaGenerator as _DeltaGenerator
  File "/usr/local/lib/python3.9/dist-packages/streamlit-0.82.0-py3.9.egg/streamlit/delta_generator.py", line 63, in <module>
    from streamlit.elements.image import ImageMixin
  File "/usr/local/lib/python3.9/dist-packages/streamlit-0.82.0-py3.9.egg/streamlit/elements/image.py", line 24, in <module>
    from PIL import Image, ImageFile
  File "/usr/lib/python3/dist-packages/PIL/Image.py", line 94, in <module>
    from . import _imaging as core
ImportError: cannot import name '_imaging' from 'PIL' (/usr/lib/python3/dist-packages/PIL/__init__.py)

I am again trying with the conda style setup and again stuck with the same error:

telegram-cloud-photo-size-1-5021774929439336708-y

File "/home/metacu/anaconda3/envs/algotrading/lib/python3.9/site-packages/streamlit/script_runner.py", line 338, in _run_script
    exec(code, module.__dict__)
File "/tmp/tmpcyxo6aui/backtester_ui.py", line 20, in <module>
    from liualgotrader.analytics.analysis import (calc_batch_revenue, count_trades,
File "/home/metacu/anaconda3/envs/algotrading/lib/python3.9/site-packages/liualgotrader/analytics/analysis.py", line 16, in <module>
    from liualgotrader.trading.trader_factory import trader_factory
amor71 commented 3 years ago

I am sorry you're having difficulties installing this. The framework has two types of visualizations, pure Jupyer Notebook and streamlit ( https://streamlit.io/), a slightly more fancy UI for data science. I think you're missing imaging libraries, causing streamlit installation to fail. This installation issue is addressed here: https://askubuntu.com/questions/156484/how-do-i-install-python-imaging-library-pil). I hope you're going to have an easy ride once you get thru this. I'm happy to jump on an online session to see if we can get this up and running for you.

On Tue, Jun 1, 2021 at 11:01 AM akgpt13 @.***> wrote:

Sure, I'm still trying to figure out. I tried with the python virtual environments and ended up spending several hours trying to get it to work on a plain vanilla Ubuntu Mate 20.04 set up from scratch. I think the documentation/wiki needs more improvement for setup so that it is easy to get started for anyone and I would be happy to contribute once I figure it out.

Below is the issue I'm currently stuck at with the venv style setup.

(env) @.***:~/liu/LiuAlgoTrader-0.3.0/liu_samples$ streamlit run https://raw.github.com/amor71/LiuAlgoTrader/master/analysis/backtester_ui.py Traceback (most recent call last): File "/usr/local/bin/streamlit", line 33, in sys.exit(load_entry_point('streamlit==0.82.0', 'console_scripts', 'streamlit')()) File "/usr/local/bin/streamlit", line 25, in importlib_load_entry_point return next(matches).load() File "/usr/lib/python3.9/importlib/metadata.py", line 77, in load module = import_module(match.group('module')) File "/usr/lib/python3.9/importlib/init.py", line 127, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "", line 1030, in _gcd_import File "", line 1007, in _find_and_load File "", line 972, in _find_and_load_unlocked File "", line 228, in _call_with_frames_removed File "", line 1030, in _gcd_import File "", line 1007, in _find_and_load File "", line 986, in _find_and_load_unlocked File "", line 680, in _load_unlocked File "", line 855, in exec_module File "", line 228, in _call_with_frames_removed File "/usr/local/lib/python3.9/dist-packages/streamlit-0.82.0-py3.9.egg/streamlit/init.py", line 76, in from streamlit.delta_generator import DeltaGenerator as _DeltaGenerator File "/usr/local/lib/python3.9/dist-packages/streamlit-0.82.0-py3.9.egg/streamlit/delta_generator.py", line 63, in from streamlit.elements.image import ImageMixin File "/usr/local/lib/python3.9/dist-packages/streamlit-0.82.0-py3.9.egg/streamlit/elements/image.py", line 24, in from PIL import Image, ImageFile File "/usr/lib/python3/dist-packages/PIL/Image.py", line 94, in from . import _imaging as core ImportError: cannot import name '_imaging' from 'PIL' (/usr/lib/python3/dist-packages/PIL/init.py)

I am again trying with the conda style setup and again stuck with the same error:

[image: telegram-cloud-photo-size-1-5021774929439336708-y] https://user-images.githubusercontent.com/85116835/120288149-87647200-c2dd-11eb-8315-df44c774f81b.jpg

File "/home/metacu/anaconda3/envs/algotrading/lib/python3.9/site-packages/streamlit/script_runner.py", line 338, in _run_script exec(code, module.dict) File "/tmp/tmpcyxo6aui/backtester_ui.py", line 20, in from liualgotrader.analytics.analysis import (calc_batch_revenue, count_trades, File "/home/metacu/anaconda3/envs/algotrading/lib/python3.9/site-packages/liualgotrader/analytics/analysis.py", line 16, in from liualgotrader.trading.trader_factory import trader_factory

— You are receiving this because you were assigned. Reply to this email directly, view it on GitHub https://github.com/amor71/LiuAlgoTrader/issues/186#issuecomment-851916590, or unsubscribe https://github.com/notifications/unsubscribe-auth/ALTIHBE6FZLJ6VUHC7WCKRLTQSHXPANCNFSM4525VQKQ .

akgpt13 commented 3 years ago

Thanks! That would be super helpful. I have messaged you on Gitter chat.

akgpt13 commented 3 years ago

Got it working, thanks to @amor71.

If anyone is struggling with installation, here's how you can do it on a fresh Ubuntu 20.04 instance:

$ sudo apt update
$ sudo apt install -y apt-transport-https ca-certificates curl gnupg-agent software-properties-common
$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
$ sudo add-apt-repository \
"deb [arch=amd64] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) stable"
$ sudo apt-get update
$sudo apt install docker-ce -y
$ sudo usermod -aG docker $USER

Log out and log back in

$ sudo curl -L "https://github.com/docker/compose/releases/download/1.28.4/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
$ sudo chmod +x /usr/local/bin/docker-compose
$ sudo add-apt-repository ppa:deadsnakes/ppa
$ sudo apt update
$ sudo apt install python3.9
$ sudo apt install python3-pip
$ sudo apt install python3.9-venv
$ mkdir venv
$ python3.9 -m venv ~/venv/liu
$ source ~/venv/liu//bin/activate
$ mkdir ~/liu ; cd ~/liu
$ pip install liualgotrader
$ export (ALPACA KEYS)=xxx
$ liu QuickStart