Skip to content

Commit

Permalink
can change UpbitDataProvider url with interval
Browse files Browse the repository at this point in the history
  • Loading branch information
msaltnet committed Aug 5, 2023
1 parent 0e5116a commit 031fcdf
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
13 changes: 12 additions & 1 deletion smtm/upbit_data_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,24 @@ class UpbitDataProvider(DataProvider):
URL = "https://api.upbit.com/v1/candles/minutes/1"
AVAILABLE_CURRENCY = {"BTC": "KRW-BTC", "ETH": "KRW-ETH", "DOGE": "KRW-DOGE", "XRP": "KRW-XRP"}

def __init__(self, currency="BTC"):
def __init__(self, currency="BTC", interval=60):
if currency not in self.AVAILABLE_CURRENCY:
raise UserWarning(f"not supported currency: {currency}")

self.logger = LogManager.get_logger(__class__.__name__)
self.query_string = {"market": self.AVAILABLE_CURRENCY[currency], "count": 1}
self.market = currency
self.interval = interval
if self.interval == 60:
self.URL = "https://api.upbit.com/v1/candles/minutes/1"
elif self.interval == 180:
self.URL = "https://api.upbit.com/v1/candles/minutes/3"
elif self.interval == 300:
self.URL = "https://api.upbit.com/v1/candles/minutes/5"
elif self.interval == 600:
self.URL = "https://api.upbit.com/v1/candles/minutes/10"
else:
raise UserWarning(f"not supported interval: {interval}")

def get_info(self):
"""실시간 거래 정보 전달한다
Expand Down
36 changes: 36 additions & 0 deletions tests/upbit_data_provider_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,39 @@ def test_initialize_from_server_NOT_initialized_when_connection_fail(self, mock_

with self.assertRaises(UserWarning):
dp.get_info()

def test_initialize_should_set_correct_url_with_interval(self):
dp = UpbitDataProvider("BTC", 60)
self.assertEqual(dp.URL, "https://api.upbit.com/v1/candles/minutes/1")
dp = UpbitDataProvider("BTC", 180)
self.assertEqual(dp.URL, "https://api.upbit.com/v1/candles/minutes/3")
dp = UpbitDataProvider("BTC", 300)
self.assertEqual(dp.URL, "https://api.upbit.com/v1/candles/minutes/5")
self.assertEqual(UpbitDataProvider.URL, "https://api.upbit.com/v1/candles/minutes/1")
dp = UpbitDataProvider("BTC", 600)
self.assertEqual(dp.URL, "https://api.upbit.com/v1/candles/minutes/10")

with self.assertRaises(UserWarning):
dp = UpbitDataProvider("BTC", 1)

@patch("requests.get")
def test_get_info_should_call_correct_url_with_different_interval(self, mock_get):
dp = UpbitDataProvider("BTC", 60)
dp.get_info()
expected_url = "https://api.upbit.com/v1/candles/minutes/1"
mock_get.assert_called_once_with(expected_url, params={"market": "KRW-BTC", "count": 1})

dp = UpbitDataProvider("BTC", 180)
dp.get_info()
expected_url = "https://api.upbit.com/v1/candles/minutes/3"
mock_get.assert_called_with(expected_url, params={"market": "KRW-BTC", "count": 1})

dp = UpbitDataProvider("BTC", 300)
dp.get_info()
expected_url = "https://api.upbit.com/v1/candles/minutes/5"
mock_get.assert_called_with(expected_url, params={"market": "KRW-BTC", "count": 1})

dp = UpbitDataProvider("BTC", 600)
dp.get_info()
expected_url = "https://api.upbit.com/v1/candles/minutes/10"
mock_get.assert_called_with(expected_url, params={"market": "KRW-BTC", "count": 1})

0 comments on commit 031fcdf

Please sign in to comment.