Welcome Features News Download Registration Support FAQ Wish list Links
Advanced stock charting and analysis program

AFL Library

This is read-only version of AFL library entry. Ability to add user formulas and comment is only available from members-only area.

Details:

Formula name: AFL to Python COM Link
Author/Uploader: Bruce Peterson - bapeters [at] terastat.com
Date/Time added: 2008-02-01 19:29:16
Origin:
Keywords: COM Python
Level: advanced
Flags: showemail,function

DISCLAIMER: Most formulas present in AFL on-line library are submitted by the users and are provided here on an "as is" and "as available" basis. AmiBroker.com makes no representations or warranties of any kind to the contents or the operation of material presented here. We do not maintain nor provide technical support for 3rd party formulas.
Description:

The AFL documentation shows how AFL may be linked to external programs using the
COM (common object model). An example is provided for implementing the IIF (infinite impulse filter)
in Visual Basic and then calling the method from AFL. Python is a modern object oriented
language with extensive support and many libraries available. This code shows how a the COM link can
be used to use Python methods from AFL.

Formula:

The AFL documentation shows how AFL may be linked to external programs using
the
COM (common object model). An example is provided for implimenting the IIF
(infinite impulse filter)
in Visual Basic and then calling the method from AFL.

Python is a modern open source object oriented language that has extensive
support on the web. 
The follow sets of code show how Python methods may be used to perform
calculations and
pass the results back to AFL. The same IIF method shown in the documentation is
shown here
as implemented in python. 

Once you have installed Python, cut and past the code into a seperate file with
an extension ".py"
Run the code and it will install itself into the registry. You can then access
the mehods and attributes
from AFL.

Since Python does not come standard with Windows, you will need to download and
install the 
Python interpreter to use this code. Web addresses for obtaining Python are
shown below.

######################################Begin Python
Code#########################################
#
# Com file framework for Python routines to be called
#  from AmiBroker
#
# This code shows how a python com server can be build to
#  allow python code to be called from an afl program.
#  more information about python is available at: www.python.org
#  other sites distributing python with some extra libraries
#  are:
#  http://code.enthought.com/
#  http://www.activestate.com/Products/languages.plex
#
# one quick note about python versus AFL -- python uses indentation
#  to delinate blocks of code where afl uses "{}" to designate blocks
#
#  data can be passed to and from AFL via method calls or attributes
#

import pythoncom
from win32com.server.exception import COMException
import win32com.server.util
import win32com.client.dynamic
import win32gui
import random


class PythonUtilities(object):
    _public_methods_=['IIR2']

    #
    # Use pythoncom.CreateGuid() 2/1/08
    #               
    _reg_clsid_="{25F32489-9E84-4D9F-8AEB-44DC9B528405}"
    _reg_desc_ =   "Utilities for Extending Amibroker AFL"
    _reg_progid_ = "PyAFL"
    _readonly_attrs_ = ['revision']

   # class attributes can be defined and used to transfer values from AFL

    _public_attrs_=['NDepVar','ArrayLen','ProbArray',
                    'X_Open','X_High','X_Low','X_Close',
                    'X_Volume','X_OI','Log']

    Log=""  # Log may be a  string use to pass text to AFL
    
    def IIR2(self,InList,coef0,coef1,coef2):
        # example infinite impulse response filter from Amibroker docs
        #
        self.revision="version 1"
        OutList=[]
##
##        print type(InList),type(coef0)
##        print coef0,coef1,coef2
##        
        InList=list(InList)
        
        if len(InList)==0:
            return OutList
        OutList=InList[0:2]
        for index in range(2,len(InList)):
           
OutList.append(coef0*InList[index]+coef1*InList[index-1]+coef2*InList[index-2])
        if OutList is None:
            return
        else:
            return OutList

        
    
        
if __name__=='__main__':
    print "Registering COM Server "
    import win32com.server.register
    win32com.server.register.UseCommandLine(PythonUtilities)

# uncomment out if you want to unregister com server 

##    from win32com.server.register import UnregisterServer
##    UnregisterServer(PythonUtilities._reg_clsid_)
##    print "Com server unregistered."    

#############################################End Python Code
####################################


//----------------------------------------------------------------------------------------Start
AFL Code
--------------------------------------------------------------------------

MyObj=CreateObject("PyAFL");  // link to python com server

Coef0=Param("Coef0",0.2,-5,5);
Coef1=Param("Ceof1",1.2,-5,5);
Coef2=Param("Coef2",-0.4,-5);

// COM object interface cannot deal with null value, set to zero to be sure

Close=Nz(Close);

 // use IIR2 method in com server to calculate smoothed value

SmValue = MyObj.IIR2(Close,Coef0,Coef1,Coef2);


Plot(C,"Close",Color=colorBlue,style=styleLine);
Plot(SmValue,"IIR2 smooth",Color=colorDarkRed,style=styleLine);


_SECTION_BEGIN("DEMA");
P = ParamField("Price field",-1);
Periods = Param("Periods", 15, 2, 200, 1, 10 );
Plot( DEMA( P, Periods ), _DEFAULT_NAME(), ParamColor( "Color", colorCycle ),
ParamStyle("Style") ); 
_SECTION_END();

//------------------------------------------------------------------------------------------End
AFL
Code------------------------------------------------------------------------------














    

Comments:

zedwghoj xlkzys
xawlijtez [at] mail.com
2008-04-23 23:12:23
vjhcdiz jgnhpmbc dien gsnbqhl cefyvl peaqtk qzhlomw


About | Privacy | Terms of Use | Contact information
Copyright © 2001 AMIBROKER.COM