돈벌고싶다

바이비트 파이썬 API - 최근 거래 데이터 본문

바이비트 python API 뜯어보기

바이비트 파이썬 API - 최근 거래 데이터

coinwithpython 2022. 5. 15. 22:28
728x90
반응형

정의

public trading records 함수. 공식 문서에서는 "Get recent trades." 라 하여, 최근 거래 데이터를 불러온다는 뜻이다. 주가 데이터를 불러오는 쿼리인 Query Kline 와 다른 점은 Public Trading Records의 경우 거래된 가격과 수량을 알려준다는 점이다. 즉 Query Kline은 시간이 기준이지만, Public Trading Records는 거래 발생이 기준이다. 이는 거래량이 기준인 것과는 다르다는 점을 유의해야 한다.

 


 

코드

import pandas as pd
from pybit import usdt_perpetual

session = usdt_perpetual.HTTP("https://api-testnet.bybit.com")
response = session.public_trading_records(symbol="BTCUSDT", limit=1000)['result']
data = pd.DataFrame(response)
data['time'] = pd.to_datetime(data['time'])
data

 

Request Parameters

Parameter Required Type Comment
symbol true string 코인명
limit false int 데이터 수. default 값은 500이며, 최대 1000개까지 가져올 수 있다.

 

Response Parameters

Parameter Type Comment
symbol string 코인명
price number 거래가 성립된 가격
qty number 물량
side string 롱/숏 포지션 포현
time string 거래된 시간(UTC 기준)

 

위 코드를 실행할 경우 output으로 정리된 데이터프레임을 얻을 수 있다.

 


 

결과 시각화 예시

ax = data[data['side']=='Sell'].plot(x="time", y="qty", color="Blue", label="Sell", kind='scatter', figsize=(10, 8))
data[data['side']=='Buy'].plot(x="time", y="qty", color="Red", label="Buy", ax=ax, kind='scatter', figsize=(10, 8))

물량의 시각화

시간 - 물량 그래프이다. 숏의 물량은 파란색으로, 롱의 물량은 빨간색으로 표현하였다.

 

ax = data[data['side']=='Sell'].plot(x="time", y="price", color="Blue", label="Sell", kind='line', figsize=(10, 8))
data[data['side']=='Buy'].plot(x="time", y="price", color="Red", label="Buy", ax=ax, kind='line', figsize=(10, 8))

거래된 가격의 시각화

시간 - 가격 그래프이다. 평균값에서 벗어나는 경우가 매우 빈번한 것을 보아, 시장가(Market) 매매를 진행할 경우 지속적으로 손해를 볼 가능성이 높음을 알 수 있다.

728x90
반응형
Comments