diff --git a/catalyst/examples/buy_and_hodl.py b/catalyst/examples/buy_and_hodl.py index 59aed942..c3b832f1 100644 --- a/catalyst/examples/buy_and_hodl.py +++ b/catalyst/examples/buy_and_hodl.py @@ -23,24 +23,24 @@ from catalyst.api import ( get_open_orders, ) -ASSET = 'USDT_BTC' - -TARGET_HODL_RATIO = 0.8 -RESERVE_RATIO = 1.0 - TARGET_HODL_RATIO - -# For all trading pairs in the poloniex bundle, the default denomination -# currently supported by Catalyst is 1/10th of a full coin. Use this constant -# to scale the price of up to that of a full coin if desired. -UNITS_PER_COIN = 10.0 def initialize(context): + context.ASSET_NAME = 'USDT_ETH' + context.TARGET_HODL_RATIO = 0.8 + context.RESERVE_RATIO = 1.0 - context.TARGET_HODL_RATIO + + # For all trading pairs in the poloniex bundle, the default denomination + # currently supported by Catalyst is 1/1000th of a full coin. Use this + # constant to scale the price of up to that of a full coin if desired. + context.TICK_SIZE = 1000.0 + context.is_buying = True - context.asset = symbol(ASSET) + context.asset = symbol(context.ASSET_NAME) def handle_data(context, data): - cash = context.portfolio.cash - target_hodl_value = TARGET_HODL_RATIO * context.portfolio.starting_cash - reserve_value = RESERVE_RATIO * context.portfolio.starting_cash + starting_cash = context.portfolio.starting_cash + target_hodl_value = context.TARGET_HODL_RATIO * starting_cash + reserve_value = context.RESERVE_RATIO * starting_cash # Cancel any outstanding orders orders = get_open_orders(context.asset) or [] @@ -48,6 +48,7 @@ def handle_data(context, data): cancel_order(order) # Stop buying after passing the reserve threshold + cash = context.portfolio.cash if cash <= reserve_value: context.is_buying = False @@ -79,8 +80,8 @@ def analyze(context=None, results=None): ax1.set_ylabel('Portfolio Value (USD)') ax2 = plt.subplot(512, sharex=ax1) - ax2.set_ylabel('{asset} (USD)'.format(asset=ASSET)) - (UNITS_PER_COIN * results[['price']]).plot(ax=ax2) + ax2.set_ylabel('{asset} (USD)'.format(asset=context.ASSET_NAME)) + (context.TICK_SIZE * results[['price']]).plot(ax=ax2) trans = results.ix[[t != [] for t in results.transactions]] buys = trans.ix[ @@ -88,7 +89,7 @@ def analyze(context=None, results=None): ] ax2.plot( buys.index, - UNITS_PER_COIN * results.price[buys.index], + context.TICK_SIZE * results.price[buys.index], '^', markersize=10, color='g', @@ -125,14 +126,3 @@ def analyze(context=None, results=None): # Show the plot. plt.gcf().set_size_inches(18, 8) plt.show() - - -def _test_args(): - """Extra arguments to use when catalyst's automated tests run this example. - """ - import pandas as pd - - return { - 'start': pd.Timestamp('2008', tz='utc'), - 'end': pd.Timestamp('2013', tz='utc'), - } diff --git a/catalyst/examples/dual_vwap.py b/catalyst/examples/dual_vwap.py index 86c73d4b..81155ba1 100644 --- a/catalyst/examples/dual_vwap.py +++ b/catalyst/examples/dual_vwap.py @@ -31,24 +31,24 @@ from catalyst.pipeline import Pipeline from catalyst.pipeline.data import CryptoPricing from catalyst.pipeline.factors.crypto import VWAP -ASSET = 'USDT_BTC' - -TARGET_INVESTMENT_RATIO = 0.8 -SHORT_WINDOW = 30 -LONG_WINDOW = 100 - -# For all trading pairs in the poloniex bundle, the default denomination -# currently supported by Catalyst is 1/10th of a full coin. Use this constant -# to scale the price of up to that of a full coin if desired. -UNITS_PER_COIN = 10.0 def initialize(context): + context.ASSET_NAME = 'USDT_BTC' + context.TARGET_INVESTMENT_RATIO = 0.8 + context.SHORT_WINDOW = 30 + context.LONG_WINDOW = 100 + + # For all trading pairs in the poloniex bundle, the default denomination + # currently supported by Catalyst is 1/1000th of a full coin. Use this + # constant to scale the price of up to that of a full coin if desired. + context.TICK_SIZE = 1000.0 + context.i = 0 - context.asset = symbol(ASSET) + context.asset = symbol(context.ASSET_NAME) set_max_leverage(1.0) - attach_pipeline(make_pipeline(), 'vwap_pipeline') + attach_pipeline(make_pipeline(context), 'vwap_pipeline') schedule_function( rebalance, @@ -59,12 +59,13 @@ def initialize(context): def before_trading_start(context, data): context.pipeline_data = pipeline_output('vwap_pipeline') -def make_pipeline(): +def make_pipeline(context): return Pipeline( columns={ 'price': CryptoPricing.open.latest, - 'short_mavg': VWAP(window_length=SHORT_WINDOW), - 'long_mavg': VWAP(window_length=LONG_WINDOW), + 'volume': CryptoPricing.volume.latest, + 'short_mavg': VWAP(window_length=context.SHORT_WINDOW), + 'long_mavg': VWAP(window_length=context.LONG_WINDOW), } ) @@ -72,7 +73,7 @@ def rebalance(context, data): context.i += 1 # skip first LONG_WINDOW bars to fill windows - if context.i < LONG_WINDOW: + if context.i < context.LONG_WINDOW: return # get pipeline data for asset of interest @@ -83,6 +84,7 @@ def rebalance(context, data): short_mavg = pipeline_data.short_mavg long_mavg = pipeline_data.long_mavg price = pipeline_data.price + volume = pipeline_data.volume # check that order has not already been placed open_orders = get_open_orders() @@ -91,9 +93,15 @@ def rebalance(context, data): if data.can_trade(context.asset): # adjust portfolio based on comparison of long and short vwap if short_mavg > long_mavg: - order_target_percent(context.asset, TARGET_INVESTMENT_RATIO) + order_target_percent( + context.asset, + context.TARGET_INVESTMENT_RATIO, + ) elif short_mavg < long_mavg: - order_target_percent(context.asset, 0.0) + order_target_percent( + context.asset, + 0.0, + ) record( price=price, @@ -101,23 +109,22 @@ def rebalance(context, data): leverage=context.account.leverage, short_mavg=short_mavg, long_mavg=long_mavg, + volume=volume, ) -# Note: this function can be removed if running -# this algorithm on quantopian.com def analyze(context=None, results=None): import matplotlib.pyplot as plt # Plot the portfolio and asset data. - ax1 = plt.subplot(511) + ax1 = plt.subplot(611) results[['portfolio_value']].plot(ax=ax1) ax1.set_ylabel('Portfolio value (USD)') - ax2 = plt.subplot(512, sharex=ax1) - ax2.set_ylabel('{asset} (USD)'.format(asset=ASSET)) - (UNITS_PER_COIN*results[['price', 'short_mavg', 'long_mavg']]).plot(ax=ax2) + ax2 = plt.subplot(612, sharex=ax1) + ax2.set_ylabel('{asset} (USD)'.format(asset=context.ASSET_NAME)) + (context.TICK_SIZE*results[['price', 'short_mavg', 'long_mavg']]).plot(ax=ax2) trans = results.ix[[t != [] for t in results.transactions]] amounts = [t[0]['amount'] for t in trans.transactions] @@ -131,24 +138,24 @@ def analyze(context=None, results=None): ax2.plot( buys.index, - UNITS_PER_COIN * results.price[buys.index], + context.TICK_SIZE * results.price[buys.index], '^', markersize=10, color='g', ) ax2.plot( sells.index, - UNITS_PER_COIN * results.price[sells.index], + context.TICK_SIZE * results.price[sells.index], 'v', markersize=10, color='r', ) - ax3 = plt.subplot(513, sharex=ax1) + ax3 = plt.subplot(613, sharex=ax1) results[['leverage', 'alpha', 'beta']].plot(ax=ax3) ax3.set_ylabel('Leverage (USD)') - ax4 = plt.subplot(514, sharex=ax1) + ax4 = plt.subplot(614, sharex=ax1) results[['cash']].plot(ax=ax4) ax4.set_ylabel('Cash (USD)') @@ -162,7 +169,7 @@ def analyze(context=None, results=None): 'benchmark_period_return', ]] - ax5 = plt.subplot(515, sharex=ax1) + ax5 = plt.subplot(615, sharex=ax1) results[[ 'treasury', 'algorithm', @@ -170,19 +177,12 @@ def analyze(context=None, results=None): ]].plot(ax=ax5) ax5.set_ylabel('Percent Change') + ax6 = plt.subplot(616, sharex=ax1) + results[['volume']].plot(ax=ax6) + ax6.set_ylabel('Volume (mBTC/day)') + plt.legend(loc=3) # Show the plot. plt.gcf().set_size_inches(18, 8) plt.show() - - -def _test_args(): - """Extra arguments to use when catalyst's automated tests run this example. - """ - import pandas as pd - - return { - 'start': pd.Timestamp('2014-01-01', tz='utc'), - 'end': pd.Timestamp('2014-11-01', tz='utc'), - }