├── README.md └── yobit.py /README.md: -------------------------------------------------------------------------------- 1 | # Python YoBit Binding 2 | 3 | Python binding for YoBit. Use at your own risk! 4 | 5 | Tips are appreciated: BTC 3NoXpUm2EeUWc1jJQhi5X7xKsneN9ReEpQ 6 | 7 | All the comments and descriptions are at [offical YoBit API page](https://yobit.net/en/api) and in the code. -------------------------------------------------------------------------------- /yobit.py: -------------------------------------------------------------------------------- 1 | """ 2 | See https://yobit.net/en/api/ 3 | """ 4 | 5 | import time 6 | import hmac 7 | import hashlib 8 | import json 9 | try: 10 | from urllib import urlencode 11 | from urlparse import urljoin 12 | except ImportError: 13 | from urllib.parse import urlencode 14 | from urllib.parse import urljoin 15 | import requests 16 | 17 | BASE_PUBLIC = 'https://yobit.net/api/3/' 18 | BASE_TRADE = 'https://yobit.net/tapi' 19 | 20 | 21 | def refactor_result(json_ob): 22 | return json.dumps(json_ob, sort_keys=True, indent=4, separators=(',', ': ')) 23 | 24 | 25 | class YoBit(object): 26 | """ 27 | API key and API secret are used for Trade API 28 | """ 29 | 30 | def __init__(self, api_key='', api_secret=''): 31 | self.api_key = str(api_key) if api_key is not None else '' 32 | self.api_secret = str(api_secret) if api_secret is not None else '' 33 | if api_key == '' or api_secret == '': 34 | print('mode: PublicAPI') 35 | else: 36 | print('mode: PublicAPI & TradeAPI') 37 | 38 | @staticmethod 39 | def __api_query_public(method, pair=None, options=None): 40 | """ 41 | Queries YoBit Public API with given method, pair and options. 42 | 43 | :param method: Query method for getting info from Public API 44 | :type method: str 45 | 46 | :param pair: Pair of currencies, example 'ltc_btc' 47 | :type pair: str 48 | 49 | :param options: Extra options for query 50 | :type options: dict 51 | 52 | :return: JSON response from YoBit Public API 53 | :rtype : dict 54 | """ 55 | if not options: 56 | options = {} 57 | if not pair: 58 | pair = '' 59 | 60 | request_url = BASE_PUBLIC + method 61 | if pair != '': 62 | request_url += '/' + pair.lower() 63 | if options != {}: 64 | request_url += '?' 65 | request_url += urlencode(options) 66 | 67 | return requests.get(request_url).json() 68 | 69 | def __api_query_trade(self, method, options=None): 70 | """ 71 | Queries YoBit Trade API with given method and options. 72 | 73 | :param method: Query method for getting info from Trade API 74 | :type method: str 75 | 76 | :param options: Extra options for query 77 | :type options: dict 78 | 79 | :return: JSON response from YoBit Trade API 80 | :rtype : dict 81 | """ 82 | if not options: 83 | options = {} 84 | 85 | request_url = BASE_TRADE 86 | options['method'] = method 87 | options['nonce'] = str(int(time.time())) 88 | 89 | body = urlencode(options) 90 | 91 | signature = hmac.new(self.api_secret.encode(), body.encode(), hashlib.sha512).hexdigest() 92 | headers = { 93 | 'Content-Type': 'application/x-www-form-urlencoded', 94 | 'Key': self.api_key, 95 | 'Sign': signature 96 | } 97 | 98 | return requests.post(request_url, data=options, headers=headers).json() 99 | 100 | def info(self): 101 | """ 102 | Used to get about server time and coin pares of the YoBit market. 103 | Response contains min_price, max_price, min_amount, and fee for each pair. 104 | 105 | :return: JSON of pairs with info 106 | :rtype : dict 107 | """ 108 | return self.__api_query_public('info') 109 | 110 | def ticker(self, pair): 111 | """ 112 | Used to get statistic data for the last 24 hours for selected pair. 113 | Response contains hight, low, avg, vol, vol_cur, last, buy, sell fields for the pair. 114 | 115 | :param pair: Pair of currencies, example 'ltc_btc' 116 | :type pair: str 117 | 118 | :return: Statistic 119 | :rtype : dict 120 | """ 121 | return self.__api_query_public('ticker', pair) 122 | 123 | def depth(self, pair, limit=150): 124 | """ 125 | Used to get information about lists of active orders for selected pair. 126 | Response contains asks and bids lists for the pair. 127 | 128 | :param pair: Pair of currencies, example 'ltc_btc' 129 | :type pair: str 130 | 131 | :param limit: Size of response (on default 150 to 2000 max) 132 | :type limit: int 133 | 134 | :return: Current information about active orders 135 | :rtype : dict 136 | """ 137 | return self.__api_query_public('depth', pair, {'limit': limit}) 138 | 139 | def trades(self, pair, limit=150): 140 | """ 141 | Used to get information about the last transactions of selected pair. 142 | Response contains type, price, amount, tid, timestamp for each transaction. 143 | 144 | :param pair: Pair of currencies, example 'ltc_btc' 145 | :type pair: str 146 | 147 | :param limit: Size of response (on default 150 to 2000 max) 148 | :type limit: int 149 | 150 | :return: Current information about transactions 151 | :rtype : dict 152 | """ 153 | return self.__api_query_public('trades', pair, {'limit': limit}) 154 | 155 | def get_info(self): 156 | """ 157 | Used to get information about user's balances and priviledges of API-key 158 | as well as server time. Response contains funds, fund_incl_orders, rights, 159 | transaction_count, open_orders, server time. 160 | 161 | :return: JSON with info 162 | :rtype : dict 163 | """ 164 | return self.__api_query_trade('getInfo') 165 | 166 | def trade(self, pair, trade_type, rate, amount): 167 | """ 168 | Used to create new orders for stock exchange trading 169 | 170 | :param pair: Pair of currencies, example 'ltc_btc' 171 | :type pair: str 172 | 173 | :param trade_type: 'buy' or 'sell' 174 | :type trade_type: str 175 | 176 | :param rate: Exchange rate for buying or selling 177 | :type rate: float 178 | 179 | :param amount: Amount of needed for buying or selling 180 | :type amount: float 181 | 182 | :return: Success, info about the order, order_id. 183 | :rtype : dict 184 | """ 185 | return self.__api_query_trade('Trade', {'pair': pair, 'type': trade_type, 'rate': rate, 'amount': amount}) 186 | 187 | def active_orders(self, pair): 188 | """ 189 | Used to get list of user's active orders. 190 | 191 | :param pair: Pair of currencies, example 'ltc_btc' 192 | :type pair: str 193 | 194 | :return: List of orders byu order_id 195 | :rtype : dict 196 | """ 197 | return self.__api_query_trade('ActiveOrders', {'pair': pair}) 198 | 199 | def order_info(self, order_id): 200 | """ 201 | Used to get detailed information about the chosen order. 202 | Response contains pair, type, start_amount, amount, rate, 203 | timestamp_created, status for the order. 204 | 205 | :param order_id: Order ID 206 | :type order_id: int 207 | 208 | :return: JSON of the order 209 | :rtype : dict 210 | """ 211 | return self.__api_query_trade('OrderInfo', {'order_id': order_id}) 212 | 213 | def cancel_order(self, order_id): 214 | """ 215 | Used to cancel the choosen order. 216 | 217 | :param order_id: Order ID 218 | :type order_id: int 219 | 220 | :return: Success and balances active after request 221 | :rtype : dict 222 | """ 223 | return self.__api_query_trade('CancelOrder', {'order_id': order_id}) 224 | 225 | def trade_history(self, pair, from_start=0, count=1000, from_id=0, end_id=100000000000, 226 | order='DESC', since=0, end=time.time() + 1000): 227 | """ 228 | Used to retrieve transaction history. 229 | Response contains list of transactions with pair, type, 230 | amount, rate, order_id, is_your_order and timestamp for each transaction. 231 | 232 | :param pair: Pair of currencies, example 'ltc_btc' 233 | :type pair: str 234 | 235 | :param from_start: Number of transaction from which response starts (default 0) 236 | :type from_start: int 237 | 238 | :param count: Quantity of transactions in response (default 1000) 239 | :type count: int 240 | 241 | :param from_id: ID of transaction from which response start (default 0) 242 | :type from_id: int 243 | 244 | :param end_id: ID of trnsaction at which response finishes (default inf) 245 | :type end_id: int 246 | 247 | :param order: Sorting order, 'ASC' for ascending and 'DESC' for descending 248 | :type order: str 249 | 250 | :param since: The time to start the display (unix time, default 0) 251 | :type since: int 252 | 253 | :param end: The time to end the display (unix time, default inf) 254 | :type end: int 255 | 256 | :return: List of transactions 257 | :rtype : dict 258 | """ 259 | options = { 260 | 'from': from_start, 261 | 'count': count, 262 | 'from_id': from_id, 263 | 'end_id': end_id, 264 | 'order': order, 265 | 'since': since, 266 | 'end': end, 267 | 'pair': pair 268 | } 269 | return self.__api_query_trade('TradeHistory', options) 270 | 271 | def get_deposit_address(self, coin_name, need_new=False): 272 | """ 273 | Used to get deposit address. 274 | 275 | :param coin_name: The name of a coin, example 'BTC' 276 | :type coin_name: str 277 | 278 | :param need_new: True or False 279 | :type need_new: bool 280 | 281 | :return: Wallet address 282 | :rtype : dict 283 | """ 284 | options = {'coinName': coin_name, 'need_new': 1 if need_new else 0} 285 | return self.__api_query_trade('GetDepositAddress', options) 286 | 287 | def withdraw_coins_to_address(self, coin_name, amount, address): 288 | """ 289 | Used to create withdrawal request. 290 | 291 | :param coin_name: The name of a coin, example 'BTC' 292 | :type coin_name: str 293 | 294 | :param amount: Amount to withdraw 295 | :type amount: float 296 | 297 | :param address: Destination address 298 | :type address: str 299 | 300 | :return: Success and server time 301 | :rtype : dict 302 | """ 303 | options = {'coinName': coin_name, 'amount': amount, 'address': address} 304 | return self.__api_query_trade('WithdrawCoinsAddress', options) --------------------------------------------------------------------------------