Support from and to time on get_historical_price

This commit is contained in:
AuroraTradingTeam
2017-11-09 07:28:25 +00:00
parent 37748957d4
commit 0eaf619d07
4 changed files with 32 additions and 7 deletions
+1 -1
View File
@@ -60,7 +60,7 @@ test: ## run tests quickly with the default Python
py.test
test-all: ## run tests on every Python version with tox
tox
tox $(PY_VERSION)
coverage: ## check code coverage quickly with the default Python
coverage run --source libcryptomarket -m pytest
+1
View File
@@ -3,6 +3,7 @@ import requests
from datetime import datetime
API_URL = "https://min-api.cryptocompare.com/data/"
MAX_QUERY_LIMIT = 2000
class CryptocompareCoinlist:
+28 -3
View File
@@ -35,6 +35,10 @@ def get_historical_prices(source='cryptocompare', symbol=None, exchange=None,
raise ValueError("Only accept input parameter limit, or from_time"
" and to_time pair.")
if (from_time is None) ^ (to_time is None):
raise ValueError("Cannot accept either from_time or to_time is "
"None")
# Parse from (first 3) and to (last 3) symbol from the parameter
# symbol.
from_sym = symbol[:3]
@@ -45,6 +49,7 @@ def get_historical_prices(source='cryptocompare', symbol=None, exchange=None,
data = []
if limit > 0:
# Get the data by limit of records
to_time = 0
while limit > 0:
@@ -55,20 +60,40 @@ def get_historical_prices(source='cryptocompare', symbol=None, exchange=None,
if len(response) == 0:
# Terminate if no further response
limit = 0
break
else:
data += response
limit -= len(response)
to_time = response[0]['time'] - 1
elif from_time is not None and to_time is not None:
# Get the data by time range
from libcryptomarket.api.cryptocompare_api import MAX_QUERY_LIMIT
from_time_ts = int(from_time.timestamp())
to_time_ts = int(to_time.timestamp())
while from_time_ts < to_time_ts:
response = func(limit=MAX_QUERY_LIMIT,
toTs=to_time_ts)['Data']
if len(response) == 0:
# Terminate if no further response
break
else:
data += response
to_time_ts = response[0]['time'] - 1
elif from_time is not None or to_time is not None:
raise NotImplementedError()
else:
data += func()['Data']
data = pd.DataFrame([CryptocompareHisto(**e).__dict__ for e in data])
# Filter only valid time range
if from_time is not None and to_time is not None:
data = data[(data['r_time'] >= from_time) &
(data['r_time'] <= to_time)]
data = data.set_index(['r_time'])
data.index.name = 'datetime'
return data
else:
raise ValueError("No source is called {0}".format(source))
+2 -3
View File
@@ -127,7 +127,7 @@ def test_get_historical_prices_cryptocompare(monkeypatch):
'volumeto': 0.2586
}]
}
}
return MockReturnClass()
@@ -139,7 +139,6 @@ def test_get_historical_prices_cryptocompare(monkeypatch):
exchange='Poloniex',
symbol='LTCBTC')
expected_result = pd.DataFrame([
{
'r_close': 0.007707,
@@ -162,4 +161,4 @@ def test_get_historical_prices_cryptocompare(monkeypatch):
}]).set_index(['r_time'])
expected_result.index.name = 'datetime'
assert_frame_equal(result, expected_result)
assert_frame_equal(result, expected_result)