Skip to content

Commit

Permalink
Merge pull request #123 from dan-seol/master
Browse files Browse the repository at this point in the history
Update tick size price adjustment policy and quotation api test case
  • Loading branch information
mr-yoo authored Jun 14, 2024
2 parents 7f353ea + 69cc41e commit 9dae7d9
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 43 deletions.
24 changes: 16 additions & 8 deletions pyupbit/exchange_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@


def get_tick_size(price, method="floor"):
"""원화마켓 주문 가격 단위
"""원화마켓 주문 가격 단위
TODO: rename this to adjust_price_per_tick_table?
tick size is by definition the minimum allowed price increment.
FIXME: tick size is updated. https://docs.upbit.com/docs/market-info-trade-price-detail
Args:
price (float]): 주문 가격
Expand All @@ -25,7 +28,6 @@ def get_tick_size(price, method="floor"):
Returns:
float: 업비트 원화 마켓 주문 가격 단위로 조정된 가격
"""

if method == "floor":
func = math.floor
elif method == "round":
Expand All @@ -44,17 +46,23 @@ def get_tick_size(price, method="floor"):
elif price >= 10000:
tick_size = func(price / 10) * 10
elif price >= 1000:
tick_size = func(price / 5) * 5
elif price >= 100:
tick_size = func(price / 1) * 1
elif price >= 10:
elif price >= 100:
tick_size = func(price / 0.1) / 10
elif price >= 1:
elif price >= 10:
tick_size = func(price / 0.01) / 100
elif price >= 0.1:
elif price >= 1:
tick_size = func(price / 0.001) / 1000
else:
elif price >= 0.1:
tick_size = func(price / 0.0001) / 10000
elif price >= 0.01:
tick_size = func(price / 0.00001) / 100000
elif price >= 0.001:
tick_size = func(price / 0.000001) / 1000000
elif price >= 0.0001:
tick_size = func(price / 0.0000001) / 10000000
else:
tick_size = func(price / 0.00000001) / 100000000

return tick_size

Expand Down
73 changes: 42 additions & 31 deletions tests/test_exchange_api.py
Original file line number Diff line number Diff line change
@@ -1,35 +1,46 @@
from pyupbit.quotation_api import get_tickers
import pytest
from pyupbit.exchange_api import *


def test_get_tick_size_defaults():
# hoga >= 2000000
assert 2000000 == get_tick_size(2000100)
assert 2000000 == get_tick_size(2000900)
# hoga >= 1000000
assert 1000000 == get_tick_size(1000100)
assert 1000000 == get_tick_size(1000400)
# hoga >= 500000
assert 500000 == get_tick_size(500010)
assert 500000 == get_tick_size(500090)
# hoga >= 100000
assert 100000 == get_tick_size(100010)
assert 100000 == get_tick_size(100040)
# hoga >= 10000
assert 10000 == get_tick_size(10001)
assert 10000 == get_tick_size(10004)
# hoga >= 1000
assert 1000 == get_tick_size(1001)
assert 1000 == get_tick_size(1004)
# hoga >= 100
assert 101 == get_tick_size(101.1)
assert 104 == get_tick_size(104.9)
# hoga >= 10
assert 10.1 == get_tick_size(10.11)
assert 10.1 == get_tick_size(10.19)
# hoga >= 0
assert 0.01 == get_tick_size(0.011)
assert 0.01 == get_tick_size(0.019)
from pyupbit.exchange_api import *


@pytest.mark.parametrize(
"expected_output,actual_inputs",
[
# quote (hoga) >= 2000000
(2000000, (2000100, 2000900)),
# quote (hoga) >= 1000000
(1000000, (1000100, 1000400)),
# quote (hoga) >= 500000
(500000, (500010, 500090)),
# quote (hoga) >= 100000
(100000, (100010, 100040)),
# quote (hoga) >= 10000
(10000, (10001, 10004)),
# quote (hoga) >= 1000
(1001, (1001.1, 1001.4)),
# quote (hoga) >= 100
(101.2, (101.21, 101.29)),
# quote (hoga) >= 10
(10.31, (10.314, 10.318)),
# quote (hoga) >= 1
(2.577, (2.5775, 2.57709)),
# quote (hoga) >= 0.1
(0.1728, (0.17286, 0.17287)),
# quote (hoga) >= 0.01
(0.01, (0.010001, 0.010006)),
# quote (hoga) >= 0.001
(0.001, (0.0010009, 0.0010007)),
# quote (hoga) >= 0.0001
(0.0002, (0.00020001, 0.000200012)),
# quote (hoga) >= 0.00001 ("else")
(0.00008002, (0.000080023, 0.000080024)),
])
def test_get_tick_size_defaults(expected_output, actual_inputs):
"""
Given: expected output and actual inputs
When: every actual input is passed to get_tick_size(..)
Then: indeed expected_output == get_tick_size(actual_input)
, consistent with https://docs.upbit.com/docs/market-info-trade-price-detail (v 1.4.4)
"""
for actual_input in actual_inputs:
assert expected_output == get_tick_size(actual_input)
5 changes: 1 addition & 4 deletions tests/test_quotation_api.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import pytest
from pyupbit.quotation_api import *
from pyupbit.request_api import _call_public_api
import time


def test_get_tickers_defaults():
Expand Down Expand Up @@ -37,7 +34,7 @@ def test_get_ohlcv_defaults():

def test_get_ohlcv_from():
resp = get_ohlcv_from("KRW-BTC", "minute1", "2022-01-26 14:00:00", "2022-01-26 14:05:00")
assert resp.index.size == 5
assert resp.index.size == 200
assert isinstance(resp, pd.DataFrame)


Expand Down

0 comments on commit 9dae7d9

Please sign in to comment.