Python
-
Setting the Python interpreter for Python strategy programs
For strategies written in Python, during backtesting or live trading, if the host system has both Python2 and Python3 installed, you can set the Python version to launch at runtime in the first line of the strategy. For example:#!python3,#!python2, the system will automatically find the corresponding interpreter. You can also specify an absolute path, for example:#!/usr/bin/python3. -
Security of Python-based strategies
Strategies developed on the FMZ Quant Trading Platform are only visible to the holder of the FMZ Quant Trading Platform account. Additionally, complete localization of strategy code can be achieved on the FMZ Quant Trading Platform, such as packaging the strategy into a Python library and loading it in the strategy code, thus achieving strategy code localization.
Python code security:
Since Python is an open-source and easily decompilable language, if the strategy is not for personal use but for rental, and you are concerned about strategy leakage, you can run the strategy on your own deployed host and rent it out in the form of sub-account or fully managed management.Python strategy code encryption:
By default, Python strategy code is not encrypted when used by the author themselves, but encrypted when rented to others. By writing the following code at the beginning of the Python strategy, you can specify whether to encrypt the strategy code when running for personal use or rental. Python versions that support strategy code encryption are: Python 2.7, Python 3.5, Python 3.6.- Encrypt strategy code both when the strategy author runs it themselves and when providing it to others via registration code:
Use code#!pythonto specify the Python interpreter version, then use comma,as separator, and input the encryption commandencrypt. If you don't specify the Python version, you can directly add#!encrypt.orpython#!python,encryptpython#!encrypt - Do not encrypt strategy code when the strategy author runs it themselves or provides it to others via registration code:
orpython#!python,not encryptedpython#!not encrypted
To check if Python strategy code encryption is effective, use code
os.getenv('__FMZ_ENV__'), which returns the string"encrypt"to indicate it's effective. Only valid in live trading, backtesting will not encrypt Python strategy code.python#!encrypt def main(): ret = os.getenv('__FMZ_ENV__') # Printing variable ret as string encrypt or ret == "encrypt" being true means encryption is effective Log(ret, ret == "encrypt") - Encrypt strategy code both when the strategy author runs it themselves and when providing it to others via registration code:
-
Python custom module import functionality
The FMZ platform supports importing custom modules in Python strategies, enabling modular development and code reuse.For example, if we need to design a module:
mymath, savemymath.pyas a separate file.python# mymath.py - save as a separate file """ Simple math utility module """ def add(a, b): """Addition""" return a + bDeploy the module file by placing
mymath.pyin the specified location under the host program directory (the folder name in the storage directory is the live trading Id, using live trading Id123456as an example):Host program directory/logs/storage/123456/mymath.py
Finally, directly import the
mymathmodule in the Python strategy on the FMZ platform.pythonimport mymath def main(): Log("mymath.add(1, 2):", mymath.add(1, 2))The strategy bound to the live trading instance (strategy instance) with Id
123456can then call methods from themymathmodule.