darwinex / dwxconnect

Seamlessly link any Trading Strategy in ANY programming language to Darwinex liquidity via MetaTrader 4 or 5. DWX Connect is your very own, fully customizable Trading API!
BSD 3-Clause "New" or "Revised" License
158 stars 84 forks source link

Error when converting text files to JSON #20

Closed sorul closed 1 year ago

sorul commented 1 year ago

Inside the DWX_Client.py file, in the functions "check_messages" and "check_historic_data" sometimes there are errors due to failure to convert the text of the file to json.

This happens because sometimes the text of the file is incomplete, so it cannot be transformed into a json. An example of "check_historic_data":

{"GBPJPY_H1": "2022.07.08 01:00": {"open": 163.50900, "high": 163.64800, "low": 163.44100, "close": 163.56500, "tick_volume": 1983.00000}, "2022"

Best regards.

elvinex commented 1 year ago

Hi, Thanks for the feedback. It is strange since I would have expected that as long as the mql side writes to the file, it should result in IOError or PermissionError which are ignored in try_read_file.

Which error type did you observe?

sorul commented 1 year ago

On the part of the try_read_file function there is no problem. It returns all the text in the file. The problem is that the file sometimes has only incomplete text. And in the end the error occurs when converting it to JSON -> JSONDecodeError

elvinex commented 1 year ago

Ok, thanks for the info. So either the python side is somehow allowed in between to read the file, which would be strange since it should be opened by mql for writing until closed. Or the mql side somehow only writes part of the string. Did it only happen for very long strings, like long price histories, or also short messages?

sorul commented 1 year ago

It's a problem in the MQL part, if you open the files in the folder you see that they are incompletely written. Or maybe Python tried to read them too early and didn't have time to finish writing them. I don't remember if it happened only with long text. But in the end I always bring the same amount of ticks, and sometimes it fails with no apparent criteria.

elvinex commented 1 year ago

Ok. Maybe it also happens when mql tries to write to the same file while python reads it. But in my experience usually one side is not allowed to do anything until the other side has closed the file. I don't have an idea how to prevent this at the moment, except just trying again if it fails.