本文整理汇总了Python中rx.disposables.CompositeDisposable.dispose方法的典型用法代码示例。如果您正苦于以下问题:Python CompositeDisposable.dispose方法的具体用法?Python CompositeDisposable.dispose怎么用?Python CompositeDisposable.dispose使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类rx.disposables.CompositeDisposable
的用法示例。
在下文中一共展示了CompositeDisposable.dispose方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_groupdisposable_addafterdispose
# 需要导入模块: from rx.disposables import CompositeDisposable [as 别名]
# 或者: from rx.disposables.CompositeDisposable import dispose [as 别名]
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
示例2: test_groupdisposable_addafterdispose
# 需要导入模块: from rx.disposables import CompositeDisposable [as 别名]
# 或者: from rx.disposables.CompositeDisposable import dispose [as 别名]
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
示例3: __init__
# 需要导入模块: from rx.disposables import CompositeDisposable [as 别名]
# 或者: from rx.disposables.CompositeDisposable import dispose [as 别名]
#.........这里部分代码省略.........
.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()
示例4: __init__
# 需要导入模块: from rx.disposables import CompositeDisposable [as 别名]
# 或者: from rx.disposables.CompositeDisposable import dispose [as 别名]
#.........这里部分代码省略.........
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()