leecallen35 / Trade-copier-Oa2Dx

Python program to copy trades from Oanda to DxTrade
5 stars 2 forks source link

The Oanda request is occassionally crashing #2

Open leecallen35 opened 4 months ago

leecallen35 commented 4 months ago

It's this line in the Trade-copier-oa2dx.py: for trand in r.response:

2024-03-18 12:51:08,679 [INFO] performing request https://api-fxtrade.oanda.com/v3/accounts/my-account-nbr/openPositions
2024-03-18 13:51:10,453 [INFO] performing request https://api-fxtrade.oanda.com/v3/accounts/my-account-nbr/openPositions
{'id': '77', 'accountID': 'my-account-nbr', 'userID': 2056580, 'batchID': '77', 'requestID': '115261941956426905', 'time': '2024-03-18T17:57:29.566163086Z', 'type': 'ORDER_CANCEL', 'orderID': '75', 'replacedByOrderID': '78', 'reason': 'CLIENT_REQUEST_REPLACED'}
{'id': '78', 'accountID': 'my-account-nbr', 'userID': 2056580, 'batchID': '77', 'requestID': '115261941956426905', 'time': '2024-03-18T17:57:29.566163086Z', 'type': 'MARKET_IF_TOUCHED_ORDER', 'instrument': 'GBP_CHF', 'units': '16093', 'price': '1.13208', 'timeInForce': 'GTC', 'triggerCondition': 'DEFAULT', 'partialFill': 'DEFAULT', 'positionFill': 'DEFAULT', 'reason': 'REPLACEMENT', 'replacesOrderID': '75'}
{'id': '79', 'accountID': 'my-account-nbr', 'userID': 2056580, 'batchID': '79', 'requestID': '115261944721587135', 'time': '2024-03-18T18:08:29.692734044Z', 'type': 'ORDER_CANCEL', 'orderID': '76', 'replacedByOrderID': '80', 'reason': 'CLIENT_REQUEST_REPLACED'}
{'id': '80', 'accountID': 'my-account-nbr', 'userID': 2056580, 'batchID': '79', 'requestID': '115261944721587135', 'time': '2024-03-18T18:08:29.692734044Z', 'type': 'LIMIT_ORDER', 'instrument': 'GBP_CHF', 'units': '16093', 'price': '1.12015', 'timeInForce': 'GTC', 'triggerCondition': 'DEFAULT', 'partialFill': 'DEFAULT', 'positionFill': 'DEFAULT', 'reason': 'REPLACEMENT', 'replacesOrderID': '76'}
--- Logging error ---
Traceback (most recent call last):
  File "/usr/lib/python3.9/site-packages/urllib3/response.py", line 697, in _update_chunk_length
    self.chunk_left = int(line, 16)
ValueError: invalid literal for int() with base 16: b''

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3.9/site-packages/urllib3/response.py", line 438, in _error_catcher
    yield
  File "/usr/lib/python3.9/site-packages/urllib3/response.py", line 764, in read_chunked
    self._update_chunk_length()
  File "/usr/lib/python3.9/site-packages/urllib3/response.py", line 701, in _update_chunk_length
    raise InvalidChunkLength(self, line)
urllib3.exceptions.InvalidChunkLength: InvalidChunkLength(got length b'', 0 bytes read)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3.9/site-packages/requests/models.py", line 753, in generate
    for chunk in self.raw.stream(chunk_size, decode_content=True):
  File "/usr/lib/python3.9/site-packages/urllib3/response.py", line 572, in stream
    for line in self.read_chunked(amt, decode_content=decode_content):
  File "/usr/lib/python3.9/site-packages/urllib3/response.py", line 793, in read_chunked
    self._original_response.close()
  File "/usr/lib64/python3.9/contextlib.py", line 137, in __exit__
    self.gen.throw(typ, value, traceback)
  File "/usr/lib/python3.9/site-packages/urllib3/response.py", line 455, in _error_catcher
    raise ProtocolError("Connection broken: %r" % e, e)
urllib3.exceptions.ProtocolError: ("Connection broken: InvalidChunkLength(got length b'', 0 bytes read)", InvalidChunkLength(got length b'', 0 bytes read))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/mnt/Personal/forex/forex-bot/Trade-copier-oa2dx.py", line 190, in main
    for trand in r.response:
  File "/home/lallen/.local/lib/python3.9/site-packages/oandapyV20/oandapyV20.py", line 289, in __stream_request
    for line in lines:
  File "/usr/lib/python3.9/site-packages/requests/models.py", line 797, in iter_lines
    for chunk in self.iter_content(chunk_size=chunk_size, decode_unicode=decode_unicode):
  File "/usr/lib/python3.9/site-packages/requests/models.py", line 756, in generate
    raise ChunkedEncodingError(e)
requests.exceptions.ChunkedEncodingError: ("Connection broken: InvalidChunkLength(got length b'', 0 bytes read)", InvalidChunkLength(got length b'', 0 bytes read))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib64/python3.9/logging/__init__.py", line 1083, in emit
    msg = self.format(record)
  File "/usr/lib64/python3.9/logging/__init__.py", line 927, in format
    return fmt.format(record)
  File "/usr/lib64/python3.9/logging/__init__.py", line 663, in format
    record.message = record.getMessage()
  File "/usr/lib64/python3.9/logging/__init__.py", line 367, in getMessage
    msg = msg % self.args
TypeError: must be real number, not ChunkedEncodingError
Call stack:
  File "/mnt/Personal/forex/forex-bot/Trade-copier-oa2dx.py", line 253, in <module>
    main()
  File "/mnt/Personal/forex/forex-bot/Trade-copier-oa2dx.py", line 250, in main
    logging.critical("Unexpected error encountered, restarting: %e", e)
Message: 'Unexpected error encountered, restarting: %e'
Arguments: (ChunkedEncodingError(ProtocolError("Connection broken: InvalidChunkLength(got length b'', 0 bytes read)", InvalidChunkLength(got length b'', 0 bytes read))),)
--- Logging error ---
Traceback (most recent call last):
  File "/usr/lib/python3.9/site-packages/urllib3/response.py", line 697, in _update_chunk_length
    self.chunk_left = int(line, 16)
ValueError: invalid literal for int() with base 16: b''

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3.9/site-packages/urllib3/response.py", line 438, in _error_catcher
    yield
  File "/usr/lib/python3.9/site-packages/urllib3/response.py", line 764, in read_chunked
    self._update_chunk_length()
  File "/usr/lib/python3.9/site-packages/urllib3/response.py", line 701, in _update_chunk_length
    raise InvalidChunkLength(self, line)
urllib3.exceptions.InvalidChunkLength: InvalidChunkLength(got length b'', 0 bytes read)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3.9/site-packages/requests/models.py", line 753, in generate
    for chunk in self.raw.stream(chunk_size, decode_content=True):
  File "/usr/lib/python3.9/site-packages/urllib3/response.py", line 572, in stream
    for line in self.read_chunked(amt, decode_content=decode_content):
  File "/usr/lib/python3.9/site-packages/urllib3/response.py", line 793, in read_chunked
    self._original_response.close()
  File "/usr/lib64/python3.9/contextlib.py", line 137, in __exit__
    self.gen.throw(typ, value, traceback)
  File "/usr/lib/python3.9/site-packages/urllib3/response.py", line 455, in _error_catcher
    raise ProtocolError("Connection broken: %r" % e, e)
urllib3.exceptions.ProtocolError: ("Connection broken: InvalidChunkLength(got length b'', 0 bytes read)", InvalidChunkLength(got length b'', 0 bytes read))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/mnt/Personal/forex/forex-bot/Trade-copier-oa2dx.py", line 190, in main
    for trand in r.response:
  File "/home/lallen/.local/lib/python3.9/site-packages/oandapyV20/oandapyV20.py", line 289, in __stream_request
    for line in lines:
  File "/usr/lib/python3.9/site-packages/requests/models.py", line 797, in iter_lines
    for chunk in self.iter_content(chunk_size=chunk_size, decode_unicode=decode_unicode):
  File "/usr/lib/python3.9/site-packages/requests/models.py", line 756, in generate
    raise ChunkedEncodingError(e)
requests.exceptions.ChunkedEncodingError: ("Connection broken: InvalidChunkLength(got length b'', 0 bytes read)", InvalidChunkLength(got length b'', 0 bytes read))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib64/python3.9/logging/__init__.py", line 1083, in emit
    msg = self.format(record)
  File "/usr/lib64/python3.9/logging/__init__.py", line 927, in format
    return fmt.format(record)
  File "/usr/lib64/python3.9/logging/__init__.py", line 663, in format
    record.message = record.getMessage()
  File "/usr/lib64/python3.9/logging/__init__.py", line 367, in getMessage
    msg = msg % self.args
TypeError: must be real number, not ChunkedEncodingError
Call stack:
  File "/mnt/Personal/forex/forex-bot/Trade-copier-oa2dx.py", line 253, in <module>
    main()
  File "/mnt/Personal/forex/forex-bot/Trade-copier-oa2dx.py", line 250, in main
    logging.critical("Unexpected error encountered, restarting: %e", e)
Message: 'Unexpected error encountered, restarting: %e'
Arguments: (ChunkedEncodingError(ProtocolError("Connection broken: InvalidChunkLength(got length b'', 0 bytes read)", InvalidChunkLength(got length b'', 0 bytes read))),)
leecallen35 commented 4 months ago

Trying this: https://github.com/psf/requests/issues/4248

Per that discussion, this is apparently a defect in the data sent by Oanda, but that code is no longer maintained so a workaround is the only approach.