def initialize(context): """ A function to define things to do at the start of the strategy """ # set the account currency, only valid for backtests set_account_currency("USD") # lot-size (mini-lot for most brokers) context.lot_size = 1000 # universe selection context.securities = [ symbol('AUD/USD'), symbol('EUR/CHF'), symbol('EUR/JPY'), symbol('EUR/USD'), symbol('GBP/USD'), symbol('NZD/USD'), symbol('USD/CAD'), symbol('USD/CHF'), symbol('USD/JPY'), ] # define strategy parameters context.params = { 'indicator_lookback': 375, 'indicator_freq': '1m', 'buy_signal_threshold': 0.5, 'sell_signal_threshold': -0.5, 'SMA_period_short': 15, 'SMA_period_long': 60, 'RSI_period': 60, 'trade_freq': 30, 'leverage': 1, 'pip_cost': 0.00003 } # variable to control trading frequency context.bar_count = 0 context.trading_hours = False # variables to track signals and target portfolio context.signals = dict((security, 0) for security in context.securities) context.target_position = dict( (security, 0) for security in context.securities) # set a timeout for trading schedule_function(stop_trading, date_rules.every_day(), time_rules.market_close(hours=0, minutes=31)) # call square off to zero out positions 30 minutes before close. schedule_function(daily_square_off, date_rules.every_day(), time_rules.market_close(hours=0, minutes=30))
def initialize(context): """ A function to define things to do at the start of the strategy """ # set the account currency, only valid for backtests set_account_currency("USD") # universe selection context.short_dollar_basket = { symbol('AUD/USD'): 1, symbol('EUR/USD'): 1, symbol('GBP/USD'): 1, symbol('NZD/USD'): 1, symbol('USD/CAD'): -1, symbol('USD/CHF'): -1, symbol('USD/JPY'): -1, } # Call rebalance function on the first trading day of each month after 2.5 hours from market open schedule_function(rebalance, date_rules.month_start(days_offset=0), time_rules.market_close(hours=2, minutes=30))
def initialize(context): """ function to define things to do at the start of the strategy """ # set the account currency, only valid for backtests set_account_currency("USD") # trading pound parity! # this should work after the European sovereign crisis settled down # and before the Brexit noise started (2012-2015) context.x = symbol('GBP/USD') context.y = symbol('EUR/USD') context.leverage = 5 context.signal = 0 # Trade entry and exit when the z_score is +/- entry_z_score and exit_z_score respectively context.entry_z_score = 2.0 context.exit_z_score = 0.5 # Lookback window context.lookback = 720 # used for zscore calculation context.z_window = 360 # Call strategy function after the London open every day schedule_function(pair_trading_strategy, date_rules.every_day(), time_rules.market_open(hours=9, minutes=30)) # square off towards to NYC close context.trading_hours = False # set a timeout for trading schedule_function(stop_trading, date_rules.every_day(), time_rules.market_close(hours=0, minutes=31)) # call square off to zero out positions 30 minutes before close. schedule_function(daily_square_off, date_rules.every_day(), time_rules.market_close(hours=0, minutes=30))