달나라 노트

Python Upbit : get_balances, get_balance (Upbit 잔고 조회, Upbit 자산 조회) 본문

Python/Python ETC

Python Upbit : get_balances, get_balance (Upbit 잔고 조회, Upbit 자산 조회)

CosmosProject 2022. 2. 27. 01:02
728x90
반응형

 

 

 

pyupbit를 이용할 때 나의 자산을 조회하려면 get_balances method를 이용합니다.

 

자산 조회부터는 민감한 정보이므로 정상적인 기능을 사용하기 위해선 access key, secret key가 필요합니다.

access key, secret key 발급을 위해선 아래 링크를 참고합시다.

access key, secret key 발급 = https://cosmosproject.tistory.com/491

 

 

 

import pyupbit as ub

access_key = 'access_key'
secret_key = 'secret_key'

upbit = ub.Upbit(access=access_key, secret=secret_key)

my_balance = upbit.get_balances()
print(my_balance)



-- Result
[{'currency': 'KRW', 'balance': '123456.02501415', 'locked': '0.0', 'avg_buy_price': '0', 'avg_buy_price_modified': True, 'unit_currency': 'KRW'}, {'currency': 'BTC', 'balance': '0.01234567', 'locked': '0.0', 'avg_buy_price': '71111111.6623', 'avg_buy_price_modified': False, 'unit_currency': 'KRW'}, {'currency': 'EOS', 'balance': '0.00123456', 'locked': '0.0', 'avg_buy_price': '9215', 'avg_buy_price_modified': False, 'unit_currency': 'KRW'}, {'currency': 'BTG', 'balance': '13.12345678', 'locked': '0.0', 'avg_buy_price': '73156.6138', 'avg_buy_price_modified': False, 'unit_currency': 'KRW'}]

 

- upbit = pb.Upbit(access=access_key, secret=secret_key)

가장 먼저 Upbit 객체를 생성해야합니다.

이때 발급받은 access key와 secret key를 인자로 전달합니다.

 

그리고 get_balances 객체를 사용하면 access key에 해당하는 유저의 자산 정보를 list 속에 dictionary가 담긴 형태로 출력해줍니다.

 

 

 

자산정보를 보기 좋게 출력해봅시다.

 

import pyupbit as ub

access_key = 'access_key'
secret_key = 'secret_key'

upbit = ub.Upbit(access=access_key, secret=secret_key)

my_balance = upbit.get_balances()

for i in my_balance:
    print(i)



-- Result
{'currency': 'KRW', 'balance': '123456.02501415', 'locked': '0.0', 'avg_buy_price': '0', 'avg_buy_price_modified': True, 'unit_currency': 'KRW'}
{'currency': 'BTC', 'balance': '0.01234567', 'locked': '0.0', 'avg_buy_price': '71111111.6623', 'avg_buy_price_modified': False, 'unit_currency': 'KRW'}
{'currency': 'EOS', 'balance': '0.00123456', 'locked': '0.0', 'avg_buy_price': '9215', 'avg_buy_price_modified': False, 'unit_currency': 'KRW'}
{'currency': 'BTG', 'balance': '13.12345678', 'locked': '0.0', 'avg_buy_price': '73156.6138', 'avg_buy_price_modified': False, 'unit_currency': 'KRW'}

현재 보유한 자산 목록에 대해 dictionary의 형태로 출력이 됩니다.

 

각 항목별 의미를 봅시다.

 

currency = 자산 종류 (KRW = 원화, BTC = 비트코인, EOS = 이오스 등등)

 

balance = 보유한 자산 현황 (KRW의 경우 단위는 원이며 코인의 경우 단위는 개수입니다.

위 내용을 보면

원화(KRW)는 123456.02501415원을 보유하고있으며

비트코인(BTC)는 0.01234567개를 보유하고 있습니다.

비트코인골드(BTG)는 13.12345678개를 보유하고 있습니다.

 

avg_buy_price = 평균 단가 (보유한 자산을 구매한 평균 단가입니다.)

BTC의 avg_buy_price는 71111111.6623 입니다. 즉, 소유한 비트코인(BTC)의 평균 단가가 71111111.6623원이라는 의미입니다.

 

unit_currentcy = 평균 단가(avg_buy_price)의 단위입니다.

위 예시에서는 모두 KRW이므로 평균단가 정보의 단위가 모두 원화라는 것으로 받아들이면 됩니다.

 

 

 

 

 

 

 

import pyupbit as ub

access_key = 'access_key'
secret_key = 'secret_key'

upbit = ub.Upbit(access=access_key, secret=secret_key)

my_balance = upbit.get_balance('KRW')
print(my_balance)

my_balance = upbit.get_balance('KRW-BTC')
print(my_balance)

my_balance = upbit.get_balance('KRW-BTG')
print(my_balance)



-- Result
616213.02501415
0.03136546
13.49135273

get_balance method는 ticker를 인자로 받아서 인자로 받은 ticker를 얼마나 보유하고있는지를 return합니다.

 

KRW는 원화 자산을 의미합니다.

 

 

 

 

 

 

728x90
반응형
Comments