Back

Python Net issue during importation

Hi,

I have been able to successfully run Python backtests locally on my machine, but I am struggling to run Jupyter notebooks properly. I am trying to execute the BasicQuantBookTemplate.ipynb locally on my machine.

When I try to execute this piece of code (which is the first cell in BasicQuantBookTemplatet.ipynb):

!pip install pythonnet
# Imports
from clr import AddReference
AddReference("System")
AddReference("QuantConnect.Common")
AddReference("QuantConnect.Research")
AddReference("QuantConnect.Indicators")
from System import *
from QuantConnect import *
from QuantConnect.Data.Custom import *
from QuantConnect.Data.Market import TradeBar, QuoteBar
from QuantConnect.Research import *
from QuantConnect.Indicators import *
from datetime import datetime, timedelta
import matplotlib.pyplot as plt
import pandas as pd

# Create an instance
qb = QuantBook()

# Select asset data
spy = qb.AddEquity("SPY")

I get the following backtrace:

Defaulting to user installation because normal site-packages is not writeable
Requirement already satisfied: pythonnet in /opt/conda/envs/py36/lib/python3.6/site-packages (2.4.0)
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-1-d06382665402> in <module>
1 get_ipython().system('pip install pythonnet')
2 # Imports
----> 3 from clr import AddReference
4 AddReference("System")
5 AddReference("QuantConnect.Common")

ImportError: System.NullReferenceException: Object reference not set to an instance of an object
at Python.Runtime.Runtime.XDecref (System.IntPtr op) [0x00041] in <c56ab175820d412caf052e079c2ab9ef>:0
at Python.Runtime.Runtime.Initialize () [0x001f0] in <c56ab175820d412caf052e079c2ab9ef>:0
at Python.Runtime.PythonEngine.Initialize (System.Collections.Generic.IEnumerable`1[T] args, System.Boolean setSysArgv) [0x0001e] in <c56ab175820d412caf052e079c2ab9ef>:0
at Python.Runtime.PythonEngine.Initialize (System.Boolean setSysArgv) [0x00005] in <c56ab175820d412caf052e079c2ab9ef>:0
at Python.Runtime.PythonEngine.InitExt () [0x00000] in <c56ab175820d412caf052e079c2ab9ef>:0

So it can find PythonNet installation properly, and yet, there is an import error in "from clr import AddReference" line. Any fixes for this?

Update Backtest







0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


I fixed this error by uncommenting one line in config.json (i.e., setting composer-dll-directory property in config.json to the directory where your Jupyter notebook is located). 

But now I get an error when I try to create a QuantBook instance.

# Create an instance
qb = QuantBook()
Exception Traceback (most recent call last)
<ipython-input-4-416c7ac85447> in <module>
21
22 # Create an instance
---> 23 qb = QuantBook()
24
25 # Select asset data

Exception: QuantBook.Main(): System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.ArgumentException: Must specify valid information for parsing in the string.
at System.Enum+EnumResult.SetFailure (System.Enum+ParseFailureKind failure, System.String failureMessageID, System.Object failureMessageFormatArgument) [0x00023] in <7cd4deb8302a478d9d393f24754841da>:0
at System.Enum.TryParseEnum (System.Type enumType, System.String value, System.Boolean ignoreCase, System.Enum+EnumResult& parseResult) [0x00077] in <7cd4deb8302a478d9d393f24754841da>:0
at System.Enum.Parse (System.Type enumType, System.String value, System.Boolean ignoreCase) [0x00010] in <7cd4deb8302a478d9d393f24754841da>:0
at System.Enum.Parse (System.Type enumType, System.String value) [0x00000] in <7cd4deb8302a478d9d393f24754841da>:0
at QuantConnect.Queues.JobQueue..ctor () [0x00040] in <60d151c1257e41089d5f35cc207a6ddd>:0
at (wrapper managed-to-native) System.Reflection.MonoCMethod.InternalInvoke(System.Reflection.MonoCMethod,object,object[],System.Exception&)
at System.Reflection.MonoCMethod.InternalInvoke (System.Object obj, System.Object[] parameters, System.Boolean wrapExceptions) [0x00005] in <7cd4deb8302a478d9d393f24754841da>:0
--- End of inner exception stack trace ---
at System.Reflection.MonoCMethod.InternalInvoke (System.Object obj, System.Object[] parameters, System.Boolean wrapExceptions) [0x00017] in <7cd4deb8302a478d9d393f24754841da>:0
at System.RuntimeType.CreateInstanceMono (System.Boolean nonPublic, System.Boolean wrapExceptions) [0x000a8] in <7cd4deb8302a478d9d393f24754841da>:0
at System.RuntimeType.CreateInstanceSlow (System.Boolean publicOnly, System.Boolean wrapExceptions, System.Boolean skipCheckThis, System.Boolean fillCache, System.Threading.StackCrawlMark& stackMark) [0x00009] in <7cd4deb8302a478d9d393f24754841da>:0
at System.RuntimeType.CreateInstanceDefaultCtor (System.Boolean publicOnly, System.Boolean skipCheckThis, System.Boolean fillCache, System.Boolean wrapExceptions, System.Threading.StackCrawlMark& stackMark) [0x00027] in <7cd4deb8302a478d9d393f24754841da>:0
at System.Activator.CreateInstance (System.Type type, System.Boolean nonPublic, System.Boolean wrapExceptions) [0x00020] in <7cd4deb8302a478d9d393f24754841da>:0
at System.Activator.CreateInstance (System.Type type, System.Boolean nonPublic) [0x00000] in <7cd4deb8302a478d9d393f24754841da>:0
at System.Activator.CreateInstance (System.Type type) [0x00000] in <7cd4deb8302a478d9d393f24754841da>:0
at QuantConnect.Util.Composer.GetExportedValueByTypeName[T] (System.String typeName) [0x000c3] in <15882b6f3206495eb733e234fe35e643>:0
at QuantConnect.Lean.Engine.LeanEngineSystemHandlers.FromConfiguration (QuantConnect.Util.Composer composer) [0x00011] in <9994176d389648808faf1d5881237ae5>:0
at QuantConnect.Research.QuantBook..ctor () [0x00052] in <8af5a254312a4ffa872a5efedfd90fef>:0
at QuantConnect.Research.QuantBook..ctor () [0x0022b] in <8af5a254312a4ffa872a5efedfd90fef>:0
at (wrapper managed-to-native) System.Reflection.MonoCMethod.InternalInvoke(System.Reflection.MonoCMethod,object,object[],System.Exception&)
at System.Reflection.MonoCMethod.InternalInvoke (System.Object obj, System.Object[] parameters, System.Boolean wrapExceptions) [0x00005] in <7cd4deb8302a478d9d393f24754841da>:0 Would highly appreciate if someone at QC could help me in resolving this issue.
0

Hi Kumar,

To help us reproduce this issue, can you outline the steps to setup the local project?

For instance,

  • How are you running it?
  • Which directory is the project in?
  • How does the project launch?
  • What are the changes to the config.json file?

Best,
Derek Melchin

0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Update Backtest





0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Loading...

This discussion is closed