├── .editorconfig ├── .gitignore ├── LICENSE ├── MANIFEST.in ├── README-dev.rst ├── README.rst ├── btsprice ├── __init__.py ├── bts_price_after_match.py ├── exchanges.py ├── feedapi.py ├── feedprice.py ├── main.py ├── metadata.py ├── misc.py ├── sina.py ├── task_exchanges.py ├── task_pusher.py └── yahoo.py ├── config.json.sample ├── docs ├── Makefile ├── make.bat └── source │ ├── README │ ├── _static │ └── .gitkeep │ ├── conf.py │ └── index.rst ├── pavement.py ├── requirements-dev.txt ├── requirements.txt ├── setup.py ├── tests └── test_main.py └── tox.ini /.editorconfig: -------------------------------------------------------------------------------- 1 | # -*- mode: conf-unix; -*- 2 | 3 | # EditorConfig is awesome: http://EditorConfig.org 4 | 5 | # top-most EditorConfig file 6 | root = true 7 | 8 | # defaults 9 | [*] 10 | insert_final_newline = true 11 | 12 | # 4 space indentation 13 | [*.{ini,py,py.tpl,rst}] 14 | indent_style = space 15 | indent_size = 4 16 | 17 | # 4-width tabbed indentation 18 | [*.{sh,bat.tpl,Makefile.tpl}] 19 | indent_style = tab 20 | indent_size = 4 21 | 22 | # and travis does its own thing 23 | [.travis.yml] 24 | indent_style = space 25 | indent_size = 2 26 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Emacs rope configuration 2 | .ropeproject 3 | .project 4 | .pydevproject 5 | .settings 6 | 7 | # pyenv version file 8 | .python-version 9 | 10 | # Python 11 | *.py[co] 12 | 13 | ## Packages 14 | *.egg 15 | *.egg-info 16 | dist 17 | build 18 | eggs 19 | parts 20 | bin 21 | var 22 | sdist 23 | deb_dist 24 | develop-eggs 25 | .installed.cfg 26 | 27 | ## Installer logs 28 | pip-log.txt 29 | 30 | ## Unit test / coverage reports 31 | .coverage 32 | .tox 33 | 34 | ## Translations 35 | *.mo 36 | 37 | ## paver generated files 38 | /paver-minilib.zip 39 | 40 | config.json 41 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Alt 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | CTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 | THE SOFTWARE. -------------------------------------------------------------------------------- /MANIFEST.in: -------------------------------------------------------------------------------- 1 | # Informational files 2 | include README.rst 3 | include LICENSE 4 | 5 | # Include docs and tests. It's unclear whether convention dictates 6 | # including built docs. However, Sphinx doesn't include built docs, so 7 | # we are following their lead. 8 | graft docs 9 | prune docs/build 10 | graft tests 11 | graft config 12 | 13 | # Exclude any compile Python files (most likely grafted by tests/ directory). 14 | global-exclude *.pyc 15 | 16 | # Setup-related things 17 | include pavement.py 18 | include requirements-dev.txt 19 | include requirements.txt 20 | include setup.py 21 | include tox.ini 22 | -------------------------------------------------------------------------------- /README-dev.rst: -------------------------------------------------------------------------------- 1 | ========================= 2 | get bitshares price 3 | ========================= 4 | 5 | .. image:: https://bitsharestalk.org/BitSharesFinalTM200.png 6 | :target: https://bitsharestalk.org 7 | 8 | This project provides bitshares price 9 | 10 | Project Setup 11 | ============= 12 | 13 | Instructions 14 | ------------ 15 | #. Clone the project :: 16 | 17 | git clone git@github.com:pch957/btsprice.git 18 | cd btsprice 19 | 20 | #. Install the project's development and runtime requirements:: 21 | 22 | sudo pip install -r requirements-dev.txt 23 | 24 | #. Install ``argparse`` package when developing for Python 2.6:: 25 | 26 | sudo pip install argparse 27 | 28 | #. copy config.json.sample to config.json, and change the parameter:: 29 | 30 | cp config.json.sample config.json 31 | vim config.json 32 | 33 | #. Run the tests:: 34 | 35 | paver test_all 36 | 37 | You should see output similar to this:: 38 | 39 | $ paver test_all 40 | ---> pavement.test_all 41 | No style errors 42 | ========================================================================= test session starts ========================================================================== 43 | platform linux2 -- Python 2.7.3[pypy-2.2.1-final] -- pytest-2.5.1 44 | collected 7 items 45 | 46 | tests/test_main.py ....... 47 | 48 | ======================================================================= 7 passed in 0.59 seconds ======================================================================= 49 | ___ _ ___ ___ ___ ___ 50 | | _ \/_\ / __/ __| __| \ 51 | | _/ _ \\__ \__ \ _|| |) | 52 | |_|/_/ \_\___/___/___|___/ 53 | 54 | The substitution performed is rather naive, so some style errors may be reported if the description or name cause lines to be too long. Correct these manually before moving to the next step. If any unit tests fail to pass, please report an issue. 55 | 56 | #. build and install:: 57 | 58 | paver build 59 | sudo paver install 60 | 61 | Supported Python Versions 62 | ========================= 63 | 64 | supports the following versions out of the box: 65 | 66 | * CPython 2.6, 2.7, 3.3 67 | * PyPy 1.9 68 | 69 | CPython 3.0-3.2 may also work but are at this point unsupported. PyPy 2.0.2 is known to work but is not run on Travis-CI. 70 | 71 | Jython_ and IronPython_ may also work, but have not been tested. If there is interest in support for these alternative implementations, please open a feature request! 72 | 73 | .. _Jython: http://jython.org/ 74 | .. _IronPython: http://ironpython.net/ 75 | 76 | Licenses 77 | ======== 78 | The code which makes up this project is licensed under the MIT/X11 license. Feel free to use it in your free software/open-source or proprietary projects. 79 | 80 | Issues 81 | ====== 82 | 83 | Please report any bugs or requests that you have using the GitHub issue tracker! 84 | 85 | Authors 86 | ======= 87 | 88 | * Alt 89 | -------------------------------------------------------------------------------- /README.rst: -------------------------------------------------------------------------------- 1 | ========================= 2 | get bitshares price 3 | ========================= 4 | 5 | .. image:: https://bitsharestalk.org/BitSharesFinalTM200.png 6 | :target: https://bitsharestalk.org 7 | 8 | This project provides bitshares price 9 | 10 | Project Setup 11 | ============= 12 | 13 | Quick Start 14 | ------------ 15 | #. Install:: 16 | 17 | $ python setup.py install --user 18 | 19 | 20 | Configuration 21 | ------------ 22 | todo 23 | 24 | Supported Python Versions 25 | ========================= 26 | 27 | supports the following versions out of the box: 28 | 29 | * CPython 2.6, 2.7, 3.3 30 | * PyPy 1.9 31 | 32 | CPython 3.0-3.2 may also work but are at this point unsupported. PyPy 2.0.2 is known to work but is not run on Travis-CI. 33 | 34 | Jython_ and IronPython_ may also work, but have not been tested. If there is interest in support for these alternative implementations, please open a feature request! 35 | 36 | .. _Jython: http://jython.org/ 37 | .. _IronPython: http://ironpython.net/ 38 | 39 | Licenses 40 | ======== 41 | The code which makes up this project is licensed under the MIT/X11 license. Feel free to use it in your free software/open-source or proprietary projects. 42 | 43 | Issues 44 | ====== 45 | 46 | Please report any bugs or requests that you have using the GitHub issue tracker! 47 | 48 | Authors 49 | ======= 50 | 51 | * Alt 52 | -------------------------------------------------------------------------------- /btsprice/__init__.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """It does cool things""" 3 | 4 | from btsprice import metadata 5 | 6 | 7 | __version__ = metadata.version 8 | __author__ = metadata.authors[0] 9 | __license__ = metadata.license 10 | __copyright__ = metadata.copyright 11 | -------------------------------------------------------------------------------- /btsprice/bts_price_after_match.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | import time 3 | import copy 4 | from math import fabs 5 | from btsprice.misc import get_median 6 | # from pprint import pprint 7 | 8 | 9 | class BTSPriceAfterMatch(object): 10 | 11 | def __init__(self, data): 12 | self.data = data 13 | self.timeout = 600 # order book can't use after 300 seconds 14 | self.order_types = ["bids", "asks"] 15 | self.orderbook = {} 16 | self.global_orderbook = {"bids": [], "asks": []} 17 | self.market_weight = {"poloniex_btc": 1, "btc38_cny": 1} 18 | self.rate_cny = {} 19 | self.callback = None 20 | 21 | def set_weight(self, _market_weight): 22 | self.market_weight = _market_weight 23 | 24 | def set_timeout(self, timeout): 25 | self.timeout = timeout 26 | 27 | def change_order_with_rate(self, _orderbook, rate): 28 | for order_type in self.order_types: 29 | for order in _orderbook[order_type]: 30 | order[0] = order[0] * rate 31 | 32 | def remove_timeout(self, data): 33 | for name in list(data.keys()): 34 | if self.timestamp - data[name]["time"] >= self.timeout: 35 | del data[name] 36 | 37 | def test_valid(self): 38 | valid_price_queue = [] 39 | valid_price_dict = {} 40 | for market in self.market_weight: 41 | if market not in self.orderbook: 42 | continue 43 | if len(self.orderbook[market]["bids"]) == 0 or \ 44 | len(self.orderbook[market]["asks"]) == 0: 45 | continue 46 | _price = ( 47 | self.orderbook[market]["bids"][0][0] + self. 48 | orderbook[market]["asks"][0][0])/2 49 | if self.market_weight[market]: 50 | valid_price_queue.append(_price) 51 | valid_price_dict[market] = _price 52 | valid_price = get_median(valid_price_queue) 53 | for market in list(self.orderbook.keys()): 54 | if market not in valid_price_dict: 55 | del self.orderbook[market] 56 | continue 57 | change = fabs((valid_price_dict[market] - valid_price)/valid_price) 58 | # if offset more than 20%, this market is invalid 59 | if change > 0.2: 60 | del self.orderbook[market] 61 | # all market is invalid if we got valid market less than 2 62 | if len(self.orderbook) < 2: 63 | return False 64 | else: 65 | return True 66 | 67 | def get_rate_cny_usd(self, rate): 68 | rate_list = [] 69 | for source in list(rate): 70 | if 'CNY' in rate[source]["USD"]: 71 | rate_list.append(1/rate[source]["USD"]["CNY"]) 72 | return get_median(rate_list) 73 | 74 | def compute_rate_cny(self): 75 | btc_ticker = self.data["ticker"].copy() 76 | # print(btc_ticker) 77 | self.remove_timeout(btc_ticker) 78 | rate_cny = {"CNY": 1.0} 79 | # rate_cny["BTC"] = price_btc["CNY"] 80 | # rate_cny["USD"] = price_btc["CNY"] / price_btc["USD"] 81 | 82 | rate = self.data["rate"] 83 | if len(rate) == 0: 84 | return 85 | rate_cny["USD"] = self.get_rate_cny_usd(rate) 86 | 87 | # rate_cny["BTC"] = 0.0 88 | # for asset in price_btc: 89 | # rate_cny["BTC"] += price_btc[asset] * rate_cny[asset] 90 | # rate_cny["BTC"] /= len(price_btc) 91 | 92 | rate_source = {} 93 | for quote in ["CNY", "USD"]: 94 | for source in list(rate): 95 | for base in rate[source][quote]: 96 | if base in rate_cny: 97 | continue 98 | if base not in rate_source: 99 | rate_source[base] = [] 100 | _rate = rate[source][quote][base] * rate_cny[quote] 101 | rate_source[base].append(_rate) 102 | for asset in rate_source: 103 | asset_rate = sum(rate_source[asset])/float(len(rate_source[asset])) 104 | for _rate in list(rate_source[asset]): 105 | if fabs((_rate - asset_rate)/asset_rate) > 0.1: 106 | asset_rate = None 107 | break 108 | if asset_rate: 109 | rate_cny[asset] = asset_rate 110 | 111 | rate_cny["TCNY"] = rate_cny["CNY"] 112 | rate_cny["TUSD"] = rate_cny["USD"] 113 | 114 | # price_btc_queue = {"CNY": [], "USD": []} 115 | price_btc_queue = [] 116 | price_btc = {} 117 | for name in btc_ticker: 118 | quote = btc_ticker[name]["quote"] 119 | if quote not in rate_cny: 120 | continue 121 | price_btc_queue.append(btc_ticker[name]["last"] * rate_cny[quote]) 122 | if len(price_btc_queue) == 0: 123 | return 124 | price_btc = get_median(price_btc_queue) 125 | # print(price_btc, price_btc_queue) 126 | rate_cny["BTC"] = price_btc 127 | self.rate_cny = rate_cny 128 | 129 | def update_orderbook(self): 130 | if not self.rate_cny: 131 | return 132 | self.global_orderbook = {"bids": [], "asks": []} 133 | self.orderbook = copy.deepcopy(self.data["orderbook"]) 134 | self.remove_timeout(self.orderbook) 135 | for market in self.orderbook: 136 | change_rate = self.rate_cny[self.orderbook[market]["quote"]] 137 | self.change_order_with_rate(self.orderbook[market], change_rate) 138 | if not self.test_valid(): 139 | return 140 | if self.callback: 141 | self.callback(self.orderbook) 142 | for market in self.orderbook: 143 | for order_type in self.order_types: 144 | self.global_orderbook[order_type].extend( 145 | self.orderbook[market][order_type]) 146 | self.global_orderbook["bids"] = sorted( 147 | self.global_orderbook["bids"], reverse=True) 148 | self.global_orderbook["asks"] = sorted(self.global_orderbook["asks"]) 149 | 150 | def get_spread_orderbook(self, spread=0.01): 151 | order_bids = [] 152 | order_asks = [] 153 | for order in self.global_orderbook["bids"]: 154 | order_bids.append([order[0]*(1 + spread), order[1]]) 155 | for order in self.global_orderbook["asks"]: 156 | order_asks.append([order[0]/(1 + spread), order[1]]) 157 | return order_bids, order_asks 158 | 159 | def get_price_list(self, order_bids, order_asks): 160 | price_list = [] 161 | for order in order_bids: 162 | if order[0] < order_asks[0][0]: 163 | break 164 | if len(price_list) == 0 or order[0] != price_list[-1]: 165 | price_list.append(order[0]) 166 | for order in order_asks: 167 | if order[0] < order_bids[0][0]: 168 | break 169 | if len(price_list) == 0 or order[0] != price_list[-1]: 170 | price_list.append(order[0]) 171 | price_list = sorted(price_list) 172 | return price_list 173 | 174 | def get_match_result(self, order_bids, order_asks, price_list): 175 | bid_volume = ask_volume = 0 176 | median_price = get_median(price_list) 177 | for order in order_bids: 178 | if order[0] < median_price: 179 | break 180 | bid_volume += order[1] 181 | for order in order_asks: 182 | if order[0] > median_price: 183 | break 184 | ask_volume += order[1] 185 | return ([bid_volume, ask_volume, median_price]) 186 | 187 | def compute_price(self, spread=0.01): 188 | self.timestamp = int(time.time()) 189 | self.compute_rate_cny() 190 | self.update_orderbook() 191 | if len(self.global_orderbook["bids"]) == 0 or \ 192 | len(self.global_orderbook["asks"]) == 0: 193 | return [0.0, 0.0, None] 194 | order_bids, order_asks = self.get_spread_orderbook(spread) 195 | price_list = self.get_price_list(order_bids, order_asks) 196 | if len(price_list) < 1: 197 | return [0.0, 0.0, (order_bids[0][0] + order_asks[0][0]) / 2] 198 | match_result = [] 199 | while True: 200 | bid_volume, ask_volume, median_price = self.get_match_result( 201 | order_bids, order_asks, price_list) 202 | # we need to find a price, which can match the max volume 203 | match_result.append([min(bid_volume, ask_volume), 204 | -(bid_volume+ask_volume), median_price]) 205 | if len(price_list) <= 1: 206 | break 207 | if(bid_volume <= ask_volume): 208 | price_list = price_list[:int(len(price_list) / 2)] 209 | else: 210 | price_list = price_list[int(len(price_list) / 2):] 211 | 212 | match_result = sorted(match_result, reverse=True) 213 | # pprint(match_result) 214 | return match_result[0] 215 | 216 | def get_valid_depth(self, price, spread=0.01): 217 | valid_depth = {} 218 | bid_price = price / (1+spread) 219 | ask_price = price * (1+spread) 220 | for market in self.orderbook: 221 | quote = self.orderbook[market]["quote"] 222 | valid_depth[market] = { 223 | "bid_price": bid_price / self.rate_cny[quote], "bid_volume": 0, 224 | "ask_price": ask_price / self.rate_cny[quote], "ask_volume": 0} 225 | for order in sorted(self.orderbook[market]["bids"], reverse=True): 226 | if order[0] < bid_price: 227 | break 228 | # valid_depth[market]["bid_price"] = \ 229 | # order[0] * self.rate_cny[quote] 230 | valid_depth[market]["bid_volume"] += order[1] 231 | for order in sorted(self.orderbook[market]["asks"]): 232 | if order[0] > ask_price: 233 | break 234 | # valid_depth[market]["ask_price"] = \ 235 | # order[0] * self.rate_cny[quote] 236 | valid_depth[market]["ask_volume"] += order[1] 237 | return valid_depth 238 | 239 | if __name__ == "__main__": 240 | import asyncio 241 | from btsprice.task_exchanges import TaskExchanges 242 | exchange_data = {} 243 | task_exchanges = TaskExchanges(exchange_data) 244 | task_exchanges.set_period(20) 245 | loop = asyncio.get_event_loop() 246 | task_exchanges.run_tasks(loop) 247 | 248 | bts_price = BTSPriceAfterMatch(exchange_data) 249 | 250 | @asyncio.coroutine 251 | def task_compute_price(): 252 | yield from asyncio.sleep(1) 253 | while True: 254 | volume, volume_sum, real_price = bts_price.compute_price( 255 | spread=0.01) 256 | if real_price: 257 | valid_depth = bts_price.get_valid_depth( 258 | price=real_price, 259 | spread=0.01) 260 | print( 261 | "price:%.5f CNY/BTS, volume:%.3f" % (real_price, volume)) 262 | print("efficent depth : %s" % valid_depth) 263 | else: 264 | print("can't get valid market order") 265 | yield from asyncio.sleep(10) 266 | loop.create_task(task_compute_price()) 267 | loop.run_forever() 268 | loop.close() 269 | -------------------------------------------------------------------------------- /btsprice/exchanges.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | import json 3 | import asyncio 4 | import aiohttp 5 | import datetime 6 | import time 7 | 8 | 9 | class Exchanges(): 10 | def __init__(self): 11 | header = { 12 | 'User-Agent': 'curl/7.35.0', 13 | 'Accept': '*/*'} 14 | 15 | self.session = aiohttp.ClientSession(headers=header) 16 | self.order_types = ["bids", "asks"] 17 | 18 | @asyncio.coroutine 19 | def orderbook_aex(self, quote="btc", base="bts"): 20 | try: 21 | url = "http://api.aex.com/depth.php" 22 | params = {'c': base, 'mk_type': quote} 23 | response = yield from asyncio.wait_for(self.session.get( 24 | url, params=params), 120) 25 | response = yield from response.read() 26 | result = json.loads(response.decode("utf-8-sig")) 27 | for order_type in self.order_types: 28 | for order in result[order_type]: 29 | order[0] = float(order[0]) 30 | order[1] = float(order[1]) 31 | order_book_ask = sorted(result["asks"]) 32 | order_book_bid = sorted(result["bids"], reverse=True) 33 | return {"bids": order_book_bid, "asks": order_book_ask} 34 | except: 35 | print("Error fetching book from aex!") 36 | 37 | @asyncio.coroutine 38 | def orderbook_bter(self, quote="cny", base="bts"): 39 | try: 40 | url = "http://data.bter.com/api/1/depth/%s_%s" % (base, quote) 41 | response = yield from asyncio.wait_for(self.session.get( 42 | url), 120) 43 | result = yield from response.json() 44 | for order_type in self.order_types: 45 | for order in result[order_type]: 46 | order[0] = float(order[0]) 47 | order[1] = float(order[1]) 48 | order_book_ask = sorted(result["asks"]) 49 | order_book_bid = sorted(result["bids"], reverse=True) 50 | return {"bids": order_book_bid, "asks": order_book_ask} 51 | except: 52 | print("Error fetching book from bter!") 53 | 54 | @asyncio.coroutine 55 | def orderbook_yunbi(self, quote="cny", base="bts"): 56 | try: 57 | url = "https://yunbi.com/api/v2/depth.json" 58 | params = {'market': base+quote, 'limit': 20} 59 | response = yield from asyncio.wait_for(self.session.get( 60 | url, params=params), 120) 61 | result = yield from response.json() 62 | for order_type in self.order_types: 63 | for order in result[order_type]: 64 | order[0] = float(order[0]) 65 | order[1] = float(order[1]) 66 | order_book_ask = sorted(result["asks"]) 67 | order_book_bid = sorted(result["bids"], reverse=True) 68 | time = int(result["timestamp"]) 69 | return { 70 | "bids": order_book_bid, "asks": order_book_ask, "time": time} 71 | except: 72 | print("Error fetching book from yunbi!") 73 | 74 | @asyncio.coroutine 75 | def orderbook_btsbots(self, quote="CNY", base="BTS"): 76 | try: 77 | url = "https://btsbots.com/api/order?max_results=100&where=" 78 | # url = "http://localhost:5000/api/order?max_results=30&where=" 79 | params = "a_s==%s;a_b==%s" % (base, quote) 80 | response = yield from asyncio.wait_for(self.session.get( 81 | url+params), 120) 82 | result = yield from response.json() 83 | order_book_ask = [] 84 | for _o in result["_items"]: 85 | order_book_ask.append([_o['p'], _o['b_s']]) 86 | params = "a_s==%s;a_b==%s" % (quote, base) 87 | response = yield from asyncio.wait_for(self.session.get( 88 | url+params), 120) 89 | result = yield from response.json() 90 | order_book_bid = [] 91 | for _o in result["_items"]: 92 | order_book_bid.append([1/_o['p'], _o['b_b']]) 93 | return { 94 | "bids": order_book_bid, "asks": order_book_ask} 95 | except: 96 | print("Error fetching book from btsbots!") 97 | 98 | @asyncio.coroutine 99 | def orderbook_poloniex(self, quote="btc", base="bts"): 100 | try: 101 | quote = quote.upper() 102 | base = base.upper() 103 | url = "http://poloniex.com/public" 104 | params = { 105 | "command": "returnOrderBook", 106 | "currencyPair": "%s_%s" % (quote, base), 107 | "depth": 150 108 | } 109 | 110 | response = yield from asyncio.wait_for(self.session.get( 111 | url, params=params), 120) 112 | result = yield from response.json() 113 | for order_type in self.order_types: 114 | for order in result[order_type]: 115 | order[0] = float(order[0]) 116 | order[1] = float(order[1]) 117 | order_book_ask = sorted(result["asks"]) 118 | order_book_bid = sorted(result["bids"], reverse=True) 119 | return {"bids": order_book_bid, "asks": order_book_ask} 120 | except Exception as e: 121 | print("Error fetching book from poloniex!") 122 | 123 | @asyncio.coroutine 124 | def orderbook_bittrex(self, quote="btc", base="bts"): 125 | try: 126 | quote = quote.upper() 127 | base = base.upper() 128 | url = "https://bittrex.com/api/v1.1/public/getorderbook" 129 | params = { 130 | "type": "both", 131 | "market": "%s-%s" % (quote, base) 132 | } 133 | 134 | response = yield from asyncio.wait_for(self.session.get( 135 | url, params=params), 120) 136 | result = yield from response.json() 137 | result = result['result'] 138 | order_book_ask = [] 139 | order_book_bid = [] 140 | for order in result['buy']: 141 | order_book_bid.append([float(order['Rate']), float(order['Quantity'])]) 142 | for order in result['sell']: 143 | order_book_ask.append([float(order['Rate']), float(order['Quantity'])]) 144 | order_book_ask = sorted(order_book_ask) 145 | order_book_bid = sorted(order_book_bid, reverse=True) 146 | return {"bids": order_book_bid, "asks": order_book_ask} 147 | except Exception as e: 148 | print("Error fetching book from bittrex!") 149 | 150 | @asyncio.coroutine 151 | def orderbook_zb(self, quote="btc", base="bts"): 152 | try: 153 | quote = quote.lower() 154 | base = base.lower() 155 | url = "http://api.zb.com/data/v1/depth" 156 | params = { 157 | "market": "%s_%s" % (base, quote), 158 | "size": 50 159 | } 160 | response = yield from asyncio.wait_for(self.session.get( 161 | url, params=params), 120) 162 | response = yield from response.read() 163 | result = json.loads(response.decode("utf-8-sig")) 164 | for order_type in self.order_types: 165 | for order in result[order_type]: 166 | order[0] = float(order[0]) 167 | order[1] = float(order[1]) 168 | order_book_ask = sorted(result["asks"]) 169 | order_book_bid = sorted(result["bids"], reverse=True) 170 | return {"bids": order_book_bid, "asks": order_book_ask} 171 | except Exception as e: 172 | print("Error fetching book from zb!") 173 | print(e) 174 | 175 | @asyncio.coroutine 176 | def orderbook_lbank(self, quote="btc", base="bts"): 177 | try: 178 | quote = quote.lower() 179 | base = base.lower() 180 | url = "https://api.lbank.info/v1/depth.do" 181 | params = { 182 | "symbol": "%s_%s" % (base, quote), 183 | "size": 60 184 | } 185 | response = yield from asyncio.wait_for(self.session.get( 186 | url, params=params), 120) 187 | response = yield from response.read() 188 | result = json.loads(response.decode("utf-8-sig")) 189 | for order_type in self.order_types: 190 | for order in result[order_type]: 191 | order[0] = float(order[0]) 192 | order[1] = float(order[1]) 193 | order_book_ask = sorted(result["asks"]) 194 | order_book_bid = sorted(result["bids"], reverse=True) 195 | return {"bids": order_book_bid, "asks": order_book_ask} 196 | except Exception as e: 197 | print("Error fetching book from lbank!") 198 | print(e) 199 | 200 | @asyncio.coroutine 201 | def orderbook_binance(self, quote="btc", base="bts"): 202 | try: 203 | quote = quote.upper() 204 | base = base.upper() 205 | url = "https://www.binance.com/api/v1/depth" 206 | params = { 207 | "symbol": "%s%s" % (base, quote) 208 | } 209 | response = yield from asyncio.wait_for(self.session.get( 210 | url, params=params), 120) 211 | response = yield from response.read() 212 | result = json.loads(response.decode("utf-8-sig")) 213 | for order_type in self.order_types: 214 | for idx, val in enumerate(result[order_type]): 215 | result[order_type][idx] = [float(val[0]), float(val[1])] 216 | order_book_ask = sorted(result["asks"]) 217 | order_book_bid = sorted(result["bids"], reverse=True) 218 | return {"bids": order_book_bid, "asks": order_book_ask} 219 | except Exception as e: 220 | print("Error fetching book from binance!") 221 | print(e) 222 | 223 | @asyncio.coroutine 224 | def orderbook_jubi(self, quote="cny", base="bts"): 225 | try: 226 | quote = quote.lower() 227 | base = base.lower() 228 | url = "https://www.jubi.com/api/v1/depth" 229 | params = { 230 | "coin": "%s" % (base) 231 | } 232 | response = yield from asyncio.wait_for(self.session.get( 233 | url, params=params), 120) 234 | response = yield from response.read() 235 | result = json.loads(response.decode("utf-8-sig")) 236 | for order_type in self.order_types: 237 | for order in result[order_type]: 238 | order[0] = float(order[0]) 239 | order[1] = float(order[1]) 240 | order_book_ask = sorted(result["asks"]) 241 | order_book_bid = sorted(result["bids"], reverse=True) 242 | return {"bids": order_book_bid, "asks": order_book_ask} 243 | except Exception as e: 244 | print("Error fetching book from jubi!") 245 | print(e) 246 | 247 | @asyncio.coroutine 248 | def orderbook_19800(self, quote="cny", base="bts"): 249 | try: 250 | quote = quote.lower() 251 | base = base.lower() 252 | url = "https://www.19800.com/api/v1/depth" 253 | params = { 254 | "market": "%s_%s" % (quote, base) 255 | } 256 | response = yield from asyncio.wait_for(self.session.get( 257 | url, params=params), 120) 258 | response = yield from response.read() 259 | result = json.loads(response.decode("utf-8-sig")) 260 | result = result['data'] 261 | order_book_ask = [] 262 | order_book_bid = [] 263 | for order in result['bids']: 264 | order_book_bid.append([float(order['Price']), float(order['Volume'])]) 265 | for order in result['asks']: 266 | order_book_ask.append([float(order['Price']), float(order['Volume'])]) 267 | order_book_ask = sorted(order_book_ask) 268 | order_book_bid = sorted(order_book_bid, reverse=True) 269 | return {"bids": order_book_bid, "asks": order_book_ask} 270 | except Exception as e: 271 | print("Error fetching book from 19800!") 272 | print(e) 273 | 274 | @asyncio.coroutine 275 | def ticker_btc38(self, quote="cny", base="bts"): 276 | try: 277 | url = "http://api.btc38.com/v1/ticker.php?c=%s&mk_type=%s" % ( 278 | base, quote) 279 | response = yield from asyncio.wait_for(self.session.get(url), 120) 280 | response = yield from response.read() 281 | result = json.loads(response.decode("utf-8-sig")) 282 | _ticker = {} 283 | _ticker["last"] = result['ticker']["last"] 284 | _ticker["vol"] = result['ticker']["vol"] 285 | _ticker["buy"] = result['ticker']["buy"] 286 | _ticker["sell"] = result['ticker']["sell"] 287 | _ticker["low"] = result['ticker']["low"] 288 | _ticker["high"] = result['ticker']["high"] 289 | for key in _ticker: 290 | _ticker[key] = float(_ticker[key]) 291 | _ticker["name"] = "btc38" 292 | return _ticker 293 | except Exception as e: 294 | print("Error fetching ticker from btc38!") 295 | print(e) 296 | 297 | @asyncio.coroutine 298 | def ticker_poloniex(self, quote="USDT", base="BTC"): 299 | try: 300 | 301 | url = "https://poloniex.com/public?command=returnTicker" 302 | response = yield from asyncio.wait_for(self.session.get(url), 120) 303 | response = yield from response.read() 304 | result = json.loads( 305 | response.decode("utf-8-sig"))["%s_%s" % (quote, base)] 306 | _ticker = {} 307 | _ticker["last"] = result["last"] 308 | _ticker["vol"] = result["baseVolume"] 309 | _ticker["buy"] = result["highestBid"] 310 | _ticker["sell"] = result["lowestAsk"] 311 | _ticker["low"] = result["low24hr"] 312 | _ticker["high"] = result["high24hr"] 313 | for key in _ticker: 314 | _ticker[key] = float(_ticker[key]) 315 | _ticker["name"] = "poloniex" 316 | return _ticker 317 | except Exception as e: 318 | print("Error fetching ticker from poloniex!") 319 | print(e) 320 | 321 | @asyncio.coroutine 322 | def ticker_btcchina(self, quote="cny", base="btc"): 323 | try: 324 | url = "https://data.btcchina.com/data/ticker?market=%s%s" % ( 325 | base, quote) 326 | response = yield from asyncio.wait_for(self.session.get(url), 120) 327 | response = yield from response.read() 328 | result = json.loads(response.decode("utf-8-sig")) 329 | _ticker = {} 330 | _ticker["last"] = result['ticker']["last"] 331 | _ticker["vol"] = result['ticker']["vol"] 332 | _ticker["buy"] = result['ticker']["buy"] 333 | _ticker["sell"] = result['ticker']["sell"] 334 | _ticker["low"] = result['ticker']["low"] 335 | _ticker["high"] = result['ticker']["high"] 336 | for key in _ticker: 337 | _ticker[key] = float(_ticker[key]) 338 | _ticker["time"] = int(result['ticker']["date"]) 339 | _ticker["name"] = "btcchina" 340 | return _ticker 341 | except Exception as e: 342 | print("Error fetching ticker from btcchina!") 343 | print(e) 344 | 345 | @asyncio.coroutine 346 | def ticker_huobi(self, base="btc"): 347 | try: 348 | url = "http://api.huobi.com/staticmarket/ticker_%s_json.js" % base 349 | response = yield from asyncio.wait_for(self.session.get(url), 120) 350 | response = yield from response.read() 351 | result = json.loads(response.decode("utf-8-sig")) 352 | _ticker = {} 353 | _ticker["last"] = result['ticker']["last"] 354 | _ticker["vol"] = result['ticker']["vol"] 355 | _ticker["buy"] = result['ticker']["buy"] 356 | _ticker["sell"] = result['ticker']["sell"] 357 | _ticker["low"] = result['ticker']["low"] 358 | _ticker["high"] = result['ticker']["high"] 359 | for key in _ticker: 360 | _ticker[key] = float(_ticker[key]) 361 | _ticker["time"] = int(result['time']) 362 | _ticker["name"] = "huobi" 363 | return _ticker 364 | except Exception as e: 365 | print("Error fetching ticker from huobi!") 366 | print(e) 367 | 368 | @asyncio.coroutine 369 | def ticker_okcoin_cn(self, quote="cny", base="btc"): 370 | try: 371 | url = "https://www.okcoin.cn/api/ticker.do?symbol=%s_%s" % ( 372 | base, quote) 373 | response = yield from asyncio.wait_for(self.session.get(url), 120) 374 | response = yield from response.read() 375 | result = json.loads(response.decode("utf-8-sig")) 376 | _ticker = {} 377 | _ticker["last"] = result['ticker']["last"] 378 | _ticker["vol"] = result['ticker']["vol"] 379 | _ticker["buy"] = result['ticker']["buy"] 380 | _ticker["sell"] = result['ticker']["sell"] 381 | _ticker["low"] = result['ticker']["low"] 382 | _ticker["high"] = result['ticker']["high"] 383 | for key in _ticker: 384 | _ticker[key] = float(_ticker[key]) 385 | _ticker["time"] = int(result['date']) 386 | _ticker["name"] = "okcoin.cn" 387 | return _ticker 388 | except Exception as e: 389 | print("Error fetching ticker from okcoin cn!") 390 | print(e) 391 | 392 | @asyncio.coroutine 393 | def ticker_okcoin_com(self, quote="usd", base="btc"): 394 | try: 395 | url = "https://www.okcoin.com/api/v1/ticker.do?symbol=%s_%s" % ( 396 | base, quote) 397 | response = yield from asyncio.wait_for(self.session.get(url), 120) 398 | response = yield from response.read() 399 | result = json.loads(response.decode("utf-8-sig")) 400 | _ticker = {} 401 | _ticker["last"] = result['ticker']["last"] 402 | _ticker["vol"] = result['ticker']["vol"] 403 | _ticker["buy"] = result['ticker']["buy"] 404 | _ticker["sell"] = result['ticker']["sell"] 405 | _ticker["low"] = result['ticker']["low"] 406 | _ticker["high"] = result['ticker']["high"] 407 | for key in _ticker: 408 | _ticker[key] = float(_ticker[key]) 409 | _ticker["time"] = int(result['date']) 410 | _ticker['name'] = 'okcoin.com' 411 | return _ticker 412 | except Exception as e: 413 | print("Error fetching ticker from okcoin com!") 414 | print(e) 415 | 416 | @asyncio.coroutine 417 | def ticker_gdax(self, quote="usd", base="btc"): 418 | try: 419 | url = "https://api.gdax.com/products/%s-%s/ticker" % ( 420 | base.upper(), quote.upper()) 421 | response = yield from asyncio.wait_for(self.session.get(url), 120) 422 | response = yield from response.read() 423 | result = json.loads(response.decode("utf-8-sig")) 424 | _ticker = {} 425 | _ticker["last"] = result["price"] 426 | _ticker["vol"] = result["volume"] 427 | _ticker["buy"] = result["bid"] 428 | _ticker["sell"] = result["ask"] 429 | for key in _ticker: 430 | _ticker[key] = float(_ticker[key]) 431 | _ticker["low"] = None 432 | _ticker["high"] = None 433 | _ticker["time"] = int( 434 | datetime.datetime.strptime( 435 | result["time"][:19]+"+0000", "%Y-%m-%dT%H:%M:%S%z").timestamp()) 436 | _ticker["name"] = "gdax" 437 | return _ticker 438 | except Exception as e: 439 | print("Error fetching ticker from gdax.com!") 440 | print(e) 441 | 442 | @asyncio.coroutine 443 | def ticker_bitstamp(self, quote="usd", base="btc"): 444 | try: 445 | url = "https://www.bitstamp.net/api/v2/ticker/%s%s" % ( 446 | base, quote) 447 | response = yield from asyncio.wait_for(self.session.get(url), 120) 448 | response = yield from response.read() 449 | result = json.loads(response.decode("utf-8-sig")) 450 | _ticker = {} 451 | _ticker["last"] = result["last"] 452 | _ticker["vol"] = result["volume"] 453 | _ticker["buy"] = result["bid"] 454 | _ticker["sell"] = result["ask"] 455 | _ticker["low"] = result["low"] 456 | _ticker["high"] = result["high"] 457 | for key in _ticker: 458 | _ticker[key] = float(_ticker[key]) 459 | _ticker["time"] = int(result['timestamp']) 460 | _ticker["name"] = "bitstamp" 461 | return _ticker 462 | except Exception as e: 463 | print("Error fetching ticker from bitstamp.net!") 464 | print(e) 465 | 466 | @asyncio.coroutine 467 | def ticker_btce(self, quote="usd", base="btc"): 468 | try: 469 | url = "https://btc-e.com/api/3/ticker/%s_%s" % ( 470 | base, quote) 471 | response = yield from asyncio.wait_for(self.session.get(url), 120) 472 | response = yield from response.read() 473 | result = json.loads(response.decode("utf-8-sig")) 474 | result = result["%s_%s" % (base, quote)] 475 | _ticker = {} 476 | _ticker["last"] = result["last"] 477 | _ticker["vol"] = result["vol_cur"] 478 | _ticker["buy"] = result["buy"] 479 | _ticker["sell"] = result["sell"] 480 | _ticker["low"] = result["low"] 481 | _ticker["high"] = result["high"] 482 | for key in _ticker: 483 | _ticker[key] = float(_ticker[key]) 484 | _ticker["time"] = int(result['updated']) 485 | _ticker["name"] = "btce" 486 | return _ticker 487 | except Exception as e: 488 | print("Error fetching ticker from btc-e.com!") 489 | print(e) 490 | 491 | @asyncio.coroutine 492 | def ticker_bitflyer(self, quote="usd", base="btc"): 493 | try: 494 | quote = quote.upper() 495 | base = base.upper() 496 | url = "https://api.bitflyer.com/v1/ticker?product_code=%s_%s" % ( 497 | base, quote) 498 | response = yield from asyncio.wait_for(self.session.get(url), 120) 499 | response = yield from response.read() 500 | result = json.loads(response.decode("utf-8-sig")) 501 | _ticker = {} 502 | _ticker["last"] = result["ltp"] 503 | for key in _ticker: 504 | _ticker[key] = float(_ticker[key]) 505 | _ticker["time"] = int(time.time()) 506 | _ticker["name"] = "bitflyer_%s" % quote 507 | return _ticker 508 | except Exception as e: 509 | print("Error fetching ticker from bitflyer.com!") 510 | print(e) 511 | 512 | @asyncio.coroutine 513 | def ticker_bitfinex(self, quote="usd", base="btc"): 514 | try: 515 | quote = quote.upper() 516 | base = base.upper() 517 | url = "https://api.bitfinex.com/v2/ticker/t%s%s" % ( 518 | base, quote) 519 | response = yield from asyncio.wait_for(self.session.get(url), 120) 520 | response = yield from response.read() 521 | result = json.loads(response.decode("utf-8-sig")) 522 | _ticker = {} 523 | _ticker["last"] = result[6] 524 | _ticker["vol"] = result[7] 525 | _ticker["buy"] = result[0] 526 | _ticker["sell"] = result[2] 527 | _ticker["low"] = result[9] 528 | _ticker["high"] = result[8] 529 | for key in _ticker: 530 | _ticker[key] = float(_ticker[key]) 531 | _ticker["time"] = int(time.time()) 532 | _ticker["name"] = "bitfinex" 533 | return _ticker 534 | except Exception as e: 535 | print("Error fetching ticker from bitfinex.com!") 536 | print(e) 537 | 538 | @asyncio.coroutine 539 | def ticker_kraken(self, quote="eur", base="btc"): 540 | try: 541 | quote = quote.upper() 542 | base = base.upper() 543 | url = "https://api.kraken.com/0/public/Ticker?pair=%s%s" % ( 544 | base, quote) 545 | response = yield from asyncio.wait_for(self.session.get(url), 120) 546 | response = yield from response.read() 547 | result = json.loads(response.decode("utf-8-sig")) 548 | for key in result['result']: 549 | result = result['result'][key] 550 | _ticker = {} 551 | _ticker["last"] = result['c'][0] 552 | for key in _ticker: 553 | _ticker[key] = float(_ticker[key]) 554 | _ticker["time"] = int(time.time()) 555 | _ticker["name"] = "kraken" 556 | return _ticker 557 | except Exception as e: 558 | print("Error fetching ticker from kraken.com!") 559 | print(e) 560 | 561 | if __name__ == "__main__": 562 | loop = asyncio.get_event_loop() 563 | exchanges = Exchanges() 564 | 565 | @asyncio.coroutine 566 | def run_task(coro, *args): 567 | while True: 568 | result = yield from coro(*args) 569 | print(result) 570 | yield from asyncio.sleep(120) 571 | 572 | tasks = [ 573 | # loop.create_task(run_task(exchanges.orderbook_btsbots)), 574 | # loop.create_task(run_task(exchanges.orderbook_btsbots, "OPEN.BTC", "BTS")), 575 | # loop.create_task(run_task(exchanges.orderbook_aex)) 576 | # loop.create_task(run_task(exchanges.orderbook_lbank, "BTC", "BTS")) 577 | loop.create_task(run_task(exchanges.orderbook_binance)) 578 | # loop.create_task(run_task(exchanges.orderbook_19800)) 579 | # loop.create_task(run_task(exchanges.orderbook_yunbi)), 580 | # loop.create_task(run_task(exchanges.orderbook_poloniex)) 581 | # loop.create_task(run_task(exchanges.ticker_btc38)), 582 | # loop.create_task(run_task(exchanges.ticker_gdax)), 583 | # loop.create_task(run_task(exchanges.ticker_btcchina)), 584 | # loop.create_task(run_task(exchanges.ticker_huobi)), 585 | # loop.create_task(run_task(exchanges.ticker_okcoin_cn)), 586 | # loop.create_task(run_task(exchanges.ticker_okcoin_com)) 587 | # loop.create_task(run_task(exchanges.ticker_bitfinex)), 588 | # loop.create_task(run_task(exchanges.ticker_bitflyer, 'jpy', 'btc')), 589 | # loop.create_task(run_task(exchanges.ticker_bitflyer, "usd", 'btc')) 590 | ] 591 | loop.run_until_complete(asyncio.wait(tasks)) 592 | loop.run_forever() 593 | -------------------------------------------------------------------------------- /btsprice/feedapi.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | ############################################################################### 3 | # 4 | # The MIT License (MIT) 5 | # 6 | # Copyright (c) Tavendo GmbH 7 | # 8 | # Permission is hereby granted, free of charge, to any person obtaining a copy 9 | # of this software and associated documentation files (the "Software"), to deal 10 | # in the Software without restriction, including without limitation the rights 11 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | # copies of the Software, and to permit persons to whom the Software is 13 | # furnished to do so, subject to the following conditions: 14 | # 15 | # The above copyright notice and this permission notice shall be included in 16 | # all copies or substantial portions of the Software. 17 | # 18 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | # THE SOFTWARE. 25 | # 26 | ############################################################################### 27 | 28 | from bts import HTTPRPC 29 | from datetime import datetime 30 | import fractions 31 | 32 | 33 | class FeedApi(object): 34 | def __init__(self, config=None): 35 | self.witnessID = None 36 | self.blackswan = [] 37 | self.feeds = {} 38 | self.my_feeds = {} 39 | self.asset_info = {} 40 | self.init_feed_temple() 41 | self.init_default() 42 | if config: 43 | self.init_config(config) 44 | self.init_chain_info() 45 | 46 | def init_feed_temple(self): 47 | self.feed_temple = { 48 | "settlement_price": { 49 | "quote": { 50 | "asset_id": "1.3.0", 51 | "amount": None 52 | }, 53 | "base": { 54 | "asset_id": None, 55 | "amount": None, 56 | } 57 | }, 58 | "maintenance_collateral_ratio": 1750, 59 | "maximum_short_squeeze_ratio": 1020, 60 | "core_exchange_rate": { 61 | "quote": { 62 | "asset_id": "1.3.0", 63 | "amount": None 64 | }, 65 | "base": { 66 | "asset_id": None, 67 | "amount": None, 68 | } 69 | } 70 | } 71 | self.core_exchange_factor = 1.01 72 | 73 | def init_default(self): 74 | self.asset_list = [ 75 | "BTC", "SILVER", "GOLD", "TRY", "SGD", "HKD", "NZD", "CNY", 76 | "MXN", "CAD", "CHF", "AUD", "GBP", "JPY", "EUR", "USD", "KRW", 77 | "ARS"] 78 | self.alias = {} 79 | self.witness = None 80 | self.password = "" 81 | self.rpc = HTTPRPC("http://localhost:8092") 82 | 83 | def init_config(self, config): 84 | self.asset_list = config["asset_list"] 85 | if "alias" in config: 86 | self.alias = config["alias"] 87 | self.witness = config["witness"] 88 | cli_wallet = config["cli_wallet"] 89 | self.password = cli_wallet["unlock"] 90 | if 'uri' in cli_wallet: 91 | uri = cli_wallet["uri"] 92 | else: 93 | uri = "http://%s:%s" % (cli_wallet["host"], cli_wallet["port"]) 94 | self.rpc = HTTPRPC(uri) 95 | self.feed_temple["maintenance_collateral_ratio"] = \ 96 | config["asset_config"]["default"]["maintenance_collateral_ratio"] 97 | self.feed_temple["maximum_short_squeeze_ratio"] = \ 98 | config["asset_config"]["default"]["maximum_short_squeeze_ratio"] 99 | self.core_exchange_factor = \ 100 | config["asset_config"]["default"]["core_exchange_factor"] 101 | self.custom = config["asset_config"] 102 | 103 | def init_chain_info(self): 104 | if self.witness: 105 | self.witnessID = self.rpc.get_witness( 106 | self.witness)["witness_account"] 107 | self.fetch_asset_info() 108 | self.fetch_feed() 109 | 110 | def encode_feed(self, asset, price, custom={}): 111 | feed_info = self.feed_temple.copy() 112 | feed_info["settlement_price"]["base"]["asset_id"] = \ 113 | self.asset_info[asset]["id"] 114 | feed_info["core_exchange_rate"]["base"]["asset_id"] = \ 115 | self.asset_info[asset]["id"] 116 | if "maintenance_collateral_ratio" in custom: 117 | feed_info["maintenance_collateral_ratio"] = \ 118 | custom["maintenance_collateral_ratio"] 119 | if "maximum_short_squeeze_ratio" in custom: 120 | feed_info["maximum_short_squeeze_ratio"] = \ 121 | custom["maximum_short_squeeze_ratio"] 122 | 123 | quote_precision = self.asset_info["BTS"]["precision"] 124 | base_precision = self.asset_info[asset]["precision"] 125 | price_settle = price * 10**(base_precision - quote_precision) 126 | if "core_exchange_factor" in custom: 127 | core_exchange_factor = custom["core_exchange_factor"] 128 | else: 129 | core_exchange_factor = self.core_exchange_factor 130 | price_rate = price_settle * core_exchange_factor 131 | price_settle = fractions.Fraction.from_float( 132 | price_settle).limit_denominator(100000) 133 | price_rate = fractions.Fraction.from_float( 134 | price_rate).limit_denominator(100000) 135 | 136 | feed_info["settlement_price"]["base"]["amount"] = \ 137 | price_settle.numerator 138 | feed_info["settlement_price"]["quote"]["amount"] = \ 139 | price_settle.denominator 140 | feed_info["core_exchange_rate"]["base"]["amount"] = \ 141 | price_rate.numerator 142 | feed_info["core_exchange_rate"]["quote"]["amount"] = \ 143 | price_rate.denominator 144 | return feed_info 145 | 146 | def get_my_feed(self): 147 | return self.my_feeds 148 | 149 | def fetch_asset_info(self): 150 | for asset in self.asset_list + ["BTS"] + list(self.alias): 151 | a = self.rpc.get_asset(asset) 152 | self.asset_info[asset] = a # resolve SYMBOL 153 | self.asset_info[a["id"]] = a # resolve id 154 | 155 | def is_blackswan(self, asset): 156 | return asset in self.blackswan 157 | 158 | def decode_feed(self, price_info): 159 | base = price_info["base"] 160 | quote = price_info["quote"] 161 | base_precision = self.asset_info[base["asset_id"]]["precision"] 162 | quote_precision = self.asset_info[quote["asset_id"]]["precision"] 163 | base_amount = (float(base["amount"])/10**base_precision) 164 | quote_amount = (float(quote["amount"])/10**quote_precision) 165 | if quote_amount == 0: 166 | return 0 167 | return float(base_amount/quote_amount) 168 | 169 | def fetch_feed(self): 170 | for asset in self.asset_list + list(self.alias): 171 | result = self.rpc.get_bitasset_data(asset) 172 | self.feeds[asset] = self.decode_feed( 173 | result["current_feed"]["settlement_price"]) 174 | self.asset_info[asset]["feed_lifetime_sec"] = \ 175 | result["options"]["feed_lifetime_sec"] 176 | if int(result['settlement_fund']) != 0: 177 | self.blackswan.append(asset) 178 | if not self.witnessID: 179 | continue 180 | for feed in result["feeds"]: 181 | if feed[0] == self.witnessID: 182 | ptimestamp = datetime.strptime( 183 | feed[1][0]+"+0000", "%Y-%m-%dT%H:%M:%S%z").timestamp() 184 | if ptimestamp == 0: 185 | continue 186 | self.my_feeds[asset] = {} 187 | self.my_feeds[asset]["timestamp"] = ptimestamp 188 | self.my_feeds[asset]["price"] = self.decode_feed( 189 | feed[1][1]["settlement_price"]) 190 | 191 | def publish_feed(self, feeds): 192 | wallet_was_unlocked = False 193 | 194 | if self.rpc.is_locked(): 195 | wallet_was_unlocked = True 196 | self.rpc.unlock(self.password) 197 | 198 | handle = self.rpc.begin_builder_transaction() 199 | for asset in feeds: 200 | custom = {} 201 | if asset in self.custom: 202 | custom = self.custom[asset] 203 | feed_info = self.encode_feed(asset, feeds[asset], custom) 204 | self.rpc.add_operation_to_builder_transaction( 205 | handle, [19, { 206 | "asset_id": self.asset_info[asset]["id"], 207 | "feed": feed_info, 208 | "publisher": self.witnessID, 209 | }]) 210 | 211 | # Set fee 212 | self.rpc.set_fees_on_builder_transaction(handle, "1.3.0") 213 | 214 | # Signing and Broadcast 215 | self.rpc.sign_builder_transaction(handle, True) 216 | 217 | if wallet_was_unlocked: 218 | self.rpc.lock() 219 | 220 | if __name__ == '__main__': 221 | feedapi = FeedApi() 222 | print(feedapi.feeds) 223 | print(feedapi.encode_feed("CNY", feedapi.feeds["CNY"])) 224 | -------------------------------------------------------------------------------- /btsprice/feedprice.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | from __future__ import print_function 5 | 6 | import asyncio 7 | from btsprice.task_exchanges import TaskExchanges 8 | from btsprice.task_pusher import TaskPusher 9 | from btsprice.bts_price_after_match import BTSPriceAfterMatch 10 | from btsprice.feedapi import FeedApi 11 | import time 12 | import logging 13 | import logging.handlers 14 | import os 15 | from prettytable import PrettyTable 16 | from math import fabs 17 | import locale 18 | locale.setlocale(locale.LC_ALL, 'C') 19 | 20 | 21 | class FeedPrice(object): 22 | 23 | def __init__(self, config=None): 24 | self.exchange_data = {} 25 | self.init_config(config) 26 | self.bts_price = BTSPriceAfterMatch(self.exchange_data) 27 | self.bts_price.callback = self.change_weight 28 | self.bts_price.set_weight(self.config["market_weight"]) 29 | self.init_tasks() 30 | 31 | self.setup_log() 32 | self.init_mpa_info() 33 | self.sample = self.config["price_limit"]["filter_minute"] / \ 34 | self.config["timer_minute"] 35 | if self.sample < 1: 36 | self.sample = 1 37 | # don't need feedapi if not witness 38 | if self.config["witness"]: 39 | self.feedapi = FeedApi(config) 40 | else: 41 | self.feedapi = None 42 | self.filter_price = None 43 | if 'alias' in self.config: 44 | self.alias = self.config['alias'] 45 | else: 46 | self.alias = {} 47 | 48 | def init_config(self, config): 49 | if config: 50 | self.config = config 51 | return 52 | config = {} 53 | config["witness"] = None 54 | config["pusher"] = {"enable": 0, "user": "", "password": ""} 55 | config["timer_minute"] = 3 56 | config["price_limit"] = { 57 | "change_min": 0.5, "change_max": 50, "spread": 0.01, 58 | "filter_minute": 30} 59 | config["market_weight"] = { 60 | "btsbots_cny": 0.5, "btsbots_usd": 0.5, "btsbots_open.btc": 1, 61 | "poloniex_btc": 1, "yunbi_cny": 1, "btc38_cny": 1, "chbtc_cny": 1} 62 | 63 | self.config = config 64 | 65 | def init_tasks(self): 66 | loop = asyncio.get_event_loop() 67 | # init task_exchanges 68 | task_exchanges = TaskExchanges(self.exchange_data) 69 | task_exchanges.set_period(int(self.config["timer_minute"])*60) 70 | 71 | # init task_pusher 72 | if self.config["pusher"]["enable"]: 73 | topic = "bts.exchanges" 74 | login_info = None 75 | if self.config["pusher"]["user"]: 76 | login_info = self.config["pusher"] 77 | task_pusher = TaskPusher(self.exchange_data) 78 | task_pusher.topic = topic 79 | task_pusher.set_expired(self.config["timer_minute"]*60+30) 80 | if "publish" in self.config["pusher"]: 81 | def publish_data(_type, _name, _data): 82 | # print("publish: %s %s" % (_type, _name)) 83 | task_pusher.pusher.publish(topic, _type, _name, _data) 84 | task_exchanges.handler = publish_data 85 | task_pusher.run_tasks(loop, login_info) 86 | 87 | task_exchanges.run_tasks(loop) 88 | 89 | def setup_log(self): 90 | # Setting up Logger 91 | self.logger = logging.getLogger('bts') 92 | self.logger.setLevel(logging.INFO) 93 | formatter = logging.Formatter( 94 | '%(asctime)s[%(levelname)s]: %(message)s') 95 | fh = logging.handlers.RotatingFileHandler("/tmp/bts_delegate_task.log") 96 | fh.setFormatter(formatter) 97 | self.logger.addHandler(fh) 98 | 99 | def init_mpa_info(self): 100 | peg_asset_list = ["KRW", "BTC", "SILVER", "GOLD", "TRY", 101 | "SGD", "HKD", "RUB", "SEK", "NZD", "CNY", 102 | "MXN", "CAD", "CHF", "AUD", "GBP", "JPY", 103 | "EUR", "USD", "SHENZHEN", "NASDAQC", "NIKKEI", 104 | "HANGSENG", "SHANGHAI", "TCNY", "TUSD", "ARS"] 105 | self.price_queue = {} 106 | for asset in peg_asset_list: 107 | self.price_queue[asset] = [] 108 | self.time_publish_feed = 0 109 | self.adjust_scale = 1.00 110 | 111 | def change_weight(self, orderbook): 112 | for order_type in self.bts_price.order_types: 113 | for market in orderbook: 114 | if market not in self.config["market_weight"]: 115 | _weight = 0.0 116 | else: 117 | _weight = self.config["market_weight"][market] 118 | for _order in orderbook[market][order_type]: 119 | _order[1] *= _weight 120 | 121 | def get_bts_price(self): 122 | # calculate real price 123 | volume, volume_sum, real_price = self.bts_price.compute_price( 124 | spread=self.config["price_limit"]["spread"]) 125 | if real_price is None: 126 | return real_price, volume 127 | self.valid_depth = self.bts_price.get_valid_depth( 128 | price=real_price, 129 | spread=self.config["price_limit"]["spread"]) 130 | self.logger.info("fetch price is %.5f CNY/BTS, volume is %.3f", 131 | real_price, volume) 132 | self.logger.info("efficent depth : %s" % self.valid_depth) 133 | return real_price, volume 134 | 135 | # these MPA's precision is 100, it's too small, 136 | # have to change the price 137 | # but we should fixed these at BTS2.0 138 | def patch_nasdaqc(self, price): 139 | if "SHENZHEN" in price: 140 | price["SHENZHEN"] /= price["CNY"] 141 | if "SHANGHAI" in price: 142 | price["SHANGHAI"] /= price["CNY"] 143 | if "NASDAQC" in price: 144 | price["NASDAQC"] /= price["USD"] 145 | if "NIKKEI" in price: 146 | price["NIKKEI"] /= price["JPY"] 147 | if "HANGSENG" in price: 148 | price["HANGSENG"] /= price["HKD"] 149 | 150 | def price_filter(self, bts_price_in_cny): 151 | self.filter_price = self.get_average_price(bts_price_in_cny) 152 | 153 | def get_median_price(self, bts_price_in_cny): 154 | median_price = {} 155 | for asset in self.price_queue: 156 | if asset not in self.bts_price.rate_cny or \ 157 | self.bts_price.rate_cny[asset] is None: 158 | continue 159 | self.price_queue[asset].append(bts_price_in_cny 160 | / self.bts_price.rate_cny[asset]) 161 | if len(self.price_queue[asset]) > self.sample: 162 | self.price_queue[asset].pop(0) 163 | median_price[asset] = sorted( 164 | self.price_queue[asset])[int(len(self.price_queue[asset]) / 2)] 165 | self.patch_nasdaqc(median_price) 166 | return median_price 167 | 168 | def get_average_price(self, bts_price_in_cny): 169 | average_price = {} 170 | for asset in self.price_queue: 171 | if asset not in self.bts_price.rate_cny or \ 172 | self.bts_price.rate_cny[asset] is None: 173 | continue 174 | self.price_queue[asset].append(bts_price_in_cny 175 | / self.bts_price.rate_cny[asset]) 176 | if len(self.price_queue[asset]) > self.sample: 177 | self.price_queue[asset].pop(0) 178 | average_price[asset] = sum( 179 | self.price_queue[asset])/len(self.price_queue[asset]) 180 | for asset in list(self.alias): 181 | alias = self.alias[asset] 182 | if alias in average_price: 183 | average_price[asset] = average_price[alias] 184 | self.patch_nasdaqc(average_price) 185 | return average_price 186 | 187 | def display_depth(self, volume): 188 | t = PrettyTable([ 189 | "market", "bid price", "bid_volume", "ask price", "ask_volume"]) 190 | t.align = 'r' 191 | t.border = True 192 | for market in sorted(self.valid_depth): 193 | _bid_price = "%.8f" % self.valid_depth[market]["bid_price"] 194 | _bid_volume = "{:,.0f}".format( 195 | self.valid_depth[market]["bid_volume"]) 196 | _ask_price = "%.8f" % self.valid_depth[market]["ask_price"] 197 | _ask_volume = "{:,.0f}".format( 198 | self.valid_depth[market]["ask_volume"]) 199 | t.add_row([ 200 | market, _bid_price, _bid_volume, _ask_price, _ask_volume]) 201 | print(t.get_string()) 202 | 203 | def display_price(self): 204 | t = PrettyTable([ 205 | "asset", "rate(CNY/)", "current(/BTS)", "current(BTS/)", 206 | "median(/BTS)", "median(BTS/)", "my feed"]) 207 | t.align = 'r' 208 | t.border = True 209 | for asset in sorted(self.filter_price): 210 | if asset in self.alias: 211 | _alias = self.alias[asset] 212 | else: 213 | _alias = asset 214 | _rate_cny = "%.3f" % (self.bts_price.rate_cny[_alias]) 215 | _price_bts1 = "%.8f" % self.price_queue[_alias][-1] 216 | _price_bts2 = "%.3f" % (1/self.price_queue[_alias][-1]) 217 | _median_bts1 = "%.8f" % self.filter_price[_alias] 218 | _median_bts2 = "%.3f" % (1/self.filter_price[_alias]) 219 | if self.feedapi and self.feedapi.my_feeds and \ 220 | asset in self.feedapi.my_feeds: 221 | _my_feed = "%.8f" % self.feedapi.my_feeds[asset]["price"] 222 | else: 223 | _my_feed = 'x' 224 | t.add_row([ 225 | asset, _rate_cny, _price_bts1, 226 | _price_bts2, _median_bts1, _median_bts2, _my_feed]) 227 | print(t.get_string()) 228 | 229 | def task_get_price(self): 230 | bts_price, volume = self.get_bts_price() 231 | if bts_price is None or volume <= 0.0: 232 | return 233 | self.price_filter(bts_price) 234 | os.system("clear") 235 | cur_t = time.strftime("%Y/%m/%d %H:%M:%S", time.localtime(time.time())) 236 | print("[%s] efficent price: %.5f CNY/BTS, depth: %s BTS" % ( 237 | cur_t, bts_price, "{:,.0f}".format(volume))) 238 | self.display_depth(volume) 239 | print() 240 | self.display_price() 241 | 242 | def check_publish(self, asset_list, my_feeds, real_price): 243 | need_publish = {} 244 | for asset in asset_list: 245 | if asset not in real_price: 246 | continue 247 | if self.feedapi.is_blackswan(asset): 248 | continue 249 | if asset not in my_feeds: 250 | need_publish[asset] = real_price[asset] 251 | continue 252 | change = fabs(my_feeds[asset]["price"] - real_price[asset]) * \ 253 | 100.0 / my_feeds[asset]["price"] 254 | if change >= self.config["price_limit"]["change_max"]: 255 | continue 256 | if asset not in my_feeds: 257 | need_publish[asset] = real_price[asset] 258 | continue 259 | if time.time() - my_feeds[asset]["timestamp"] > \ 260 | self.feedapi.asset_info[asset]["feed_lifetime_sec"] - 600: 261 | need_publish[asset] = real_price[asset] 262 | continue 263 | if change > self.config["price_limit"]["change_min"]: 264 | need_publish[asset] = real_price[asset] 265 | continue 266 | return need_publish 267 | 268 | def task_publish_price(self): 269 | if not self.config["witness"]: 270 | return 271 | self.feedapi.fetch_feed() 272 | feed_need_publish = self.check_publish( 273 | self.feedapi.asset_list + list(self.alias), 274 | self.feedapi.my_feeds, self.filter_price) 275 | if feed_need_publish: 276 | self.logger.info("publish feeds: %s" % feed_need_publish) 277 | self.feedapi.publish_feed(feed_need_publish) 278 | 279 | @asyncio.coroutine 280 | def run_task(self): 281 | config_timer = int(self.config["timer_minute"])*60 282 | while True: 283 | try: 284 | self.task_get_price() 285 | if self.filter_price: 286 | self.task_publish_price() 287 | except Exception as e: 288 | print(e) 289 | self.logger.exception(e) 290 | if self.filter_price: 291 | timer = config_timer 292 | else: 293 | timer = 3 294 | yield from asyncio.sleep(timer) 295 | 296 | def execute(self): 297 | loop = asyncio.get_event_loop() 298 | loop.create_task(self.run_task()) 299 | loop.run_forever() 300 | loop.close() 301 | 302 | if __name__ == '__main__': 303 | feedprice = FeedPrice() 304 | feedprice.execute() 305 | -------------------------------------------------------------------------------- /btsprice/main.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | """Program entry point""" 4 | 5 | from __future__ import print_function 6 | 7 | import argparse 8 | import sys 9 | 10 | from btsprice import metadata 11 | from btsprice.feedprice import FeedPrice 12 | import json 13 | 14 | 15 | def main(argv): 16 | """Program entry point. 17 | 18 | :param argv: command-line arguments 19 | :type argv: :class:`list` 20 | """ 21 | author_strings = [] 22 | for name, email in zip(metadata.authors, metadata.emails): 23 | author_strings.append('Author: {0} <{1}>'.format(name, email)) 24 | 25 | epilog = ''' 26 | {project} {version} 27 | 28 | {authors} 29 | URL: <{url}> 30 | '''.format( 31 | project=metadata.project, 32 | version=metadata.version, 33 | authors='\n'.join(author_strings), 34 | url=metadata.url) 35 | 36 | arg_parser = argparse.ArgumentParser( 37 | prog=argv[0], 38 | formatter_class=argparse.RawDescriptionHelpFormatter, 39 | description=metadata.description, 40 | epilog=epilog) 41 | arg_parser.add_argument( 42 | '--config', type=argparse.FileType('r'), 43 | help='config file') 44 | arg_parser.add_argument( 45 | '-V', '--version', 46 | action='version', 47 | version='{0} {1}'.format(metadata.project, metadata.version)) 48 | 49 | args = arg_parser.parse_args(args=argv[1:]) 50 | 51 | config_info = {} 52 | if (args.config): 53 | config_info = json.load(args.config) 54 | 55 | feedprice = FeedPrice(config_info) 56 | feedprice.execute() 57 | 58 | return 0 59 | 60 | 61 | def entry_point(): 62 | """Zero-argument entry point for use with setuptools/distribute.""" 63 | raise SystemExit(main(sys.argv)) 64 | 65 | 66 | if __name__ == '__main__': 67 | entry_point() 68 | -------------------------------------------------------------------------------- /btsprice/metadata.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """Project btsprice 3 | 4 | Information describing the project. 5 | """ 6 | 7 | # The package name, which is also the "UNIX name" for the project. 8 | package = 'btsprice' 9 | project = "btsprice" 10 | project_no_spaces = project.replace(' ', '') 11 | version = '0.2.56' 12 | description = 'get price for BitShares' 13 | authors = ['Alt'] 14 | authors_string = ', '.join(authors) 15 | emails = ['pch957@163.com'] 16 | license = 'MIT' 17 | copyright = '2015 ' + authors_string 18 | url = 'https://github.com/pch957/btsprice' 19 | -------------------------------------------------------------------------------- /btsprice/misc.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | 4 | def get_median(prices): 5 | lenth = len(prices) 6 | if lenth == 0: 7 | return None 8 | prices = sorted(prices) 9 | _index = int(lenth / 2) 10 | if lenth % 2 == 0: 11 | median_price = float((prices[_index - 1] + prices[_index])) / 2 12 | else: 13 | median_price = prices[_index] 14 | return median_price 15 | -------------------------------------------------------------------------------- /btsprice/sina.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | import asyncio 3 | import aiohttp 4 | import re 5 | 6 | 7 | def is_float_try(str): 8 | try: 9 | float(str) 10 | return True 11 | except ValueError: 12 | return False 13 | 14 | 15 | class Sina(object): 16 | def __init__(self): 17 | header = { 18 | 'content-type': 'application/json', 19 | 'User-Agent': 'Mozilla/5.0 Gecko/20100101 Firefox/22.0'} 20 | self.session = aiohttp.ClientSession(headers=header) 21 | self.param_s = {} 22 | self.quote = {} 23 | self.scale = {} 24 | self.init_param_dict1() 25 | self.init_param_dict2() 26 | self.rate = {'CNY': {'CNY': 1.0}, 'USD': {'USD': 1.0}} 27 | 28 | def init_param_dict1(self): 29 | assets = ["CNY", "KRW", "TRY", "SGD", "HKD", "RUB", "SEK", "NZD", 30 | "MXN", "CAD", "CHF", "AUD", "GBP", "JPY", "EUR", "ARS"] 31 | for asset in assets: 32 | self.param_s[asset] = "fx_s%susd" % asset.lower() 33 | 34 | self.param_s["GOLD"] = "hf_XAU" 35 | self.param_s["SILVER"] = "hf_XAG" 36 | for asset in self.param_s: 37 | self.quote[asset] = "USD" 38 | 39 | def init_param_dict2(self): 40 | # todo:"OIL", GAS", "DIESEL" 41 | self.param_s["SHENZHEN"] = 'sz399106' 42 | self.quote["SHENZHEN"] = "CNY" 43 | self.param_s["SHANGHAI"] = 'sh000001' 44 | self.quote["SHANGHAI"] = "CNY" 45 | 46 | def get_query_param(self, assets): 47 | query_string = ','.join( 48 | '%s' % (self.param_s[asset]) for asset in assets) 49 | return query_string 50 | 51 | @asyncio.coroutine 52 | def fetch_price(self, assets=None): 53 | if assets is None: 54 | assets = self.param_s.keys() 55 | url = "http://hq.sinajs.cn/list=" 56 | try: 57 | params = self.get_query_param(assets) 58 | response = yield from asyncio.wait_for(self.session.get( 59 | url+params), 120) 60 | response = yield from response.read() 61 | # print(response) 62 | price_info = dict(zip(assets, response.decode("gbk").splitlines())) 63 | price = {} 64 | for asset in assets: 65 | pattern = re.compile(r'"(.*)"') 66 | # print(price_info[asset]) 67 | data = pattern.findall(price_info[asset])[0] 68 | if self.param_s[asset][:3] == "hf_": 69 | price[asset] = data.split(',')[0] 70 | elif self.param_s[asset][:3] == "fx_": 71 | price[asset] = data.split(',')[1] 72 | else: 73 | price[asset] = data.split(',')[3] 74 | if is_float_try(price[asset]) and float(price[asset]) > 0.0: 75 | scale = 1.0 76 | if asset in self.scale: 77 | scale = self.scale[asset] 78 | if self.quote[asset] == "CNY": 79 | self.rate["CNY"][asset] = float(price[asset]) 80 | elif self.quote[asset] == "USD": 81 | self.rate["USD"][asset] = float(price[asset]) 82 | else: 83 | self.rate["USD"][asset] = float(price[asset]) * \ 84 | float(price[self.quote[asset]]) * scale 85 | # need throw a exception is not float 86 | else: 87 | raise 88 | except Exception as e: 89 | print("Error fetching results from sina!", e) 90 | # print(self.rate) 91 | return self.rate 92 | 93 | if __name__ == "__main__": 94 | loop = asyncio.get_event_loop() 95 | sina = Sina() 96 | loop.run_until_complete(sina.fetch_price()) 97 | loop.run_forever() 98 | -------------------------------------------------------------------------------- /btsprice/task_exchanges.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | from btsprice.exchanges import Exchanges 3 | from btsprice.yahoo import Yahoo 4 | from btsprice.sina import Sina 5 | import time 6 | import asyncio 7 | 8 | 9 | class TaskExchanges(object): 10 | def __init__(self, data={}): 11 | self.period = 120 12 | self.exchanges = Exchanges() 13 | self.yahoo = Yahoo() 14 | self.sina = Sina() 15 | self.handler = None 16 | data_type = ["orderbook", "ticker", "rate"] 17 | for _type in data_type: 18 | if _type not in data: 19 | data[_type] = {} 20 | self.data = data 21 | 22 | def set_period(self, sec): 23 | self.period = sec 24 | 25 | @asyncio.coroutine 26 | def fetch_orderbook(self, name, quote, coro, *args): 27 | time_end = int(time.time()) 28 | orderbook = self.data["orderbook"] 29 | while True: 30 | time_begin = time_end 31 | _orderbook = yield from coro(*args) 32 | time_end = int(time.time()) 33 | if _orderbook: 34 | orderbook[name] = _orderbook 35 | orderbook[name]["quote"] = quote 36 | if "time" not in _orderbook: 37 | orderbook[name]["time"] = time_end 38 | if self.handler: 39 | self.handler("orderbook", name, orderbook[name]) 40 | time_left = self.period - (time_end - time_begin) 41 | if time_left <= 1: 42 | time_left = 1 43 | time_end += time_left 44 | yield from asyncio.sleep(time_left) 45 | 46 | @asyncio.coroutine 47 | def fetch_ticker(self, name, quote, coro, *args): 48 | time_end = int(time.time()) 49 | ticker = self.data["ticker"] 50 | while True: 51 | time_begin = time_end 52 | _ticker = yield from coro(*args) 53 | time_end = int(time.time()) 54 | if _ticker: 55 | _ticker["quote"] = quote 56 | if "time" not in _ticker: 57 | _ticker["time"] = time_end 58 | ticker[name] = _ticker 59 | if self.handler: 60 | self.handler("ticker", name, _ticker) 61 | time_left = self.period - (time_end - time_begin) 62 | if time_left <= 1: 63 | time_left = 1 64 | time_end += time_left 65 | yield from asyncio.sleep(time_left) 66 | 67 | @asyncio.coroutine 68 | def fetch_yahoo_rate(self): 69 | time_end = int(time.time()) 70 | rate = self.data["rate"] 71 | while True: 72 | time_begin = time_end 73 | _rate = yield from self.yahoo.fetch_price() 74 | time_end = int(time.time()) 75 | if _rate: 76 | _rate["time"] = time_end 77 | rate["yahoo"] = _rate 78 | if self.handler: 79 | self.handler("rate", "yahoo", _rate) 80 | time_left = self.period - (time_end - time_begin) 81 | if time_left <= 1: 82 | time_left = 1 83 | time_end += time_left 84 | yield from asyncio.sleep(time_left) 85 | 86 | @asyncio.coroutine 87 | def fetch_sina_rate(self): 88 | time_end = int(time.time()) 89 | rate = self.data["rate"] 90 | while True: 91 | time_begin = time_end 92 | _rate = yield from self.sina.fetch_price() 93 | time_end = int(time.time()) 94 | if _rate: 95 | _rate["time"] = time_end 96 | rate["Sina"] = _rate 97 | if self.handler: 98 | self.handler("rate", "Sina", _rate) 99 | time_left = self.period - (time_end - time_begin) 100 | if time_left <= 1: 101 | time_left = 1 102 | time_end += time_left 103 | yield from asyncio.sleep(time_left) 104 | 105 | def run_tasks_ticker(self, loop): 106 | return [ 107 | loop.create_task(self.fetch_ticker( 108 | "poloniex", "USD", 109 | self.exchanges.ticker_poloniex, "USDT", "BTC")), 110 | # loop.create_task(self.fetch_ticker( 111 | # "btce", "USD", 112 | # self.exchanges.ticker_btce, "usd", "btc")), 113 | loop.create_task(self.fetch_ticker( 114 | "bitstamp", "USD", 115 | self.exchanges.ticker_bitstamp, "usd", "btc")), 116 | loop.create_task(self.fetch_ticker( 117 | "gdax", "USD", 118 | self.exchanges.ticker_gdax, "usd", "btc")), 119 | # loop.create_task(self.fetch_ticker( 120 | # "btcchina", "CNY", 121 | # self.exchanges.ticker_btcchina, "cny", "btc")), 122 | # loop.create_task(self.fetch_ticker( 123 | # "huobi", "CNY", 124 | # self.exchanges.ticker_huobi, "btc")), 125 | # loop.create_task(self.fetch_ticker( 126 | # "okcoin_cn", "CNY", 127 | # self.exchanges.ticker_okcoin_cn, "cny", "btc")), 128 | loop.create_task(self.fetch_ticker( 129 | "okcoin_com", "USD", 130 | self.exchanges.ticker_okcoin_com, "usd", "btc")), 131 | loop.create_task(self.fetch_ticker( 132 | "bitfinex", "USD", 133 | self.exchanges.ticker_bitfinex, "usd", "btc")), 134 | loop.create_task(self.fetch_ticker( 135 | "kraken", "EUR", 136 | self.exchanges.ticker_kraken, "eur", "btc")), 137 | loop.create_task(self.fetch_ticker( 138 | "bitflyer_usd", "USD", 139 | self.exchanges.ticker_bitflyer, "usd", "btc")), 140 | loop.create_task(self.fetch_ticker( 141 | "bitflyer_jpy", "JPY", 142 | self.exchanges.ticker_bitflyer, "jpy", "btc")), 143 | ] 144 | 145 | def run_tasks_orderbook(self, loop): 146 | return [ 147 | loop.create_task(self.fetch_orderbook( 148 | "btsbots_cny", "CNY", 149 | self.exchanges.orderbook_btsbots, "CNY", "BTS")), 150 | loop.create_task(self.fetch_orderbook( 151 | "btsbots_usd", "USD", 152 | self.exchanges.orderbook_btsbots, "USD", "BTS")), 153 | loop.create_task(self.fetch_orderbook( 154 | "btsbots_open.btc", "BTC", 155 | self.exchanges.orderbook_btsbots, "OPEN.BTC", "BTS")), 156 | loop.create_task(self.fetch_orderbook( 157 | "aex_btc", "BTC", 158 | self.exchanges.orderbook_aex, "btc", "bts")), 159 | loop.create_task(self.fetch_orderbook( 160 | "zb_btc", "BTC", 161 | self.exchanges.orderbook_zb, "btc", "bts")), 162 | loop.create_task(self.fetch_orderbook( 163 | "zb_usdt", "USD", 164 | self.exchanges.orderbook_zb, "usdt", "bts")), 165 | loop.create_task(self.fetch_orderbook( 166 | "lbank_btc", "BTC", 167 | self.exchanges.orderbook_lbank, "btc", "bts")), 168 | loop.create_task(self.fetch_orderbook( 169 | "binance_btc", "BTC", 170 | self.exchanges.orderbook_binance, "btc", "bts")), 171 | loop.create_task(self.fetch_orderbook( 172 | "poloniex_btc", "BTC", 173 | self.exchanges.orderbook_poloniex, "btc", "bts")) 174 | # loop.create_task(self.fetch_orderbook( 175 | # "yunbi_cny", "CNY", 176 | # self.exchanges.orderbook_yunbi, "cny", "bts")), 177 | # loop.create_task(self.fetch_orderbook( 178 | # "jubi_cny", "CNY", 179 | # self.exchanges.orderbook_jubi, "cny", "bts")), 180 | # loop.create_task(self.fetch_orderbook( 181 | # "19800_cny", "CNY", 182 | # self.exchanges.orderbook_19800, "cny", "bts")), 183 | # loop.create_task(self.fetch_orderbook( 184 | # "bittrex_btc", "BTC", 185 | # self.exchanges.orderbook_bittrex, "btc", "bts")), 186 | ] 187 | 188 | def run_tasks(self, loop): 189 | return [ 190 | loop.create_task(self.fetch_yahoo_rate()), 191 | loop.create_task(self.fetch_sina_rate()) 192 | ] + \ 193 | self.run_tasks_orderbook(loop) + \ 194 | self.run_tasks_ticker(loop) 195 | 196 | 197 | if __name__ == "__main__": 198 | loop = asyncio.get_event_loop() 199 | task_exchanges = TaskExchanges() 200 | task_exchanges.set_period(20) 201 | tasks = task_exchanges.run_tasks(loop) 202 | 203 | @asyncio.coroutine 204 | def task_display(): 205 | my_data = task_exchanges.data 206 | while True: 207 | for _type in my_data: 208 | for _name in my_data[_type]: 209 | if "done" not in my_data[_type][_name]: 210 | print("got %s: %s" % (_type, _name)) 211 | my_data[_type][_name]["done"] = None 212 | yield from asyncio.sleep(1) 213 | tasks += [loop.create_task(task_display())] 214 | loop.run_until_complete(asyncio.wait(tasks)) 215 | loop.run_forever() 216 | loop.close() 217 | -------------------------------------------------------------------------------- /btsprice/task_pusher.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | import asyncio 4 | from btspusher import Pusher 5 | import time 6 | 7 | 8 | class TaskPusher(object): 9 | def __init__(self, data={}): 10 | self.expired = 150 11 | self.topic = "bts.exchanges" 12 | data_type = ["orderbook", "ticker", "rate"] 13 | for _type in data_type: 14 | if _type not in data: 15 | data[_type] = {} 16 | self.data = data 17 | 18 | def run_tasks(self, loop, login_info=None): 19 | def onData(_type, _name, _data, *args, **kwargs): 20 | if not _type or not _name or not _data: 21 | return 22 | if _type not in self.data: 23 | return 24 | _time = int(time.time()) 25 | # only update the data which is expired 26 | if _name in self.data[_type] and \ 27 | _time - self.data[_type][_name]["time"] < self.expired: 28 | return 29 | # print("use:", _type, _name) 30 | self.data[_type][_name] = _data 31 | self.pusher = Pusher(loop, login_info) 32 | self.pusher.sync_subscribe(onData, self.topic) 33 | 34 | def set_expired(self, sec): 35 | self.expired = sec 36 | 37 | 38 | if __name__ == "__main__": 39 | exchange_data = {} 40 | task_pusher = TaskPusher(exchange_data) 41 | topic = "public.exchanges" 42 | 43 | def publish_data(_type, _name, _data): 44 | print("publish: %s %s" % (_type, _name)) 45 | task_pusher.pusher.publish(topic, _type, _name, _data) 46 | 47 | from btsprice.task_exchanges import TaskExchanges 48 | task_exchanges = TaskExchanges(exchange_data) 49 | task_exchanges.handler = publish_data 50 | task_exchanges.set_period(20) 51 | 52 | loop = asyncio.get_event_loop() 53 | 54 | task_pusher.topic = topic 55 | task_pusher.run_tasks(loop) 56 | task_exchanges.run_tasks(loop) 57 | 58 | loop.run_forever() 59 | loop.close() 60 | -------------------------------------------------------------------------------- /btsprice/yahoo.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | import asyncio 3 | import aiohttp 4 | 5 | 6 | def is_float_try(str): 7 | try: 8 | float(str) 9 | return True 10 | except ValueError: 11 | return False 12 | 13 | 14 | class Yahoo(object): 15 | def __init__(self): 16 | header = { 17 | 'content-type': 'application/json', 18 | 'User-Agent': 'Mozilla/5.0 Gecko/20100101 Firefox/22.0'} 19 | self.session = aiohttp.ClientSession(headers=header) 20 | self.param_s = {} 21 | self.quote = {} 22 | self.scale = {} 23 | self.init_param_dict1() 24 | self.init_param_dict2() 25 | self.init_param_dict3() 26 | self.rate = {'CNY': {'CNY': 1.0}, 'USD': {'USD': 1.0}} 27 | 28 | def init_param_dict1(self): 29 | assets = ["CNY", "KRW", "TRY", "SGD", "HKD", "RUB", "SEK", "NZD", 30 | "MXN", "CAD", "CHF", "AUD", "GBP", "JPY", "EUR", "BTC", "ARS"] 31 | for asset in assets: 32 | self.param_s[asset] = asset + "USD=X" 33 | 34 | # todo, GOLD/SILVER wrong from yahoo 35 | # self.param_s["GOLD"] = "XAUUSD=X" 36 | # self.param_s["SILVER"] = "XAGUSD=X" 37 | for asset in self.param_s: 38 | self.quote[asset] = "USD" 39 | 40 | def init_param_dict2(self): 41 | # todo:"OIL", GAS", "DIESEL" 42 | self.param_s["SHENZHEN"] = '399106.SZ' 43 | self.quote["SHENZHEN"] = "CNY" 44 | # todo, wrong from yahoo 45 | # self.param_s["SHANGHAI"] = '000001.SS' 46 | # self.quote["SHANGHAI"] = "CNY" 47 | self.param_s["NASDAQC"] = '^IXIC' 48 | self.quote["NASDAQC"] = "USD" 49 | self.param_s["NIKKEI"] = '^N225' 50 | self.quote["NIKKEI"] = "JPY" 51 | self.param_s["HANGSENG"] = '^HSI' 52 | self.quote["HANGSENG"] = "HKD" 53 | 54 | def init_param_dict3(self): 55 | self.param_s["BDR.AAPL"] = 'AAPL' 56 | self.quote["BDR.AAPL"] = "USD" 57 | self.scale["BDR.AAPL"] = 0.001 58 | 59 | def get_query_param(self, assets): 60 | query_string = ','.join( 61 | '%s' % (self.param_s[asset]) for asset in assets) 62 | params = {'s': query_string, 'f': 'l1', 'e': '.csv'} 63 | return params 64 | 65 | @asyncio.coroutine 66 | def fetch_price(self, assets=None): 67 | if assets is None: 68 | assets = self.param_s.keys() 69 | url = "http://download.finance.yahoo.com/d/quotes.csv" 70 | try: 71 | params = self.get_query_param(assets) 72 | response = yield from asyncio.wait_for(self.session.get( 73 | url, params=params), 120) 74 | response = yield from response.read() 75 | price = dict(zip(assets, response.split())) 76 | for asset in assets: 77 | if is_float_try(price[asset]): 78 | scale = 1.0 79 | if asset in self.scale: 80 | scale = self.scale[asset] 81 | if self.quote[asset] == "CNY": 82 | self.rate["CNY"][asset] = float(price[asset]) 83 | elif self.quote[asset] == "USD": 84 | self.rate["USD"][asset] = float(price[asset]) 85 | else: 86 | self.rate["USD"][asset] = float(price[asset]) * \ 87 | float(price[self.quote[asset]]) * scale 88 | # need throw a exception is not float 89 | else: 90 | raise 91 | # there is a bug for yahoo api.... 92 | if asset == "GOLD" or asset == "SILVER": 93 | if self.rate["USD"][asset] < 1: 94 | self.rate["USD"][asset] = 1/self.rate["USD"][asset] 95 | except Exception as e: 96 | print("Error fetching results from yahoo!", e) 97 | # print(self.rate) 98 | return self.rate 99 | 100 | if __name__ == "__main__": 101 | loop = asyncio.get_event_loop() 102 | yahoo = Yahoo() 103 | loop.run_until_complete(yahoo.fetch_price()) 104 | loop.run_forever() 105 | -------------------------------------------------------------------------------- /config.json.sample: -------------------------------------------------------------------------------- 1 | { 2 | "witness": "", 3 | "timer_minute": 2, 4 | "asset_list": [ 5 | "BTC", "SILVER", "GOLD", "TRY", "SGD", "HKD", "NZD", "CNY", 6 | "MXN", "CAD", "CHF", "AUD", "GBP", "JPY", "EUR", "USD", "KRW", "TUSD", "ARS"], 7 | "alias": { 8 | "RUBLE": "RUB" 9 | }, 10 | "cli_wallet": { 11 | "host" : "localhost", 12 | "port" : 8092, 13 | "user" : "", 14 | "passwd" : "", 15 | "unlock" : "" 16 | }, 17 | "price_limit": { 18 | "common": "only update price feed when the change rate is between 1% ~ 50%", 19 | "change_min": 0.5, 20 | "change_max": 50, 21 | "common": "spread bid price to price*(1+spread), ask price to price*(1-spread)", 22 | "common": "excute the order book, get the real price", 23 | "spread": 0.01, 24 | "common": "we use the average price in this time", 25 | "filter_minute": 30 26 | }, 27 | "market_weight": { 28 | "btsbots_cny": 1.0, 29 | "btsbots_usd": 1.0, 30 | "btsbots_open.btc": 1.0, 31 | "aex_btc": 1.0, 32 | "zb_usdt": 1.0, 33 | "zb_btc": 1.0, 34 | "lbank_btc": 1.0, 35 | "binance_btc": 1.0, 36 | "poloniex_btc": 1.0 37 | }, 38 | "asset_config": { 39 | "default": { 40 | "core_exchange_factor": 1.20, 41 | "maintenance_collateral_ratio": 1750, 42 | "maximum_short_squeeze_ratio": 1100 43 | }, 44 | "TUSD": {"maximum_short_squeeze_ratio": 1050} 45 | }, 46 | "pusher": { 47 | "common": "set enable to 1, if you want to subscribe data from pusher service", 48 | "enable": "1", 49 | "user": "", 50 | "password": "" 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /docs/Makefile: -------------------------------------------------------------------------------- 1 | # Makefile for Sphinx documentation 2 | # 3 | 4 | # You can set these variables from the command line. 5 | SPHINXOPTS = -W 6 | SPHINXBUILD = sphinx-build 7 | PAPER = 8 | BUILDDIR = build 9 | 10 | # Internal variables. 11 | PAPEROPT_a4 = -D latex_paper_size=a4 12 | PAPEROPT_letter = -D latex_paper_size=letter 13 | ALLSPHINXOPTS = -d $(BUILDDIR)/doctrees $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) source 14 | # the i18n builder cannot share the environment and doctrees with the others 15 | I18NSPHINXOPTS = $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) source 16 | 17 | .PHONY: help clean html dirhtml singlehtml pickle json htmlhelp qthelp devhelp epub latex latexpdf text man changes linkcheck doctest gettext 18 | 19 | help: 20 | @echo "Please use \`make ' where is one of" 21 | @echo " html to make standalone HTML files" 22 | @echo " dirhtml to make HTML files named index.html in directories" 23 | @echo " singlehtml to make a single large HTML file" 24 | @echo " pickle to make pickle files" 25 | @echo " json to make JSON files" 26 | @echo " htmlhelp to make HTML files and a HTML help project" 27 | @echo " qthelp to make HTML files and a qthelp project" 28 | @echo " devhelp to make HTML files and a Devhelp project" 29 | @echo " epub to make an epub" 30 | @echo " latex to make LaTeX files, you can set PAPER=a4 or PAPER=letter" 31 | @echo " latexpdf to make LaTeX files and run them through pdflatex" 32 | @echo " text to make text files" 33 | @echo " man to make manual pages" 34 | @echo " texinfo to make Texinfo files" 35 | @echo " info to make Texinfo files and run them through makeinfo" 36 | @echo " gettext to make PO message catalogs" 37 | @echo " changes to make an overview of all changed/added/deprecated items" 38 | @echo " linkcheck to check all external links for integrity" 39 | @echo " doctest to run all doctests embedded in the documentation (if enabled)" 40 | 41 | clean: 42 | -rm -rf $(BUILDDIR)/* 43 | 44 | html: 45 | $(SPHINXBUILD) -b html $(ALLSPHINXOPTS) $(BUILDDIR)/html 46 | @echo 47 | @echo "Build finished. The HTML pages are in $(BUILDDIR)/html." 48 | 49 | dirhtml: 50 | $(SPHINXBUILD) -b dirhtml $(ALLSPHINXOPTS) $(BUILDDIR)/dirhtml 51 | @echo 52 | @echo "Build finished. The HTML pages are in $(BUILDDIR)/dirhtml." 53 | 54 | singlehtml: 55 | $(SPHINXBUILD) -b singlehtml $(ALLSPHINXOPTS) $(BUILDDIR)/singlehtml 56 | @echo 57 | @echo "Build finished. The HTML page is in $(BUILDDIR)/singlehtml." 58 | 59 | pickle: 60 | $(SPHINXBUILD) -b pickle $(ALLSPHINXOPTS) $(BUILDDIR)/pickle 61 | @echo 62 | @echo "Build finished; now you can process the pickle files." 63 | 64 | json: 65 | $(SPHINXBUILD) -b json $(ALLSPHINXOPTS) $(BUILDDIR)/json 66 | @echo 67 | @echo "Build finished; now you can process the JSON files." 68 | 69 | htmlhelp: 70 | $(SPHINXBUILD) -b htmlhelp $(ALLSPHINXOPTS) $(BUILDDIR)/htmlhelp 71 | @echo 72 | @echo "Build finished; now you can run HTML Help Workshop with the" \ 73 | ".hhp project file in $(BUILDDIR)/htmlhelp." 74 | 75 | qthelp: 76 | $(SPHINXBUILD) -b qthelp $(ALLSPHINXOPTS) $(BUILDDIR)/qthelp 77 | @echo 78 | @echo "Build finished; now you can run "qcollectiongenerator" with the" \ 79 | ".qhcp project file in $(BUILDDIR)/qthelp, like this:" 80 | @echo "# qcollectiongenerator $(BUILDDIR)/qthelp/pythonapiforBitshares.qhcp" 81 | @echo "To view the help file:" 82 | @echo "# assistant -collectionFile $(BUILDDIR)/qthelp/pythonapiforBitshares.qhc" 83 | 84 | devhelp: 85 | $(SPHINXBUILD) -b devhelp $(ALLSPHINXOPTS) $(BUILDDIR)/devhelp 86 | @echo 87 | @echo "Build finished." 88 | @echo "To view the help file:" 89 | @echo "# mkdir -p $HOME/.local/share/devhelp/pythonapiforBitshares" 90 | @echo "# ln -s $(BUILDDIR)/devhelp $HOME/.local/share/devhelp/pythonapiforBitshares" 91 | @echo "# devhelp" 92 | 93 | epub: 94 | $(SPHINXBUILD) -b epub $(ALLSPHINXOPTS) $(BUILDDIR)/epub 95 | @echo 96 | @echo "Build finished. The epub file is in $(BUILDDIR)/epub." 97 | 98 | latex: 99 | $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex 100 | @echo 101 | @echo "Build finished; the LaTeX files are in $(BUILDDIR)/latex." 102 | @echo "Run \`make' in that directory to run these through (pdf)latex" \ 103 | "(use \`make latexpdf' here to do that automatically)." 104 | 105 | latexpdf: 106 | $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex 107 | @echo "Running LaTeX files through pdflatex..." 108 | $(MAKE) -C $(BUILDDIR)/latex all-pdf 109 | @echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex." 110 | 111 | text: 112 | $(SPHINXBUILD) -b text $(ALLSPHINXOPTS) $(BUILDDIR)/text 113 | @echo 114 | @echo "Build finished. The text files are in $(BUILDDIR)/text." 115 | 116 | man: 117 | $(SPHINXBUILD) -b man $(ALLSPHINXOPTS) $(BUILDDIR)/man 118 | @echo 119 | @echo "Build finished. The manual pages are in $(BUILDDIR)/man." 120 | 121 | texinfo: 122 | $(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo 123 | @echo 124 | @echo "Build finished. The Texinfo files are in $(BUILDDIR)/texinfo." 125 | @echo "Run \`make' in that directory to run these through makeinfo" \ 126 | "(use \`make info' here to do that automatically)." 127 | 128 | info: 129 | $(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo 130 | @echo "Running Texinfo files through makeinfo..." 131 | make -C $(BUILDDIR)/texinfo info 132 | @echo "makeinfo finished; the Info files are in $(BUILDDIR)/texinfo." 133 | 134 | gettext: 135 | $(SPHINXBUILD) -b gettext $(I18NSPHINXOPTS) $(BUILDDIR)/locale 136 | @echo 137 | @echo "Build finished. The message catalogs are in $(BUILDDIR)/locale." 138 | 139 | changes: 140 | $(SPHINXBUILD) -b changes $(ALLSPHINXOPTS) $(BUILDDIR)/changes 141 | @echo 142 | @echo "The overview file is in $(BUILDDIR)/changes." 143 | 144 | linkcheck: 145 | $(SPHINXBUILD) -b linkcheck $(ALLSPHINXOPTS) $(BUILDDIR)/linkcheck 146 | @echo 147 | @echo "Link check complete; look for any errors in the above output " \ 148 | "or in $(BUILDDIR)/linkcheck/output.txt." 149 | 150 | doctest: 151 | $(SPHINXBUILD) -b doctest $(ALLSPHINXOPTS) $(BUILDDIR)/doctest 152 | @echo "Testing of doctests in the sources finished, look at the " \ 153 | "results in $(BUILDDIR)/doctest/output.txt." 154 | -------------------------------------------------------------------------------- /docs/make.bat: -------------------------------------------------------------------------------- 1 | @ECHO OFF 2 | 3 | REM Command file for Sphinx documentation 4 | 5 | if "%SPHINXBUILD%" == "" ( 6 | set SPHINXBUILD=sphinx-build 7 | ) 8 | set BUILDDIR=build 9 | set SPHINXOPTS=-W 10 | set ALLSPHINXOPTS=-d %BUILDDIR%/doctrees %SPHINXOPTS% source 11 | set I18NSPHINXOPTS=%SPHINXOPTS% source 12 | if NOT "%PAPER%" == "" ( 13 | set ALLSPHINXOPTS=-D latex_paper_size=%PAPER% %ALLSPHINXOPTS% 14 | set I18NSPHINXOPTS=-D latex_paper_size=%PAPER% %I18NSPHINXOPTS% 15 | ) 16 | 17 | if "%1" == "" goto help 18 | 19 | if "%1" == "help" ( 20 | :help 21 | echo.Please use `make ^` where ^ is one of 22 | echo. html to make standalone HTML files 23 | echo. dirhtml to make HTML files named index.html in directories 24 | echo. singlehtml to make a single large HTML file 25 | echo. pickle to make pickle files 26 | echo. json to make JSON files 27 | echo. htmlhelp to make HTML files and a HTML help project 28 | echo. qthelp to make HTML files and a qthelp project 29 | echo. devhelp to make HTML files and a Devhelp project 30 | echo. epub to make an epub 31 | echo. latex to make LaTeX files, you can set PAPER=a4 or PAPER=letter 32 | echo. text to make text files 33 | echo. man to make manual pages 34 | echo. texinfo to make Texinfo files 35 | echo. gettext to make PO message catalogs 36 | echo. changes to make an overview over all changed/added/deprecated items 37 | echo. linkcheck to check all external links for integrity 38 | echo. doctest to run all doctests embedded in the documentation if enabled 39 | goto end 40 | ) 41 | 42 | if "%1" == "clean" ( 43 | for /d %%i in (%BUILDDIR%\*) do rmdir /q /s %%i 44 | del /q /s %BUILDDIR%\* 45 | goto end 46 | ) 47 | 48 | if "%1" == "html" ( 49 | %SPHINXBUILD% -b html %ALLSPHINXOPTS% %BUILDDIR%/html 50 | if errorlevel 1 exit /b 1 51 | echo. 52 | echo.Build finished. The HTML pages are in %BUILDDIR%/html. 53 | goto end 54 | ) 55 | 56 | if "%1" == "dirhtml" ( 57 | %SPHINXBUILD% -b dirhtml %ALLSPHINXOPTS% %BUILDDIR%/dirhtml 58 | if errorlevel 1 exit /b 1 59 | echo. 60 | echo.Build finished. The HTML pages are in %BUILDDIR%/dirhtml. 61 | goto end 62 | ) 63 | 64 | if "%1" == "singlehtml" ( 65 | %SPHINXBUILD% -b singlehtml %ALLSPHINXOPTS% %BUILDDIR%/singlehtml 66 | if errorlevel 1 exit /b 1 67 | echo. 68 | echo.Build finished. The HTML pages are in %BUILDDIR%/singlehtml. 69 | goto end 70 | ) 71 | 72 | if "%1" == "pickle" ( 73 | %SPHINXBUILD% -b pickle %ALLSPHINXOPTS% %BUILDDIR%/pickle 74 | if errorlevel 1 exit /b 1 75 | echo. 76 | echo.Build finished; now you can process the pickle files. 77 | goto end 78 | ) 79 | 80 | if "%1" == "json" ( 81 | %SPHINXBUILD% -b json %ALLSPHINXOPTS% %BUILDDIR%/json 82 | if errorlevel 1 exit /b 1 83 | echo. 84 | echo.Build finished; now you can process the JSON files. 85 | goto end 86 | ) 87 | 88 | if "%1" == "htmlhelp" ( 89 | %SPHINXBUILD% -b htmlhelp %ALLSPHINXOPTS% %BUILDDIR%/htmlhelp 90 | if errorlevel 1 exit /b 1 91 | echo. 92 | echo.Build finished; now you can run HTML Help Workshop with the ^ 93 | .hhp project file in %BUILDDIR%/htmlhelp. 94 | goto end 95 | ) 96 | 97 | if "%1" == "qthelp" ( 98 | %SPHINXBUILD% -b qthelp %ALLSPHINXOPTS% %BUILDDIR%/qthelp 99 | if errorlevel 1 exit /b 1 100 | echo. 101 | echo.Build finished; now you can run "qcollectiongenerator" with the ^ 102 | .qhcp project file in %BUILDDIR%/qthelp, like this: 103 | echo.^> qcollectiongenerator %BUILDDIR%\qthelp\pythonapiforBitshares.qhcp 104 | echo.To view the help file: 105 | echo.^> assistant -collectionFile %BUILDDIR%\qthelp\pythonapiforBitshares.qhc 106 | goto end 107 | ) 108 | 109 | if "%1" == "devhelp" ( 110 | %SPHINXBUILD% -b devhelp %ALLSPHINXOPTS% %BUILDDIR%/devhelp 111 | if errorlevel 1 exit /b 1 112 | echo. 113 | echo.Build finished. 114 | goto end 115 | ) 116 | 117 | if "%1" == "epub" ( 118 | %SPHINXBUILD% -b epub %ALLSPHINXOPTS% %BUILDDIR%/epub 119 | if errorlevel 1 exit /b 1 120 | echo. 121 | echo.Build finished. The epub file is in %BUILDDIR%/epub. 122 | goto end 123 | ) 124 | 125 | if "%1" == "latex" ( 126 | %SPHINXBUILD% -b latex %ALLSPHINXOPTS% %BUILDDIR%/latex 127 | if errorlevel 1 exit /b 1 128 | echo. 129 | echo.Build finished; the LaTeX files are in %BUILDDIR%/latex. 130 | goto end 131 | ) 132 | 133 | if "%1" == "text" ( 134 | %SPHINXBUILD% -b text %ALLSPHINXOPTS% %BUILDDIR%/text 135 | if errorlevel 1 exit /b 1 136 | echo. 137 | echo.Build finished. The text files are in %BUILDDIR%/text. 138 | goto end 139 | ) 140 | 141 | if "%1" == "man" ( 142 | %SPHINXBUILD% -b man %ALLSPHINXOPTS% %BUILDDIR%/man 143 | if errorlevel 1 exit /b 1 144 | echo. 145 | echo.Build finished. The manual pages are in %BUILDDIR%/man. 146 | goto end 147 | ) 148 | 149 | if "%1" == "texinfo" ( 150 | %SPHINXBUILD% -b texinfo %ALLSPHINXOPTS% %BUILDDIR%/texinfo 151 | if errorlevel 1 exit /b 1 152 | echo. 153 | echo.Build finished. The Texinfo files are in %BUILDDIR%/texinfo. 154 | goto end 155 | ) 156 | 157 | if "%1" == "gettext" ( 158 | %SPHINXBUILD% -b gettext %I18NSPHINXOPTS% %BUILDDIR%/locale 159 | if errorlevel 1 exit /b 1 160 | echo. 161 | echo.Build finished. The message catalogs are in %BUILDDIR%/locale. 162 | goto end 163 | ) 164 | 165 | if "%1" == "changes" ( 166 | %SPHINXBUILD% -b changes %ALLSPHINXOPTS% %BUILDDIR%/changes 167 | if errorlevel 1 exit /b 1 168 | echo. 169 | echo.The overview file is in %BUILDDIR%/changes. 170 | goto end 171 | ) 172 | 173 | if "%1" == "linkcheck" ( 174 | %SPHINXBUILD% -b linkcheck %ALLSPHINXOPTS% %BUILDDIR%/linkcheck 175 | if errorlevel 1 exit /b 1 176 | echo. 177 | echo.Link check complete; look for any errors in the above output ^ 178 | or in %BUILDDIR%/linkcheck/output.txt. 179 | goto end 180 | ) 181 | 182 | if "%1" == "doctest" ( 183 | %SPHINXBUILD% -b doctest %ALLSPHINXOPTS% %BUILDDIR%/doctest 184 | if errorlevel 1 exit /b 1 185 | echo. 186 | echo.Testing of doctests in the sources finished, look at the ^ 187 | results in %BUILDDIR%/doctest/output.txt. 188 | goto end 189 | ) 190 | 191 | :end 192 | -------------------------------------------------------------------------------- /docs/source/README: -------------------------------------------------------------------------------- 1 | Run `sphinx-apidoc -o . ../../bts' in this directory. 2 | 3 | This will generate `modules.rst' and `bts.rst'. 4 | 5 | Then include `modules.rst' in your `index.rst' file. -------------------------------------------------------------------------------- /docs/source/_static/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pch957/btsprice/8a6913dfc0d74e668e116855ea8bb1caf3af6c04/docs/source/_static/.gitkeep -------------------------------------------------------------------------------- /docs/source/conf.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | # This file is based upon the file generated by sphinx-quickstart. However, 4 | # where sphinx-quickstart hardcodes values in this file that you input, this 5 | # file has been changed to pull from your module's metadata module. 6 | # 7 | # This file is execfile()d with the current directory set to its containing 8 | # dir. 9 | # 10 | # Note that not all possible configuration values are present in this 11 | # autogenerated file. 12 | # 13 | # All configuration values have a default; values that are commented out 14 | # serve to show the default. 15 | 16 | import os 17 | import sys 18 | 19 | # If extensions (or modules to document with autodoc) are in another directory, 20 | # add these directories to sys.path here. If the directory is relative to the 21 | # documentation root, use os.path.abspath to make it absolute, like shown here. 22 | sys.path.insert(0, os.path.abspath('../..')) 23 | 24 | # Import project metadata 25 | from bts import metadata 26 | 27 | # -- General configuration ---------------------------------------------------- 28 | 29 | # If your documentation needs a minimal Sphinx version, state it here. 30 | #needs_sphinx = '1.0' 31 | 32 | # Add any Sphinx extension module names here, as strings. They can be 33 | # extensions coming with Sphinx (named 'sphinx.ext.*') or your custom ones. 34 | extensions = ['sphinx.ext.autodoc', 'sphinx.ext.intersphinx', 35 | 'sphinx.ext.todo', 'sphinx.ext.coverage', 'sphinx.ext.viewcode'] 36 | 37 | # show todos 38 | todo_include_todos = True 39 | 40 | # Add any paths that contain templates here, relative to this directory. 41 | templates_path = ['_templates'] 42 | 43 | # The suffix of source filenames. 44 | source_suffix = '.rst' 45 | 46 | # The encoding of source files. 47 | #source_encoding = 'utf-8-sig' 48 | 49 | # The master toctree document. 50 | master_doc = 'index' 51 | 52 | # General information about the project. 53 | project = metadata.project 54 | copyright = metadata.copyright 55 | 56 | # The version info for the project you're documenting, acts as replacement for 57 | # |version| and |release|, also used in various other places throughout the 58 | # built documents. 59 | # 60 | # The short X.Y version. 61 | version = metadata.version 62 | # The full version, including alpha/beta/rc tags. 63 | release = metadata.version 64 | 65 | # The language for content autogenerated by Sphinx. Refer to documentation 66 | # for a list of supported languages. 67 | #language = None 68 | 69 | # There are two options for replacing |today|: either, you set today to some 70 | # non-false value, then it is used: 71 | #today = '' 72 | # Else, today_fmt is used as the format for a strftime call. 73 | #today_fmt = '%B %d, %Y' 74 | 75 | # List of patterns, relative to source directory, that match files and 76 | # directories to ignore when looking for source files. 77 | exclude_patterns = [] 78 | 79 | # The reST default role (used for this markup: `text`) to use for all 80 | # documents. 81 | #default_role = None 82 | 83 | # If true, '()' will be appended to :func: etc. cross-reference text. 84 | #add_function_parentheses = True 85 | 86 | # If true, the current module name will be prepended to all description 87 | # unit titles (such as .. function::). 88 | #add_module_names = True 89 | 90 | # If true, sectionauthor and moduleauthor directives will be shown in the 91 | # output. They are ignored by default. 92 | #show_authors = False 93 | 94 | # The name of the Pygments (syntax highlighting) style to use. 95 | pygments_style = 'sphinx' 96 | 97 | # A list of ignored prefixes for module index sorting. 98 | #modindex_common_prefix = [] 99 | 100 | 101 | # -- Options for HTML output -------------------------------------------------- 102 | 103 | # The theme to use for HTML and HTML Help pages. See the documentation for 104 | # a list of builtin themes. 105 | html_theme = 'nature' 106 | 107 | # Theme options are theme-specific and customize the look and feel of a theme 108 | # further. For a list of options available for each theme, see the 109 | # documentation. 110 | #html_theme_options = {} 111 | 112 | # Add any paths that contain custom themes here, relative to this directory. 113 | #html_theme_path = [] 114 | 115 | # The name for this set of Sphinx documents. If None, it defaults to 116 | # " v documentation". 117 | #html_title = None 118 | 119 | # A shorter title for the navigation bar. Default is the same as html_title. 120 | #html_short_title = None 121 | 122 | # The name of an image file (relative to this directory) to place at the top 123 | # of the sidebar. 124 | #html_logo = None 125 | 126 | # The name of an image file (within the static path) to use as favicon of the 127 | # docs. This file should be a Windows icon file (.ico) being 16x16 or 32x32 128 | # pixels large. 129 | #html_favicon = None 130 | 131 | # Add any paths that contain custom static files (such as style sheets) here, 132 | # relative to this directory. They are copied after the builtin static files, 133 | # so a file named "default.css" will overwrite the builtin "default.css". 134 | html_static_path = ['_static'] 135 | 136 | # If not '', a 'Last updated on:' timestamp is inserted at every page bottom, 137 | # using the given strftime format. 138 | #html_last_updated_fmt = '%b %d, %Y' 139 | 140 | # If true, SmartyPants will be used to convert quotes and dashes to 141 | # typographically correct entities. 142 | #html_use_smartypants = True 143 | 144 | # Custom sidebar templates, maps document names to template names. 145 | #html_sidebars = {} 146 | 147 | # Additional templates that should be rendered to pages, maps page names to 148 | # template names. 149 | #html_additional_pages = {} 150 | 151 | # If false, no module index is generated. 152 | #html_domain_indices = True 153 | 154 | # If false, no index is generated. 155 | #html_use_index = True 156 | 157 | # If true, the index is split into individual pages for each letter. 158 | #html_split_index = False 159 | 160 | # If true, links to the reST sources are added to the pages. 161 | #html_show_sourcelink = True 162 | 163 | # If true, "Created using Sphinx" is shown in the HTML footer. Default is True. 164 | #html_show_sphinx = True 165 | 166 | # If true, "(C) Copyright ..." is shown in the HTML footer. Default is True. 167 | #html_show_copyright = True 168 | 169 | # If true, an OpenSearch description file will be output, and all pages will 170 | # contain a tag referring to it. The value of this option must be the 171 | # base URL from which the finished HTML is served. 172 | #html_use_opensearch = '' 173 | 174 | # This is the file name suffix for HTML files (e.g. ".xhtml"). 175 | #html_file_suffix = None 176 | 177 | # Output file base name for HTML help builder. 178 | htmlhelp_basename = metadata.project_no_spaces + 'doc' 179 | 180 | 181 | # -- Options for LaTeX output ------------------------------------------------- 182 | 183 | latex_elements = { 184 | # The paper size ('letterpaper' or 'a4paper'). 185 | #'papersize': 'letterpaper', 186 | 187 | # The font size ('10pt', '11pt' or '12pt'). 188 | #'pointsize': '10pt', 189 | 190 | # Additional stuff for the LaTeX preamble. 191 | #'preamble': '', 192 | } 193 | 194 | # Grouping the document tree into LaTeX files. List of tuples 195 | # (source start file, target name, title, author, 196 | # documentclass [howto/manual]). 197 | latex_documents = [ 198 | ('index', metadata.project_no_spaces + '.tex', 199 | metadata.project + ' Documentation', metadata.authors_string, 200 | 'manual'), 201 | ] 202 | 203 | # The name of an image file (relative to this directory) to place at the top of 204 | # the title page. 205 | #latex_logo = None 206 | 207 | # For "manual" documents, if this is true, then toplevel headings are parts, 208 | # not chapters. 209 | #latex_use_parts = False 210 | 211 | # If true, show page references after internal links. 212 | #latex_show_pagerefs = False 213 | 214 | # If true, show URL addresses after external links. 215 | #latex_show_urls = False 216 | 217 | # Documents to append as an appendix to all manuals. 218 | #latex_appendices = [] 219 | 220 | # If false, no module index is generated. 221 | #latex_domain_indices = True 222 | 223 | 224 | # -- Options for manual page output ------------------------------------------- 225 | 226 | # One entry per manual page. List of tuples 227 | # (source start file, name, description, authors, manual section). 228 | man_pages = [ 229 | ('index', metadata.package, metadata.project + ' Documentation', 230 | metadata.authors_string, 1) 231 | ] 232 | 233 | # If true, show URL addresses after external links. 234 | #man_show_urls = False 235 | 236 | 237 | # -- Options for Texinfo output ----------------------------------------------- 238 | 239 | # Grouping the document tree into Texinfo files. List of tuples 240 | # (source start file, target name, title, author, 241 | # dir menu entry, description, category) 242 | texinfo_documents = [ 243 | ('index', metadata.project_no_spaces, 244 | metadata.project + ' Documentation', metadata.authors_string, 245 | metadata.project_no_spaces, metadata.description, 'Miscellaneous'), 246 | ] 247 | 248 | # Documents to append as an appendix to all manuals. 249 | #texinfo_appendices = [] 250 | 251 | # If false, no module index is generated. 252 | #texinfo_domain_indices = True 253 | 254 | # How to display URL addresses: 'footnote', 'no', or 'inline'. 255 | #texinfo_show_urls = 'footnote' 256 | 257 | 258 | # Example configuration for intersphinx: refer to the Python standard library. 259 | intersphinx_mapping = { 260 | 'python': ('http://docs.python.org/', None), 261 | } 262 | 263 | # Extra local configuration. This is useful for placing the class description 264 | # in the class docstring and the __init__ parameter documentation in the 265 | # __init__ docstring. See 266 | # for more 267 | # information. 268 | autoclass_content = 'both' 269 | -------------------------------------------------------------------------------- /docs/source/index.rst: -------------------------------------------------------------------------------- 1 | python api for Bitshares 2 | ======================== 3 | 4 | Contents: 5 | 6 | .. toctree:: 7 | :maxdepth: 2 8 | 9 | 10 | .. only:: html 11 | 12 | Indices and tables 13 | ================== 14 | 15 | * :ref:`genindex` 16 | * :ref:`modindex` 17 | * :ref:`search` 18 | -------------------------------------------------------------------------------- /pavement.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | from __future__ import print_function 4 | 5 | import os 6 | import sys 7 | import time 8 | import subprocess 9 | 10 | # Import parameters from the setup file. 11 | sys.path.append('.') 12 | from setup import ( 13 | setup_dict, get_project_files, print_success_message, 14 | print_failure_message, _lint, _test, _test_all, 15 | CODE_DIRECTORY, DOCS_DIRECTORY, TESTS_DIRECTORY, PYTEST_FLAGS) 16 | 17 | from paver.easy import options, task, needs, consume_args 18 | from paver.setuputils import install_distutils_tasks 19 | 20 | options(setup=setup_dict) 21 | 22 | install_distutils_tasks() 23 | 24 | ## Miscellaneous helper functions 25 | 26 | 27 | def print_passed(): 28 | # generated on http://patorjk.com/software/taag/#p=display&f=Small&t=PASSED 29 | print_success_message(r''' ___ _ ___ ___ ___ ___ 30 | | _ \/_\ / __/ __| __| \ 31 | | _/ _ \\__ \__ \ _|| |) | 32 | |_|/_/ \_\___/___/___|___/ 33 | ''') 34 | 35 | 36 | def print_failed(): 37 | # generated on http://patorjk.com/software/taag/#p=display&f=Small&t=FAILED 38 | print_failure_message(r''' ___ _ ___ _ ___ ___ 39 | | __/_\ |_ _| | | __| \ 40 | | _/ _ \ | || |__| _|| |) | 41 | |_/_/ \_\___|____|___|___/ 42 | ''') 43 | 44 | 45 | class cwd(object): 46 | """Class used for temporarily changing directories. Can be though of 47 | as a `pushd /my/dir' then a `popd' at the end. 48 | """ 49 | def __init__(self, newcwd): 50 | """:param newcwd: directory to make the cwd 51 | :type newcwd: :class:`str` 52 | """ 53 | self.newcwd = newcwd 54 | 55 | def __enter__(self): 56 | self.oldcwd = os.getcwd() 57 | os.chdir(self.newcwd) 58 | return os.getcwd() 59 | 60 | def __exit__(self, type_, value, traceback): 61 | # This acts like a `finally' clause: it will always be executed. 62 | os.chdir(self.oldcwd) 63 | 64 | 65 | ## Task-related functions 66 | 67 | def _doc_make(*make_args): 68 | """Run make in sphinx' docs directory. 69 | 70 | :return: exit code 71 | """ 72 | if sys.platform == 'win32': 73 | # Windows 74 | make_cmd = ['make.bat'] 75 | else: 76 | # Linux, Mac OS X, and others 77 | make_cmd = ['make'] 78 | make_cmd.extend(make_args) 79 | 80 | # Account for a stupid Python "bug" on Windows: 81 | # 82 | with cwd(DOCS_DIRECTORY): 83 | retcode = subprocess.call(make_cmd) 84 | return retcode 85 | 86 | 87 | ## Tasks 88 | 89 | @task 90 | @needs('doc_html', 'setuptools.command.sdist') 91 | def sdist(): 92 | """Build the HTML docs and the tarball.""" 93 | pass 94 | 95 | 96 | @task 97 | def test(): 98 | """Run the unit tests.""" 99 | raise SystemExit(_test()) 100 | 101 | 102 | @task 103 | def lint(): 104 | # This refuses to format properly when running `paver help' unless 105 | # this ugliness is used. 106 | ('Perform PEP8 style check, run PyFlakes, and run McCabe complexity ' 107 | 'metrics on the code.') 108 | raise SystemExit(_lint()) 109 | 110 | 111 | @task 112 | def test_all(): 113 | """Perform a style check and run all unit tests.""" 114 | retcode = _test_all() 115 | if retcode == 0: 116 | print_passed() 117 | else: 118 | print_failed() 119 | raise SystemExit(retcode) 120 | 121 | 122 | @task 123 | @consume_args 124 | def run(args): 125 | """Run the package's main script. All arguments are passed to it.""" 126 | # The main script expects to get the called executable's name as 127 | # argv[0]. However, paver doesn't provide that in args. Even if it did (or 128 | # we dove into sys.argv), it wouldn't be useful because it would be paver's 129 | # executable. So we just pass the package name in as the executable name, 130 | # since it's close enough. This should never be seen by an end user 131 | # installing through Setuptools anyway. 132 | from btsprice.main import main 133 | raise SystemExit(main([CODE_DIRECTORY] + args)) 134 | 135 | 136 | @task 137 | def commit(): 138 | """Commit only if all the tests pass.""" 139 | if _test_all() == 0: 140 | subprocess.check_call(['git', 'commit']) 141 | else: 142 | print_failure_message('\nTests failed, not committing.') 143 | 144 | 145 | @task 146 | def coverage(): 147 | """Run tests and show test coverage report.""" 148 | try: 149 | import pytest_cov # NOQA 150 | except ImportError: 151 | print_failure_message( 152 | 'Install the pytest coverage plugin to use this task, ' 153 | "i.e., `pip install pytest-cov'.") 154 | raise SystemExit(1) 155 | import pytest 156 | pytest.main(PYTEST_FLAGS + [ 157 | '--cov', CODE_DIRECTORY, 158 | '--cov-report', 'term-missing', 159 | TESTS_DIRECTORY]) 160 | 161 | 162 | @task # NOQA 163 | def doc_watch(): 164 | """Watch for changes in the docs and rebuild HTML docs when changed.""" 165 | try: 166 | from watchdog.events import FileSystemEventHandler 167 | from watchdog.observers import Observer 168 | except ImportError: 169 | print_failure_message('Install the watchdog package to use this task, ' 170 | "i.e., `pip install watchdog'.") 171 | raise SystemExit(1) 172 | 173 | class RebuildDocsEventHandler(FileSystemEventHandler): 174 | def __init__(self, base_paths): 175 | self.base_paths = base_paths 176 | 177 | def dispatch(self, event): 178 | """Dispatches events to the appropriate methods. 179 | :param event: The event object representing the file system event. 180 | :type event: :class:`watchdog.events.FileSystemEvent` 181 | """ 182 | for base_path in self.base_paths: 183 | if event.src_path.endswith(base_path): 184 | super(RebuildDocsEventHandler, self).dispatch(event) 185 | # We found one that matches. We're done. 186 | return 187 | 188 | def on_modified(self, event): 189 | print_failure_message('Modification detected. Rebuilding docs.') 190 | # # Strip off the path prefix. 191 | # import os 192 | # if event.src_path[len(os.getcwd()) + 1:].startswith( 193 | # CODE_DIRECTORY): 194 | # # sphinx-build doesn't always pick up changes on code files, 195 | # # even though they are used to generate the documentation. As 196 | # # a workaround, just clean before building. 197 | doc_html() 198 | print_success_message('Docs have been rebuilt.') 199 | 200 | print_success_message( 201 | 'Watching for changes in project files, press Ctrl-C to cancel...') 202 | handler = RebuildDocsEventHandler(get_project_files()) 203 | observer = Observer() 204 | observer.schedule(handler, path='.', recursive=True) 205 | observer.start() 206 | try: 207 | while True: 208 | time.sleep(1) 209 | except KeyboardInterrupt: 210 | observer.stop() 211 | observer.join() 212 | 213 | 214 | @task 215 | @needs('doc_html') 216 | def doc_open(): 217 | """Build the HTML docs and open them in a web browser.""" 218 | doc_index = os.path.join(DOCS_DIRECTORY, 'build', 'html', 'index.html') 219 | if sys.platform == 'darwin': 220 | # Mac OS X 221 | subprocess.check_call(['open', doc_index]) 222 | elif sys.platform == 'win32': 223 | # Windows 224 | subprocess.check_call(['start', doc_index], shell=True) 225 | elif sys.platform == 'linux2': 226 | # All freedesktop-compatible desktops 227 | subprocess.check_call(['xdg-open', doc_index]) 228 | else: 229 | print_failure_message( 230 | "Unsupported platform. Please open `{0}' manually.".format( 231 | doc_index)) 232 | 233 | 234 | @task 235 | def get_tasks(): 236 | """Get all paver-defined tasks.""" 237 | from paver.tasks import environment 238 | for _task in environment.get_tasks(): 239 | print(_task.shortname) 240 | 241 | 242 | @task 243 | def doc_html(): 244 | """Build the HTML docs.""" 245 | retcode = _doc_make('html') 246 | 247 | if retcode: 248 | raise SystemExit(retcode) 249 | 250 | 251 | @task 252 | def doc_clean(): 253 | """Clean (delete) the built docs.""" 254 | retcode = _doc_make('clean') 255 | 256 | if retcode: 257 | raise SystemExit(retcode) 258 | -------------------------------------------------------------------------------- /requirements-dev.txt: -------------------------------------------------------------------------------- 1 | # Runtime requirements 2 | --requirement requirements.txt 3 | 4 | # Testing 5 | pytest==2.5.1 6 | py==1.4.19 7 | mock==1.0.1 8 | 9 | # Linting 10 | flake8==2.1.0 11 | mccabe==0.2.1 12 | pep8==1.4.6 13 | pyflakes==0.7.3 14 | 15 | # Documentation 16 | Sphinx==1.2 17 | docutils==0.11 18 | Jinja2==2.7.1 19 | MarkupSafe==0.18 20 | Pygments==1.6 21 | 22 | # Miscellaneous 23 | Paver==1.2.1 24 | colorama==0.2.7 25 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | bts 2 | btspusher 3 | prettytable 4 | aiohttp 5 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | from __future__ import print_function 3 | 4 | import os 5 | import sys 6 | import imp 7 | import subprocess 8 | 9 | # Python 2.6 subprocess.check_output compatibility. Thanks Greg Hewgill! 10 | if 'check_output' not in dir(subprocess): 11 | def check_output(cmd_args, *args, **kwargs): 12 | proc = subprocess.Popen( 13 | cmd_args, *args, 14 | stdout=subprocess.PIPE, stderr=subprocess.PIPE, **kwargs) 15 | out, err = proc.communicate() 16 | if proc.returncode != 0: 17 | raise subprocess.CalledProcessError(args) 18 | return out 19 | subprocess.check_output = check_output 20 | 21 | from setuptools import setup, find_packages 22 | from setuptools.command.test import test as TestCommand 23 | from distutils import spawn 24 | 25 | try: 26 | import colorama 27 | colorama.init() # Initialize colorama on Windows 28 | except ImportError: 29 | # Don't require colorama just for running paver tasks. This allows us to 30 | # run `paver install' without requiring the user to first have colorama 31 | # installed. 32 | pass 33 | 34 | # Add the current directory to the module search path. 35 | sys.path.insert(0, os.path.abspath('.')) 36 | 37 | # Constants 38 | CODE_DIRECTORY = 'btsprice' 39 | DOCS_DIRECTORY = 'docs' 40 | TESTS_DIRECTORY = 'tests' 41 | PYTEST_FLAGS = ['--doctest-modules'] 42 | 43 | # Import metadata. Normally this would just be: 44 | # 45 | # from btsprice import metadata 46 | # 47 | # However, when we do this, we also import `btsprice/__init__.py'. If this 48 | # imports names from some other modules and these modules have third-party 49 | # dependencies that need installing (which happens after this file is run), the 50 | # script will crash. What we do instead is to load the metadata module by path 51 | # instead, effectively side-stepping the dependency problem. Please make sure 52 | # metadata has no dependencies, otherwise they will need to be added to 53 | # the setup_requires keyword. 54 | metadata = imp.load_source( 55 | 'metadata', os.path.join(CODE_DIRECTORY, 'metadata.py')) 56 | 57 | 58 | # Miscellaneous helper functions 59 | 60 | def get_project_files(): 61 | """Retrieve a list of project files, ignoring hidden files. 62 | 63 | :return: sorted list of project files 64 | :rtype: :class:`list` 65 | """ 66 | if is_git_project() and has_git(): 67 | return get_git_project_files() 68 | 69 | project_files = [] 70 | for top, subdirs, files in os.walk('.'): 71 | for subdir in subdirs: 72 | if subdir.startswith('.'): 73 | subdirs.remove(subdir) 74 | 75 | for f in files: 76 | if f.startswith('.'): 77 | continue 78 | project_files.append(os.path.join(top, f)) 79 | 80 | return project_files 81 | 82 | 83 | def is_git_project(): 84 | return os.path.isdir('.git') 85 | 86 | 87 | def has_git(): 88 | return bool(spawn.find_executable("git")) 89 | 90 | 91 | def get_git_project_files(): 92 | """Retrieve a list of all non-ignored files, including untracked files, 93 | excluding deleted files. 94 | 95 | :return: sorted list of git project files 96 | :rtype: :class:`list` 97 | """ 98 | cached_and_untracked_files = git_ls_files( 99 | '--cached', # All files cached in the index 100 | '--others', # Untracked files 101 | # Exclude untracked files that would be excluded by .gitignore, etc. 102 | '--exclude-standard') 103 | uncommitted_deleted_files = git_ls_files('--deleted') 104 | 105 | # Since sorting of files in a set is arbitrary, return a sorted list to 106 | # provide a well-defined order to tools like flake8, etc. 107 | return sorted(cached_and_untracked_files - uncommitted_deleted_files) 108 | 109 | 110 | def git_ls_files(*cmd_args): 111 | """Run ``git ls-files`` in the top-level project directory. Arguments go 112 | directly to execution call. 113 | 114 | :return: set of file names 115 | :rtype: :class:`set` 116 | """ 117 | cmd = ['git', 'ls-files'] 118 | cmd.extend(cmd_args) 119 | return set(subprocess.check_output(cmd).splitlines()) 120 | 121 | 122 | def print_success_message(message): 123 | """Print a message indicating success in green color to STDOUT. 124 | 125 | :param message: the message to print 126 | :type message: :class:`str` 127 | """ 128 | try: 129 | import colorama 130 | print(colorama.Fore.GREEN + message + colorama.Fore.RESET) 131 | except ImportError: 132 | print(message) 133 | 134 | 135 | def print_failure_message(message): 136 | """Print a message indicating failure in red color to STDERR. 137 | 138 | :param message: the message to print 139 | :type message: :class:`str` 140 | """ 141 | try: 142 | import colorama 143 | print(colorama.Fore.RED + message + colorama.Fore.RESET, 144 | file=sys.stderr) 145 | except ImportError: 146 | print(message, file=sys.stderr) 147 | 148 | 149 | def read(filename): 150 | """Return the contents of a file. 151 | 152 | :param filename: file path 153 | :type filename: :class:`str` 154 | :return: the file's content 155 | :rtype: :class:`str` 156 | """ 157 | with open(os.path.join(os.path.dirname(__file__), filename)) as f: 158 | return f.read() 159 | 160 | 161 | def _lint(): 162 | """Run lint and return an exit code.""" 163 | # Flake8 doesn't have an easy way to run checks using a Python function, so 164 | # just fork off another process to do it. 165 | 166 | # Python 3 compat: 167 | # - The result of subprocess call outputs are byte strings, meaning we need 168 | # to pass a byte string to endswith. 169 | project_python_files = [filename for filename in get_project_files() 170 | if filename.endswith(b'.py')] 171 | retcode = subprocess.call( 172 | ['flake8', '--max-complexity=10'] + project_python_files) 173 | if retcode == 0: 174 | print_success_message('No style errors') 175 | return retcode 176 | 177 | 178 | def _test(): 179 | """Run the unit tests. 180 | 181 | :return: exit code 182 | """ 183 | # Make sure to import pytest in this function. For the reason, see here: 184 | # # NOPEP8 185 | import pytest 186 | # This runs the unit tests. 187 | # It also runs doctest, but only on the modules in TESTS_DIRECTORY. 188 | return pytest.main(PYTEST_FLAGS + [TESTS_DIRECTORY]) 189 | 190 | 191 | def _test_all(): 192 | """Run lint and tests. 193 | 194 | :return: exit code 195 | """ 196 | return _lint() + _test() 197 | 198 | 199 | # The following code is to allow tests to be run with `python setup.py test'. 200 | # The main reason to make this possible is to allow tests to be run as part of 201 | # Setuptools' automatic run of 2to3 on the source code. The recommended way to 202 | # run tests is still `paver test_all'. 203 | # See 204 | # Code based on # NOPEP8 205 | class TestAllCommand(TestCommand): 206 | def finalize_options(self): 207 | TestCommand.finalize_options(self) 208 | # These are fake, and just set to appease distutils and setuptools. 209 | self.test_suite = True 210 | self.test_args = [] 211 | 212 | def run_tests(self): 213 | raise SystemExit(_test_all()) 214 | 215 | 216 | # define install_requires for specific Python versions 217 | python_version_specific_requires = [] 218 | 219 | # as of Python >= 2.7 and >= 3.2, the argparse module is maintained within 220 | # the Python standard library, otherwise we install it as a separate package 221 | if sys.version_info < (2, 7) or (3, 0) <= sys.version_info < (3, 3): 222 | python_version_specific_requires.append('argparse') 223 | 224 | 225 | # See here for more options: 226 | # 227 | setup_dict = dict( 228 | name=metadata.package, 229 | version=metadata.version, 230 | author=metadata.authors[0], 231 | author_email=metadata.emails[0], 232 | maintainer=metadata.authors[0], 233 | maintainer_email=metadata.emails[0], 234 | url=metadata.url, 235 | include_package_data=True, 236 | description=metadata.description, 237 | long_description=read('README.rst'), 238 | # Find a list of classifiers here: 239 | # 240 | classifiers=[ 241 | 'Development Status :: 1 - Planning', 242 | 'Environment :: Console', 243 | 'Intended Audience :: Developers', 244 | 'License :: OSI Approved :: MIT License', 245 | 'Natural Language :: English', 246 | 'Operating System :: OS Independent', 247 | 'Programming Language :: Python :: 2.6', 248 | 'Programming Language :: Python :: 2.7', 249 | 'Programming Language :: Python :: 3.3', 250 | 'Programming Language :: Python :: Implementation :: PyPy', 251 | 'Topic :: Documentation', 252 | 'Topic :: Software Development :: Libraries :: Python Modules', 253 | 'Topic :: System :: Installation/Setup', 254 | 'Topic :: System :: Software Distribution', 255 | ], 256 | packages=find_packages(exclude=(TESTS_DIRECTORY,)), 257 | install_requires=[ 258 | "bts", "btspusher", "prettytable", "aiohttp" 259 | # your module dependencies 260 | ] + python_version_specific_requires, 261 | # Allow tests to be run with `python setup.py test'. 262 | tests_require=[ 263 | 'pytest==2.5.1', 264 | 'mock==1.0.1', 265 | 'flake8==2.1.0', 266 | ], 267 | cmdclass={'test': TestAllCommand}, 268 | zip_safe=False, # don't use eggs 269 | entry_points={ 270 | 'console_scripts': [ 271 | 'btsprice = btsprice.main:entry_point' 272 | ], 273 | } 274 | ) 275 | 276 | 277 | def main(): 278 | setup(**setup_dict) 279 | 280 | 281 | if __name__ == '__main__': 282 | main() 283 | -------------------------------------------------------------------------------- /tests/test_main.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | # The parametrize function is generated, so this doesn't work: 4 | # 5 | # from pytest.mark import parametrize 6 | # 7 | import pytest 8 | parametrize = pytest.mark.parametrize 9 | 10 | from btsprice.misc import get_median 11 | 12 | 13 | class TestMain(object): 14 | logfile = open("/tmp/test-btsprice.log", 'a') 15 | 16 | def test_get_median(self): 17 | assert get_median([1, 2, 3, 4]) == 2.5 18 | assert get_median([1, 2, 3]) == 2 19 | assert get_median([1]) == 1 20 | assert get_median([]) is None 21 | -------------------------------------------------------------------------------- /tox.ini: -------------------------------------------------------------------------------- 1 | # Tox (http://tox.testrun.org/) is a tool for running tests in 2 | # multiple virtualenvs. This configuration file will run the test 3 | # suite on all supported python versions. To use it, "pip install tox" 4 | # and then run "tox" from this directory. 5 | # 6 | # To run tox faster, check out Detox 7 | # (https://pypi.python.org/pypi/detox), which runs your tox runs in 8 | # parallel. To use it, "pip install detox" and then run "detox" from 9 | # this directory. 10 | 11 | [tox] 12 | envlist = py26,py27,py33,pypy,docs 13 | 14 | [testenv] 15 | deps = 16 | --no-deps 17 | --requirement 18 | {toxinidir}/requirements-dev.txt 19 | commands = paver test_all 20 | 21 | [testenv:docs] 22 | basepython = python 23 | commands = paver doc_html 24 | --------------------------------------------------------------------------------