lucas-campagna / mt5linux

MetaTrader5 for linux users
MIT License
72 stars 39 forks source link

Issue with the copy_rates_range function #21

Open thmmcoelho opened 3 weeks ago

thmmcoelho commented 3 weeks ago

When I try to use the mt5 copy_rates_range function, I get the following error log:

Traceback (most recent call last):
  File "/opt/.pycharm_helpers/pydev/_pydevd_bundle/pydevd_exec2.py", line 3, in Exec
    exec(exp, global_vars, local_vars)
  File "<input>", line 1, in <module>
  File "/usr/local/lib/python3.8/site-packages/mt5linux/__init__.py", line 2290, in copy_rates_range
    return rpyc.utils.classic.obtain(self.__conn.eval(code))
  File "/usr/local/lib/python3.8/site-packages/rpyc/core/netref.py", line 240, in __call__
    return syncreq(_self, consts.HANDLE_CALL, args, kwargs)
  File "/usr/local/lib/python3.8/site-packages/rpyc/core/netref.py", line 63, in syncreq
    return conn.sync_request(handler, proxy, *args)
  File "/usr/local/lib/python3.8/site-packages/rpyc/core/protocol.py", line 473, in sync_request
    return self.async_request(handler, *args, timeout=timeout).value
  File "/usr/local/lib/python3.8/site-packages/rpyc/core/async_.py", line 102, in value
    raise self._obj
  File "<string>", line 1
    mt5.copy_rates_range("WINV24", 2, datetime.datetime(2024, 9, 2, 0, 0, tzinfo=datetime.timezone(datetime.timedelta(days=-1, seconds=75600), '-03')), 2024-09-03 00:00:00)
                                                                                                                                                              ^
_get_exception_class.<locals>.Derived: leading zeros in decimal integer literals are not permitted; use an 0o prefix for octal integers

This is my code:

start_date = '2024-09-02 00:00:00'
finish_date = '2024-09-03 00:00:00'
time_start = datetime.strptime(start_date, '%Y-%m-%d %H:%M:%S')
time_finish = datetime.strptime(finish_date, '%Y-%m-%d %H:%M:%S')
time_delta = time_finish - time_start
if time_delta.days <= 0:
    range_start = time_finish - timedelta(days=1)
else:
    range_start = time_start
mt5.execute('import datetime')
mt5.execute('import pytz')
rates = mt5.copy_rates_range(symbol, mt5.TIMEFRAME_M2, range_start, time_finish)

I tried to add mt5.execute('import pytz') and mt5.execute('import datetime'), as mentioned in https://github.com/lucas-campagna/mt5linux/issues/3, but this does not work.

Masses3581 commented 5 days ago

@thmmcoelho: I had the exact same problem. By chance, I saw that the author has implemented a fix for this in the mt5linux -> init.py. Copy the file again into your IDE from the host, and then the retrieval with date_to will work. I added 2 lines, and it worked. Hope this helps