From ba0208910f59de83c7adbc214888a02e16e25bef Mon Sep 17 00:00:00 2001 From: Frederic Fortier Date: Sat, 6 Jan 2018 19:37:53 -0500 Subject: [PATCH] BUG: Fixed issue #142 by removing unecessary capital_base check since we are now validating positions and cash against the exchange --- catalyst/exchange/exchange_algorithm.py | 4 -- catalyst/exchange/utils/exchange_utils.py | 14 ++++++ catalyst/utils/run_algo.py | 61 +---------------------- 3 files changed, 15 insertions(+), 64 deletions(-) diff --git a/catalyst/exchange/exchange_algorithm.py b/catalyst/exchange/exchange_algorithm.py index 8e369f8e..efd10553 100644 --- a/catalyst/exchange/exchange_algorithm.py +++ b/catalyst/exchange/exchange_algorithm.py @@ -551,10 +551,6 @@ class ExchangeTradingAlgorithmLive(ExchangeTradingAlgorithmBase): positions, returning the available cash, and raising error if the data goes out of sync. - Parameters - ---------- - attempt_index: int - Returns ------- float diff --git a/catalyst/exchange/utils/exchange_utils.py b/catalyst/exchange/utils/exchange_utils.py index ab16f71d..ebc678d2 100644 --- a/catalyst/exchange/utils/exchange_utils.py +++ b/catalyst/exchange/utils/exchange_utils.py @@ -733,3 +733,17 @@ def get_candles_df(candles, field, freq, bar_count, end_dt, df.dropna(inplace=True) return df + + +def fetch_capital_base(exchange): + """ + Fetch the base currency amount required to bootstrap + the algorithm against the exchange. + + The algorithm cannot continue without this value. + + :param exchange: the targeted exchange + :param attempt_index: + :return capital_base: the amount of base currency available for + trading + """ diff --git a/catalyst/utils/run_algo.py b/catalyst/utils/run_algo.py index 015f91e4..af67c29f 100644 --- a/catalyst/utils/run_algo.py +++ b/catalyst/utils/run_algo.py @@ -15,6 +15,7 @@ from catalyst.data.data_portal import DataPortal from catalyst.exchange.exchange_pricing_loader import ExchangePricingLoader, \ TradingPairPricing from catalyst.exchange.utils.factory import get_exchange +from redo import retry try: from pygments import highlight @@ -199,66 +200,6 @@ def _run(handle_data, first_trading_day=pd.to_datetime('today', utc=True) ) - def fetch_capital_base(exchange, attempt_index=0): - """ - Fetch the base currency amount required to bootstrap - the algorithm against the exchange. - - The algorithm cannot continue without this value. - - :param exchange: the targeted exchange - :param attempt_index: - :return capital_base: the amount of base currency available for - trading - """ - try: - log.debug('retrieving capital base in {} to bootstrap ' - 'exchange {}'.format(base_currency, exchange_name)) - balances = exchange.get_balances() - except ExchangeRequestError as e: - if attempt_index < 20: - log.warn( - 'could not retrieve balances on {}: {}'.format( - exchange.name, e - ) - ) - sleep(5) - return fetch_capital_base(exchange, attempt_index + 1) - - else: - raise ExchangeRequestErrorTooManyAttempts( - attempts=attempt_index, - error=e - ) - - if base_currency in balances: - base_currency_available = balances[base_currency]['free'] - log.info( - 'base currency available in the account: {} {}'.format( - base_currency_available, base_currency - ) - ) - - return base_currency_available - else: - raise BaseCurrencyNotFoundError( - base_currency=base_currency, - exchange=exchange_name - ) - - if not simulate_orders: - for exchange_name in exchanges: - exchange = exchanges[exchange_name] - balance = fetch_capital_base(exchange) - - if balance < capital_base: - raise NotEnoughCapitalError( - exchange=exchange_name, - base_currency=base_currency, - balance=balance, - capital_base=capital_base, - ) - sim_params = create_simulation_parameters( start=start, end=end,