Is it possible to run Python on Amibroker?

@Waz
I’ve same experience as yourself.
I can confirm registered correctly and see with MS oleview.

Also tried COM in excel using VB module and worked well.
Exactly same as book I referenced earlier post.

in AFL code, AmiBroker closes without error message etc.
Code in a chart caused AmiBroker to close once it’s layout was selected.
So I ran AFL code in editor and debug mode only.
No different, but now could step through and variable/object watch window.

Note: AB help states CreateObject(“name”) starts COM server.
Perhaps python code should be client side COM?

Once I tried AB COM CreateObject(“Broker Application”),
then code for python COM; CreateObject(“ABTestObj.example”).
It didn’t close AmiBroker, but this occurred only once.

I had changed code to (object)
Class ABTestObj(Object):
so there was an object.
AB recognised the object but still no pointer.
Usually AB closed executing next line

Haven’t tried VB script to use ActiveXObject, since don’t think it’ll be different.

Stackoverflow forum has posts where client side GetModule() call is necessary even if coding server.
It has something to do with the COM pointer which is exactly this problem.
No information otherwise or why.

So 2 things to try
-client COM code
-GetModule()
Hope helps with tracking problem down.
regards
FE

1 Like

The following works for me.
Python Code. Save the code to a file and run from command line.
For example: python pythoncom.py

# COM Server  
import pythoncom
import math

class ABTestObj:
    _public_methods_ = ["MyABMethod"]   #your public method
    _reg_progid_ = "ABTest.main"   # name of your com server
    # use pythoncom.CreateGuid()  to create your ID
    _reg_clsid_ = "{0215255D-AB32-485C-BB5C-B918E0E8CFB6}" # create/register COM GUID

    def MyABMethod(self, factor, input_array):
        arr = list(input_array)

        for index in range(len(arr)):
            if not arr[index]:
                arr[index] = 0.0
        return [x*factor for x in arr]

if __name__ == "__main__":
    # run "python PyComTestAB.py 
    #    register the COM server
    # run "python PyComTestAB.py --unregister"
    #    to unregister it
    print("Registering COM server...")
    import win32com.server.register
    win32com.server.register.UseCommandLine(ABTestObj)

Here is the AmiBroker code. Run this as an exploration.

ABex = CreateObject("ABTest.main");
ret_value = ABex.MyABMethod(2, Close);

Filter = 1;
AddColumn(C,"C");
AddColumn(ret_value,"ret_value");

Cesar
Alvarez Quant Trading

10 Likes

Thank you for your code.
Unfortunately, it provides the same results as some similar codes mentionned above (from AB library and Marketcalls): the COM server seems to be properly registered but AB closes without any error message!

1 Like

@Waz
Have you an update for this?

I’m thinking this may be incompatible between 32-bit pythoncom and ABx64.
I’m not sure of this either.
Sorry I can’t be more assistance, stumped.

Unfortunately, no improvement on that topic.
My configuration is 32 bits for both AB and Python.

Hello , I wanna know is it possible that using Python 3.6 integrate with
Amibroker?
I have seen this example
http://www.amibroker.com/library/detail.php?id=1064&hilite=CreateObject

How ever this example is too old , last maintain day is 2008.
And it seem only could integrate Amibroker with Python2 ?

If i can use python 3.6 to create COM , which module I can use it?

tks a lot

1 Like

Why don’t you decide which thread to use? I think - I this is the proper one. You have created 3 threads on the same topic:



1 Like

First, “Is it possible to run python on Amibroker?” is the question that how to run python code on Amibroker .
And the article I post recently is the question that how to integrate Amibroker with python 3.6 instead of python2 .
So thats not the same issue.

Secound , Because of the review process, I originally thought that my article did not send a document, so I post the article again , I will pay attention to this later.

I am not familiar with the process of this forum, did not find the way to delete articles .
If the administrator has the permission please help me delete one of them (I post recently)

Thank you for informing

1 Like

The short answer is yes. I’m actually dabbling with it using this Python version:

3.6.0 | packaged by conda-forge | (default, Feb 9 2017, 14:54:13) [MSC v.1900 64 bit (AMD64)]

This is the string returned in my Python COM server from a function called in an AFL exploration in the following way:

_TRACE(PY2AB.python_version());

and the corresponding Python code (part of the COM server code) is:

import sys
    .....
    .....
    # Returns a string.   
    def python_version(self):
        return sys.version 

My Python COM server is modeled as the example in the AFL library referenced in a previous post.

You probably need to double check your Python installation (and bitness) to ensure that everything needed at runtime is in the path and working well in the native mode (using IDLE to check is a simple way to do it).

1 Like

P.S. Then start with a very basic example with just one function like in the post of @CesarA (get rid of all the extra code and “import” you added) and see if it works. The add one thing at the time and test again (I found that some modules / functions, when used, generate strange COM errors; for instance, I had some Memory related issues converting to SafeArrays).
Rinse and repeat.

1 Like

Frequently I hear the question What is the easiest programming language to learn and why? and/or similar questions: there are a lot of different opinions.

Learnig any programming language will also improve your ability to code in AFL.

For sure Python is gaining a lot of popularity and today is one of the most widely used programming languages in the area of Machine/Deep Learning.

It also offers many free libraries related to finance and data analysis. For instance, the widely use Pandas library was originally developed to be used by banks and hedge funds.
Its main rival for statistical and data science is "R"

This is an old blog article that compare some Python features vs Java (still the most widely used programming language - beware a lot of jargon....)

The best suggestion I can give to any serious self-study beginner that want to learn a popular programming language is to buy a Deitel book from the series "How to program" (in particular for Java, C, C++, C#, VB). These books, frequently used in schools as textbooks, are huge and very expensive but it is easy to find the previous edition used copies (to learn the basic of a programming language there is no immediate need to get the latest version).
Unfortunately, their only title for Python is very old and why the principles of programming are the same, today it is a lot better to start with Python 3. I know that they are actively working to release a new Python book (in this case it is better to wait for its release)

An entirely different approach, more geared to absolute beginners/casual programmers, is the one of the "Head First" series from "O'Reilly.

Finally, I think that Pascal (now surviving in some way via Delphi) was the best and easiest programming language to learn!

6 Likes

I am struggling to make changes stick to this code. On the initial registration of the com server, all works fine, but when I edit the python code to something other than close*factor, these changes are not reflected in the explorer results.

I unregister the server, then register it again, but no luck.

I change the function name in the code, and then reference that new function in the amibroker afl file, but then Amibroker tells me the COM method/function does not exit.

It's like there is something referring to the old code whenever the server is called. Have I missed something? How can I make regular edits to the code and have that reflected on Amibroker?

Thanks in advance for any pointers!

Adrian

2 Likes

@adrianm68 do you update the public_methods = [] list, each time you add a new function, delete one, or change the existing functions names?

This Quick Start to Server side COM and Python may help you.

2 Likes

Thanks beppe for the guide. I think I have worked out my issue - will report back if I have further issues.

Cheers,

I'm finding the connection between amibroker and Python to be not very robust. For instance, if I run the code via iPython (CesarA code above), it works. If I run it in Jupyter notebook, the execution works fine, with the statement that the com server is registered, but I end up with Amibroker indicating the COM/object handle is null?

Then I go back to the script that does work, add a few more lines of python code, unrelated to the COM server, and I get a successfully registered COM server according to Python, but once again, Amibroker indicates the null handle again.

It seems the only thing that works is the exact code that CesarA posted above. Any tips on how I can make this connection more reliable?

Adrian

1 Like

Look for problems in Python.

Other programs including Windows Scripting host, JScript, VBScript, AmiQuote itself (which uses OLE to do ALL communication with AmiBroker), etc don't have any problem.

@adrianm68, while I use Jupyter extensively to explore Python modules and to test how to use some functions, I never employed it to register the COM server.

In general, when I have some new code working in a notebook, I copy/merge the relevant parts to an external text code editor file where I already have a working version of the COM server code.

I then further verify that the server side interfaces part of the code is as expected.
Some module specific data types that work with no issues in Jupyter, or running a script in the shell, need to be converted to native Python types before assigning them to the return values.
(This is done to avoid any Win32Com error at runtime, ensuring that return types are convertible between Python and COM).

As I said in a previous message, it is important also to verify the congruence of the Win32 specific attributes to reflect any addition, deletion or change to any previously implemented functions.

After saving the modified .py file, I re-register the server running it from the command line. This seems to work well for me.

In any case, due to the complexity of the Python environment (especially running under Windows), and the ever-evolving modules I think that it is a good practice to keep track of previous working versions using some form of version control like Git.

1 Like

Thanks Guys.

I am sure it's my mostly new python skills that are letting me down. I'll go back and hit the books and try again when I'm a bit more competent in the language.

Cheers,

Adrian

1 Like

This is OLD THREAD but YES you can run Python from AmiBroker now, thanks to AmiPy:

1 Like