0

I have this code for MQL5.

#property indicator_chart_window
#property indicator_buffers 1
#property indicator_plots 1

#import  "Indicator\\..\\Libraries\\PythonConnect.dll"

int OnInit()
  {
//---
   return(INIT_SUCCEEDED);
  }

int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
//---
   Print(PythonConnect1::TRENDFINDER());
//--- return value of prev_calculated for next call
   return(rates_total);
  }

I have created the PyhtonConnect.dll. But when I am running the code i am getting the following error:

2020.01.01 00:00:00   System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.IO.FileNotFoundException: Could not load file or assembly 'Python.Runtime, Version=2.5.1.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.
2020.01.01 00:00:00      at PythonConnect.PythonConnect1.TRENDFINDER()
2020.01.01 00:00:00      --- End of inner exception stack trace ---
2020.01.01 00:00:00      at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
2020.01.01 00:00:00      at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
2020.01.01 00:00:00      at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
2020.01.01 00:00:00      at Wrapper.Wrapper.Call(MethodInfo method, Int64 owner, Int64 parameters, Int64 result)
2020.01.01 00:00:00   .Net runtime error

I am not getting the way to resolve it. I do not know why the file is not found, whereas it is placed inside the same folder where the PythonConnect.dll is placed.

Please somebody with dll experience let me know what might be the way to resolve it.

Also, to mention, I have tried #import "Indicators\\..\\Libraries\\Python.Runtime.dll". But still I am getting the same error.

Jaffer Wilson
  • 7,029
  • 10
  • 62
  • 139
  • The path is relative to the location of where the c# executable is located. Try a full path starting with "c:\" or a network drive \\myserver\ – jdweng Nov 25 '20 at 11:38
  • @jdweng here I you say I need to make the changes? I cannot change the way of MQL5, but can do something with the C#. – Jaffer Wilson Nov 25 '20 at 11:47
  • The MQL5 library has to stay were it was built since the reference files are relative to the build location. So you have to add the MQL5 library to you c# project by adding a Reference to the library and browsing for the dll. So when you get done remove the library from your c# bin folder. Make sure it is not getting copied back in to the c# bin folder after compiling. – jdweng Nov 25 '20 at 12:20
  • @jdweng Previousy I have loaded the dll from the same folder. But it was not having any dependency and worked well. After I have added Python.Rumtime.dll to the reference of the project, I started getting the above error. Please can you tell me why it is this happening and how I can resolve it? – Jaffer Wilson Nov 25 '20 at 12:50
  • What do you mean by "loaded dll from the same folder"? Is the dll being copied to you project bin folder or are you keeping the dll where it current exists? The Python.Runtime.dll has to be located with respect to where the dll is trying to locate the Python file. You have to be careful with Python. I was using PythonIron last year in a c# project and found Python 4 only had a max filename size (including path) of 128 characters. When I tried running from a Network Drive it would fail but work on my local drive. – jdweng Nov 25 '20 at 13:22
  • @jdweng I have compiled the Library and the dll is formed inside the folder location that I have specified in MQL5. Along with the PythonConnect.dll, I have got the Python.Runtime.dll automatically stored with the PythonConnect.dll file. I just do not understand why the error is occurring. Why the Python.Runtime.dll is not getting loaded? If you have doubt then please let me know I will explain it. – Jaffer Wilson Nov 25 '20 at 14:26
  • Where is the Experts folder? "Experts\\..\\Libraries\\Python.Runtime.dll" – jdweng Nov 25 '20 at 14:28
  • It is in the MQL5/Experts/myproject/Libraries – Jaffer Wilson Nov 25 '20 at 14:34
  • How does the dll find the folder Experts? It is a relative path with respect to the location of the dll. – jdweng Nov 25 '20 at 14:40
  • Actually, you will understand it with the documentation of MQL5 #import: https://www.mql5.com/en/docs/runtime/imports – Jaffer Wilson Nov 25 '20 at 14:42
  • I looked at the documentation earlier. That is why I asked if the dll was located in your project bin folder or where it was originally build. The code is not finding the Experts folder. – jdweng Nov 25 '20 at 14:55
  • Please do not let my bounty go waste. – Jaffer Wilson Nov 29 '20 at 06:09
  • See if following helps : https://stackoverflow.com/questions/14633695/how-to-install-python-for-net-on-windows – jdweng Nov 29 '20 at 12:42
  • @jdweng Thank you for the reference. But this is not the issue. I am not able to fetch a dll from a dll. And the to be fetched dll is in the same folder where the dll is placed. – Jaffer Wilson Nov 29 '20 at 13:08
  • did you read following link : https://pypi.org/project/pythonnet/ – jdweng Nov 29 '20 at 17:57
  • @jdweng Yes, I did. What new you have found in it? I have tried everything in mind but nothing is working. – Jaffer Wilson Nov 30 '20 at 05:51
  • Does the PythonConnect script run by itself with same python script? Your error indicates a file used by the python script cannot be found. Not the dll being found. What version of python are you using? I had similar issue last year and found the pathname was too long. I was using a Network drive and the pathname (including the filename) was > 128 characters. When I moved code to my local c:\ drive it worked. I had to put a break point in the code and look at the instance of the python object before and after the exception to understand what failed. – jdweng Nov 30 '20 at 09:11
  • @jdweng Before creating a dll I used Python code in WinForm, using the same Python.Rumtime.Dll, it working fine. But when I created a Dll using the Python.Runtime.DLL, I got the above error. I am using local system and now network based dependency. my Python location is on `C:\Python37`. So there is no way it might exceed any character limit. I am using `Python 3.7.9`. – Jaffer Wilson Nov 30 '20 at 11:32
  • No. I had an application that was working in the c:\Program File folder. When I went to use on Network Drive I Tried relative pathnames and python converted to a full network path (ie \\myserver\folder1\folder2\filename). I then tried mapping a d:\ folder and again python converted to a full network path name. I had no control over the network pathname since it was a standard company pathname. I was using Pythopn 4 that had limits of 128 characters. I think new version of python may have longer limit. – jdweng Nov 30 '20 at 11:39
  • If you want to know where Windows is looking for files, download Microsoft's ProcMon (https://learn.microsoft.com/en-us/sysinternals/downloads/procmon). This shows all file access attempts. Search for "Python.Runtime.dll" after running your program, and you'll see the path it's looking on as a sequence of "FILE NOT FOUND" errors. This may help narrow down what's going on. It may be that you can just add the path to your PATH environment variable. – Rodney Richardson Dec 02 '20 at 15:33
  • I have solved similar problems in the past by using the "AppDomain.CurrentDomain.AssemblyResolve" event which will allow you to see where dot net is looking for an assembly and if necessary, "interfere" with the process. – Glen Dec 03 '20 at 00:43
  • @Glen I will check that. – Jaffer Wilson Dec 03 '20 at 09:51

0 Answers0