달나라 노트

Python Upbit : buy_limit_order, sell_limit_order, cancel_order (지정가 주문, Upbit 주문하기, 코인 구매하기, 코인 주문, 코인 구매, 매도, 매수, 매도 주문, 매수 주문, 주문 취소) 본문

Python/Python ETC

Python Upbit : buy_limit_order, sell_limit_order, cancel_order (지정가 주문, Upbit 주문하기, 코인 구매하기, 코인 주문, 코인 구매, 매도, 매수, 매도 주문, 매수 주문, 주문 취소)

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

 

 

 

buy_limit_order method를 이용하면 코인을 지정가에 구매할 수 있습니다.

sell_limit_order method를 이용하면 코인을 지정가에 판매할 수 있습니다.

 

Syntax

buy_limit_order(ticker='KRW-ETH',
                price=3000000,
                volume=0.003)

ticker = 구매를 원하는 코인의 ticker

price = 구매하고 싶은 가격

volume = 구매할 개수

 

 

sell_limit_order(ticker='KRW-ETH',
                 price=3000000,
                 volume=0.003)

ticker = 판매를 원하는 코인의 ticker

price = 판매하고 싶은 가격

volume = 판매할 개수

 

 

buy_limit_order, sell_limit_order method는 사용법은 완전 동일합니다.

 

 

 

 

 

 


 

 

 

 

buy_limit_order (코인 매수)

 

import pyupbit as ub

access_key = 'access_key'
secret_key = 'secret_key'

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

buy_result = upbit.buy_limit_order(ticker='KRW-ETH',
                                   price=3000000,
                                   volume=0.003)
print(buy_result)



-- Result
{'uuid': 'adrg189adrg48we-w6wf-w6d3-6s1f-ws9e18fg63rs', 'side': 'bid', 'ord_type': 'limit', 'price': '3000000.0', 'state': 'wait', 'market': 'KRW-ETH', 'created_at': '2022-02-27T01:28:54+09:00', 'volume': '0.003', 'remaining_volume': '0.003', 'reserved_fee': '4.5', 'remaining_fee': '4.5', 'paid_fee': '0.0', 'locked': '9004.5', 'executed_volume': '0.0', 'trades_count': 0}

 

위 코드는 매수 예시입니다.

 

- buy_result = upbit.buy_limit_order(ticker='KRW-ETH', price=3000000, volume=0.003)

원화마켓 이더리움(KRW-ETH)을

3000000원에

0.003개 매수하는 주문을 등록하라는 의미입니다.

 

위 코드를 실행한 후 실제 업비트 어플을 켜서 들어가보면 아래처럼 정상적으로 매수 주문이 들어간 것을 알 수 있습니다.

 

 

 

 

buy_limit_order method의 결과로 return되는 dictionary를 좀 더 보기 쉽게 표시하면 아래와 같습니다.

주요한 항목별 의미를 봅시다.

 

{
'uuid': 'adrg189adrg48we-w6wf-w6d3-6s1f-ws9e18fg63rs',
'side': 'bid',
'ord_type': 'limit',
'price': '3000000.0',
'state': 'wait',
'market': 'KRW-ETH',
'created_at': '2022-02-27T01:28:54+09:00',
'volume': '0.003',
'remaining_volume': '0.003',
'reserved_fee': '4.5',
'remaining_fee': '4.5',
'paid_fee': '0.0',
'locked': '9004.5',
'executed_volume': '0.0',
'trades_count': 0
}

uuid = 거래 ID같은 개념이라고 보면 됩니다. 이 ID를 이용해서 걸려있는 매수/매도 주문을 취소할 수도 있습니다.

side = bid인 경우 매수 주문, ask인 경우 매도 주문을 의미합니다.

price = 매수/매도할 가격입니다.

market = 매수/매도할 코인의 ticker입니다.

createdat = 매수/매도 주문이 생성된 시간입니다.

volume = 매수/매도할 개수입니다.

remaining_volume = 남아있는 매수/매도 개수입니다.

 

 

 

 

 

 


 

 

 

 

 

 

sell_limit_order (코인 매도)

 

import pyupbit as ub

access_key = 'access_key'
secret_key = 'secret_key'

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

sell_result = upbit.sell_limit_order(ticker='KRW-BTC', price=70000000, volume=0.01)
print(sell_result)


-- Result
{'uuid': 's6e1f3sd-38w6-d7s4-yu8j-a5e1fg3s86d', 'side': 'ask', 'ord_type': 'limit', 'price': '70000000.0', 'state': 'wait', 'market': 'KRW-BTC', 'created_at': '2022-02-27T01:43:36+09:00', 'volume': '0.01', 'remaining_volume': '0.01', 'reserved_fee': '0.0', 'remaining_fee': '0.0', 'paid_fee': '0.0', 'locked': '0.01', 'executed_volume': '0.0', 'trades_count': 0}

 

 

sell_limit_order도 사용법과 그 결과값은 비슷합니다.

 

 

위 코드를 실행하고 업비트 어플의 거래내역 창을 가보면 아래처럼 정상적으로 등록된 것을 볼 수 있습니다.

 

 

 

 

 

 


 

 

 

 

 

 

 

cancel_order (주문 취소)

 

import pyupbit as ub

access_key = 'access_key'
secret_key = 'secret_key'

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

result = upbit.cancel_order(uuid='e78w6s6f-31s3-u8f5-e85s-s5e1g36s8e')
print(result)

 

주문 취소는 cancel_order method를 이용해서 가능하며

위처럼 그냥 uuid값만 전달해주면 해당 uuid에 대한 매도 주문 또는 매수 주문이 취소됩니다.

 

 

 

 

 

 

728x90
반응형
Comments