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_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 _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_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 _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_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 provider_get_new_orders(): return ( ((1,), None), ((3,), d(order_type='order_type', amount=3, price='price')), )
def provider_get_balance(): return ( (('event',), None), ((events.BalanceEvent('currency', 'value'),), d(balance='value', change=Decimal(0))), ((events.BalanceEvent('currency', 1), events.BalanceEvent('currency', 2)), d(balance=2, change=1)), )
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))