Error handling improvements

This commit is contained in:
Frederic Fortier
2017-08-19 19:25:06 -04:00
parent 1045dcd5bd
commit 2c9c4b0d05
3 changed files with 19 additions and 9 deletions
+6 -6
View File
@@ -37,18 +37,18 @@ def handle_data(context, data):
# bar_count=20,
# frequency='1d'
# )
ohlc = data.history(context.asset,
fields=['price', 'volume'],
bar_count=120,
frequency='1m'
)
# ohlc = data.history(context.asset,
# fields=['price', 'volume'],
# bar_count=120,
# frequency='1m'
# )
# ohlc = data.history([context.asset, symbol('iot_usd')],
# fields=['price', 'volume'],
# bar_count=20,
# frequency='1d'
# )
hist_price = ohlc['price']
# hist_price = ohlc['price']
cash = context.portfolio.cash
log.info('base currency available: {cash}'.format(cash=cash))
+4 -3
View File
@@ -11,6 +11,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
from datetime import time
from time import sleep
import logbook
import catalyst.protocol as zp
@@ -129,7 +130,7 @@ class ExchangeTradingAlgorithm(TradingAlgorithm):
'update portfolio attempt {}: {}'.format(attempt_index, e)
)
if attempt_index < self.retry_update_portfolio:
time.sleep(self.retry_delay)
sleep(self.retry_delay)
self._update_portfolio(attempt_index + 1)
def _check_open_orders(self, attempt_index=0):
@@ -140,7 +141,7 @@ class ExchangeTradingAlgorithm(TradingAlgorithm):
'check open orders attempt {}: {}'.format(attempt_index, e)
)
if attempt_index < self.retry_check_open_orders:
time.sleep(self.retry_delay)
sleep(self.retry_delay)
return self._check_open_orders(attempt_index + 1)
def handle_data(self, data):
@@ -174,7 +175,7 @@ class ExchangeTradingAlgorithm(TradingAlgorithm):
'order attempt {}: {}'.format(attempt_index, e)
)
if attempt_index < self.retry_order:
time.sleep(self.retry_delay)
sleep(self.retry_delay)
return self._order(
asset, amount, limit_price, stop_price, style,
attempt_index + 1)
+9
View File
@@ -15,6 +15,8 @@ from time import sleep
from logbook import Logger
import pandas as pd
import signal
import sys
from catalyst.gens.sim_engine import (
BAR,
@@ -57,6 +59,13 @@ class ExchangeClock(object):
self._before_trading_start_bar_yielded = True
def __iter__(self):
def signal_handler(signal, frame):
log.info('You pressed Ctrl+C!')
# yield pd.Timestamp.utcnow(), BAR
sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)
yield pd.Timestamp.utcnow(), SESSION_START
while True: