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

 
MrBrooklin:

Thank you, Maxim!

I'm already trying it out. The cycle has started, but the file is not opening yet. I will leave the loop in operation and wait.

Regards, Vladimir.


P.S. Pardon! Didn't pay attention at once to what stands hour. I changed it to minute EUR_USD_QUOTE.csv file was opened but I got this message in Run tab of Pycharm:

�訡��: �� 㤠���� ���� ����� "excel.exe".

Then after another minute or so, the file closed and reopened, but in Exel a tab appeared on the left side of the spreadsheet asking me to save the previously opened text. I waited a bit longer to see what would happen next, but then the cycle stopped and a message like this appeared:

�ᯥ譮: ����� "EXCEL.EXE", � �����䨪��஬ 4208, � �����襭.
�ᯥ譮: ����� "EXCEL.EXE", � �����䨪��஬ 3168, �� �����襭.
Traceback (most recent call last):
File "C:/Users/Vladimir/PycharmProjects/My_Python_Project/9.py", line 37, in <module>
get_data(save_file='EUR_USD_QUOTE.csv')
File "C:/Users/Vladimir/PycharmProjects/My_Python_Project/9.py", line 23, in get_data
with open(save_file, 'w') as save:
PermissionError: [Errno 13] Permission denied: 'EUR_USD_QUOTE.csv'

This is the result so far, but I already made progress!

Sincerely, Vladimir.

Maybe Excel didn't have time to close and was trying to access the file already, then

while True:
    t = datetime.now().hour
    if(t2 != t):
        os.system("TASKKILL /F /IM excel.exe") 
	time.sleep(10)
        get_data(save_file='EUR_USD_QUOTE.csv')
        t2 = t
    time.sleep(10)

Add a 5-10 second delay here, and the program will wait for Excel to close

I have not decided what to do with the window on the left )

 
MrBrooklin:

I want to learn how to take quotes on the Internet using the language Python to watch them not in the terminal, but in some file, for example, csv. I thought it was the most basic task, but it turned out that it wasn't. Maybe I'm on the wrong track, but the one who is not looking for anything, he finds nothing.

Now I'll move on to the code you've proposed. After the code was launched, a message appeared in the Pycharm development environment :

Traceback (most recent call last):
File "C:/Users/Vladimir/PycharmProjects/My_Python_Project/10.py", line 25, in <module>
main()
File "C:/Users/Vladimir/PycharmProjects/My_Python_Project/10.py", line 13, in main
eurusd_bid = soup.find('td', 'pid-1-bid').text.replace(',', '.')
AttributeError: 'NoneType' object has no attribute 'text'

Process finished with exit code 1

What do I need to change to see how the code works?

Respectfully, Vladimir.

The reason why it's not working is because this site has blocked you from sending requests. The fact that you had to fake a user agent must have been a hint that you were doing something they didn't want you to do. Web scanning is way beyond the scope of this topic, so we should stop talking about it and get back to the subject at hand. Do you have any questions about using the MetaTrader5 package?

 
Maxim Dmitrievsky:

It may not have time to close Excel and is trying to access the file, then

add a 5-10 second delay, the program will wait for Excel to close

I have not yet decided what to do with the left window )

Maxim, thank you very much for the code, which you have helped me to improve. The left panel is of little concern at the moment. I will add your new code later, most likely at the weekend, as I have been completely swamped at work today and need a rest. I will let you know as soon as I get new results.

Regards, Vladimir.

 

On which offset does the MT5 library return the time of open orders?

def checkPositions(self):
        return mt5.positions_get(symbol=self.symbol)

pos = trade.checkPositions()
pos[0].time

>>> pos[0].time
1598035196

datetime.datetime.fromtimestamp(pos[0].time).strftime("%A, %B %d, %Y %I:%M:%S")

>>> datetime.datetime.fromtimestamp(pos[0].time).strftime("%A, %B %d, %Y %I:%M:%S")
'Saturday, August 22, 2020 01:39:56'

But in the terminal it is a different time

How do I correctly synchronize the time in the python program with the time of orders (positions)?

 
Maxim Dmitrievsky:

On which offset does the MT5 library return the time of open orders?

But in the terminal it is a different time

How do I correctly synchronize the time in the python program with the time of orders (positions)?

First you need to know the exact time zone of the trade server. Then create a time zone object using pytz. Finally, pass the time zone object to a method.

import datetime as dt

import pytz
import pymt5adapter as mta


def main():
    broker_timezone = pytz.timezone('Etc/UTC')
    for position in mta.positions_get():
        time = dt.datetime.fromtimestamp(position.time, tz=broker_timezone)
        print(time)


if __name__ == '__main__':
    with mta.connected():
        main()
https://stackoverflow.com/questions/13866926/is-there-a-list-of-pytz-timezones
Is there a list of Pytz Timezones?
Is there a list of Pytz Timezones?
  • 2012.12.13
  • ipegasus
  • stackoverflow.com
I would like to know what are all the possible values for the timezone argument in the Python library pytz. How to do it?
 
nicholish en:

First you must know the exact time zone of the trade server. Then create a time zone object using pytz. Finally, pass the time zone object to the method.

https://stackoverflow.com/questions/13866926/is-there-a-list-of-pytz-timezones

Thanks, I'll try it like this

I would like to synchronise the time automatically as the trading server may change
 
Maxim Dmitrievsky:

thanks, I'll try this

I would like to synchronize the time automatically, because the trade server may change

UNIX timestamps are timezone agnostic so you need to know the broker timezone to give the timestamp timezone aware context. The only way to do that is to know the trade-server's timezone setting. I don't think that there is any way to obtain that information programmatically. What you can do is create a config file to map the different trade servers that you may be using to timezone. Example:


config.json

{
    "servers": {
        "MetaQuotes-Demo": {
            "timezone": "Europe/Moscow"
        },
        "AMPGlobalUSA-Demo": {
            "timezone": "Etc/UTC"
        }
    }
}
 
nicholish en:

UNIX timestamps are timezone agnostic so you need to know the broker timezone to give the timestamp timezone aware context. The only way to do that is to know the trade-server's timezone setting. I don't think that there is any way to obtain that information programmatically. What you can do is create a config file to map the different trade servers that you may be using to timezone. Example:


config.json

well... yes

easier way without pytz:

pt = datetime.datetime.utcfromtimestamp(pos[0].time) // position open time by mt5 time
tt = datetime.datetime.utcnow() + datetime.timedelta(hours=3)   // current server time (from mt5 clock)

delta = tt-pt
>>> delta
datetime.timedelta(seconds=2139, microseconds=506786) // position lifetime
Also we can open one pending order far from the price, before starting EA. And compare difference in hours between 'pt' and
datetime.utcnow()
to automatically offset time
 
Maxim Dmitrievsky :

well ... yes

easier way without pytz:

Also we can open one pending order far from the price, before start EA. And compare difference in hours between 'pt' and to automatically offset time

I thought the same thing but then I remembered that the order time is stored in a UNIX timestamp which does not have a timezone. In other words, you cannot extrapolate the broker timezone from the order timestamp because timestamps are the same no matter the timezone. If you want to know the GMT offset you have to know the current terminal (broker) time and there's currently no way to use python to get the current terminal time or GMT offset.

 
Maxim Dmitrievsky:

It may not have time to close Excel and is trying to access the file, then

you may add a 5-10 second delay, the program will wait for Excel to close

I have not decided what to do with the window on the left )

Hello Maxim!

I promised you earlier that at the weekend I would check the variant with delay code addition. I have checked it, but the delay has not affected anything. I have no problems with the left tab, because I simply do not pay attention to it.

Once again, thank you very much for your assistance. I will not ask any more questions in this thread.

Regards, Vladimir.