MetaTrader 5 Python User Group - the summary - page 16

 
amirhossein rahbari:

Hello dear friends ,

I am a beginner user in meta trader , I am so interested in using python API for my trading tasks,

but when I was trying to load all the symbols by 

symbols=mt5.symbols_get()

it return None as symbols but when I wrote in other script

rates = mt5.copy_rates_from("غسالم", mt5.TIMEFRAME_D1, utc_from)

it gives corresponding data to غسالم  and works well . 

because I want to trade on all symbols and filter them  it is mandatory to access their list but unfortunately the symbols_get is not working . please help me to find solution 

my broker  : Mofid

and symbols are written in Persian.

Your terminal needs to be upgraded to the latest beta version. Sign in to a MetaQuotes Demo account then go to Help>Check for updates. One thing to note is that the MetaTrader5 package will never raise an exception so you have to ask if there were any errors after operations by calling last_error(). If you want to use standard python conventions you can use the pymt5adapter instead so you can encapsulate your code in try: except...

import pymt5adapter as mta


def main():
    try:
        symbols = mta.symbols_get()
    except mta.MT5Error as e:
        print(e.error_code, e.description)
    else:
        print(f'First symbol is {symbols[0].name}')
    finally:
        print("Main finished")


if __name__ == '__main__':
    with mta.connected(raise_on_errors=True):
        main()
 

Thank you for adding the sys.argv feature! That's awesome and will definitely help with scripts that are dropped on the chart. Thanks for all the hard work making that happen. I am having an issue with the implentation, however. The values returned for the timeframes are unexpected. Instead of returning the timeframe const enum value, I am getting the equivalent of the number of minutes in the period. This is unexpected given the API has different values for the timeframes. Example:

import sys

import pymt5adapter as mt5


def time_frame_to_str(tf):
    tfs = {
        mt5.TIMEFRAME_M1 : 'M1',
        mt5.TIMEFRAME_M2 : 'M2',
        mt5.TIMEFRAME_M3 : 'M3',
        mt5.TIMEFRAME_M4 : 'M4',
        mt5.TIMEFRAME_M5 : 'M5',
        mt5.TIMEFRAME_M6 : 'M6',
        mt5.TIMEFRAME_M10: 'M10',
        mt5.TIMEFRAME_M12: 'M12',
        mt5.TIMEFRAME_M15: 'M15',
        mt5.TIMEFRAME_M20: 'M20',
        mt5.TIMEFRAME_M30: 'M30',
        mt5.TIMEFRAME_H1 : 'H1',
        mt5.TIMEFRAME_H2 : 'H2',
        mt5.TIMEFRAME_H4 : 'H4',
        mt5.TIMEFRAME_H3 : 'H3',
        mt5.TIMEFRAME_H6 : 'H6',
        mt5.TIMEFRAME_H8 : 'H8',
        mt5.TIMEFRAME_H12: 'H12',
        mt5.TIMEFRAME_D1 : 'D1',
        mt5.TIMEFRAME_W1 : 'W1',
        mt5.TIMEFRAME_MN1: 'MN1',
    }
    return tfs.get(tf, f'{tf} not a valid enum constant value for timeframe')


def main():
    if not len(sys.argv) == 3:
        print('Missing symbol and timeframe args')
        return
    symbol, timeframe = sys.argv[1], int(sys.argv[2])
    tf_str = time_frame_to_str(timeframe)
    print(f'Script was dropped on {symbol}, {tf_str}')
    try:
        rates = mt5.copy_rates_from_pos(symbol, timeframe, 0, 1)
        print(rates)
    except mt5.MT5Error as e:
        print(f'CopyRates failed: {e.error_code}, {e.description}')


if __name__ == '__main__':
    with mt5.connected(raise_on_errors=True):
        main()

The output when running this example script on an H1 chart:

Script was dropped on EPM20, 60 not a valid enum constant value for timeframe
CopyRates failed: -2, Terminal: Invalid params('EPM20', 60, 0, 1){}
 
nicholi shen:

Thank you for adding the sys.argv feature! That's awesome and will definitely help with scripts that are dropped on the chart. Thanks for all the hard work making that happen. I am having an issue with the implentation, however. The values returned for the timeframes are unexpected. Instead of returning the timeframe const enum value, I am getting the equivalent of the number of minutes in the period. This is unexpected given the API has different values for the timeframes. Example:

The output when running this example script on an H1 chart:

I updated the pymt5adapter package and added a function named parse_args which will automatically convert the minutes being passed by the terminal to the appropriate timeframe. 

import pymt5adapter as mta


def main():
    symbol, tf = mta.parse_args()
    print(symbol, tf.name)
    rates = mta.copy_rates_from_pos(symbol, tf, 0, 1)
    print(rates)


if __name__ == '__main__':
    with mta.connected(raise_on_errors=True):
        main()

To update:

pip install -U pymt5adapter
 
nicholi shen:

Thank you for adding the sys.argv feature! That's awesome and will definitely help with scripts that are dropped on the chart. Thanks for all the hard work making that happen. I am having an issue with the implentation, however. The values returned for the timeframes are unexpected. Instead of returning the timeframe const enum value, I am getting the equivalent of the number of minutes in the period. This is unexpected given the API has different values for the timeframes. Example:

The output when running this example script on an H1 chart:

This is our mistake, will be fixed in next beta.
 

Hi everyone.
i just wanted to ask a question regarding python integration with metatrader...
i am trying to initialize metatrader 5 via python (with predefined login details), everything works nice for the same broker account, but as soon as i try and initialize the metatrader with the login details from some other broker , then it does not login the account 

for eg.
if i have  BrokerABC  metatrader platform installed
and When i run the script with this code with the server address and login and password of "Broker ABC" account, everything works great:

if not mt5.initialize(path=C:\Program Files\Broker ABC\terminal.exe, login=123456, server= BrokerABC-Demo,password=123123, ):


But when i run the script with this code containing the account login details of some other broker(XYZBroker), on the Broker ABC platform
it opens up the metatrader platform , but it does not login the account 

if not mt5.initialize(path=C:\Program Files\Broker ABC\terminal.exe, login=654321, server= XYZBroker-Demo,password=654321, ):


any suggestions , why it wont login the account from any other broker on a different broker's metatrader platform?

 
mhdumer016:

Hi everyone.
i just wanted to ask a question regarding python integration with metatrader...
i am trying to initialize metatrader 5 via python (with predefined login details), everything works nice for the same broker account, but as soon as i try and initialize the metatrader with the login details from some other broker , then it does not login the account 

for eg.
if i have  BrokerABC  metatrader platform installed
and When i run the script with this code with the server address and login and password of "Broker ABC" account, everything works great:


But when i run the script with this code containing the account login details of some other broker(XYZBroker), on the Broker ABC platform
it opens up the metatrader platform , but it does not login the account 


any suggestions , why it wont login the account from any other broker on a different broker's metatrader platform?

You have to check the value of MetaTrader5.last_error. 

 
nicholi shen:

You have to check the value of MetaTrader5.last_error. 

hi nicholi shen thanks for replying.
i did check the errors , but there is nothing  i can find about it, here is the error that python ide shows :

 initialize() failed, error code = (-10003, "IPC initialize failed, Pipe server didn't answer in 60 sec")

running the python code does open up the metatrader platform , but it does not login with the provided details, but when i manually go to the metatrader and  click on accounts-> login-> and enter the same login details from the python script then it successfully login the account

 
mhdumer016:

hi nicholi shen thanks for replying.
i did check the errors , but there is nothing  i can find about it, here is the error that python ide shows :

 initialize() failed, error code = (-10003, "IPC initialize failed, Pipe server didn't answer in 60 sec")

running the python code does open up the metatrader platform , but it does not login with the provided details, but when i manually go to the metatrader and  click on accounts-> login-> and enter the same login details from the python script then it successfully login the account

init_kwargs = dict(
    path=r'C:\Program Files\Broker ABC\terminal64.exe',
    login=654321,
    server='XYZBroker-Demo',
    password='654321',
)
assert type(init_kwargs['password']) is str
assert 'terminal64.exe' in init_kwargs['path']

if not mt5.initialize(**init_kwargs):
    pass
 
nicholi shen:
Do you mean to replace the current initialization code with the code you provided ?

i did something like this : 
import MetaTrader5 as mt5
import kwargs
# display data on the MetaTrader 5 package

print("MetaTrader5 package author: ",mt5.__author__)
print("MetaTrader5 package version: ",mt5.__version__)
 
# establish MetaTrader 5 connection to a specified trading account
init_kwargs = dict(
    path=r'C:\Program Files\Broker ABC\terminal64.exe',
    login=35385303,
    server='XYZBroker-Demo',
    password='cv5sjsze',
)
assert type(init_kwargs['password']) is str
assert 'terminal64.exe' in init_kwargs['path']

if not mt5.initialize(**init_kwargs):
    pass 

 
# display data on connection status, server name and trading account
print(mt5.terminal_info())
# display data on MetaTrader 5 version
print(mt5.version())
 
# shut down connection to the MetaTrader 5 terminal
mt5.shutdown()

but still nothing...

it launches the mertatrader and try to login with the details , but never logs in the account...

this is what happens: https://paste.pics/299c254cdd8b42cc0a2203f9ea118a2c

 
mhdumer016:
Do you mean to replace the current initialization code with the code you provided ?

i did something like this : 

but still nothing...

it does not login the account and it returns this in the python IDE : 

If everything works when you call initialize without args but breaks when you try and call it with args, then you are inputting something wrong.