mirror of
https://github.com/wassname/catalyst.git
synced 2026-07-13 11:43:07 +08:00
BUG: for issue #237, checking considering open orders when verifying the exchange balance for each positions
This commit is contained in:
@@ -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,
|
||||
|
||||
@@ -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,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user