You are viewing a single comment's thread from:

RE: Python upbit(업빗) 시세 정보 및 프리미엄 계산

in #busy6 years ago (edited)

이 파이썬코드를, 업비트가 아니라 빗썸시세-빗파 시세를 비교해서 프리미엄을 산출하려면 어떻게 코드를 수정해야할까요? ^^; 꼭 좀 알려주세요~!

*하기와 같이 빗썸api형식을 넣고, 그걸로 계산하는 걸로 변경했는데.. 에러가 납니다. 그런데 어디에서 문제인지 모르겠습니다 ㅠㅠ [ #변경함 으로 변경부분은 표시했습니다]

import requests
import json
import threading
import time

headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36'}

https://crix-api-endpoint.upbit.com/v1/crix/trades/days?code=CRIX.UPBIT.KRW-BTC&count=2

def upbit_get_last_trades(name):

url = "https://api.bithumb.com/public/ticker/{CoinName}&count=1".format(CoinName=name) #변경함

try:
    trades = requests.get(url, headers=headers).json()
except:
    print("exception occured at upbit_get_last_trades.")
    return
else:
    return trades[0]

get exchanges krw

https://quotation-api-cdn.dunamu.com/v1/forex/recent?codes=FRX.KRWUSD,FRX.KRWJPY,FRX.KRWCNY,FRX.KRWEUR

def upbit_get_usd_krw():
url = 'https://quotation-api-cdn.dunamu.com/v1/forex/recent?codes=FRX.KRWUSD'
try:
exchange =requests.get(url, headers=headers).json()
except:
print("exception occured at upbit_get_usd_krw()")
return
else:
return exchange[0]['basePrice']

https://crix-api-endpoint.upbit.com/v1/crix/trades/ticks?code=CRIX.UPBIT.KRW-BTC

def get_last_price(coin_name):
url = 'https://crix-api-endpoint.upbit.com/v1/crix/trades/ticks?code=CRIX.UPBIT.KRW-{coin_name}'.format(coin_name=coin_name)
try:
tickValue = requests.get(url, headers=headers).json()
except:
print("exception occured at get_last_price()")
return
else:
return tickValue[0]['tradePrice']

BID float Price of last highest bid

BID_SIZE float Size of the last highest bid

ASK float Price of last lowest ask

ASK_SIZE float Size of the last lowest ask

DAILY_CHANGE float Amount that the last price has changed since yesterday

DAILY_CHANGE_PERC float Amount that the price has changed expressed in percentage terms

LAST_PRICE float Price of the last trade

VOLUME float Daily volume

HIGH float Daily high

LOW float Daily low

def bitfinex_get_ticker(coin_name):
url = "https://api.bitfinex.com/v2/ticker/{coin}".format(coin=coin_name)
try:
info = requests.get(url, headers=headers).json() # [BID ,BID_SIZE, ASK, ASK_SIZE, DAILY_CHANGE, DAILY_CHANGE_PERC, LAST_PRICE, VOLUME, HIGH, LOW]
except:
print("exception occured.")
return
dict_info = {}
keys = [ 'BID', 'BID_SIZE', 'ASK', 'ASK_SIZE', 'DAILY_CHANGE', 'DAILY_CHANGE_PERC', 'LAST_PRICE', 'VOLUME', 'HIGH', 'LOW']
if len(info) != len(keys):
return
idx = 0
for key in keys:
dict_info[key] = info[idx]
idx = idx + 1
return dict_info

def calculation():
# get upbit price
upbit_trade_BTCKRW = upbit_get_last_trades("BTC")
if upbit_trade_BTCKRW is None:
print("try later...")
return False
upbit_trade_ADAKRW = upbit_get_last_trades("ADA")
if upbit_trade_ADAKRW is None:
print("try later...")
return False
upbit_trade_XRPKRW = upbit_get_last_trades("XRP")
if upbit_trade_XRPKRW is None:
print("try later...")
return False
upbit_trade_QTUMKRW = upbit_get_last_trades("QTUM")
if upbit_trade_QTUMKRW is None:
print("try later...")
return False
# get bitfinex price
bitfinexBTCUSD = bitfinex_get_ticker('tBTCUSD')
if bitfinexBTCUSD is None:
print("try later...")
return False
upbit_prevClosing_BTCKRW = upbit_trade_BTCKRW["prevClosingPrice"]
upbit_tradePrice_BTCKRW = upbit_trade_BTCKRW["closing_price"] #변경함
upbit_chg_ratio_BTCKRW = ((upbit_tradePrice_BTCKRW-upbit_prevClosing_BTCKRW)/upbit_prevClosing_BTCKRW) * 100

upbit_prevClosing_ADAKRW =   upbit_trade_ADAKRW["prevClosingPrice"]
upbit_tradePrice_ADAKRW = upbit_trade_ADAKRW["tradePrice"]
upbit_chg_ratio_ADAKRW = ((upbit_tradePrice_ADAKRW-upbit_prevClosing_ADAKRW)/upbit_prevClosing_ADAKRW) * 100

upbit_prevClosing_XRPKRW =   upbit_trade_XRPKRW["prevClosingPrice"]
upbit_tradePrice_XRPKRW = upbit_trade_XRPKRW["tradePrice"]
upbit_chg_ratio_XRPKRW = ((upbit_tradePrice_XRPKRW-upbit_prevClosing_XRPKRW)/upbit_prevClosing_XRPKRW) * 100

upbit_prevClosing_QTUMKRW =   upbit_trade_QTUMKRW["prevClosingPrice"]
upbit_tradePrice_QTUMKRW = upbit_trade_QTUMKRW["tradePrice"]
upbit_chg_ratio_QTUMKRW = ((upbit_tradePrice_QTUMKRW-upbit_prevClosing_QTUMKRW)/upbit_prevClosing_QTUMKRW) * 100


LastBTCUSD = bitfinexBTCUSD['LAST_PRICE']
USDKRW = upbit_get_usd_krw() # from KEB Bank
bitfinex_BTCKRW = LastBTCUSD * USDKRW


# calculate premium
diff = upbit_tradePrice_BTCKRW - bitfinex_BTCKRW
preminum = (diff / bitfinex_BTCKRW) * 100


print("%12s-------------" % time.strftime("%H:%M:%S", time.localtime(upbit_trade_BTCKRW["timestamp"]/1000)))
print("%12s = %s"%('bitfi(USD)', format(LastBTCUSD, ",.2f")))
print("%12s = %s"%('Diff(KRW)', format(diff, ",.2f")))
print("%12s = %.2f %%"%('Preminum', preminum))
print("%12s = %s(%.2f %%)"%('BTC(KRW)', format(upbit_tradePrice_BTCKRW, ",.1f"), upbit_chg_ratio_BTCKRW))
print("%12s = %s(%.2f %%)"%('ADA(KRW)', format( int(upbit_tradePrice_ADAKRW), ",d"), upbit_chg_ratio_ADAKRW))
print("%12s = %s(%.2f %%)"%('XRP(KRW)', format( int(upbit_tradePrice_XRPKRW), ",d"), upbit_chg_ratio_XRPKRW))
print("%12s = %s(%.2f %%)"%('QTUM(KRW)', format( int(upbit_tradePrice_QTUMKRW), ",d"), upbit_chg_ratio_QTUMKRW))
print("----------------------")

return True

def on_timer():
if calculation():
timer = threading.Timer(30, on_timer)
else:
timer = threading.Timer(60, on_timer)
timer.start()

timer = threading.Timer(1, on_timer)
timer.start()

preminum.py
Displaying preminum.py