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

 
MetaQuotes:

A new build of the python library 5.0.25 has been released

Please update via:

Gentlemen, can we ask to add objects like:

TradePosition(ticket=55970661, time=1583523524, time_msc=1583523524484, time_update=1583523524, time_update_msc=1583523524484, 
type=1, magic=0, identifier=55970661, reason=0, volume=0.04, price_open=1.13022, sl=0.0, tp=0.0, price_current=1.13179, swap=0.0, 
profit=-6.28, symbol='EURUSD', comment='', external_id='55970661')

add pls or __dict__, or as_dict()

In python it is much more convenient to operate with standard structures, the same dict/list.

('__add__', <method-wrapper '__add__' of TradePosition object at 0x085A1808>)
('__class__', <class 'TradePosition'>)
('__contains__', <method-wrapper '__contains__' of TradePosition object at 0x085A1808>)
....
....
....
('symbol', 'EURUSD')
('ticket', 55970661)
('time', 1583523524)
('time_msc', 1583523524484)
('time_update', 1583523524)
('time_update_msc', 1583523524484)
('tp', 0.0)
('type', 1)
('volume', 0.04)

It would be nice to pull dict from this class (and other similar ones) and work with it.

 

Question about initialize(), there are

initialize(
   path                      // path to the MetaTrader 5 terminal EXE file
   timeout=TIMEOUT,          // timeout
   login=LOGIN,              // account number
   password="PASSWORD",      // password
   server="SERVER"           // server name as it is specified in the terminal
   )

respectively in path paths to two terminals which run /portable

'c:\Works\MT5 - terminals\MetaTrader 5\terminal64.exe'

и 

'c:\Works\MT5 - terminals\Alpari MT5\terminal64.exe'

but allinitialize calls go to the last running instance of the terminal.

respectively MT build 2360 and MetaTrader5-5.0.26-cp38-cp38-win32.whl

What's wrong? path should be used with some other logic, or should something else be passed to /portable?

 

Show the actual code, please.

We don't accept parameters in the paths yet.

 
Renat Fatkhullin:

Show the actual code, please.

We don't accept parameters in the paths yet.

Here is the code:

import MetaTrader5 as mt5

def check_order ():
    if mt5.initialize(path='c:\Works\MT5 - terminals\MetaTrader 5\terminal64.exe', 
                            login=25153683, server='MetaQuotes-Demo', password=''):
                print("mt5.version(): ", mt5.version())
                print("mt5.terminal_info()", mt5.terminal_info())
                print("mt5.account_info()", mt5.account_info())
                symbol="GBPUSD"
                if mt5.symbol_select(symbol,True):
                    info = mt5.symbol_info(symbol)
                    point=mt5.symbol_info(symbol).point
                    request = {
                        "action": mt5.TRADE_ACTION_DEAL,
                        "symbol": symbol,
                        "volume": 0.01,
                        "type": mt5.ORDER_TYPE_BUY,
                        "price": mt5.symbol_info_tick(symbol).ask,
                        "sl": mt5.symbol_info_tick(symbol).ask-100*point,
                        "tp": mt5.symbol_info_tick(symbol).ask+100*point,
                        "deviation": 10,
                        "magic": 234000,
                        "comment": "python script",
                    }
 
                    result = mt5.order_check(request)
                    print(result)
		 mt5.shutdown()


    if mt5.initialize(path='c:\Works\MT5 - terminals\Alpari MT5\terminal64.exe', 
                            login=50518455, server='Alpari-MT5-Demo', password=''):
                print("mt5.version(): ", mt5.version())
                print("mt5.terminal_info()", mt5.terminal_info())
                print("mt5.account_info()", mt5.account_info())
                symbol="GBPUSD"
                if mt5.symbol_select(symbol,True):
                    info = mt5.symbol_info(symbol)
                    point=mt5.symbol_info(symbol).point
                    request = {
                        "action": mt5.TRADE_ACTION_DEAL,
                        "symbol": symbol,
                        "volume": 0.01,
                        "type": mt5.ORDER_TYPE_BUY,
                        "price": mt5.symbol_info_tick(symbol).ask,
                        "sl": mt5.symbol_info_tick(symbol).ask-100*point,
                        "tp": mt5.symbol_info_tick(symbol).ask+100*point,
                        "deviation": 10,
                        "magic": 234000,
                        "comment": "python script",
                    }
 
                    result = mt5.order_check(request)
                    print(result)
		 mt5.shutdown()


if __name__ == "__main__":
    check_order()

Last running terminal:

"c:\Works\MT5 - terminals\ForexClub MT5\terminal64.exe" 

Judging by the screenshot, the call went to the last running one.

I'd like to call the specific ones that were accessed.

 
Renat Fatkhullin:

We don't accept the parameters in the tracks yet.

Mmm ... I don't quite understand this point, the value but not the parameter, this is where I take the connection parameters out of the config.

            if mt5.initialize(path=acc['path'], login=acc['login'], server=acc['server'], password=acc['pass']):
                print("mt5.version(): ", mt5.version())
                print("mt5.terminal_info()", mt5.terminal_info())
                print("mt5.account_info()", mt5.account_info())
 
Дмитрий Прокопьев:

Question about initialize(), there are

respectively in path paths to two terminals which run /portable

but allinitialize calls go to the last running instance of the terminal.

respectively MT build 2360 and MetaTrader5-5.0.26-cp38-cp38-win32.whl

What's wrong? path should be used with some other logic, or should something else be passed to /portable?

Do you want 2 terminals in one script to work simultaneously? It's not possible, next initialize call will break connection with the first terminal, for that you need to enter connection entity to distinguish different terminals. It only works with one current connection.

initialize has one more parameter (not yet documented)mt5.initialize(blabla, portable=True) tostart terminal hard in portable mode.

 
Almaz:

Do you want to operate 2 terminals simultaneously in one script? This is not possible, the next initialize call will break the connection to the first terminal, so you need to enter a connection entity to distinguish between the different terminals. It only works with one current connection.

initialize has one more parameter (not yet documented)mt5.initialize(blabla, portable=True) to start terminal in portable mode.

Yes, you should work with multiple terminals running inportable mode. You can't initialize connection with two different terminals at the same time. No questions here.

I tried it to address terminals one by one:

if mt5.initialize(login=11111, path='/works/MT5-1'...):
        ...   
        mt5.shutdown()
else:
   print('error')

if mt5.initialize(login=2222, path='/works/MT5-2'...):
        ...   
        mt5.shutdown()
else:
   print('error')

in this scheme, it doesn't work. In all cases it crashes to the last terminal manually started in the system. Even if it was started from

path='/works/MT5-3'
mt5.initialize(blabla, portable=True)- didn't help much, it still prompts the last terminal started in the system.
 

There's a question here.

Forum on trading, automated trading systems and strategy testing

MetaTrader 5 Python User Group - Summary

nikoli shen, 2020.03.07 15:46

There is a problem with downloading the MetaTrader 5 package using the pip version that comes with python. A workaround is to update the pip. Command to update pip in Windows:
python -m pip install -U pip

or in venv, using

(inside virtualenv):easy_install -U pip

 
Almaz:

Do you want to operate 2 terminals simultaneously in one script? This is not possible, the next initialize call will break the connection to the first terminal, you need to enter a connection entity to distinguish between the different terminals. It only works with one current connection.

initialize has one more parameter (not yet documented)mt5.initialize(blabla, portable=True) to start terminal hard in portable mode.

Note that mt5.terminal_info() shows

mt5.version():  [500, 2360, '06 Mar 2020']
mt5.terminal_info() TerminalInfo(community_account=False, community_connection=False, connected=True, dlls_allowed=False, trade_allowed=True, 
tradeapi_disabled=False, email_enabled=False, ftp_enabled=False, notifications_enabled=False, mqid=False, build=2360, maxbars=100000000, codepage=0, ping_last=42381,
community_balance=0.0, retransmission=7.976601967561818, company='MetaQuotes Software Corp.', name='MetaTrader 5', language='English', 
path='C:\\Works\\MT5 - terminals\\MetaTrader 5', data_path='C:\\Works\\MT5 - terminals\\MetaTrader 5', commondata_path='C:\\Users\\rip\\AppData\\Roaming\\MetaQuotes\\Terminal\\Common')
mt5.version():  [500, 2360, '06 Mar 2020']
mt5.terminal_info() TerminalInfo(community_account=False, community_connection=False, connected=True, dlls_allowed=False, trade_allowed=True, 
tradeapi_disabled=False, email_enabled=False, ftp_enabled=False, notifications_enabled=False, mqid=False, build=2360, maxbars=100000000, codepage=0, ping_last=40849,
community_balance=0.0, retransmission=7.976601967561818, company='MetaQuotes Software Corp.', name='MetaTrader 5', language='English', 
path='C:\\Works\\MT5 - terminals\\MetaTrader 5', data_path='C:\\Works\\MT5 - terminals\\MetaTrader 5', commondata_path='C:\\Users\\rip\\AppData\\Roaming\\MetaQuotes\\Terminal\\Common')

valuepath='C:\\\Works\\\\\MT5 - terminals\\MetaTrader 5', data_path='C:\Works\\\\MT5 - terminals\\\MetaTrader 5',commondata_path='C:\Users\\\rip\AppData\\\Roaming\\MetaQuotes\\Terminal\\\Common'

are identical. Is it supposed to be like this?

The code is below:

import MetaTrader5 as mt5

def check_order ():
    if mt5.initialize(path='c:\\Works\\MT5 - terminals\\MetaTrader 5\\terminal64.exe', 
                            login=25153683, server='MetaQuotes-Demo', password='',  portable=True):
        print("mt5.version(): ", mt5.version())
        print("mt5.terminal_info()", mt5.terminal_info())
        mt5.shutdown()

    if mt5.initialize(path='c:\\Works\\MT5 - terminals\\Alpari MT5\\terminal64.exe', 
                            login=50518455, server='Alpari-MT5-Demo', password='',  portable=True):
        print("mt5.version(): ", mt5.version())
        print("mt5.terminal_info()", mt5.terminal_info())
        mt5.shutdown()

if __name__ == "__main__":
    check_order()
 
Almaz:

Do you want to operate 2 terminals simultaneously in one script? This is not possible, the next initialize call will break the connection to the first terminal, you need to enter a connection entity to distinguish between different terminals. It only works with one current connection.


Is creating an entity (connection) possible in principle? I don't think it's that necessary, but for future reference.

When installing another script (Py) on the chart, the first one is reset. Is this the way it should be?