Repository: freqtrade/freqtrade

Issue:
KuCoin could not update buy order
## Step 1: Have you search for this issue before posting it?

If you have discovered a bug in the bot, please [search our issue tracker](https://github.com/freqtrade/freqtrade/issues?q=is%3Aissue). 
If it hasn't been reported, please create a new issue.

## Step 2: Describe your environment

  * Operating system: Ubuntu 1604
  * Python Version: 3.7.3
  * CCXT version: 1.18.1008
  * Branch: Develop
  * Last Commit ID:f0206a90b1504c40e74b1c6bae4c719779cdee85
  
## Step 3: Describe the problem:

The bot is able to place a market buy order on KuCoin. But after the order is been filled(which I can comfirm on exchange), the bot couldn't update the 'completed' market buy order.

### Steps to reproduce:
  
### Observed Results:

  * What happened?
The bot gave a 'Bad request' error, saying it could not get trades due to networking error. I suspect that the link of the request, i.e. "https://openapi-v2.kucoin.com/api/v1/fills?symbol=BTC-USDT&startAt=1564998855.765331" is not right, since the timestamp is a decimal number somehow, in stead of an integer.

  * What did you expect to happen?
I expect the bot to get an update when the buy order has been filled.

### Relevant code exceptions or logs:

  ```
 2019-08-05 11:04:05,947 - freqtrade.freqtradebot - INFO - Found open order for Trade(id=1, pair=BTC/USDT, amount=0.01698307, open_rate=11694.00000000, open_since=an hour ago)
2019-08-05 11:04:06,517 - freqtrade.exchange.exchange - WARNING - get_trades_for_order() returned exception: "Could not get trades due to networking error. Message: GET https://openapi-v2.kucoin.com/api/v1/fills?symbol=BTC-USDT&startAt=1564998855.765331 400  {"code":"400000","msg":"Bad Request"}"
  ```



Discussion:
I'm not sure if this bug is with us or ccxt, since i'm not clear on the type ccxt expects for "since", therefore i raised this question to the ccxt project.
https://github.com/ccxt/ccxt/issues/5636

Once they get back - usually within a day or two - we should have clarity on how to approach this issue



*Note: i don't expect this responsiveness from ccxt - it's just what my experience has been so far.*


Resolving patch:
diff --git a/freqtrade/exchange/exchange.py b/freqtrade/exchange/exchange.py
--- a/freqtrade/exchange/exchange.py
+++ b/freqtrade/exchange/exchange.py
@@ -725,7 +725,8 @@ def get_trades_for_order(self, order_id: str, pair: str, since: datetime) -> Lis
             return []
         try:
             # Allow 5s offset to catch slight time offsets (discovered in #1185)
-            my_trades = self._api.fetch_my_trades(pair, since.timestamp() - 5)
+            # since needs to be int in milliseconds
+            my_trades = self._api.fetch_my_trades(pair, int((since.timestamp() - 5) * 1000))
             matched_trades = [trade for trade in my_trades if trade['order'] == order_id]
 
             return matched_trades


Test patch:
diff --git a/freqtrade/tests/exchange/test_exchange.py b/freqtrade/tests/exchange/test_exchange.py
--- a/freqtrade/tests/exchange/test_exchange.py
+++ b/freqtrade/tests/exchange/test_exchange.py
@@ -2,7 +2,7 @@
 # pragma pylint: disable=protected-access
 import copy
 import logging
-from datetime import datetime
+from datetime import datetime, timezone
 from random import randint
 from unittest.mock import MagicMock, Mock, PropertyMock
 
@@ -11,8 +11,8 @@
 import pytest
 from pandas import DataFrame
 
-from freqtrade import (DependencyException, OperationalException,
-                       TemporaryError, InvalidOrderException)
+from freqtrade import (DependencyException, InvalidOrderException,
+                       OperationalException, TemporaryError)
 from freqtrade.exchange import Binance, Exchange, Kraken
 from freqtrade.exchange.exchange import API_RETRY_COUNT
 from freqtrade.resolvers.exchange_resolver import ExchangeResolver
@@ -1361,7 +1361,7 @@ def test_name(default_conf, mocker, exchange_name):
 @pytest.mark.parametrize("exchange_name", EXCHANGES)
 def test_get_trades_for_order(default_conf, mocker, exchange_name):
     order_id = 'ABCD-ABCD'
-    since = datetime(2018, 5, 5)
+    since = datetime(2018, 5, 5, tzinfo=timezone.utc)
     default_conf["dry_run"] = False
     mocker.patch('freqtrade.exchange.Exchange.exchange_has', return_value=True)
     api_mock = MagicMock()
@@ -1391,6 +1391,13 @@ def test_get_trades_for_order(default_conf, mocker, exchange_name):
     orders = exchange.get_trades_for_order(order_id, 'LTC/BTC', since)
     assert len(orders) == 1
     assert orders[0]['price'] == 165
+    assert api_mock.fetch_my_trades.call_count == 1
+    # since argument should be
+    assert isinstance(api_mock.fetch_my_trades.call_args[0][1], int)
+    assert api_mock.fetch_my_trades.call_args[0][0] == 'LTC/BTC'
+    # Same test twice, hardcoded number and doing the same calculation
+    assert api_mock.fetch_my_trades.call_args[0][1] == 1525478395000
+    assert api_mock.fetch_my_trades.call_args[0][1] == int(since.timestamp() - 5) * 1000
 
     ccxt_exceptionhandlers(mocker, default_conf, api_mock, exchange_name,
                            'get_trades_for_order', 'fetch_my_trades',