๐ Python Upbit API๋ ๊ฐ๋จํ๊ณ ๋ช ๋ฃํ Upbit API Wrapper ์ ๋๋ค. ๐
pip ๋ช ๋ น์ด๋ก ๊ฐ๋จํ๊ฒ ์ค์นํ ์ ์์ต๋๋ค.
pip install python-upbit-api๊ฐ์ข
์์ธ ์ ๋ณด๋ฅผ ์ป๊ธฐ ์ํ QUOTATION API๋ง ์ฌ์ฉํ๋ค๋ฉด ์ธ์ฆ ์ ๋ณด ์์ด ์ด๊ธฐํ ํ ์ฌ์ฉํฉ๋๋ค. upbit.exceptions.ApiKeyError ์์ธ๊ฐ ๋ฐ์ํฉ๋๋ค.
# example.py from upbit import Upbit upbit = Upbit() res = upbit.get_markets() data = res.json() upbit.get_accounts() # upbit.exceptions.ApiKeyError ๋ฐ์์ธ์ฆ์ด ํ์ํ EXCHANGE API๋ฅผ ์ฌ์ฉํด์ผ ํ๋ค๋ฉด ์ธ์ฆ ์ ๋ณด๋ฅผ ์ด๊ธฐํ์ ๋ฑ๋ก ํ ์ฌ์ฉํฉ๋๋ค.
๐๐ปโโ๏ธ QUOTATION API๋ ์ฌ์ฉํ ์ ์์ต๋๋ค.
.env ํ๊ฒฝ์ค์ ํ์ผ์ ๋ฐ๊ธ ๋ฐ์ ์ธ์ฆ ์ ๋ณด๋ฅผ ์ค์ ํฉ๋๋ค.
UPBIT_OPEN_API_ACCESS_KEY=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx UPBIT_OPEN_API_SECRET_KEY=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx # example.py import os from upbit import Upbit access_key = os.environ.get('UPBIT_OPEN_API_ACCESS_KEY') secret_key = os.environ.get('UPBIT_OPEN_API_SECRET_KEY') upbit = Upbit(access_key, secret_key) res = upbit.get_accounts() data = res.json()์
๋นํธ REST API๋ฅผ Upbit ํด๋์ค์ ๋ฉ์๋๋ก ์ ๊ณตํฉ๋๋ค. ์์ธํ ๋ด์ฉ์ upbit.py ํ์ผ ๋ด docstring ์ฐธ์กฐ.
| ๋ฉ์๋๋ช | ||
|---|---|---|
| ์์ฐ | ์ ์ฒด ๊ณ์ข ์กฐํ | get_accounts |
| ์ฃผ๋ฌธ | ์ฃผ๋ฌธ ๊ฐ๋ฅ ์ ๋ณด | get_order_chance |
| ๊ฐ๋ณ ์ฃผ๋ฌธ ์กฐํ | get_order | |
| ์ฃผ๋ฌธ ๋ฆฌ์คํธ ์กฐํ | get_orders | |
| id๋ก ์ฃผ๋ฌธ๋ฆฌ์คํธ ์กฐํ | get_orders_by_id | |
| ์ฒด๊ฒฐ ๋๊ธฐ ์ฃผ๋ฌธ (Open Order) ์กฐํ | get_open_orders | |
| ์ข ๋ฃ๋ ์ฃผ๋ฌธ (Closed Order) ์กฐํ | get_closed_orders | |
| ์ฃผ๋ฌธ ์ทจ์ ์ ์ | delete_order | |
| ์ฃผ๋ฌธํ๊ธฐ | create_order | |
| ์ถ๊ธ | ์ถ๊ธ ๋ฆฌ์คํธ ์กฐํ | get_withdraws |
| ๊ฐ๋ณ ์ถ๊ธ ์กฐํ | get_withdraw | |
| ์ถ๊ธ ๊ฐ๋ฅ ์ ๋ณด | get_withdraw_chance | |
| ๋์งํธ ์์ฐ ์ถ๊ธํ๊ธฐ | create_withdraw_coin | |
| ์ํ ์ถ๊ธํ๊ธฐ | create_withdraw_krw | |
| ์ถ๊ธ ํ์ฉ ์ฃผ์ ๋ฆฌ์คํธ ์กฐํ | get_withdraw_addresses | |
| ์ ๊ธ | ์ ๊ธ ๋ฆฌ์คํธ ์กฐํ | get_deposits |
| ๊ฐ๋ณ ์ ๊ธ ์กฐํ | get_deposit | |
| ์ ๊ธ ์ฃผ์ ์์ฑ ์์ฒญ | create_coin_address | |
| ์ ์ฒด ์ ๊ธ ์ฃผ์ ์กฐํ | get_coin_addresses | |
| ๊ฐ๋ณ ์ ๊ธ ์ฃผ์ ์กฐํ | get_coin_address | |
| ์ํ ์ ๊ธํ๊ธฐ | create_deposit_krw | |
| ๊ณ์ ์ฃผ ํ์ธ(ํธ๋๋ธ๋ฃฐ ๊ฒ์ฆ)๊ฐ๋ฅ ๊ฑฐ๋์ ๋ฆฌ์คํธ ์กฐํ | get_vasps | |
| ์ ๊ธ UUID๋ก ํธ๋๋ธ๋ฃฐ ๊ฒ์ฆํ๊ธฐ | verify_travel_rule_by_uuid | |
| ์ ๊ธ TxID๋ก ํธ๋๋ธ๋ฃฐ ๊ฒ์ฆํ๊ธฐ | verify_travel_rule_by_txid | |
| ์๋น์ค ์ ๋ณด | ์ ์ถ๊ธ ํํฉ | get_wallet_status |
| API ํค ๋ฆฌ์คํธ ์กฐํ | get_api_keys | |
| ์์ธ ์ข ๋ชฉ ์กฐํ | ๋ง์ผ ์ฝ๋ ์กฐํ | get_markets |
| ์์ธ ์บ๋ค ์กฐํ | ์ด(Second) ์บ๋ค | get_candles_second |
| ๋ถ(Minute) ์บ๋ค | get_candles_minute | |
| ์ผ(Day) ์บ๋ค | get_candles_day | |
| ์ฃผ(Week) ์บ๋ค | get_candles_week | |
| ์(Month) ์บ๋ค | get_candles_month | |
| ์ฐ(Year) ์บ๋ค | get_candles_year | |
| ์์ธ ์ฒด๊ฒฐ ์กฐํ | ์ต๊ทผ ์ฒด๊ฒฐ ๋ด์ญ | get_trades_ticks |
| ์์ธ ํ์ฌ๊ฐ ์กฐํ | ํ์ฌ๊ฐ ์ ๋ณด | get_ticker |
| ์์ธ ํ์ฌ๊ฐ ์กฐํ | ๋ง์ผ ๋จ์ ํ์ฌ๊ฐ ์ ๋ณด | get_tickers_by_quote |
| ์์ธ ํธ๊ฐ ์กฐํ | ํธ๊ฐ ์ ๋ณด | get_orderbook |
Requests ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ฌ์ฉํฉ๋๋ค.
-
requests.Response๊ฐ์ฒด๋ฅผ ๊ทธ๋๋ก ๋ฐํํฉ๋๋ค.# example.py from upbit import Upbit upbit = Upbit() res = upbit.get_markets() data = res.json() status_code = res.status_code
-
requests.adapters.HTTPAdapterํด๋์ค๋ฅผ ์ฌ์ฉํ์ฌ ์ฌ์๋ ํ์ ๋ฑ์ ์ค์ ํ ์ ์์ต๋๋ค.# example.py from upbit import Upbit import requests adapter = requests.adapters.HTTPAdapter(max_retries=3) upbit = Upbit(http_adapter=adapter)
-
requests.Session.request์์ฒญ ํ๋ผ๋ฏธํฐtimeout๊ฐ์ ์ค์ ํ ์ ์์ต๋๋ค.# example.py from upbit import Upbit upbit = Upbit(timeout=(10, 5)) # ๋ชจ๋ ์์ฒญ์ ์ค์ ํ timeout res = upbit.get_markets(timeout=10) # ์ด ์์ฒญ์๋ง ์ค์ ํ timeout
๋ชจ๋ API ์์ฒญ์์ ๋ฐ์ํ requests.exceptions.HTTPError ์์ธ๋ ์ด ์์ธ๋ฅผ ์์ํ upbit.exceptions.UpbitHTTPError ์์ธ๋ก ๋ฐ์ํฉ๋๋ค.
์๋ Upbit ๊ณต์ ๋ฌธ์์ ๊ณต๊ฐํ ์๋ฌ ์ ํ์ ๋ง๋ ์์ธ ์์ ๊ฐ์ง๋๋ค.
# example.py from upbit import Upbit, TooManyRequests, UpbitClientError, UpbitServerError upbit = Upbit() try: res = upbit.get_markets() except TooManyRequests as e: status_code = e.response.status_code # 429 # ...์์ธ ์ฒ๋ฆฌ ์ฝ๋ except UpbitClientError as e: res = e.response # ...์์ธ ์ฒ๋ฆฌ ์ฝ๋ except UpbitServerError as e: res = e.response # ...์์ธ ์ฒ๋ฆฌ ์ฝ๋์
๋นํธ API Remaining-Req ์๋ต ํค๋์ ๋ด๊ธด ์์ฌ ์์ฒญ ์๋ฅผ upbit.models.RemainingReq ๊ฐ์ฒด๋ก ์ ๊ณต๋์ด ์ ๋ณด์ ์ฝ๊ฒ ์ ๊ทผ ํ ์ ์์ต๋๋ค. ์์ฒญ ๊ทธ๋ฃน๋ณ ์ต์ ๊ฐ์ ํ๋กํผํฐ์ ์ ์ฅํ๋ฉฐ get_remaining_reqs() ํจ์๋ฅผ ํธ์ถํ๋ฉด ๊ทธ๋ฃน์ ์ต์ ์์ฌ ์์ฒญ ์ ๊ฐ์ฒด๋ฅผ ํ์ธ ํ ์ ์์ต๋๋ค.
์ฐธ๊ณ : Exchange API ์์ฌ ์์ฒญ ์ ํ์ธ ๋ฐฉ๋ฒ
# example.py from upbit import Upbit from upbit.models import RemainingReq upbit = Upbit() res = upbit.get_candles_day('KRW-BTC') rr: RemainingReq = upbit.get_remaining_reqs('candles') rr.minute rr.updated # ์๋ต ํค๋๋ ๊ทธ๋๋ก ์ฌ์ฉ ๊ฐ๋ฅ rr_text = res.headers['Remaining-Req'] # 'group=candles; min=59; sec=4'Upbit API ์์ฒญ ํ๋ผ๋ฏธํฐ์ ์ฌ์ฉํ ๋ค์ํ ๋ฐ์ดํฐ ๋ชจ๋ธ์ ํ์ ์ผ๋ก ์ ๊ณตํฉ๋๋ค.
# example.py from upbit.models import OrderSide # ... # Type Hint order_side: OrderSide = 'bid' res = upbit.create_order(market='KRW-BTC', side=order_side, ord_type='limit', price='100', volume='0.01')์ ์์ฑํ Docstring ๋ด์ฉ๊ณผ ํ์ ์ผ๋ก IDE์์ ์ฌ์ฉํ๊ธฐ ํธ๋ฆฌํฉ๋๋ค. ๐๐ก
์์ง ์ง์ํ์ง ์์ต๋๋ค. ์ฐธ๊ณ ์ด์
์ต์ ๋ณ๊ฒฝ ์ฌํญ์ ์๋์์ ํ์ธํ ์ ์์ต๋๋ค.