Discussion of article "MetaTrader 5 and Python integration: receiving and sending data" - page 9

You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
Hello, Maxim!
I am a bit advanced in my knowledge of Python, but I can't find detailed information about joint access to csv file from Python and a third-party programme, for example, the same Expert Advisor written in MQL5. Simply put, the Python code periodically writes information to the csv file, and the MQL5 Expert Advisor reads the information from it and executes the commands I need. Is such joint access from both sides even possible?
Regards, Vladimir.
No, but both programmes can try to open the file if it is not occupied by another programme. And they should close it after reading/writing.
No, but both programmes can try to open a file if it is not occupied by another programme. And should close it after reading/writing.
Thank you, Maxim, for the explanation, especially about closing the file after reading/writing. This is the point I needed to understand.
Regards, Vladimir.
No, but both programmes can try to open a file if it is not occupied by another programme. And should close it after reading/writing.
Hello, Maxim, I've taken this point into account. Please help me to understand one more point, namely, the encoding of text in a csv file created using Python and the subsequent reading of this text by an Expert Advisor created in MQL5. I have reviewed a lot of information on the MQL5 website and other Internet resources, but I have not found a clear explanation. In general, the programme code created using the Python language opens a new csv file and writes the information I need into it. For simplicity of adjustment is written while one word in Russian, which was actually sparsen from the site. The word in the open csv file I read without problems, but the Expert Advisor does not understand the encoding of this word. That's for sure, because when writing the EA (by the way, I managed to write the first independent code!) I used a csv file with the same name and wrote the same word in cell A1, but only from the keyboard. I tried to change everything in both codes (I mean in Python and MQL5), but nothing works yet.
Can you tell me what encoding (encoding=?) to use for the Python code:
and what encoding (encoding=?) to use for MQL5 code:
to make them "friends"? I've been changing these encodings for several days now, and I can't think what to do next. And there is only one single point that prevents normal work.
Perhaps I haven't found all the encodings for Python yet, but the most interesting thing is that when the MQL5 code has the FILE_ANSI encoding, which is given in the line above, and when I write the word, for example, "buy" on the keyboard in a csv file, then after saving this file the Expert Advisor reads and works perfectly.
Sometimes I think: maybe it's not about encodings at all! What is it then?
Please help me to understand what is the reason!
Regards, Vladimir.
P.S. I finally found at least one reasonable explanation: "In Python 3, the default encoding is UTF-8".
Good, to avoid problems, it is better to write to a binary file and convert the output back to characters. In this article, in my opinion, this is what is done in the example with sockets. That is, the FILE_BIN flag
Good, to avoid problems, it is better to write to a binary file and convert the output back to characters. In this article, in my opinion, this is what is done in the example with sockets. That is, the FILE_BIN flag
Hi Maxim, thanks to your hints I found this information on the Internet: "Binary files, unlike text files, store information as a set of bytes. To work with them in Python you need the built-in pickle module ".
In other words, if we write any word in Pyhon program code as a set of bytes using the installed pickle module , changing 'w' to 'wb' .
and then in an Expert Advisor written in MQL5 set FILE_BIN flag instead of FILE_ANSI flag , everything should work? Probably you should not write to a csv file, but to a dat file.
Thank you very much, Maxim, for the hint, I will try it tonight!
Regards, Vladimir.Hi Maxim, thanks to your help I found this information on the Internet: "Unlike text files, binary files store information as a set of bytes. To work with them in Python, you need the built-in pickle module ".
In other words, if we write any word in Pyhon program code as a set of bytes using the installed pickle module , changing 'w' to 'wb' .
and then in an Expert Advisor written in MQL5 set FILE_BIN flag instead of FILE_ANSI flag , everything should work? Probably you should not write to a csv file, but to a dat file.
Thank you very much, Maxim, for the hint, tonight I will try it!
Regards, Vladimir.not necessarily, you can just select the encoding
st = b 'hello world'
or
'helloworld '.encode('ascii')
and write it to a file. Then try to read it in mt5
You don't have to, you can just select the encoding
st = b 'hello world
or
'helloworld '.encode('ascii')
and write it to a file. Then try to read it in mt5
Once again I say and will not get tired of saying a HUGE THANK YOU, Maxim, and God grant you many years of life!
The most important thing for me is that you told me what the essence of the problem is, and I found the solution even easier. I left everything in the Expert Advisor as it was when testing the csv file with the word in cell A1 entered from the computer keyboard. But for the programme code written in Python, I found this hint in the Internet:
with open(save_file, 'w', encoding='cp1251', errors='replace', newline='') as save:
I highlighted in red what was missing! I hope that this line will also be useful for someone when writing code in Python!
Regards, Vladimir.
Good afternoon.
I have a problem to solve in python in matplotlib/plotly:
1. There is an OHLC for rendering in the main window
2. There is data to draw in the subwindow
How to code to have a simple drawing? give me a simple python code
I'm having trouble with this code:
It says
I'm still new to python. Can you tell me what's wrong?
And I have a problem with this code:
it says
I'm still new to python. Can you tell me what's wrong?