def _on_open(self, ws): tick_sizes = {} step_sizes = {} exchange_info_url = 'https://api.binance.com/api/v1/exchangeInfo' exchange_info = json.loads(urlopen(exchange_info_url).read().decode()) for symbol_info in exchange_info['symbols']: symbol = symbol_info['symbol'] if symbol not in self._symbols: continue filters = symbol_info['filters'] ftype = 'filterType' tick_size = first(filters, lambda f: f[ftype] == 'PRICE_FILTER')['tickSize'] step_size = first(filters, lambda f: f[ftype] == 'LOT_SIZE')['stepSize'] tick_sizes[symbol] = Decimal(tick_size.rstrip('0')) step_sizes[symbol] = Decimal(step_size.rstrip('0')) for i in range(len(self.pairs)): pair = self.pairs[i] name = '{}/{}'.format(*map(self._convert_sym, pair)) symbol = self._symbols[i] exchange = exchanges.BinanceExchange.instance() ts = tick_sizes[symbol] ss = step_sizes[symbol] market_ = market.BasicMarket(name, pair, symbol, exchange, ts, ss) self.markets.append(market_)
def start(self): if not self._pairs: print("Warning: Pairs not set for Bitstamp client!") return bitstamp_info_url = 'https://www.bitstamp.net/api/v2/trading-pairs-info/' bitstamp_info = json.loads(urlopen(bitstamp_info_url).read().decode()) exchange = exchanges.BitstampExchange.instance() for pair in self._pairs: symbol = ''.join(pair).lower() pair_info = first(bitstamp_info, lambda i: i['url_symbol'] == symbol) name = pair_info['name'] tick_size = Decimal('0.1')**Decimal(pair_info['counter_decimals']) step_size = Decimal('0.1')**Decimal(pair_info['base_decimals']) market_ = market.BasicMarket(name, pair, symbol, exchange, tick_size, step_size) self._markets[symbol] = market_ self._did_recieve[symbol] = False self._pusher.connect()
def on_open(self): self.markets = [] disp_names = ['{}/{}'.format(*p) for p in self._pairs] product_info = self._cli.get_products() for i in range(len(self._pairs)): exchange = exchanges.GDAXExchange.instance() try: pinfo = first(product_info, lambda x: x['id'] == self.products[i]) except: print(product_info) exit() tick_size = Decimal(pinfo['quote_increment']) market_ = market.BasicMarket(disp_names[i], self._pairs[i], self.products[i], exchange, tick_size, Decimal('0.00000001')) self.markets.append(market_) self.channels = [ { 'name': 'level2', 'product_ids': self.products }, { 'name': 'matches', 'product_ids': self.products } ]
def __init__(self, grammar): self.states = [] production_prim = Production("S_prim -> " + grammar.distinguished) show = str(production_prim) + " \n " + str(grammar) self.G = Grammar(show, "S_prim") self.insert_state(SLR_state([production_prim], 0)) self.states[0].expand(self.states[0].items, self.G) self.goto = {} self.queue = Queue() self.queue.put(self.states[0]) self.firsts = first(self.G) self.follows = follow(self.G, self.firsts) self.move_action()
def _process_events(self): i = 0 while i < len(self._event_buffer): msg = self._event_buffer[i] if len( self.markets) == 1 else self._event_buffer[i]['data'] symbol = msg['s'] market = first(self.markets, lambda m: m.symbol() == symbol) first_update = msg['U'] end_update = msg['u'] if not self._got_book[symbol]: self._reset_book(market) if symbol not in self._last_update: i += 1 elif end_update <= self._last_update[symbol]: del self._event_buffer[i] elif first_update > self._last_update[symbol] + 1: print("Skipped on {}: {}!".format('Binance', market.disp_name())) print(self._last_update[symbol]) exit() else: bids = msg['b'] asks = msg['a'] tick_size = market.tick_size() step_size = market.step_size() for bid in bids: price = Decimal(bid[0]).quantize(tick_size) size = Decimal(bid[1]).quantize(step_size) market.update_bid(price, size) for ask in asks: price = Decimal(ask[0]).quantize(tick_size) size = Decimal(ask[1]).quantize(step_size) market.update_ask(price, size) del self._event_buffer[i] self._last_update[symbol] = end_update
def on_message(self, msg): msg_type = msg['type'] if msg_type == 'subscriptions': return if 'product_id' not in msg: print("Bad MSG:", msg) product = msg['product_id'] market = first(self.markets, lambda m: m.symbol() == product) if msg_type == 'snapshot': print("Got GDAX", product, "snapshot:", len(json.dumps(msg)), "bytes") qinc = market.tick_size() ssize = market.step_size() bids = { Decimal(p).quantize(qinc): Decimal(s).quantize(ssize) for (p, s) in msg['bids'] } asks = { Decimal(p).quantize(qinc): Decimal(s).quantize(ssize) for (p, s) in msg['asks'] } market.reset_book(bids, asks) elif msg_type == 'last_match' or msg_type == 'match': qinc = market.tick_size() price = Decimal(msg['price']).quantize(qinc) market.match(price) elif msg_type == 'l2update': changes = msg['changes'] qinc = market.tick_size() ssize = market.step_size() for change in changes: side = change[0] price = Decimal(change[1]).quantize(qinc) size = Decimal(change[2]).quantize(ssize) if side == 'buy': market.update_bid(price, size) else: market.update_ask(price, size)
new_t = time.perf_counter() elapsed_str = Decimal(new_t - old_t).quantize(Decimal('0.001')) print("Arbitrages found: {}.".format(len(arb_paths)), "Time: {}s".format(elapsed_str)) arb_paths = [path for path in arb_paths if path.base_asset != KRW] if verbose: print("#################################################") print("ARBITRAGE PATHS:") for path in arb_paths: print(path) print("#################################################") time.sleep(15) gdax_btc_market = first(gdax_cli.markets, lambda m: m.pair() == (BTC, USD)) bithumb_btc_market = first(bithumb_cli.markets, lambda m: m.pair() == (BTC, KRW)) krw_usd_rate = fixer.get_rate('KRW', 'USD', fixer_key) print("Using USD/KRW rate:", str(1 / krw_usd_rate)[:7]) def get_best_path(paths, asset_amounts): best_roi = Decimal('0') best_path = None for path in arb_paths: start_amount = asset_amounts[path.base_asset] path_res = market_path.run_path(path, start_amount) if path_res[1]: continue
from tools import table_decorator, ObjectStorage, first from dataset import Dataset, DatasetFactory from sklearn.linear_model import LinearRegression import pandas as pd algorithms = [LinearRegression(n_jobs=-1)] def classifiers(): return list(map(lambda alg: Classifier(alg), algorithms)) def confidence_table(rows): content = pd.DataFrame(list(map(lambda row: (row[0], row[1] * 100), rows)), columns=['Algorithm', 'Confidence (%)']) return table_decorator("Results", content) data_frame = DataSources().google_actions() data_set = DatasetFactory().create_from(data_frame) classifiers = classifiers() train_info = map(lambda clr: [ clr.name(), clr.train(data_set), ], classifiers) print(confidence_table(train_info)) ObjectStorage().save(first(classifiers)) print("...trained data saved!")