def initialize(context): context.lookback = 12 * 21 context.offset = 1 * 21 context.min_volume = 1E8 context.max_size = 10 context.min_size = 5 context.weight = 0 context.universe = [] if context.broker.name == 'regT': context.hedge = symbol('SPY') elif context.broker.name == 'nse-backtest': context.hedge = symbol('NIFTY-I') else: raise ValueError(f'this broker not supported:{context.broker.name}') context.hedge_threshold = 10000 schedule_function(strategy, date_rules.month_start(days_offset=0), time_rules.market_close(hours=2, minutes=30)) attach_pipeline(make_strategy_pipeline(context), name='strategy_pipeline') schedule_function(hedge, date_rules.every_day(), time_rules.market_open(hours=0, minutes=30)) schedule_function(hedge, date_rules.every_day(), time_rules.market_close(hours=0, minutes=30))
def initialize(context): """ API function to define things to do at the start of the strategy. """ # set strategy parameters context.lookback_data = 60 context.lookback_long = 20 context.leverage = 2.0 # reset everything at start daily_reset(context) # create our universe create_universe(context) # schedule calculation at the end of opening range (30 minutes) schedule_function(calculate_trading_metrics, date_rules.every_day(), time_rules.market_open(hours=0, minutes=30)) # schedule entry rules schedule_function(no_more_entry, date_rules.every_day(), time_rules.market_open(hours=1, minutes=30)) # schedule exit rules schedule_function(unwind, date_rules.every_day(), time_rules.market_close(hours=0, minutes=30)) # set trading costs set_commission(commission.PerShare(cost=0.0, min_trade_cost=0.0)) set_slippage(slippage.FixedSlippage(0.00))
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): """ 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))