nicholishen / pymt5adapter---DEPRECATED

A drop-in pythonic adapter for the MetaTrader5 package to enhance usability.
BSD 2-Clause "Simplified" License
86 stars 31 forks source link

copy_rates_range() does not work with datetime objects #7

Open ahmadazizi opened 3 years ago

ahmadazizi commented 3 years ago

Hi, There is an issue when passing python datetime objects to copy_rates_range(symbol, timeframe, date_from, date_to)

Here is a part of the stack trace:

--- Logging error ---
Traceback (most recent call last):
  File "C:\Program Files\Python\lib\logging\__init__.py", line 1081, in emit
    msg = self.format(record)
  File "C:\Program Files\Python\lib\logging\__init__.py", line 925, in format
    return fmt.format(record)
  File "C:\Program Files\Python\lib\logging\__init__.py", line 664, in format
    record.message = record.getMessage()
  File "C:\Program Files\Python\lib\logging\__init__.py", line 367, in getMessage
    msg = str(self.msg)
  File "d:\pydev\exol\backend\.venv\lib\site-packages\pymt5adapter\helpers.py", line 26, in __str__
    res = f"{msg}\t{json.dumps(self)}"
  File "C:\Program Files\Python\lib\json\__init__.py", line 231, in dumps
    return _default_encoder.encode(obj)
  File "C:\Program Files\Python\lib\json\encoder.py", line 199, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "C:\Program Files\Python\lib\json\encoder.py", line 257, in iterencode
    return _iterencode(o, 0)
  File "C:\Program Files\Python\lib\json\encoder.py", line 179, in default
    raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type datetime is not JSON serializable
Call stack:
  File "C:\Program Files\Python\lib\runpy.py", line 194, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "C:\Program Files\Python\lib\runpy.py", line 87, in _run_code
    exec(code, run_globals)

I was able to solve this by passing timestamps instead of datetime objects, The docs says that either timestamp or datetime is acceptable. Have to mention that copy_rates_from() is fine with datetime.