diff --git a/catalyst/exchange/exchange.py b/catalyst/exchange/exchange.py index bab87d3c..2f3ce3ed 100644 --- a/catalyst/exchange/exchange.py +++ b/catalyst/exchange/exchange.py @@ -21,7 +21,6 @@ from catalyst.exchange.utils.exchange_utils import get_exchange_symbols, \ resample_history_df, has_bundle from logbook import Logger - log = Logger('Exchange', level=LOG_LEVEL) @@ -657,16 +656,21 @@ class Exchange: return df - def _check_low_balance(self, currency, balances, amount): + def _check_low_balance(self, currency, balances, amount, open_orders=None): free = balances[currency]['free'] if currency in balances else 0.0 + if open_orders: + # TODO: make sure that this works + free += sum([order.amount for order in open_orders]) + if free < amount: return free, True else: return free, False - def sync_positions(self, positions, cash=None, check_balances=False): + def sync_positions(self, positions, open_orders=None, cash=None, + check_balances=False): """ Update the portfolio cash and position balances based on the latest ticker prices. @@ -695,7 +699,7 @@ class Exchange: balances=balances, amount=cash, ) - if is_lower: + if is_lower and not open_orders: raise NotEnoughCashError( currency=self.base_currency, exchange=self.name, diff --git a/catalyst/exchange/exchange_algorithm.py b/catalyst/exchange/exchange_algorithm.py index 5c83a74a..dc1f18e1 100644 --- a/catalyst/exchange/exchange_algorithm.py +++ b/catalyst/exchange/exchange_algorithm.py @@ -591,8 +591,6 @@ class ExchangeTradingAlgorithmLive(ExchangeTradingAlgorithmBase): if base_currency is None: base_currency = exchange.base_currency - # Don't check the cash if there are open orders. This could - # results in false positives. orders = [] for asset in self.blotter.open_orders: asset_orders = self.blotter.open_orders[asset] @@ -602,6 +600,7 @@ class ExchangeTradingAlgorithmLive(ExchangeTradingAlgorithmBase): required_cash = self.portfolio.cash if not orders else None cash, positions_value = exchange.sync_positions( positions=exchange_positions, + open_orders=orders, check_balances=check_balances, cash=required_cash, )