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

 
Vladimir Perervenko:

Is the creation of 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?

I don't think it would be difficult to create another entity, it would just be another level of abstraction for the lib.

This is no longer considered using the lib from the terminal, but as a full-fledged gateway to terminals on the python side.

Better to put more utility into the lib itself :) now.

 
Дмитрий Прокопьев:

I don't think it would be difficult to create another entity, it would just be another level of abstraction for the lib.

This is no longer considered using the lib from the terminal, but as a full-fledged gateway to terminals on the python side.

Better to put more utility into the lib itself :) now.

My terminal startup fails regularly, haven't figured out the reason. I must have a bug somewhere with the python installation... I'll use lib as gateway to terminal (via services). I'll put it to the test on Monday.

 
Дмитрий Прокопьев:

mt5.initialize(blabla, portable=True)- didn't help much, it still asks for the last terminal started in the system.

Try without named parameter path, pass path as first unnamed parameter:

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

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

try without named parameter path, pass path as first unnamed parameter:

It worked correctly. Thank you.

Bug?

 
Дмитрий Прокопьев:

Everything worked correctly. Thank you. (chuckles)

A bug?

Not a bug.

The first parameter is not named and is fixed. But just in case it will be named in the next version.

 
Renat Fatkhullin:

Not a bug.

The first parameter is unnamed and fixed. But just in case it will be named in the next version.

Aha, so I didn't understand the documentation specifically.

Thank you.

 
Really need something to get the "start of a new bar" out of the terminal. Indicators are not so important. Python turns out to have a library with indicator calculation too.
 

Question. There is a positions_get function. The documentation describes three modes of its operation.

positions_get

Gets open positions with filtering by symbol or ticket. It can be called in 3 different ways.


Called without parameters. Returns the open positions for all symbols.

positions_get()

Call with specifying a symbol, for which it wants to get open positions.

positions_get(

symbol="SYMBOL" // symbol name

)

Call specifying the ticket of the position.

positions_get(

ticket=TICKET // ticket

)

Parameters

symbol="SYMBOL" [in] The name of the symbol. Optional named parameter. If a symbol is specified, the ticket parameter is ignored.

ticket=TICKET [in] The position ticket (POSITION_TICKET). Optional named parameter.


Variant positions_get() - no questions, works fine.

But the other two do not return anything.

	 pos_info = mt5.positions_get(symbol = "GBPUSD")	
	 print(mt5.last_error())

The second callback variant:

        result = mt5.order_send(request)        
        if result.retcode == 0 or result.retcode == 10009:      
                pos_info = mt5.positions_get(ticket = result.order)
                print("mt5.last_error(): %s" % (mt5.last_error()))

last_error also returns a strange construct:

2020-03-09 22:25:41 [DEBUG][module: processing.py] add_position: Possition open - OK! MqlTradeResult: {
    "retcode": 10009,
    "deal": 134756209,
    "order": 161108536,
    "volume": 0.01,
    "price": 1.31089,
    "bid": 1.31066,
    "ask": 1.31089,
    "comment": "Request executed",
    "request_id": 98,
    "retcode_external": 0
}
mt5.last_error(): [1, '']

Where to look? What is wrong?

 
Дмитрий Прокопьев:

A call with a position ticket.

positions_get(

ticket=TICKET //ticket

)

Parameters

symbol="SYMBOL" [in] The name of the symbol. Optional named parameter. If a symbol is specified, the ticket parameter is ignored.

ticket=TICKET [in] The position ticket (POSITION_TICKET). Optional named parameter.


Variant positions_get() - no questions, works fine.

But the other two do not return anything.

The second calling variant:

last_error also returns a strange construct:

2020-03-09 22:25:41 [DEBUG][module: processing.py] add_position: Possition open - OK! MqlTradeResult: {
    "retcode": 10009,
    "deal": 134756209,
    "order": 161108536,
    "volume": 0.01,
    "price": 1.31089,
    "bid": 1.31066,
    "ask": 1.31089,
    "comment": "Request executed",
    "request_id": 98,
    "retcode_external": 0
}
mt5.last_error(): [1, '']

What to look for? What is wrong?

Check whether your account type is netting or hedging. For netting, the ticket of an already existing position will not change, and there is no point in looking for a position after the topping up using the order number.

 

last_error тоже возвращает странную конструкцию:

error code 1, this is success mt5.RES_S_OK, the next build will write [1, 'Success']. Codes with negative values are errors mt5.RES_E_xxx.
Reason: