AmiPy plug-in: AmiBroker-Python integration
UPDATE April 4, 2023: Version 1.0.3 released!
With permission from @Tomasz I am announcing here the next version of my AmiBroker-Python integration plugin. This plugin allows you to call Python functions from AFL side. This opens out possibility to use wide selection of Python libraries such as numpy, scilearn, tensorflow and such in AmiBroker.
It takes a great amount of time developing a program for public use so if You are using this plug-in on every-day basis please consider making a donation. The link below allows you to send small amount of money ($5/$10/$25 of your choice) to me. Thank you very much for your generous support.
Click here to donate by PayPal
PS:
I don't have any more ideas what to add to this plugin so it may be that this will be the final version of it.
If You have any idea what could be enhansed feel free to post it on this thread.
If you want help with anything use the lastest version of AmiPy
Download link:
AmiPy 1.0.3: https://ev1.pl/amipy/AmiPy_1_0_3.zip (64 bit only)
VirusTotal file check 100% clean
AmiPy Changelog: https://ev1.pl/amipy/ChangeLog.html
Minimum requirements
AmiBroker version 6.30 or higher x64 (64-bit only)
Installation
- Install Python 3.x x64 version where x>=7 (3.7 or greater) (newer are encouraged) (Download Python | Python.org)
- install numpy module:
pip install numpy
(in cmd) - from AmiPy.zip extract AmiPy.dll into "Plugins" subfolder of AmiBroker 64-bit
AFL functions
PyLoadFromFile( context, fileName )
Argument | Type | Description |
---|---|---|
context | string | Name of the context |
filename | string | Path to file that should be loaded. Can be raw code (.py file) or compiled code (.pyc file). |
Return value: Doesnot return any value
PyEvalFunction( context, PyfunctionName, ... [fn args] )
Argument | Type | Description |
---|---|---|
context | string | Name of the context |
PyfunctionName | string | function name (name of callable object) definied in global scope. |
... [fn args] | float / string / array | arguments used in call of python function1 |
Return value: return value of called function2
1AFL to Python variable conversions:
AFL type | Python type |
---|---|
number | float |
string | string |
array | 1-D numpy array (np.ndarray) |
2Python to AFL variable conversions:
Python type | AFL type |
---|---|
number convertable to float (e.g. int,float,bool) | number |
string | string |
1-D numpy array (np.ndarray) of size of AFL array and convertable to float array | array |
2-D numpy array (np.ndarray) convertable to float array | matrix |
You can also return numpy scalar containing any mentioned above type.
Python module - AmiPy
This Python build-in module allows to call specific AmiBroker functions from your python program.
WARNING: This is a build-in module which means that it is only accessible, when running python from the plugin. That means you will not be able to use and/or import this module when started from command line/VS Code etc. It also means that you cannot install it (e.g. by pip) to external python implementation.
AmiPy methods
Print( str )
prints string in AFL Debugger
AFL equivalent of printf( "%s", str );
Error( str )
prompts AFL error with content string
AFL equivalent of Error( str );
Initialization file
After first time AmiPy is used in AmiBroker's working directory should be a initialization file: AmiPy.ini
[Logging]
; where log file is stored, default: AmiPy.log
Path=<string>
; If given event is logged
; default: error, warning and main_ev are true and all else false
; when you want support you need to set all below mentioned options to true but for `debug` and for crush reports debug must also be set to true
error=<bool>
warning=<bool>
fn_start=<bool>
fn_end=<bool>
main_ev=<bool>
debug=<bool>
Common problems
-
Problem:
- PyLoadFromFile prints error: "Cannot start array API (no numpy?)".
- Giving PyEvalFunction array as argument or returning from called function array-like object prompts error "Exception caught in external DLL [...]".
Solution:
- Install numpy on your device on the version of python denoted in AmiPy.log.
-
Problem:
- PyLoadFromFile and PyEvalFunction not showing in AFL
- In AmiBroker's Tools->Plug-ins window AmiPy doesn't show up.
Solution:
- Ensure that you have python 3.x (where x>=7) installed
- Ensure that you have python (python3.dll) in PATH
-
Problem:
- AmiPy does not load correct version of python
Solution:
- Ensure that AmiPy sees python3.dll of the version that you want to load before any others. The simplest way to do this is to copy it from python's to AmiBroker's directory.
Tips
Sometimes Python modules take significant amount of time to load and/or initialize. In such situation it may be worthwhile to load/initialize only once.
To load part of file only once (e.g. to create variables) you can use in Python code:
import numpy as np
import AmiPy
if '__' + __file__ + '_initialized' not in globals():
globals()['__' + __file__ + '_initialized'] = True
# ... put your time-consuming initialization code ...
# ... so it is run only once here
#
# ... your code ...
Examples
- Evaluating simple functions
AFL:
PyLoadFromFile("example1", "python.py");
Close2 = PyEvalFunction("example1", "funct", Close);
Diff = PyEvalFunction("example1", "diff", High, Low );
Plot( Close, "Close" );
Plot( Close2, "Close + 2" );
python python.py
:
import numpy as np
import AmiPy
def funct( array ):
AmiPy.Print( "This message was sent from Python!\n" )
return array + 2
def diff( a, b ):
return a - b