Forex



Go Back   Forex Trading > Programming > MetaTrader
Forex Forum Register More recent Calendar Advertising Others Help






Register
Welcome to Forex-TSD!, one of the largest Forex forums worldwide, where you will be able to find the most complete and reliable Forex information imaginable.

From the list below, select the forum that you want to visit and register to post, as many times you want. It’s absolutely free. Click here for registering on Forex-TSD.

Exclusive Forum
The Exclusive Forum is the only paid section. Once you subscribe, you will get free access to real cutting-edge Trading Systems (automated and not), Indicators, Signals, Articles, etc., that will help and guide you, in ways that you could only imagine, with your Forex trading.
  • Elite Section
    Get access to private discussions, specialized support, indicators and trading systems reported every week.
  • Advanced Elite Section
    For professional traders, trading system developers and any other member who may need to use and/or convert, the most cutting-edge exclusive indicators and trading systems for MT4 and MT5.
See more

Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old 08-16-2009, 03:11 AM
Junior Member
 
Join Date: Mar 2009
Posts: 4
7bit is on a distinguished road
bindings for python 2.6

Hello,

i have a minimal example that will crash metatrader:

Code:
#import "python26.dll"
void Py_Initialize();
void Py_Finalize();
int PyRun_SimpleString(string code);
#import


int init(){
    Py_Initialize();
    PyRun_SimpleString("x=42");
    Py_Finalize();
   
    return(0);
}
From what i have read elsewhere using string in mql4 will pass a char* pointer to the function and thats exactly what this particular function wants. writing a dll with freepascal that takes the exact same argument (PChar) and passes it unmodifed to PyRun_SimpleString() will work.

Can anybody enlighten me why the above code will provoke a crash, although the argument passed to it is indeed a pointer to the string, exactly as required by the function? And why it will *not* crash when i define the *exact* same function in my own dll and just pass on the unmodified argument to the original function in python26.dll?

TIA,
Bernd
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!StumbleUpon this Post!Reddit this Post!Facebook this Post!BlinkList this Post!Google Bookmarks this Post!Yahoo! My Web this Post!
Reply With Quote
  #2 (permalink)  
Old 08-16-2009, 09:02 PM
Junior Member
 
Join Date: Mar 2009
Posts: 4
7bit is on a distinguished road
bindings for python 2.6

because i was not able to call the python interpreter directly i have written a wrapper with even a little abstraction:

Link: py26 (dll into libraries, mqh into include)

You can now execute arbitrary Python code from mql, import modules, run threads, file and socket I/O, interface with databases, run network servers, interface with other lanuages like for example R via rpy, everything you can think of.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!StumbleUpon this Post!Reddit this Post!Facebook this Post!BlinkList this Post!Google Bookmarks this Post!Yahoo! My Web this Post!
Reply With Quote
  #3 (permalink)  
Old 08-17-2009, 11:41 PM
Junior Member
 
Join Date: Mar 2009
Posts: 4
7bit is on a distinguished road
Quote:
Originally Posted by newdigital View Post
Yes, sorry.
I moved your posts to separated thread.
I will forgive you ;-) [you can safely delete the posting with the many colors, i was a bit upset because i didnt understand what was going on, i have since then calmed down completely]

I can even answer my question from the first posting: It is not because of the char* pointer, it is because of calling conventions: python26.dll uses __cdecl and metatrader expects __stdcall, the crash happens because the functions in python26.dll will not pop the stack when returning. Therefore the unavoidable need for a wrapper dll which will provide a __stdcall interface to metatrader and call python with __cdecl

I have already identified a new problem: all experts running at the same time will share the same __main__ namespace, so i will have to rethink the abstraction so that it allows to easily specify the namespace when accessing variables and not force you to operate only in __main__
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!StumbleUpon this Post!Reddit this Post!Facebook this Post!BlinkList this Post!Google Bookmarks this Post!Yahoo! My Web this Post!
Reply With Quote
  #4 (permalink)  
Old 08-20-2009, 10:41 PM
Junior Member
 
Join Date: Mar 2009
Posts: 4
7bit is on a distinguished road
I have made some progress: I was able to solve a little problem with threads. Now the interpreter runs in its own thread what effectively means you are now able to have multithreaded expert advisors, while metatrader does something else all python threads started (using the thread module) inside a PyExecute() call keep running in the background even after this function has returned.

All currrent files can be found here: Metatrader Python Integration (7bit)

Attached is a screenshot of what i was able to do with it, shortly after i got it working
Attached Images
File Type: png python_threads_now_working.png (62.2 KB, 45 views)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!StumbleUpon this Post!Reddit this Post!Facebook this Post!BlinkList this Post!Google Bookmarks this Post!Yahoo! My Web this Post!
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off
Forum Jump


All times are GMT. The time now is 10:44 PM.



Search Engine Friendly URLs by vBSEO 3.2.0 ©2008, Crawlability, Inc.