diff --git a/catalyst/examples/dual_moving_average.py b/catalyst/examples/dual_moving_average.py index 00eb01af..88a6fad5 100644 --- a/catalyst/examples/dual_moving_average.py +++ b/catalyst/examples/dual_moving_average.py @@ -4,8 +4,7 @@ import pandas as pd from logbook import Logger from catalyst import run_algorithm -from catalyst.api import (record, symbol, order_target_percent, - get_open_orders) +from catalyst.api import (record, symbol, order_target_percent,) from catalyst.exchange.utils.stats_utils import extract_transactions NAMESPACE = 'dual_moving_average' @@ -21,7 +20,7 @@ def initialize(context): def handle_data(context, data): # define the windows for the moving averages short_window = 2 - long_window = 3 + long_window = 5 # Skip as many bars as long_window to properly compute the average context.i += 1 @@ -63,7 +62,7 @@ def handle_data(context, data): # Since we are using limit orders, some orders may not execute immediately # we wait until all orders are executed before considering more trades. - orders = get_open_orders(context.asset) + orders = context.blotter.open_orders if len(orders) > 0: return diff --git a/catalyst/exchange/ccxt/ccxt_exchange.py b/catalyst/exchange/ccxt/ccxt_exchange.py index 40f4b4d3..875661e9 100644 --- a/catalyst/exchange/ccxt/ccxt_exchange.py +++ b/catalyst/exchange/ccxt/ccxt_exchange.py @@ -980,7 +980,8 @@ class CCXT(Exchange): ) raise ExchangeRequestError(error=e) - def cancel_order(self, order_param, asset_or_symbol=None): + def cancel_order(self, order_param, + asset_or_symbol=None, params={}): order_id = order_param.id \ if isinstance(order_param, Order) else order_param @@ -992,7 +993,8 @@ class CCXT(Exchange): try: symbol = self.get_symbol(asset_or_symbol) \ if asset_or_symbol is not None else None - self.api.cancel_order(id=order_id, symbol=symbol) + self.api.cancel_order(id=order_id, + symbol=symbol, params= params) except (ExchangeError, NetworkError) as e: log.warn( diff --git a/catalyst/exchange/exchange.py b/catalyst/exchange/exchange.py index a0a247fb..20cbe967 100644 --- a/catalyst/exchange/exchange.py +++ b/catalyst/exchange/exchange.py @@ -705,7 +705,7 @@ class Exchange: balances=balances, amount=cash, ) - if is_lower and not open_orders: + if is_lower: raise NotEnoughCashError( currency=self.base_currency, exchange=self.name, @@ -932,7 +932,8 @@ class Exchange: """ @abstractmethod - def cancel_order(self, order_param, symbol_or_asset=None): + def cancel_order(self, order_param, + symbol_or_asset=None, params={}): """Cancel an open order. Parameters @@ -941,6 +942,7 @@ class Exchange: The order_id or order object to cancel. symbol_or_asset: str|TradingPair The catalyst symbol, some exchanges need this + params: """ pass diff --git a/catalyst/exchange/exchange_algorithm.py b/catalyst/exchange/exchange_algorithm.py index c827af4a..5bf103fa 100644 --- a/catalyst/exchange/exchange_algorithm.py +++ b/catalyst/exchange/exchange_algorithm.py @@ -388,6 +388,7 @@ class ExchangeTradingAlgorithmLive(ExchangeTradingAlgorithmBase): self.stats_minutes = 1 self._last_orders = [] + self._last_open_orders = [] self.trading_client = None super(ExchangeTradingAlgorithmLive, self).__init__(*args, **kwargs) @@ -791,12 +792,17 @@ class ExchangeTradingAlgorithmLive(ExchangeTradingAlgorithmBase): self.nullify_frame_stats(now=data.current_dt) self.performance_needs_update = False - orders = list(self.perf_tracker.todays_performance.orders_by_id.keys()) - if orders != self._last_orders: + last_orders_list = list(self.blotter.orders.keys()) + open_orders_list = list(self.blotter.open_orders.keys()) + + if last_orders_list != self._last_orders or \ + open_orders_list != self._last_open_orders: self.performance_needs_update = True - # Saving current orders to detect changes in the next frame - self._last_orders = copy.deepcopy(orders) + # Saving current order positions + # to detect changes in the next frame + self._last_orders = copy.deepcopy(last_orders_list) + self._last_open_orders = copy.deepcopy(open_orders_list) if self.performance_needs_update: self.perf_tracker.update_performance() @@ -1011,13 +1017,19 @@ class ExchangeTradingAlgorithmLive(ExchangeTradingAlgorithmBase): args=(order_id,)) @api_method - def cancel_order(self, order_param, exchange_name): + def cancel_order(self, order_param, exchange_name, + symbol=None, params={}): """Cancel an open order. Parameters ---------- order_param : str or Order The order_id or order object to cancel. + + exchange_name: name of exchange from + which you want to cancel the order + symbol: + params: """ exchange = self.exchanges[exchange_name] @@ -1031,4 +1043,4 @@ class ExchangeTradingAlgorithmLive(ExchangeTradingAlgorithmBase): sleeptime=self.attempts['retry_sleeptime'], retry_exceptions=(ExchangeRequestError,), cleanup=lambda: log.warn('cancelling order again.'), - args=(order_id,)) + args=(order_id, symbol, params))