-
코인레일 API Python 모듈 공유합니다.programing/봇만들기 2017. 10. 19. 14:24
API를 필요로 하시는 분들에게 유용한 정보를 알려드리려고 합니다. 코인레일에 대해서 포스팅을 할 예정인데 그 이 전에 Python을 이용한 봇을 만들고 싶으신 분들에게 python 소스코드를 공개합니다. 내용은 아래와 같으며, 해당 소스는 github에서 다운로드 받을 수 있습니다.
코인레일
제가 봇을 제작하면서 여러가지 문제점이 있어서 고객센터에 문의를 했는데 친절하게 소스코드와 함께 답변이 와서 여러분들과 함께 공유하고자 합니다. 소스코드는 전적으로 코인레일에서 작성한 내용이며, 본인에게 맞는 형식으로 수정해서 사용하면, 보다 더 유용하게 쓸 수 있을 것입니다.
1. 오더북 조회
123456789import httplib2url = 'https://api.coinrail.co.kr/public/orderbook'currency = 'btc-krw'if __name__ == "__main__":http = httplib2.Http()response, content = http.request(url+'?'+'currency='+currency, 'GET')print contentcs 2. 잔고 조회
123456789101112131415161718192021222324252627282930313233343536373839404142434445import base64import simplejson as jsonimport hashlibimport hmacimport httplib2import timeACCESS_KEY = ''SECRET_KEY = ''def get_encoded_payload(payload):dumped_json = json.dumps(payload)encoded_json = base64.b64encode(dumped_json)return encoded_jsondef get_signature(encoded_payload, secret_key):signature = hmac.new(str(secret_key), str(encoded_payload), hashlib.sha512);return signature.hexdigest()def get_response(url, payload):encoded_payload = get_encoded_payload(payload)headers = {'content-type': 'application/json','X-COINRAIL-PAYLOAD': encoded_payload,'X-COINRAIL-SIGNATURE': get_signature(encoded_payload, SECRET_KEY)}http = httplib2.Http()response, content = http.request(url, 'POST', headers=headers, body=encoded_payload)return contentdef get_balance():url = 'https://api.coinrail.co.kr/balance'payload = {"access_key": ACCESS_KEY,"timestamp" : int(round(time.time() * 1000))}response = get_response(url, payload)print responsecontent = json.loads(response)return contentif __name__ == "__main__":print get_balance()cs 3. 일반 매수
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849import base64import simplejson as jsonimport hashlibimport hmacimport httplib2import timeACCESS_KEY = ''SECRET_KEY = ''currency = 'btc-krw'def get_encoded_payload(payload):dumped_json = json.dumps(payload)encoded_json = base64.b64encode(dumped_json)return encoded_jsondef get_signature(encoded_payload, secret_key):signature = hmac.new(str(secret_key), str(encoded_payload), hashlib.sha512);return signature.hexdigest()def get_response(url, payload):encoded_payload = get_encoded_payload(payload)headers = {'content-type': 'application/json','X-COINRAIL-PAYLOAD': encoded_payload,'X-COINRAIL-SIGNATURE': get_signature(encoded_payload, SECRET_KEY)}http = httplib2.Http()response, content = http.request(url, 'POST', headers=headers, body=encoded_payload)return contentdef limit_buy():url = 'https://api.coinrail.co.kr/order/limit/buy'payload = {"access_key": ACCESS_KEY,"currency": currency,"price" : 4900000,"qty" : 0.1,"timestamp" : int(round(time.time() * 1000))}response = get_response(url, payload)print responsecontent = json.loads(response)return contentif __name__ == "__main__":print limit_buy()cs 4. 일반 매도
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849import base64import simplejson as jsonimport hashlibimport hmacimport httplib2import timeACCESS_KEY = ''SECRET_KEY = ''currency = 'btc-krw'def get_encoded_payload(payload):dumped_json = json.dumps(payload)encoded_json = base64.b64encode(dumped_json)return encoded_jsondef get_signature(encoded_payload, secret_key):signature = hmac.new(str(secret_key), str(encoded_payload), hashlib.sha512);return signature.hexdigest()def get_response(url, payload):encoded_payload = get_encoded_payload(payload)headers = {'content-type': 'application/json','X-COINRAIL-PAYLOAD': encoded_payload,'X-COINRAIL-SIGNATURE': get_signature(encoded_payload, SECRET_KEY)}http = httplib2.Http()response, content = http.request(url, 'POST', headers=headers, body=encoded_payload)return contentdef limit_sell():url = 'https://api.coinrail.co.kr/order/limit/sell'payload = {"access_key": ACCESS_KEY,"currency": currency,"price" : 4900000,"qty" : 0.1,"timestamp" : int(round(time.time() * 1000))}response = get_response(url, payload)print responsecontent = json.loads(response)return contentif __name__ == "__main__":print limit_sell()cs 5. 거래내역 확인
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849import base64import simplejson as jsonimport hashlibimport hmacimport httplib2import timeACCESS_KEY = ''SECRET_KEY = ''currency = 'strat-krw'def get_encoded_payload(payload):dumped_json = json.dumps(payload)encoded_json = base64.b64encode(dumped_json)return encoded_jsondef get_signature(encoded_payload, secret_key):signature = hmac.new(str(secret_key), str(encoded_payload), hashlib.sha512);return signature.hexdigest()def get_response(url, payload):encoded_payload = get_encoded_payload(payload)headers = {'content-type': 'application/json','X-COINRAIL-PAYLOAD': encoded_payload,'X-COINRAIL-SIGNATURE': get_signature(encoded_payload, SECRET_KEY)}http = httplib2.Http()response, content = http.request(url, 'POST', headers=headers, body=encoded_payload)return contentdef get_trade_history():url = 'https://api.coinrail.co.kr/trade/completed'payload = {"access_key": ACCESS_KEY,"currency": currency,"offset" : 0,"count" : 20,"timestamp" : int(round(time.time() * 1000))}response = get_response(url, payload)print responsecontent = json.loads(response)return contentif __name__ == "__main__":print get_trade_history()cs 추후에 API에 관련된 내용과 자동 거래봇을 만드는 방법에 대해서 자세하게 다룰 예정이니 많은 관심 부탁드립니다. 댓글로 부족하거나 잘못된 정보 혹은 궁금한 내용들이 있으면, 해당 주제로 자세하게 포스팅 하겠습니다.
Github에서 소스코드 다운로드 받으세요
'programing > 봇만들기' 카테고리의 다른 글
Poloniexlendingbot 사용 방법 (0) 2018.01.07 텔레그램 봇 Telegram bot / @BotFather 토큰발급 방법 (0) 2017.12.19 폴로닉스 랜딩봇 사용 방법 (0) 2017.12.14 폴로닉스(Poloniex) 입금하기 (0) 2017.11.23 가상화폐 거래소 API를 활용한 봇 만들기 준비 '코인원' (0) 2017.11.09