QuantConnect / Lean

Lean Algorithmic Trading Engine by QuantConnect (Python, C#)
https://lean.io
Apache License 2.0
9.85k stars 3.27k forks source link

Loader.TryCreatePythonAlgorithm(): System.NullReferenceException: Object reference not set to an instance of an object #4937

Closed kgoderis closed 3 years ago

kgoderis commented 3 years ago

In https://github.com/QuantConnect/Lean/issues/4930 I already flagged that a docker image can not be build out of the box. I now have managed to do so by moving some packages from conda to pip for installation, as well as change the order of installation a bit. It relates to mlfinlab and fastai basically

The foundation docker looks like

#
#   LEAN Foundation Docker Container 20200428
#   Cross platform deployment for multiple brokerages
#   Intended to be used in conjunction with Dockerfile. This is just the foundation common OS+Dependencies required.
#

# Use base system for cleaning up wayward processes
FROM phusion/baseimage:0.9.22

MAINTAINER QuantConnect <contact@quantconnect.com>

# Use baseimage-docker's init system.
CMD ["/sbin/my_init"]

# Have to add env TZ=UTC. See https://github.com/dotnet/coreclr/issues/602
RUN env TZ=UTC

# Install OS Packages:
# Misc tools for running Python.NET and IB inside a headless container.
RUN add-apt-repository ppa:ubuntu-toolchain-r/test && apt-get update
RUN apt-get install -y git bzip2 clang cmake curl unzip wget python3-pip python-opengl zlib1g-dev && \
    apt-get install -y xvfb libxrender1 libxtst6 libxi6 libglib2.0-dev libopenmpi-dev libstdc++6 openmpi-bin && \
# Install R
    apt-get install -y r-base pandoc libcurl4-openssl-dev

# Java for running IB inside container:
RUN apt-get install -y openjdk-8-jdk openjdk-8-jre

# Install IB Gateway: Installs to ~/Jts
RUN wget http://cdn.quantconnect.com/interactive/ibgateway-stable-standalone-linux-x64-v978.2c.sh && \
    chmod 777 ibgateway-stable-standalone-linux-x64-v978.2c.sh && \
    ./ibgateway-stable-standalone-linux-x64-v978.2c.sh -q && \
    wget -O ~/Jts/jts.ini http://cdn.quantconnect.com/interactive/ibgateway-latest-standalone-linux-x64-v974.4g.jts.ini && \
    rm ibgateway-stable-standalone-linux-x64-v978.2c.sh

# Mono C# for LEAN:
# From https://github.com/mono/docker/blob/master/
RUN apt-get update && rm -rf /var/lib/apt/lists/*
RUN apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF
RUN echo "deb http://download.mono-project.com/repo/ubuntu stable-xenial/snapshots/5.12.0.226 main" > /etc/apt/sources.list.d/mono-xamarin.list && \
    apt-get update && apt-get install -y binutils mono-complete ca-certificates-mono mono-vbnc nuget referenceassemblies-pcl && \
    apt-get install -y fsharp && rm -rf /var/lib/apt/lists/* /tmp/*

# Install miniconda
ENV CONDA="Miniconda3-4.5.12-Linux-x86_64.sh"
ENV PATH="/opt/miniconda3/bin:${PATH}"
RUN wget https://cdn.quantconnect.com/miniconda/${CONDA} && \
    bash ${CONDA} -b -p /opt/miniconda3 && rm -rf ${CONDA} && \
    ln -s /opt/miniconda3/lib/libpython3.6m.so /usr/lib/libpython3.6m.so

# Install supported third party python packages
# Updates conda and pip
RUN conda update -y conda pip

# Updates pip
RUN pip install --upgrade pip

# Sets python version to 3.6.8
RUN conda install -y python=3.6.8
RUN conda clean -y --all

# Install essential packages
RUN conda install -y                \
    cython=0.29.15                  \
    numpy=1.18.1                    \
    pandas=0.25.3                   \
    wrapt=1.12.1

RUN pip install mlfinlab==0.15.0 

# Install non-math packages
RUN conda install -y                \
    astropy=4.0.1.post1             \
    beautifulsoup4=4.9.0            \
    blaze=0.11.3                    \
    dill=0.3.1.1                    \
    jsonschema=3.2.0                \
    lxml=4.5.0                      \
    msgpack-python=1.0.0            \
    numba=0.46                      \
    setuptools-git=1.2              \
    xarray=0.15.1

RUN conda install -y -c plotly      \
    plotly=4.6.0

RUN conda install -y -c conda-forge \
    jupyterlab=2.1.0

# Install TensorFlow 1.15.2
RUN pip install tensorflow==1.15.2

# Install math/ML packages
RUN conda install -y                \
    docutils=0.14                   \
    cvxopt=1.2.0                    \
    gensim=3.8.0                    \
    keras=2.3.1                     \
    lightgbm=2.3.0                  \
    nltk=3.4.5                      \
    pomegranate=0.11.1              \
    tensorflow-base=1.15.0          \
    python-graphviz=0.8.4

# Install requirement for fbprophet
RUN pip install cmdstanpy==0.4

# Install math/ML from conda-forge
RUN conda install -y -c conda-forge \
    copulae=0.3.1                   \
    featuretools=0.13.4             \
    fbprophet=0.6                   \
    pulp=1.6.8                      \
    pymc3=3.8                       \
    rauth=0.7.3                     \
    scikit-learn=0.21.3             \
    scikit-multiflow=0.4.1          \
    scikit-optimize=0.7.4           \
    theano=1.0.4                    \
    tsfresh=0.15.1                  \
    tslearn=0.3.1                   \
    tweepy=3.8.0                    \
    pywavelets=1.1.1                \
    umap-learn=0.4.1

# Install math/ML from pytorch
RUN conda install -y -c pytorch     \
    pytorch=1.5.0                   \
    torchvision=0.6.0

# Install math/ML from fastai
RUN conda install -y -c fastai      \
    nvidia-ml-py3=7.352.0           

#RUN conda install -y -c fastai      \
#    fastai=1.0.60

RUN pip install fastai==1.0.60

RUN conda clean -y --all

# Install from PIP I
RUN pip install arch==4.14          \
    copulalib==1.1.0                \
    copulas==0.3.0                  \
    creme==0.5.1                    \
    cufflinks==0.17.3               \
    gym==0.17.1                     \
    ipywidgets==7.5.1

# Install from PIP II
RUN pip install deap==1.3.1         \
    cvxpy==1.1.0a3                  \
#    mlfinlab==0.9.3                 \
    pykalman==0.9.5                 \
    pyportfolioopt==1.1.0           \
    pyramid-arima==0.9.0            \
    pyro-ppl==1.3.1                 \
    riskparityportfolio==0.1.6

# Install from PIP III
RUN pip install sklearn==0.0        \
    stable-baselines==2.10.0        \
    statistics==1.0.3.5             \
    statsmodels==0.11.1             \
    tensorforce==0.5.4              \
    QuantLib-Python==1.18           \
    xgboost==1.0.2                  \
    dtw-python==1.0.5

# Install from PIP IV
RUN pip install cntk==2.7           \
    mxnet==1.6                      \
    gluonts==0.4.3                  \
    gplearn==0.4.1                  \
    jax==0.1.64                     \
    jaxlib==0.1.45                  \
    keras-rl==0.4.2                 \
    pennylane==0.8.1

# Install Google Neural Tangents after JAX
RUN pip install neural-tangents==0.2.1

RUN pip install --upgrade mplfinance==0.12.3a3
RUN pip install --upgrade hmmlearn==0.2.3
RUN python -m nltk.downloader -d /usr/share/nltk_data punkt
RUN python -m nltk.downloader -d /usr/share/nltk_data vader_lexicon

# Install TA-lib for python
RUN wget http://cdn.quantconnect.com/ta-lib/ta-lib-0.4.0-src.tar.gz && \
    tar -zxvf ta-lib-0.4.0-src.tar.gz && cd ta-lib && \
    ./configure --prefix=/usr && make && make install && \
    pip install TA-lib && cd .. && rm -irf ta-lib

# Install DX Analytics
RUN wget https://cdn.quantconnect.com/dx/dx-master-9fab393.zip && \
    unzip -q dx-master-9fab393 && cd dx-master && \
    python setup.py install && cd .. && rm -irf dx-master

# Install py-earth
RUN wget https://cdn.quantconnect.com/py-earth/py-earth-master-b209d19.zip && \
    unzip -q py-earth-master-b209d19.zip && cd py-earth-master && \
    python setup.py install && cd .. && rm -irf py-earth-master

# Install fastText
RUN wget https://cdn.quantconnect.com/fastText/fastText-master-6d7c77c.zip && \
    unzip -q fastText-master-6d7c77c.zip && cd fastText-master && \
    python setup.py install && cd .. && rm -irf fastText-master

# Update ODO
RUN conda remove --force-remove -y odo
RUN wget https://cdn.quantconnect.com/odo/odo-master-9fce669.zip && \
    unzip -q odo-master-9fce669.zip && cd odo-master && \
    python setup.py install && cd .. && rm -irf odo-master

# Install Auto-KS
RUN wget https://cdn.quantconnect.com/auto_ks/auto_ks-master-b39e8f3.zip && \
    unzip -q auto_ks-master-b39e8f3.zip && cd auto_ks-master && \
    python setup.py install && cd .. && rm -irf auto_ks-master

# Install Pyrb
RUN wget https://cdn.quantconnect.com/pyrb/pyrb-master-d02b56a.zip && \
    unzip -q pyrb-master-d02b56a.zip && cd pyrb-master && \
    python setup.py install && cd .. && rm -irf pyrb-master

# Install SSM
RUN wget https://cdn.quantconnect.com/ssm/ssm-master-34b50d4.zip && \
    unzip -q ssm-master-34b50d4.zip && cd ssm-master && \
    python setup.py install && cd .. && rm -irf ssm-master

# Install Tigramite
RUN wget https://cdn.quantconnect.com/tigramite/tigramite-master-eee4809.zip && \
    unzip -q tigramite-master-eee4809.zip && cd tigramite-master && \
    python setup.py install && cd .. && rm -irf tigramite-master

# Install H2O
RUN wget https://cdn.quantconnect.com/h2o/h2o-3.30.0.1.zip && \
    unzip -q h2o-3.30.0.1.zip && \
    pip install h2o-3.30.0.1/python/h2o-3.30.0.1-py2.py3-none-any.whl && \
    rm -irf h2o-3.30.0.1

# Delete temporary zip files
RUN rm -irf *.zip *.gz

# Remove black-listed packages
RUN pip uninstall -y s3transfer
RUN conda remove --force-remove -y s3transfer
RUN conda clean -y --all

# List all packages
RUN conda list

Following the remainder of the instructions I tried to run a python algorithm, it fails when the Python.Runtime is initialised. I get the following output:

  at <unknown> <0xffffffff>
  at (wrapper managed-to-native) Python.Runtime.NativeMethods.dlsym (intptr,string) [0x0000b] in <6502d9c22ea54a59ae1303c418913e75>:0
  at Python.Runtime.NativeMethods.GetProcAddress (intptr,string) [0x0001a] in <6502d9c22ea54a59ae1303c418913e75>:0
  at Python.Runtime.Runtime.Initialize () [0x002a7] in <6502d9c22ea54a59ae1303c418913e75>:0
  at Python.Runtime.PythonEngine.Initialize (System.Collections.Generic.IEnumerable`1<string>,bool) [0x0001e] in <6502d9c22ea54a59ae1303c418913e75>:0
  at Python.Runtime.PythonEngine.Initialize (bool) [0x00005] in <6502d9c22ea54a59ae1303c418913e75>:0
  at Python.Runtime.PythonEngine.Initialize () [0x00000] in <6502d9c22ea54a59ae1303c418913e75>:0
  at QuantConnect.Python.PythonInitializer.Initialize () [0x0001a] in <93012fbbed3a459db22cf267f229f2e7>:0

Still, in order to get things going, I have changed Python.Runtime.dll.config file to

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <dllmap os="linux" dll="python3.6m" target = "/usr/lib/libpython3.6m.so"/>
</configuration>

with a direct reference to the .so in /usr/lib (did not bother creating a virtual env)

What am I missing here, or what it the docker image on Docker Hub containing that is different from what the Dockerfile ought to build ? (Because, everything runs ok if I used the pre-built image on Docker Hub instead of trying to build one myself)

Martin-Molinero commented 3 years ago

Hey @kgoderis!

What am I missing here, or what it the docker image on Docker Hub containing that is different from what the Dockerfile ought to build ? (Because, everything runs ok if I used the pre-built image on Docker Hub instead of trying to build one myself)

How are you building the binaries (OS/builder version)? One of the reasons could be that Windows pythonNet isn't the same binary than the one used in Linux, there's a ForceLinuxBuild cross build flag for windows you could try using. This is why we created the docker hub images and recommend using them instead.

kgoderis commented 3 years ago

@Martin-Molinero MacOS X 10.15 with Visual Studio. Is that flag also for MacOS

The reason I want to build my own container is that I need TensorFlow 2.3 and TF-agents for my project. And maybe some other more recent ML libs that are not in the image, or that are due for an upgrade

Martin-Molinero commented 3 years ago

Hey @kgoderis, Foundation docker image is building again and has been udpated, we will slowly keep updating it to keep up with requests. Suggest directly working on top of the image adding any desired changes/packages, instead of building it from scratch, would this work for you?

Is that flag also for MacOS

yes, it will be required because you are building binaries that are going to be executed by the linux docker image, right? Based on the provided stack exception I can't tell what's wrong, haven't seen it before I believe, but the flag will be a must I think

Martin-Molinero commented 3 years ago

Closing for now since this isn't a Lean bug. Hope the previous comment solved the issue seen 👍