From ddf0c480a030e8a83e63fdc8b66b057b88fb24ce Mon Sep 17 00:00:00 2001 From: Frederic Fortier Date: Tue, 12 Dec 2017 14:43:06 -0500 Subject: [PATCH] BLD: testing each sample algo and fixing an issue with data.history --- catalyst/examples/buy_and_hodl.py | 1 - catalyst/examples/buy_btc_simple.py | 17 ++++++++- catalyst/examples/buy_low_sell_high.py | 21 ++++++++++-- catalyst/examples/buy_low_sell_high_live.py | 38 ++++++++------------- catalyst/examples/simple_loop.py | 2 +- catalyst/exchange/exchange_data_portal.py | 8 +---- 6 files changed, 52 insertions(+), 35 deletions(-) diff --git a/catalyst/examples/buy_and_hodl.py b/catalyst/examples/buy_and_hodl.py index 6722665f..af4774ea 100644 --- a/catalyst/examples/buy_and_hodl.py +++ b/catalyst/examples/buy_and_hodl.py @@ -61,7 +61,6 @@ def handle_data(context, data): context.asset, target_hodl_value, limit_price=price * 1.1, - stop_price=price * 0.9, ) record( diff --git a/catalyst/examples/buy_btc_simple.py b/catalyst/examples/buy_btc_simple.py index 52463d58..51c88adb 100644 --- a/catalyst/examples/buy_btc_simple.py +++ b/catalyst/examples/buy_btc_simple.py @@ -21,8 +21,9 @@ To see which assets are available on each exchange, visit: https://www.enigma.co/catalyst/status ''' - +from catalyst import run_algorithm from catalyst.api import order, record, symbol +import pandas as pd def initialize(context): @@ -32,3 +33,17 @@ def initialize(context): def handle_data(context, data): order(context.asset, 1) record(btc=data.current(context.asset, 'price')) + + +if __name__ == '__main__': + run_algorithm( + capital_base=10000, + data_frequency='daily', + initialize=initialize, + handle_data=handle_data, + exchange_name='bitfinex', + algo_namespace='buy_and_hodl', + base_currency='usd', + start=pd.to_datetime('2015-03-01', utc=True), + end=pd.to_datetime('2017-10-31', utc=True), + ) diff --git a/catalyst/examples/buy_low_sell_high.py b/catalyst/examples/buy_low_sell_high.py index b3afb723..f4c53973 100644 --- a/catalyst/examples/buy_low_sell_high.py +++ b/catalyst/examples/buy_low_sell_high.py @@ -13,6 +13,7 @@ instructions on how to install the required dependencies. import talib from logbook import Logger +from catalyst import run_algorithm from catalyst.api import ( order, order_target_percent, @@ -21,6 +22,7 @@ from catalyst.api import ( get_open_orders, ) from catalyst.exchange.stats_utils import get_pretty_stats +import pandas as pd algo_namespace = 'buy_low_sell_high_xrp' log = Logger(algo_namespace) @@ -101,8 +103,8 @@ def _handle_data(context, data): if price < cost_basis: is_buy = True - elif(position.amount > 0 - and price > cost_basis * (1 + context.PROFIT_TARGET)): + elif (position.amount > 0 + and price > cost_basis * (1 + context.PROFIT_TARGET)): profit = (price * position.amount) - (cost_basis * position.amount) log.info('closing position, taking profit: {}'.format(profit)) order_target_percent( @@ -157,3 +159,18 @@ def handle_data(context, data): def analyze(context, stats): log.info('the daily stats:\n{}'.format(get_pretty_stats(stats))) pass + + +if __name__ == '__main__': + run_algorithm( + capital_base=10000, + data_frequency='daily', + initialize=initialize, + handle_data=handle_data, + analyze=analyze, + exchange_name='poloniex', + algo_namespace='buy_and_hodl', + base_currency='usd', + start=pd.to_datetime('2015-03-01', utc=True), + end=pd.to_datetime('2017-10-31', utc=True), + ) diff --git a/catalyst/examples/buy_low_sell_high_live.py b/catalyst/examples/buy_low_sell_high_live.py index 422033a6..cfd5854c 100644 --- a/catalyst/examples/buy_low_sell_high_live.py +++ b/catalyst/examples/buy_low_sell_high_live.py @@ -41,7 +41,7 @@ def _handle_data(context, data): context.asset, fields='price', bar_count=20, - frequency='1d' + frequency='1D' ) rsi = talib.RSI(prices.values, timeperiod=14)[-1] log.info('got rsi: {}'.format(rsi)) @@ -88,8 +88,8 @@ def _handle_data(context, data): if price < cost_basis: is_buy = True - elif(position.amount > 0 - and price > cost_basis * (1 + context.PROFIT_TARGET)): + elif (position.amount > 0 + and price > cost_basis * (1 + context.PROFIT_TARGET)): profit = (price * position.amount) - (cost_basis * position.amount) log.info('closing position, taking profit: {}'.format(profit)) order_target_percent( @@ -146,23 +146,15 @@ def analyze(context, stats): pass -run_algorithm( - capital_base=100000, - initialize=initialize, - handle_data=handle_data, - analyze=analyze, - exchange_name='poloniex', - start=pd.to_datetime('2017-5-01', utc=True), - end=pd.to_datetime('2017-10-16', utc=True), - base_currency='usdt', - data_frequency='daily' -) -# run_algorithm( -# initialize=initialize, -# handle_data=handle_data, -# analyze=analyze, -# exchange_name='poloniex', -# live=True, -# algo_namespace=algo_namespace, -# base_currency='btc' -# ) +if __name__ == '__main__': + run_algorithm( + capital_base=0.001, + initialize=initialize, + handle_data=handle_data, + analyze=analyze, + exchange_name='bittrex', + live=True, + algo_namespace=algo_namespace, + base_currency='btc', + simulate_orders=True, + ) diff --git a/catalyst/examples/simple_loop.py b/catalyst/examples/simple_loop.py index f6312096..ce977e0a 100644 --- a/catalyst/examples/simple_loop.py +++ b/catalyst/examples/simple_loop.py @@ -127,7 +127,7 @@ run_algorithm( initialize=initialize, handle_data=handle_data, analyze=None, - exchange_name='gdax', + exchange_name='poloniex', live=True, algo_namespace='simple_loop', base_currency='eth', diff --git a/catalyst/exchange/exchange_data_portal.py b/catalyst/exchange/exchange_data_portal.py index 474df98e..02d88ca0 100644 --- a/catalyst/exchange/exchange_data_portal.py +++ b/catalyst/exchange/exchange_data_portal.py @@ -238,12 +238,6 @@ class DataPortalExchangeLive(DataPortalExchangeBase): """ exchange = self.exchanges[exchange_name] - if end_dt >= pd.Timestamp.utcnow().floor('1T'): - is_current = True - - else: - is_current = False - df = exchange.get_history_window( assets, end_dt, @@ -251,7 +245,7 @@ class DataPortalExchangeLive(DataPortalExchangeBase): frequency, field, data_frequency, - is_current) + False) return df def get_exchange_spot_value(self, exchange_name, assets, field, dt,