def test_groupdisposable_addafterdispose(): disp1 = [False] disp2 = [False] def action1(): disp1[0] = True d1 = Disposable.create(action1) def action2(): disp2[0] = True d2 = Disposable.create(action2) g = CompositeDisposable(d1) assert g.length == 1 g.dispose() assert disp1[0] assert g.length == 0 g.add(d2) assert disp2[0] assert g.length == 0
def test_groupdisposable_addafterdispose(): disp1 = False disp2 = False def action1(): nonlocal disp1 disp1 = True d1 = Disposable(action1) def action2(): nonlocal disp2 disp2 = True d2 = Disposable(action2) g = CompositeDisposable(d1) assert g.length == 1 g.dispose() assert disp1 assert g.length == 0 g.add(d2) assert disp2 assert g.length == 0
class Trader: POLL_IMMEDIATELY = 1 POLL_SERVER_TIME_INTERVAL = 1000 POLL_PRICE_INTERVAL = 10000 POLL_BALANCE_INTERVAL = 600000 POLL_ACTIVE_ORDERS_INTERVAL = 3600000 POLL_COMPLETED_ORDERS_INTERVAL = 10000 SHOW_TIME_AND_PRICE_INTERVAL = 600000 REASON_PRICE_JUMP = 0 REASON_ORDER_COMPLETED = 1 def __init__(self, options: TradingOptions, events: Observable, commands: Observable): self._subscription = None self._options = options self._events = events self._commands = commands def __repr__(self): return 'Trader(pair=%s)' % self._options.pair def _get_time(self): return (self._events .filter(lambda event: isinstance(event, events.TimeEvent)) .map(lambda event: event.value)) def _get_price(self): return (self._events .filter(lambda event: isinstance(event, events.PriceEvent)) .filter(lambda event: event.pair == self._options.pair) .map(lambda event: event.value)) def _get_balance(self, event_stream, currency): return (event_stream .filter(lambda event: isinstance(event, events.BalanceEvent)) .filter(lambda event: event.currency == currency) .map(lambda event: event.value) .scan(lambda p, balance: d(balance=balance, change=(Decimal(0) if p.balance is None else balance - p.balance)), d(balance=None, change=None))) def _get_first_currency_balance(self): return self._get_balance(self._events, self._options.pair.first) def _get_second_currency_balance(self): return self._get_balance(self._events, self._options.pair.second) def _get_active_orders(self): return (self._events .filter(lambda event: isinstance(event, events.ActiveOrdersEvent)) .filter(lambda event: event.pair == self._options.pair) .map(lambda event: event.orders)) def _get_completed_orders_singly(self, event_stream, pair): return (event_stream .filter(lambda event: isinstance(event, events.CompletedOrdersEvent)) .filter(lambda event: event.pair == pair) .map(lambda event: event.orders) .scan(lambda p, orders: d(orders=orders, change=(set() if p.orders is None else set(orders) - set(p.orders))), d(orders=None, change=None)) .map(lambda p: p.change) .switch_map(Observable.from_iterable)) def _get_jumping_price(self): return (self._get_price() .scan(lambda prev, price: prev if prev and abs(price - prev) / prev < self._options.price_jump_value else price) .distinct_until_changed() .skip(1)) def init(self): logger.info('Starting %s', self) self._subscription = CompositeDisposable( self._subscribe_for_poll_server_time(), self._subscribe_for_poll_price(), self._subscribe_for_poll_balance(), self._subscribe_for_poll_active_orders(), self._subscribe_for_poll_completed_orders(), self._subscribe_for_time_and_price(), self._subscribe_for_balance(), self._subscribe_for_active_orders(), self._subscribe_for_completed_orders(), self._subscribe_for_jumping_price() ) def _subscribe_for_poll_server_time(self): return (Observable .timer(self.POLL_IMMEDIATELY, self.POLL_SERVER_TIME_INTERVAL, MAIN_THREAD) .subscribe(lambda count: self._commands.on_next(commands.GetServerTimeCommand()))) def _subscribe_for_poll_price(self): return (Observable .timer(self.POLL_IMMEDIATELY, self.POLL_PRICE_INTERVAL, MAIN_THREAD) .subscribe(lambda count: self._commands.on_next(commands.GetPriceCommand(self._options.pair)))) def _subscribe_for_poll_balance(self): return CompositeDisposable( (Observable .timer(self.POLL_IMMEDIATELY, self.POLL_BALANCE_INTERVAL, MAIN_THREAD) .subscribe(lambda count: self._commands.on_next(commands.GetBalanceCommand(self._options.pair.first)))), (Observable .timer(self.POLL_IMMEDIATELY, self.POLL_BALANCE_INTERVAL, MAIN_THREAD) .subscribe(lambda count: self._commands.on_next(commands.GetBalanceCommand(self._options.pair.second)))) ) def _subscribe_for_poll_active_orders(self): return (Observable .timer(self.POLL_IMMEDIATELY, self.POLL_ACTIVE_ORDERS_INTERVAL, MAIN_THREAD) .subscribe(lambda count: self._commands.on_next(commands.GetActiveOrdersCommand(self._options.pair)))) def _subscribe_for_poll_completed_orders(self): return (Observable .timer(self.POLL_IMMEDIATELY, self.POLL_COMPLETED_ORDERS_INTERVAL, MAIN_THREAD) .subscribe(lambda count: self._commands.on_next(commands.GetCompletedOrdersCommand(self._options.pair)))) def _subscribe_for_time_and_price(self): return (Observable .combine_latest( self._get_time(), self._get_price(), d('time', 'price') ) .throttle_first(self.SHOW_TIME_AND_PRICE_INTERVAL, MAIN_THREAD) .subscribe(lambda p: logger.info('[%s] Time now is %s, price is %s', self._options.pair, p.time, p.price))) def _subscribe_for_balance(self): return (Observable .combine_latest( self._get_first_currency_balance().map(lambda p: d(balance1=p.balance, change1=p.change)), self._get_second_currency_balance().map(lambda p: d(balance2=p.balance, change2=p.change)), d('balance1', 'change1', 'balance2', 'change2') ) .distinct_until_changed(lambda p: (p.balance1, p.balance2)) .subscribe(lambda p: logger.info('[%s] Balance is %s %s (%s) and %s %s (%s)', self._options.pair, p.balance1, self._options.pair.first, p.change1, p.balance2, self._options.pair.second, p.change2))) def _subscribe_for_active_orders(self): return CompositeDisposable( (self._get_active_orders() .subscribe(lambda orders: logger.info('[%s] Active orders: %s', self._options.pair, ', '.join(map(repr, orders))) if orders else logger.info('[%s] No active orders found', self._options.pair))), (self._get_active_orders() .switch_map(Observable.from_iterable) .filter(lambda order: datetime.utcnow() - order.created > config.ORDER_OUTDATE_PERIOD) .subscribe(self._cancel_order)) ) def _get_new_orders(self, completed_orders, min_amount): return (completed_orders .map(self._get_type_and_amount_and_price_for_new_order) .map(d('order_type', 'amount', 'price')) .filter(lambda p: p.amount >= min_amount)) def _get_new_sell_orders(self, event_stream, pair, min_amount): return (Observable .combine_latest( (self._get_new_orders(self._get_completed_orders_singly(event_stream, pair), min_amount) .filter(lambda p: p.order_type == Order.TYPE_SELL)), self._get_first_currency_balance().map(lambda p: p.balance), d('amount', 'price', 'balance') ) .distinct_until_changed(lambda p: (p.amount, p.price)) .filter(lambda p: p.amount <= p.balance)) def _get_new_buy_orders(self, event_stream, pair, min_amount): return (Observable .combine_latest( (self._get_new_orders(self._get_completed_orders_singly(event_stream, pair), min_amount) .filter(lambda p: p.order_type == Order.TYPE_BUY)), self._get_second_currency_balance().map(lambda p: p.balance), d('amount', 'price', 'balance') ) .distinct_until_changed(lambda p: (p.amount, p.price)) .filter(lambda p: p.amount * p.price <= p.balance)) def _subscribe_for_completed_orders(self): return CompositeDisposable( (self._get_completed_orders_singly(self._events, self._options.pair) .subscribe(lambda order: logger.info('[%s] %s completed', self._options.pair, order))), (self._get_new_sell_orders(self._events, self._options.pair, self._options.min_amount) .subscribe(lambda p: self._create_sell_order(p.amount, p.price, self.REASON_ORDER_COMPLETED))), (self._get_new_buy_orders(self._events, self._options.pair, self._options.min_amount) .subscribe(lambda p: self._create_buy_order(p.amount, p.price, self.REASON_ORDER_COMPLETED))) ) def _subscribe_for_jumping_price(self): return CompositeDisposable( (Observable .combine_latest( self._get_jumping_price().map(partial(self._get_new_price, Order.TYPE_SELL)), self._get_first_currency_balance().map(lambda p: p.balance), d('price', 'balance') ) .distinct_until_changed(lambda p: p.price) .filter(lambda p: self._options.deal_amount <= p.balance) .subscribe(lambda p: self._create_sell_order(self._options.deal_amount, p.price, self.REASON_PRICE_JUMP))), (Observable .combine_latest( self._get_jumping_price().map(partial(self._get_new_price, Order.TYPE_BUY)), self._get_second_currency_balance().map(lambda p: p.balance), d('price', 'balance') ) .distinct_until_changed(lambda p: p.price) .filter(lambda p: self._options.deal_amount * p.price <= p.balance) .subscribe(lambda p: self._create_buy_order(self._options.deal_amount, p.price, self.REASON_PRICE_JUMP))), ) def _get_type_and_amount_and_price_for_new_order(self, order): if order.type == Order.TYPE_SELL: return Order.TYPE_BUY, order.amount, self._get_new_price(Order.TYPE_BUY, order.price) if order.type == Order.TYPE_BUY: return Order.TYPE_SELL, order.amount, self._get_new_price(Order.TYPE_SELL, order.price) raise Exception('unknown order type %s' % order.type) def _get_new_price(self, order_type, price): margin = self._options.margin + self._get_random_margin_jitter(self._options.margin_jitter) if order_type == Order.TYPE_SELL: return normalize_value(price + price * margin, self._options.pair.second.places) if order_type == Order.TYPE_BUY: return normalize_value(price - price * margin, self._options.pair.second.places) raise Exception('unknown order type %s' % order_type) def _create_sell_order(self, amount, price, reason): logger.info('[%s] Create sell order: %s for %s, reason is %s', self._options.pair, amount, price, reason) self._commands.on_next(commands.CreateSellOrderCommand(self._options.pair, amount, price)) def _create_buy_order(self, amount, price, reason): logger.info('[%s] Create buy order: %s for %s, reason is %s', self._options.pair, amount, price, reason) self._commands.on_next(commands.CreateBuyOrderCommand(self._options.pair, amount, price)) def _get_random_margin_jitter(self, jitter): return normalize_value(Decimal(uniform(-float(jitter), float(jitter))), 4) def _cancel_order(self, order): logger.info('[%s] Cancel outdated order %s created %s (%s ago)', self._options.pair, order, order.created, datetime.utcnow() - order.created) self._commands.on_next(commands.CancelOrderCommand(order.id)) def deinit(self): logger.info('Stopping %s', self) if self._subscription is not None: self._subscription.dispose()
class ExchangeConnector: def __init__(self, events: Observable, commands: Observable): self._subscription = None self._public_api = _PublicApiConnector() self._trade_api = _TradeApiConnector(config.API_KEY, config.API_SECRET) self._events = events self._commands = commands def __repr__(self): return 'ExchangeConnector()' def init(self): logger.info('Starting %s', self) self._subscription = CompositeDisposable( self._subscribe_for_get_server_time_command(), self._subscribe_for_get_price_command(), self._subscribe_for_get_balance_command(), self._subscribe_for_get_active_orders_command(), self._subscribe_for_get_completed_orders_command(), self._subscribe_for_create_sell_order_command(), self._subscribe_for_create_buy_order_command(), self._subscribe_for_cancel_order_command(), ) def run(self): IOLoop.instance().start() def _subscribe_for_get_server_time_command(self): return (self._commands .filter(lambda command: isinstance(command, commands.GetServerTimeCommand)) .subscribe(lambda command: self._get_server_time())) def _subscribe_for_get_price_command(self): return (self._commands .filter(lambda command: isinstance(command, commands.GetPriceCommand)) .subscribe(lambda command: self._get_price(command.pair))) def _subscribe_for_get_balance_command(self): return (self._commands .filter(lambda command: isinstance(command, commands.GetBalanceCommand)) .subscribe(lambda command: self._get_balance(command.currency))) def _subscribe_for_get_active_orders_command(self): return (self._commands .filter(lambda command: isinstance(command, commands.GetActiveOrdersCommand)) .subscribe(lambda command: self._get_active_orders(command.pair))) def _subscribe_for_get_completed_orders_command(self): return (self._commands .filter(lambda command: isinstance(command, commands.GetCompletedOrdersCommand)) .subscribe(lambda command: self._get_completed_orders(command.pair))) def _subscribe_for_create_sell_order_command(self): return (self._commands .filter(lambda command: isinstance(command, commands.CreateSellOrderCommand)) .subscribe(lambda command: self._create_sell_order(command.pair, command.amount, command.price))) def _subscribe_for_create_buy_order_command(self): return (self._commands .filter(lambda command: isinstance(command, commands.CreateBuyOrderCommand)) .subscribe(lambda command: self._create_buy_order(command.pair, command.amount, command.price))) def _subscribe_for_cancel_order_command(self): return (self._commands .filter(lambda command: isinstance(command, commands.CancelOrderCommand)) .subscribe(lambda command: self._cancel_order(command.order_id))) @coroutine def _get_server_time(self): server_time = datetime.utcnow() self._events.on_next(events.TimeEvent(server_time)) @coroutine def _get_price(self, pair): try: price = yield self._public_api.get_price(_currency_pair_to_string(pair)) except Exception as e: logger.warn('Cannot get price: %s', e) else: self._events.on_next(events.PriceEvent(pair, normalize_value(price, pair.second.places))) @coroutine def _get_balance(self, currency): try: balance = yield self._trade_api.get_balance(currency.name.lower()) except Exception as e: logger.warn('Cannot get balance: %s', e) else: amount = normalize_value(balance, currency.places) self._events.on_next(events.BalanceEvent(currency, amount)) @coroutine def _get_active_orders(self, pair): try: orders = yield self._trade_api.get_active_orders(_currency_pair_to_string(pair)) orders = sorted((Order(int(order['id']), Order.TYPE_SELL if order['type'] == 'sell' else Order.TYPE_BUY, normalize_value(order['amount'], pair.first.places), normalize_value(order['price'], pair.second.places), order['created'], None) for order in orders), key=lambda order: order.price) except Exception as e: logger.warn('Cannot get active orders: %s', e) else: self._events.on_next(events.ActiveOrdersEvent(pair, orders)) @coroutine def _get_completed_orders(self, pair): try: orders = yield self._trade_api.get_completed_orders(_currency_pair_to_string(pair)) orders = sorted((Order(int(order['id']), Order.TYPE_SELL if order['type'] == 'sell' else Order.TYPE_BUY, normalize_value(order['amount'], pair.first.places), normalize_value(order['price'], pair.second.places), None, order['completed']) for order in orders), key=lambda order: order.completed, reverse=True) except Exception as e: logger.warn('Cannot get completed orders: %s', e) else: self._events.on_next(events.CompletedOrdersEvent(pair, orders)) @coroutine def _create_sell_order(self, pair, amount, price): logger.debug('Creating sell order (%s %s for %s %s)', amount, pair.first, price, pair.second) try: balance = yield self._trade_api.create_order(_TradeApiConnector.ORDER_TYPE_SELL, _currency_pair_to_string(pair), amount, price) except Exception as e: logger.debug('Cannot create sell order: %s', e) else: self._send_balance_events(balance) @coroutine def _create_buy_order(self, pair, amount, price): logger.debug('Creating buy order (%s %s for %s %s)', amount, pair.first, price, pair.second) try: balance = yield self._trade_api.create_order(_TradeApiConnector.ORDER_TYPE_BUY, _currency_pair_to_string(pair), amount, price) except Exception as e: logger.debug('Cannot create buy order: %s', e) else: self._send_balance_events(balance) @coroutine def _cancel_order(self, order_id): logger.debug('Cancelling order %s', order_id) try: balance = yield self._trade_api.cancel_order(order_id) except Exception as e: logger.debug('Cannot cancel order: %s', e) else: self._send_balance_events(balance) def _send_balance_events(self, balance): for currency in CURRENCIES: amount = balance.get(currency.name.lower()) if amount is not None: self._commands.on_next(events.BalanceEvent(currency, normalize_value(amount, currency.places))) def deinit(self): logger.info('Stopping %s', self) if self._subscription is not None: self._subscription.dispose()
d = build_gui(data) adf = os.system("clear") return json.dumps(d, indent=4, sort_keys=True) if __name__ == '__main__': signal.signal(signal.SIGTERM, signal_handler) signal.signal(signal.SIGINT, signal_handler) log.info('Starting BFG') # Make sure to init bfg before I start using it disposables = None try: betfair_access_layer.login(config.BETFAIR_USER, config.BETFAIR_PASSWORD, config.BETFAIR_APP_KEY) cache = cache_emitter() strategy_disposable = cache.let(strategy).subscribe( take_action, log.error) gui_disposable = cache \ .observe_on(gui_worker)\ .scan(lambda agg, new: agg.transform([new['marketId']], new), seed=m()) \ .map(render) \ .subscribe(print, log.error, lambda: log.warning('!!!THE STREAM COMPLETED')) disposables = CompositeDisposable(strategy_disposable, gui_disposable) while True: time.sleep(0.5) except ServiceExit: print('Shutting down takes 5 secs') disposables.dispose() betfair_access_layer.shut_down()