mirror of
https://github.com/wassname/catalyst.git
synced 2026-06-29 20:33:46 +08:00
Error handling improvements
This commit is contained in:
@@ -29,7 +29,7 @@ def initialize(context):
|
||||
|
||||
|
||||
def handle_data(context, data):
|
||||
log.info('handling bar {data}'.format(data=data))
|
||||
log.info('handling bar {}'.format(data.current_dt))
|
||||
# price_history = data.history(symbol('iot_usd'),
|
||||
# fields='price',
|
||||
# bar_count=20,
|
||||
@@ -129,8 +129,8 @@ def analyze(context, stats):
|
||||
|
||||
exchange_conn = dict(
|
||||
name='bitfinex',
|
||||
key='',
|
||||
secret=b'',
|
||||
key='yCN1b2LsLpjt8jmS4xi2ZZSfW9DRq4r9h2Aa9WOrKFr',
|
||||
secret=b'VOW6R7zbmBGsLh49FIK76qkHrwr2ovNb4FQ1N1d3cyC',
|
||||
base_currency='usd'
|
||||
)
|
||||
run_algorithm(
|
||||
|
||||
@@ -29,7 +29,7 @@ from catalyst.utils.api_support import (
|
||||
|
||||
from catalyst.utils.calendars.trading_calendar import days_at_time
|
||||
from catalyst.exchange.exchange_errors import (
|
||||
ExchangeRequestError
|
||||
ExchangeRequestError,
|
||||
)
|
||||
|
||||
log = logbook.Logger("ExchangeTradingAlgorithm")
|
||||
@@ -164,6 +164,8 @@ class ExchangeTradingAlgorithm(TradingAlgorithm):
|
||||
if attempt_index < self.retry_check_open_orders:
|
||||
sleep(self.retry_delay)
|
||||
return self._check_open_orders(attempt_index + 1)
|
||||
else:
|
||||
return list()
|
||||
|
||||
def handle_data(self, data):
|
||||
if not self.is_running:
|
||||
@@ -222,6 +224,8 @@ class ExchangeTradingAlgorithm(TradingAlgorithm):
|
||||
return self._order(
|
||||
asset, amount, limit_price, stop_price, style,
|
||||
attempt_index + 1)
|
||||
else:
|
||||
return None
|
||||
|
||||
@api_method
|
||||
@disallowed_in_before_trading_start(OrderInBeforeTradingStart())
|
||||
@@ -255,6 +259,8 @@ class ExchangeTradingAlgorithm(TradingAlgorithm):
|
||||
if attempt_index < self.retry_get_open_orders:
|
||||
sleep(self.retry_delay)
|
||||
return self._get_open_orders(asset, attempt_index + 1)
|
||||
else:
|
||||
return []
|
||||
|
||||
@error_keywords(sid='Keyword argument `sid` is no longer supported for '
|
||||
'get_open_orders. Use `asset` instead.')
|
||||
|
||||
@@ -7,6 +7,12 @@ class ExchangeRequestError(ZiplineError):
|
||||
).strip()
|
||||
|
||||
|
||||
class ExchangeRequestErrorTooManyAttempts(ZiplineError):
|
||||
msg = (
|
||||
'Request failed: {error}, giving up after {attempts} attempts'
|
||||
).strip()
|
||||
|
||||
|
||||
class InvalidHistoryFrequencyError(ZiplineError):
|
||||
msg = (
|
||||
'History frequency {frequency} not supported by the exchange.'
|
||||
|
||||
@@ -3,6 +3,7 @@ import re
|
||||
from runpy import run_path
|
||||
import sys
|
||||
import warnings
|
||||
from time import sleep
|
||||
|
||||
import pandas as pd
|
||||
|
||||
@@ -39,6 +40,10 @@ from catalyst.exchange.data_portal_exchange import DataPortalExchange
|
||||
from catalyst.exchange.bitfinex import Bitfinex
|
||||
from catalyst.exchange.asset_finder_exchange import AssetFinderExchange
|
||||
from catalyst.exchange.exchange_portfolio import PortfolioMemoryStore
|
||||
from catalyst.exchange.exchange_errors import (
|
||||
ExchangeRequestError,
|
||||
ExchangeRequestErrorTooManyAttempts
|
||||
)
|
||||
from logbook import Logger
|
||||
|
||||
log = Logger('run_algo')
|
||||
@@ -246,14 +251,35 @@ def _run(handle_data,
|
||||
first_trading_day=pd.to_datetime('today', utc=True)
|
||||
)
|
||||
choose_loader = None
|
||||
|
||||
def fetch_portfolio(attempt_index=0):
|
||||
"""
|
||||
Fetch the portfolio for the exchange
|
||||
We can't continue on error because it is required to bootstrap
|
||||
the algorithm.
|
||||
:param attempt_index:
|
||||
:return:
|
||||
"""
|
||||
try:
|
||||
return exchange.portfolio
|
||||
except ExchangeRequestError as e:
|
||||
if attempt_index < 20:
|
||||
sleep(5)
|
||||
return fetch_portfolio(attempt_index + 1)
|
||||
else:
|
||||
raise ExchangeRequestErrorTooManyAttempts(
|
||||
attempts=attempt_index,
|
||||
error=e
|
||||
)
|
||||
|
||||
portfolio = fetch_portfolio()
|
||||
sim_params = create_simulation_parameters(
|
||||
start=start,
|
||||
end=end,
|
||||
capital_base=exchange.portfolio.starting_cash,
|
||||
capital_base=portfolio.starting_cash,
|
||||
emission_rate='minute',
|
||||
data_frequency='minute'
|
||||
)
|
||||
# sim_params = None
|
||||
else:
|
||||
env = TradingEnvironment(environ=environ)
|
||||
choose_loader = None
|
||||
|
||||
Reference in New Issue
Block a user