MetaTrader 5 Python User Group - how to use Python in Metatrader - page 73

 
And another interesting question is if I run a python program with all its environment in a docker container and the terminal is outside, will that work?
 
Dmitri Custurov:
If the python program internally does logging, multithreading, socket connection to the terminal, will it all work correctly if you run it on a chart?

The terminal runs its own interpreter and redirects the standard output to the terminal's expert tab and internal logging. The interpreter process terminates when you close the diagram, so you will not be able to register any "atexit" callback functions. Also, you will not be able to access the log text immediately as the terminal does not record the log file as log events arrive. You can try my shell package which will log immediately.

import atexit
import logging
import os
from pathlib import Path

import pymt5adapter as mta


def main(conn):
    try:
        symbol, timeframe = mta.parse_args()
        conn.logger.info(f'Script dropped on {symbol} {mta.TIMEFRAME(timeframe).name}')
        return True
    except TypeError:
        conn.logger.warning(f'Missing... Drop on chart or start script with commandline args...')
        return False


if __name__ == '__main__':
    desktop_log_path = Path.home() / 'Desktop/python_mt5.log'
    atexit.register(lambda: os.startfile(desktop_log_path))
    logger = mta.get_logger(path_to_logfile=desktop_log_path, loglevel=logging.DEBUG)
    with mta.connected(raise_on_errors=True, logger=logger) as conn:
        main(conn)
 
Dmitri Custurov : And another interesting question is if I run a python program with all its environment in a docker container, and the terminal is outside, so will it work?

I assume you are using linux containers for the docker, so it will not work with the MetaTrader5 package. You can use python virtualenv. It won't work if you put the script on the chart, but it will work if you activate the environment and run it from the command line.

 
What about extended authorisation when not only username/password but also ssl certificate password is required?
 
Dmitri Custurov:
What about extended authorisation when not only username/password but also ssl certificate password is required?

The password of the certificate is set by the user at the time of generation. The public key is then extracted from the certificate and given to the broker for registration. Read the instructions on the broker's website.

 
Alexey Viktorov:

The password of the certificate is set by the user at the time of generation. The public key is then extracted from the certificate and given to the broker for registration. Read the instructions on the broker's website.

I'm on the subject of this one.

initialize(
   path,                     // путь к EXE-файлу терминала MetaTrader 5
   login=LOGIN,              // номер счета
   password="PASSWORD",      // пароль
   server="SERVER",          // имя сервера, как оно задано в терминале
   timeout=TIMEOUT,          // таймаут
   portable=False            // режим portable
   )

There is no password field for the certificate. How will the connection from python behave if extended authorisation is used in the terminal?

 
Dmitri Custurov:

I'm talking about this one.

There is no password field for the certificate. How will the connection from python behave if advanced authentication is used in the terminal?

You should have written the full question right away. I don't know that and would have kept silent.

 
Dmitri Custurov:

I'm talking about this one.

There is no password field for the certificate. How will the connection from python behave if the terminal uses extended authentication?

This will use a certificate already activated and saved in the terminal.

But you can put a pfx file in /config/certificates yourself
 
Alexey Viktorov:

You should have written the full question straight away. I don't know that and I would have kept quiet.

I apologise for that. I thought it would be clear from the context of this thread, I should have been clearer.

 
Renat Fatkhullin:
A certificate already activated and saved in the terminal will be used.

But you can put the pfx file in /config/certificates yourself

Thank you.

Reason: