Python Bridge

Python is an popular interactive script language, created by Guido van Rossum and first released in 1991. Unlike C, whitespace and line feeds matter for Python syntax, and enforce a clean code style with correct indentations. Python is interpreted and thus slow compared to compiled languages like C. Its strengths are a simple and more modern syntax, and access to all sorts of libraries for machine learning and data analysis.

The Python bridge (Zorro 2.04 and above) allows calling Python functions from a Zorro script and using their results for trade signals. A script can start a Python session, send price data or indicator values to it, and use Python machine learning packages for training and prediction. Many problems can be solved in lite-C as well as in Python, but Python often has already a dedicated command implemented or library available for that.

Installation and test

It's not necessary to install Python for running Python commands and scripts. Zorro already contains a Python interpreter (python36.dll). But a Python 3.6 installation is required for using external Python packages. Here's the procedure: 

Make sure that the package is installed to your Python 3.6.8 folder, so don't use the pip --user option. Afterwards the package can be accessed with the import statement.

The following functions are available for sending data to Python, performing computations, and receiving data back:

pyStart(string Filename, int Mode): int

Start a new Python session, and optionally load Python code from the file Filename. This function must be called before any Python computations can be done. Returns 0 when the Python session could not be started, otherwise nonzero. The Python session will stay alive until the end of the simulation or Zorro session.  



Name of a .py file in the Strategy folder containing all needed Python functions and variables (f.i. ""), or 0 for not loading a file.


1 for logging Python output and errors to the files PyOut.txt and PyError.txt in the Log folder; otherwise 0.

pyX(string Code): int

Execute Python Code. It can contain an expression, like "a = b + c", a function call, a function definition, an import, or any other valid Python statement. If the code cannot be excuted, 0 is returned, otherwise nonzero.


Code Python code to be executed. Separate lines with \n and mind whitespace indentations.

pySet(string Name, int n)

pySet(string Name, var d)

pySet(string Name, var *v, int elements)

Store an int, var, series or array in the Python variable Name. Since the target type depends on the variable type, make sure to use the correct type. When storing constants, typecast them with (int) or (var) if in doubt. 


name Name of the Python variable.
i int value to be assigned to a Python integer variable.
d var value to be assigned to a Python floating point variable.
v Pointer of the var array or series to be assigned to a Python list.
elements Length of the vector or series. Make sure to give the exact number of elements.

pyInt(string Name): int

pyVar(string Name): var

pyVec(string Name, var *v, int elements)

Return the Python variable with the given Name as an int, double, or array.


Name Name of a Python variable that must have been defined before.
v Pointer of the var array to be filled with the Python list.
elements Number of elements of the vector; must be identical in the Python code and in the lite-C script.



function main()
  if(!pyStart(0,1)) {
    printf("Error - Python won't start!");

  var Vec[5] = { 0,1,2,3,4 };
  pyX("for i in range(5): PyVec[i] *= 10\n");

  int i;
  printf("\nReturned: ");
  for(i=0; i<5; i++) printf("%.0f ",Vec[i]);

// test a function
  pyX("def PySum(V):\n Sum = 0.0\n for X in V:\n Sum += X\n return Sum\n\n");
  pyX("Result = PySum(PyVec)");
  printf("\nSum: %.0f",pyVar("Result"));

See also:

Code conversion, R Bridge, Python cheat sheet

► latest version online