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): """ 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): context.lookback = 12*21 context.offset = 1*21 context.size = 5 context.weight = 0 context.candidates = [] context.universe = [ symbol('SPY'), # large cap symbol('QQQ'), # tech symbol('VUG'), # growth symbol('QUAL'), # quality symbol('MTUM'), # momentum symbol('IWM'), # small cap symbol('USMV'), # min vol symbol('HDV'), # dividend symbol('VEU'), # world equity symbol('VWO'), # EM equity symbol('DBC'), # commodities symbol('USO'), # oil symbol('GLD'), # gold symbol('AGG'), # bonds symbol('TIP'), # inflation ] attach_pipeline(make_strategy_pipeline(context), name='strategy_pipeline') schedule_function(rebalance, date_rules.month_start(days_offset=0), time_rules.market_close(hours=2, 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): context.params = {'lookback': 12, 'min_volume': 1E7} schedule_function(strategy, date_rules.month_start(), time_rules.market_close(minutes=1)) attach_pipeline(make_strategy_pipeline(context), name='strategy_pipeline')
def initialize(context): """ function to define things to do at the start of the strategy """ # The context variables can be accessed by other methods context.params = {'lookback': 12, 'percentile': 0.05, 'min_volume': 1E8} # Call rebalance function on the first trading day of each month schedule_function(strategy, date_rules.month_start(), time_rules.market_close(minutes=1)) # Set up the pipe-lines for strategies attach_pipeline(make_strategy_pipeline(context), name='strategy_pipeline')
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))
def initialize(context): """ A function to define things to do at the start of the strategy """ # universe selection context.universe = [ symbol('AMZN'), symbol('FB'), symbol('AAPL'), symbol('GOOGL'), symbol('NFLX'), ] # 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): ''' A function to define things to do at the start of the strategy ''' # universe selection context.universe = [symbol('NIFTY-I'), symbol('BANKNIFTY-I')] # 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': 300, 'BBands_period': 300, 'ADX_period': 120, 'trade_freq': 15, 'leverage': 1 } # variable to control trading frequency context.bar_count = 0 # variables to track target portfolio context.weights = dict((security, 0.0) for security in context.universe) # set trading cost and slippage to zero set_commission(commission.PerShare(cost=0.0, min_trade_cost=0.0)) set_slippage(slippage.FixedSlippage(0.00)) # create the list of experts as well as the agent controlling them expert1 = Advisor('bbands_ea', expert_advisor_1, context.universe) expert2 = Advisor('maxover_ea', expert_advisor_2, context.universe) expert3 = Advisor('rsi_ea', expert_advisor_3, context.universe) expert4 = Advisor('sup_res_ea', expert_advisor_4, context.universe) context.agent = Agent([expert1, expert2, expert3, expert4]) # schedule agent weights updates schedule_function(update_agent_weights, date_rules.week_start(), time_rules.market_close())
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 """ context.weights = {} # the weights to trade # strategy parameters context.params = {'lookback_vol':252, 'lookback_ret':5, 'percentile':0.05, 'min_volume':1E8, 'universe':100, } # Call rebalance function on the first trading day of each week schedule_function(strategy, date_rules.week_start(), time_rules.market_close(minutes=30)) # Set up the pipeline attach_pipeline(make_strategy_pipeline(context), name='strategy_pipeline')
def initialize(context): """ A function to define things to do at the start of the strategy """ # universe selection context.long_portfolio = [ symbol('AMZN'), symbol('AAPL'), symbol('WMT'), symbol('MU'), symbol('BAC'), symbol('KO'), symbol('BA'), symbol('AXP') ] # 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): """ A function to define things to do at the start of the strategy """ # universe selection context.long_portfolio = [ symbol('DIVISLAB'), symbol('SUNPHARMA'), symbol('MARUTI'), symbol('AMARAJABAT'), symbol('BPCL'), symbol('BAJFINANCE'), symbol('HDFCBANK'), symbol('ASIANPAINT'), symbol('TCS') ] # 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))