Is it possible to run Python on Amibroker?

howto
Tags: #<Tag:0x00007f2d7b18b708>

#21

@Waz
Not sure what’s wrong if registered correctly.
Vaguely recall similar question on this forum.
– meant that Pointer not returned when AFL COM client created with

ABex = CreateObject(“ABTestObject.example”);

command
Sorry can’t enlighten.
Perhaps @jamesfisher example above this post could help
FE


#22

It can only happen when object is not properly registered and/or does not exist.


#23

On Python’s side, the registration seems successfull:
run ABtest.py
Registering COM server…
Requesting elevation and retrying…
Registering COM server…
Registered: ABTestObj.example

In the register, I can see miscellaneous ABTestObj entries.

Is there any way to check the registration of the COM server?


#24

@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


#25

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


#26

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!


#27

@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.


#28

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


#29

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


#30

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:




#31

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


#32

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).


#33

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.