From ef54bac7f3ff8ad25ab8e966ab4fcd4efda90921 Mon Sep 17 00:00:00 2001 From: mjjo53 Date: Thu, 4 Jan 2018 09:40:02 +0900 Subject: [PATCH] initial commit --- .idea/CoinTrader.iml | 12 ++ .idea/misc.xml | 4 + .idea/modules.xml | 8 + .idea/workspace.xml | 354 +++++++++++++++++++++++++++++++++++++++++++ api_test.py | 67 ++++++++ datamgr.py | 12 ++ main.py | 26 ++++ xcoin_api_client.py | 92 +++++++++++ 8 files changed, 575 insertions(+) create mode 100644 .idea/CoinTrader.iml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/workspace.xml create mode 100644 api_test.py create mode 100644 datamgr.py create mode 100644 main.py create mode 100644 xcoin_api_client.py diff --git a/.idea/CoinTrader.iml b/.idea/CoinTrader.iml new file mode 100644 index 0000000..6f63a63 --- /dev/null +++ b/.idea/CoinTrader.iml @@ -0,0 +1,12 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..6f389aa --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..1ff1501 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml new file mode 100644 index 0000000..8497e7c --- /dev/null +++ b/.idea/workspace.xml @@ -0,0 +1,354 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + usec_Time + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1514737524011 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/api_test.py b/api_test.py new file mode 100644 index 0000000..da1ea76 --- /dev/null +++ b/api_test.py @@ -0,0 +1,67 @@ +#! /usr/bin/env python +# XCoin API-call sample script (for Python 3.X) +# +# @author btckorea +# @date 2017-04-11 +# +# +# First, Build and install pycurl with the following commands:: +# (if necessary, become root) +# +# https://pypi.python.org/pypi/pycurl/7.43.0#downloads +# +# tar xvfz pycurl-7.43.0.tar.gz +# cd pycurl-7.43.0 +# python setup.py --libcurl-dll=libcurl.so install +# python setup.py --with-openssl install +# python setup.py install + +import sys +from xcoin_api_client import * +import pprint + + +api_key = "api_connect_key" +api_secret = "api_secret_key" + +api = XCoinAPI(api_key, api_secret) + +rgParams = { + "order_currency": "xrp", + "payment_currency": "KRW" +}; + + +# +# public api +# +# /public/ticker +# /public/recent_ticker +# /public/orderbook +# /public/recent_transactions + +result = api.xcoinApiCall("/public/ticker/" + rgParams["order_currency"], rgParams) +print("status: " + result["status"]) +print("last: " + result["data"]["closing_price"]) +print("sell: " + result["data"]["sell_price"]) +print("buy: " + result["data"]["buy_price"]) + + +# +# private api +# +# endpoint => parameters +# /info/current +# /info/account +# /info/balance +# /info/wallet_address + +#result = api.xcoinApiCall("/info/account", rgParams); +#print("status: " + result["status"]); +#print("created: " + result["data"]["created"]); +#print("account id: " + result["data"]["account_id"]); +#print("trade fee: " + result["data"]["trade_fee"]); +#print("balance: " + result["data"]["balance"]); + +sys.exit(0) + diff --git a/datamgr.py b/datamgr.py new file mode 100644 index 0000000..f2e52a8 --- /dev/null +++ b/datamgr.py @@ -0,0 +1,12 @@ + +class Tick: + def __init__(self): + self.time = None + self.price = None + + +class DataMgr: + def __init__(self): + self.tick_list = None + + def insert(self, time, price, ask): diff --git a/main.py b/main.py new file mode 100644 index 0000000..7641e01 --- /dev/null +++ b/main.py @@ -0,0 +1,26 @@ +from xcoin_api_client import * +import sched +import time + +api_key = "api_connect_key" +api_secret = "api_secret_key" +api = XCoinAPI(api_key, api_secret) + +rgParams = { + "order_currency": "xrp", + "payment_currency": "KRW" +} + +scheduler = sched.scheduler(time.time, time.sleep) +timer_delay = 0.2 + + +def main(arg): + result = api.xcoinApiCall("/public/recent_transactions/" + rgParams["order_currency"], rgParams) + if result is not None: + print("["+time.strftime("%H:%M:%S")+"]" + " status: " + result["status"] + " last: " + result["data"]["closing_price"]) + + scheduler.enter(timer_delay, 1, main, (arg, )) + +scheduler.enter(timer_delay, 1, main, (scheduler, )) +scheduler.run() diff --git a/xcoin_api_client.py b/xcoin_api_client.py new file mode 100644 index 0000000..900f03f --- /dev/null +++ b/xcoin_api_client.py @@ -0,0 +1,92 @@ +# +# XCoin API-call related functions +# +# @author btckorea +# @date 2017-04-12 +# +# Compatible with python3 version. + +import sys +import time +import math +import base64 +import hmac, hashlib +import urllib.parse +import pycurl +import json + + +class XCoinAPI: + api_url = "https://api.bithumb.com" + api_key = "" + api_secret = "" + + def __init__(self, api_key, api_secret): + self.api_key = api_key + self.api_secret = api_secret + self.contents = bytearray() + + def body_callback(self, buf): + self.contents.extend(buf) + + def microtime(self, get_as_float=False): + if get_as_float: + return time.time() + else: + return '%f %d' % math.modf(time.time()) + + def usec_time(self): + mt = self.microtime(False) + mt_array = mt.split(" ")[:2] + return mt_array[1] + mt_array[0][2:5] + + def xcoinApiCall(self, endpoint, rgParams): + # 1. Api-Sign and Api-Nonce information generation. + # 2. Request related information from the Bithumb API server. + # + # - nonce: it is an arbitrary number that may only be used once. + # - api_sign: API signature information created in various combinations values. + + endpoint_item_array = { + "endpoint": endpoint + } + + self.contents = bytearray() + uri_array = dict(endpoint_item_array, **rgParams) # Concatenate the two arrays. + str_data = urllib.parse.urlencode(uri_array) + + nonce = self.usec_time() + + data = endpoint + chr(0) + str_data + chr(0) + nonce + utf8_data = data.encode('utf-8') + + key = self.api_secret + utf8_key = key.encode('utf-8') + + h = hmac.new(bytes(utf8_key), utf8_data, hashlib.sha512) + hex_output = h.hexdigest() + utf8_hex_output = hex_output.encode('utf-8') + + api_sign = base64.b64encode(utf8_hex_output) + utf8_api_sign = api_sign.decode('utf-8') + + curl_handle = pycurl.Curl() + curl_handle.setopt(pycurl.POST, 1) + curl_handle.setopt(pycurl.POSTFIELDS, str_data) + + url = self.api_url + endpoint + curl_handle.setopt(curl_handle.URL, url) + curl_handle.setopt(curl_handle.HTTPHEADER, ['Api-Key: ' + self.api_key, 'Api-Sign: ' + utf8_api_sign, 'Api-Nonce: ' + nonce]) + curl_handle.setopt(curl_handle.WRITEFUNCTION, self.body_callback) + curl_handle.perform() + + # response_code = curl_handle.getinfo(pycurl.RESPONSE_CODE); # Get http response status code. + + curl_handle.close() + + try: + result = json.loads(self.contents.decode('utf-8')) + except: + result = None + + return result