def initialize(context):
    # Turn off the slippage model
    set_slippage(slippage.FixedSlippage(spread=0.0))
    # Set the commission model
    set_commission(commission.PerShare(cost=0.01, min_trade_cost=1.0))
    context.day = -1 # using zero-based counter for days
    context.set_benchmark(symbol('DIA'))
    context.assets = []
    print('Setup investable assets...')
    for ticker in asset_tickers:
        #print(ticker)
        context.assets.append(symbol(ticker))
    context.n_asset = len(context.assets)
    context.n_portfolio = 40 # num mean-variance efficient portfolios to compute
    context.today = None
    context.tau = None
    context.min_data_window = 756 # min of 3 yrs data for calculations
    context.first_rebal_date = None
    context.first_rebal_idx = None
    context.weights = None
    # Schedule dynamic allocation calcs to occur 1 day before month end - note that
    # actual trading will occur on the close on the last trading day of the month
    schedule_function(rebalance,
                  date_rule=date_rules.month_end(days_offset=1),
                  time_rule=time_rules.market_close())
    # Record some stuff every day
    schedule_function(record_vars,
                  date_rule=date_rules.every_day(),
                  time_rule=time_rules.market_close())
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
    context.profit_target = 1.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.005, min_trade_cost=0.0))
    set_slippage(slippage.FixedSlippage(0.00))
示例#3
0
def initialize(context):
    """
    Called once at the start of the algorithm.
    """

    set_slippage(slippage.FixedSlippage(spread=0.00))
    set_commission(commission.PerShare(cost=0, min_trade_cost=0))

    schedule_function(rebalance, TRADE_FREQ,
                      date_rules.every_day(),
                      time_rules.market_open(hours=1, minutes=30),
    )

    schedule_function(record_vars, date_rules.every_day(),
                      time_rules.market_close())

    ml_pipeline = make_ml_pipeline(universe,
                                   n_forward_days=N_FORWARD_DAYS,
                                   window_length=TRAINING_PERIOD)

    # Create our dynamic stock selector.
    attach_pipeline(ml_pipeline, 'ml_model')

    context.past_predictions = {}
    context.ic = 0
    context.rmse = 0
    context.mae = 0
    context.returns_spread_bps = 0
示例#4
0
def initialize(context):

    # Get continuous futures for Light Sweet Crude Oil...
    context.crude_oil = continuous_future('CL', roll='calendar')
    # ... and RBOB Gasoline
    context.gasoline = continuous_future('RB', roll='calendar')

    # If Zipline has trouble pulling the default benchmark, try setting the
    # benchmark to something already in your bundle
    set_benchmark(context.crude_oil)

    # Ignore commissions and slippage for now
    set_commission(us_futures=commission.PerTrade(cost=0))
    set_slippage(us_futures=slippage.FixedSlippage(spread=0.0))

    # Long and short moving average window lengths
    context.long_ma = 65
    context.short_ma = 5

    # True if we currently hold a long position on the spread
    context.currently_long_the_spread = False
    # True if we currently hold a short position on the spread
    context.currently_short_the_spread = False

    # Rebalance pairs every day, 30 minutes after market open
    schedule_function(func=rebalance_pairs,
                      date_rule=date_rules.every_day(),
                      time_rule=time_rules.market_open(minutes=30))

    # Record Crude Oil and Gasoline Futures prices everyday
    schedule_function(record_price, date_rules.every_day(),
                      time_rules.market_open())
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('FXCM:AUD/USD'):1,
                               symbol('FXCM:EUR/USD'):1,
                               symbol('FXCM:GBP/USD'):1,
                               symbol('FXCM:NZD/USD'):1,
                               symbol('FXCM:USD/CAD'):-1,
                               symbol('FXCM:USD/CHF'):-1,
                               symbol('FXCM: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))
    
    # set trading cost and slippage to zero
    set_commission(fx=commission.PipsCost(cost=0.00))
    set_slippage(fx=slippage.FixedSlippage(0.00))
示例#6
0
def initialize(context):
    # Set benchmark to short-term Treasury note ETF (SHY) since strategy is dollar neutral
    set_benchmark(symbol('AAPL'))

    # Schedule our rebalance function to run at the end of each day.
    # Modified the timing to 5 mins earlier than the market close to reduce the fail to book warnings
    # schedule_function(my_rebalance, date_rules.every_day(), time_rules.market_close(minutes=5))
    # Try to change it to open and see what happened -- HY
    schedule_function(my_rebalance, date_rules.every_day(),
                      time_rules.market_open(minutes=5))

    # Record variables at the end of each day.
    # schedule_function(my_record_vars, date_rules.every_day(), time_rules.market_close())

    # Get intraday prices today before the close if you are not skipping the most recent data
    # schedule_function(get_prices,date_rules.every_day(), time_rules.market_close(minutes=10))
    # Try to get the price data when the market is opening -- HY
    # schedule_function(get_prices, date_rules.every_day(), time_rules.market_open(minutes=1))

    # Set commissions and slippage to 0 to determine pure alpha
    set_commission(commission.PerShare(cost=0, min_trade_cost=0))
    set_slippage(slippage.FixedSlippage(spread=0))

    # Number of quantiles for sorting returns for mean reversion
    context.nq = 5

    # Number of quantiles for sorting volatility over five-day mean reversion period
    context.nq_vol = 3

    # Create our pipeline and attach it to our algorithm.
    my_pipe = make_pipeline()
    attach_pipeline(my_pipe, 'my_pipeline')
示例#7
0
def initialize(context):
    # Benchmark against the Dow Jones Industrial Average (DIA)
    api.set_benchmark(symbol('DIA'))

    # stop when trying to handle missing data
    api.set_nodata_policy(api.NoDataPolicy.EXCEPTION)

    # These are the default commission and slippage settings.  Change them to fit your
    # brokerage fees. These settings only matter for backtesting.  When you trade this
    # algorithm, they are moot - the brokerage and real market takes over.
    api.set_commission(api.commission.PerTrade(cost=0.03))
    api.set_slippage(api.slippage.VolumeShareSlippage(volume_limit=0.25, price_impact=0.1))

    # create an instance of the Dow30 class and set it within context
    context.dow30 = dow_constituents

    # next trade year
    context.year = 0

    # set to True to trigger a rebalance
    context.trade = False

    # for tracking max leverage
    context.mx_lvrg = 0

    # check for possible trades daily
    api.schedule_function(func=rebalance, date_rule=api.date_rules.every_day(), time_rule=api.time_rules.market_open(hours=1))
def initialize(context):
    """
        A function to define things to do at the start of the strategy
    """
    # universe selection
    context.securities = [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,
        'ROC_period_short': 30,
        'ROC_period_long': 120,
        'BBands_period': 300,
        'trade_freq': 5,
        'leverage': 2
    }

    # variable to control trading frequency
    context.bar_count = 0

    # 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 trading cost and slippage to zero
    set_commission(commission.PerShare(cost=0.0, min_trade_cost=0.0))
    set_slippage(slippage.FixedSlippage(0.00))
示例#9
0
def initialize(context):

    # Let's set a look up date inside our backtest to ensure we grab the correct security
    #set_symbol_lookup_date('2015-01-01')

    # Use a very liquid set of stocks for quick order fills
    context.symbol = symbol('SPY')
    #context.stocks = symbols(['TWX','AIG','PSX','EMC','YHOO','MDY','TNA','CHK','FXI',
    #                            'PEP','SBUX','VZ','VWO','TWC','HAL','MDLZ','CAT','TSLA',
    #                            'MU','PM','WYNN','MET',NOV BRK_B SNDK ESRX YELP])
    #set_universe(universe.DollarVolumeUniverse(99.5, 100))
    #set_benchmark(symbol('SPY'))

    # set a more realistic commission for IB, remove both this and slippage when live trading in IB
    set_commission(commission.PerShare(cost=0.014, min_trade_cost=1.4))

    # Default slippage values, but here to mess with for fun.
    set_slippage(
        slippage.VolumeShareSlippage(volume_limit=0.25, price_impact=0.1))

    # Use dicts to store items for plotting or comparison
    context.next_pred_price = {}  # Current cycles prediction

    #Change us!
    context.history_len = 500  # How many days in price history for training set
    context.out_of_sameple_bin_size = 2
    context.score_filter = -1000.0
    context.action_to_move_percent = 0.0

    # Register 2 histories that track daily prices,
    # one with a 100 window and one with a 300 day window
    add_history(context.history_len, '1d', 'price')
    context.i = 0
示例#10
0
def initialize(context):
    """
    Called once at the start of a backtest, and once per day at
    the start of live trading.
    """
    # Attach the pipeline to the algo
    algo.attach_pipeline(make_pipeline(), 'pipeline')

    # Set SPY as benchmark
    algo.set_benchmark(algo.sid("FIBBG000BDTBL9"))

    # identify down gaps immediately after the opening
    algo.schedule_function(
        find_down_gaps,
        algo.date_rules.every_day(),
        algo.time_rules.market_open(minutes=1),
    )

    # at 9:40, short stocks that gapped down
    algo.schedule_function(
        short_down_gaps,
        algo.date_rules.every_day(),
        algo.time_rules.market_open(minutes=10),
    )

    # close positions 5 minutes before the close
    algo.schedule_function(
        close_positions,
        algo.date_rules.every_day(),
        algo.time_rules.market_close(minutes=5),
    )

    # Set commissions and slippage
    algo.set_commission(commission.PerShare(cost=0.0))
    algo.set_slippage(slippage.FixedBasisPointsSlippage(basis_points=3.0))
    def initialize(context):
        """ Called once at the start of the algorithm. """

        set_slippage(slippage.VolumeShareSlippage(volume_limit=0.025, price_impact=0.1))
        set_commission(commission.PerShare(cost=0.01, min_trade_cost=1.00))
        set_max_leverage(1.0)

        # Rebalance every day, 1 hour after market open.
        schedule_function(
            context.my_rebalance,
            date_rules.every_day(),
            time_rules.market_open(hours=1)
        )

        # Close all positions every day, 30 minutes before market close.
        schedule_function(
            context.close_positions,
            date_rules.every_day(),
            time_rules.market_close(minutes=30)
        )

        # Create risk manager
        context.risk_manager = RiskManager(context, daily_risk)

        # Create our dynamic stock selector.
        attach_pipeline(context.make_screener(), 'stock_screener')
示例#12
0
def initialize(context):

    # Let's set a look up date inside our backtest to ensure we grab the correct security
    #set_symbol_lookup_date('2015-01-01')
    

    # Use a very liquid set of stocks for quick order fills
    context.symbol = symbol('SPY')
    #context.stocks = symbols(['TWX','AIG','PSX','EMC','YHOO','MDY','TNA','CHK','FXI',
    #                            'PEP','SBUX','VZ','VWO','TWC','HAL','MDLZ','CAT','TSLA',
    #                            'MU','PM','WYNN','MET',NOV BRK_B SNDK ESRX YELP])
    #set_universe(universe.DollarVolumeUniverse(99.5, 100))
    #set_benchmark(symbol('SPY'))
    
    # set a more realistic commission for IB, remove both this and slippage when live trading in IB
    set_commission(commission.PerShare(cost=0.014, min_trade_cost=1.4))
    
    # Default slippage values, but here to mess with for fun.
    set_slippage(slippage.VolumeShareSlippage(volume_limit=0.25, price_impact=0.1))
        
    # Use dicts to store items for plotting or comparison
    context.next_pred_price = {} # Current cycles prediction
    
    #Change us!
    context.history_len              = 500    # How many days in price history for training set
    context.out_of_sameple_bin_size  = 2
    context.score_filter             = -1000.0
    context.action_to_move_percent   = 0.0

    # Register 2 histories that track daily prices,
    # one with a 100 window and one with a 300 day window
    add_history(context.history_len, '1d', 'price')
    context.i = 0
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
    context.advisor = Advisor('bbands_ea',expert_advisor, context.universe)

    # schedule agent weights updates
    pass
示例#14
0
    def initialize(self, context):
        if self.verbose: print("Starting the robo advisor")

        # Populate Portfolios
        self.all_portfolios = initialize_portfolio(self.verbose)

        # Set Commission model
        self.initialize_commission(country=self.country, platform=self.trading_platform)

        # Set Slippage model
        set_slippage(slippage.FixedSlippage(spread=0.0))  # assume spread of 0

        # Schedule Rebalancing check
        rebalance_check_freq = date_rules.month_end()
        if (self.rebalance_freq == 'daily'): rebalance_check_freq = date_rules.every_day()
        elif (self.rebalance_freq == 'weekly'): rebalance_check_freq = date_rules.week_end()

        if self.verbose: print('Rebalance checks will be done %s' % self.rebalance_freq)
        schedule_function(
            func=self.before_trading_starts,
            date_rule=rebalance_check_freq,
            time_rule=time_rules.market_open(hours=1))

        # record daily weights at the end of each day
        schedule_function(
            func=record_current_weights,
            date_rule=date_rules.every_day(),
            time_rule=time_rules.market_close()
        )
def initialize(context):
    # Provide the bid-ask spread for each of the securities in the universe.
    spreads = {
        sid(24): 0.05,
        sid(3766): 0.08
    }

    # Initialize slippage settings given the parameters of our model
    set_slippage(PerStockSpreadSlippage(spreads))
def initialize(context):
    context.fut = continuous_future('ES', roll='calendar')

    # Ignore commissions and slippage for now
    set_commission(us_futures=commission.PerTrade(cost=0))
    set_slippage(us_futures=slippage.FixedSlippage(spread=0.0))

    context.i = 0
    context.invested = False
示例#17
0
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('FXCM:AUD/USD'),
        symbol('FXCM:EUR/CHF'),
        symbol('FXCM:EUR/JPY'),
        symbol('FXCM:EUR/USD'),
        symbol('FXCM:GBP/USD'),
        symbol('FXCM:NZD/USD'),
        symbol('FXCM:USD/CAD'),
        symbol('FXCM:USD/CHF'),
        symbol('FXCM: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 trading cost and slippage to zero
    set_commission(fx=commission.PipsCost(cost=context.params['pip_cost']))
    set_slippage(fx=slippage.FixedSlippage(0.00))

    # 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))
示例#18
0
def initialize(context):
    set_slippage(us_futures=InstantSlippage())
    set_commission(us_futures=PerTrade(0))
    context.contracts = [
        continuous_future(contract,
                          offset=0,
                          adjustment='mul',
                          roll='volume')
        for contract in contracts]
    context.min_max = {}
示例#19
0
 def initialize_testing_algo(self, context):
     set_commission(PerShare())
     set_slippage(VolumeShareSlippage())
     if self.syms is None:
         self.syms = self.__validate_symbols__(self.ticker_syms)
     self.portfolio_memory = PortfolioMemory(len(self.syms),
                                             self.config.n_history)
     schedule_function(self.testing_rebalance,
                       date_rule=date_rules.month_start(),
                       time_rule=time_rules.market_open(minutes=1))
示例#20
0
def initialize(context):
    set_slippage(us_futures=InstantSlippage())
    set_commission(us_futures=PerTrade(0))
    context.contracts = [
        continuous_future(contract, offset=0, adjustment='mul', roll='volume')
        for contract in contracts
    ]
    context.min_max = {}
    # auxiliary variables selectively used in various portfolio optimization methods
    context.counter = 0
    context.target_portfolio = pd.Series()
def initialize(context):
    """Setup: register pipeline, schedule rebalancing,
        and set trading params"""
    attach_pipeline(compute_factors(), 'factor_pipeline')
    schedule_function(rebalance,
                      date_rules.week_start(),
                      time_rules.market_open(),
                      calendar=calendars.US_EQUITIES)

    set_commission(us_equities=commission.PerShare(cost=0.00075, min_trade_cost=.01))
    set_slippage(us_equities=slippage.VolumeShareSlippage(volume_limit=0.0025, price_impact=0.01))
示例#22
0
def initialize(context):
    '''
        Called once at the start of the strategy execution. 
        This is the place to define things to do at the start of the strategy.
    '''
    # set the account base currency and strategy parameters
    set_account_currency('USD')
    context.params = {
        'verbose': False,
        'leverage': 1,
        'rebalance_freq': '15m',
        'no_overnight_position': True,
        'pip_cost': 0.00008,
        'rollover_spread': 0.00,
        'BBands_period': 1440,
        'SMA_period_short': 150,
        'SMA_period_long': 600,
        'indicator_lookback': 1440,  # max of all lookbacks!!!
        'indicator_freq': '1m',
        'buy_signal_threshold': 0.5,
        'sell_signal_threshold': -0.5
    }

    # define the strategy instruments universe
    context.universe = [
        symbol('FXCM:AUD/USD'),
        symbol('FXCM:EUR/USD'),
        symbol('FXCM:NZD/USD'),
        symbol('FXCM:USD/CAD'),
        symbol('FXCM:USD/CHF'),
    ]
    context.ccy_universe = [
        'AUD', 'CAD', 'CHF', 'EUR', 'GBP', 'JPY', 'NZD', 'USD'
    ]

    # function to schedule roll-overs, at 5 PM EST or 9 PM UTC (3 hours before midnight)
    schedule_function(compute_rollovers, date_rules.every_day(),
                      time_rules.market_close(hours=3, minutes=0))

    # set up cost structures, we assume a $1 per $10K all-in cost
    set_commission(fx=commission.PipsCost(cost=context.params['pip_cost']))
    set_slippage(fx=slippage.FixedSlippage(spread=0.00))

    # variables to track signals and target portfolio
    context.signals = dict((security, 0) for security in context.universe)
    context.weights = dict((security, 0) for security in context.universe)

    # Call rebalance function, see below under standard helper functions to modify
    rebalance_scheduler(context)

    # make the back-test lighter
    context.perf_tracker.keep_transactions = False
    context.perf_tracker.keep_orders = False
示例#23
0
def initialize(context):
    
    context.has_ordered = False
    context.order_id = None
    context.counter = 0
    context.oil_historical_data = database_wrapper_zipline.load_data_from_passdb(['USCRWTIC INDEX'], source = 'BLP', timezone_indication = 'UTC', start = None, end = None, instruments = [], set_price = 'close', align_dates = True, set_volume_val = 1e6)
    context.expert_params = {'exclude_fast': True, 'ml_method': 'Ridge Regression', 'dim': 3, 'ar_order': 2, 'k_number': 20, 'stride_size': 2, 'dim_red': None, 'wavelet_type': 'haar'}
    context.processing_params = {'returns': False, 'logarithms': True}
    context.method = 'multiscale_autoregressive'
    
    set_commission(commission.PerShare(cost=0.01, min_trade_cost=1.0))
    set_slippage(slippage.FixedSlippage(spread=0.0)) # remove slippage
示例#24
0
def initialize(context):
    # This code runs once, when the sim starts up
    log.debug('scheduling rebalance and recording')

    set_slippage(slippage.FixedSlippage(spread=0.0))
    set_commission(commission.PerShare(cost=0, min_trade_cost=0))

    schedule_function(func=rebalance,
                      date_rule=date_rules.month_end(),
                      time_rule=time_rules.market_close(minutes=15))

    schedule_function(func=record_daily,
                      date_rule=date_rules.every_day(),
                      time_rule=time_rules.market_close())
示例#25
0
    def initialize(self, context):
        add_history(200, '1d', 'price')
        set_slippage(slippage.FixedSlippage(spread=0.0))
        set_commission(commission.PerShare(cost=0.01, min_trade_cost=1.0))
        context.tick = 0

        dp_data = self.data
        df_data = pd.DataFrame(index=dp_data.axes[1])
        df_data['close'] = dp_data[:, :, 'close']
        df_data['open'] = dp_data[:, :, 'open']
        df_data['high'] = dp_data[:, :, 'high']
        df_data['low'] = dp_data[:, :, 'low']
        df_data['volume'] = dp_data[:, :, 'volume']

        self.atr = atr_per_close(df_data, atrLen = self.atr_len)
        context.longstop = 0
示例#26
0
def initialize(context):

    # slippage model
    set_slippage(slippage.FixedSlippage(spread=0))

    # stock universe - list of tickers
    securities = ['AAPL', 'ADBE', 'AIG', 'AMGN', 'BA']
    # change string list of tickers into asset list using symbol function
    context.sec_symbols = [symbol(s) for s in securities]
    print(len(context.sec_symbols))

    # schedule functions
    schedule_function(trade_market_open, date_rules.every_day(),
                      time_rules.market_open())
    schedule_function(cancel_open, date_rules.every_day(),
                      time_rules.market_close())
def initialize(context):
    '''
        Called once at the start of the strategy execution. 
        This is the place to define things to do at the start of the strategy.
    '''
    # set the account base currency and strategy parameters
    set_account_currency('USD')
    context.params = {
        'verbose': False,
        'leverage': 2,
        'rebalance_freq': 'monthly',
        'no_overnight_position': False,
        'pip_cost': 0.0001,
        'rollover_spread': 0.00,
        'positions': 2,
        'lookback': 26 * 6
    }

    # define the strategy instruments universe
    context.universe = [
        symbol('FXCM:AUD/USD'),
        symbol('FXCM:EUR/CHF'),
        symbol('FXCM:EUR/JPY'),
        symbol('FXCM:EUR/USD'),
        symbol('FXCM:GBP/JPY'),
        symbol('FXCM:GBP/USD'),
        symbol('FXCM:NZD/USD'),
        symbol('FXCM:USD/CAD'),
        symbol('FXCM:USD/CHF'),
        symbol('FXCM:USD/JPY'),
    ]
    context.ccy_universe = [
        'AUD', 'CAD', 'CHF', 'EUR', 'GBP', 'JPY', 'NZD', 'USD'
    ]

    # function to schedule roll-overs, at 5 PM EST or 9 PM UTC (3 hours before midnight)
    schedule_function(compute_rollovers, date_rules.every_day(),
                      time_rules.market_close(hours=3, minutes=0))

    # set up cost structures, we assume a $1 per $10K all-in cost
    set_commission(fx=commission.PipsCost(cost=context.params['pip_cost']))
    set_slippage(fx=slippage.FixedSlippage(spread=0.00))

    # Call rebalance function, see below under standard helper functions to modify
    rebalance_scheduler(context)
示例#28
0
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('FXCM:GBP/USD')
    context.y = symbol('FXCM: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))

    # set trading cost and slippage to zero
    set_commission(fx=commission.PipsCost(cost=0.00))
    set_slippage(fx=slippage.FixedSlippage(0.00))

    # 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):
    
    context.strategy_verbose = params.verbose
    
    context.is_invested = [False]*len(fields_tickers) # indication if we are invested in a particular asset
    context.shorted = [False]*len(fields_tickers) # indication if we are short in a particular asset
    context.longed = [False]*len(fields_tickers) # indication if we are long in a particular asset

    context.expected_prices = [0]*len(fields_tickers) # expected price for a particular asset in a given period
    context.expected_returns = [0]*len(fields_tickers) # expected return for a particular asset in a given period
    context.expiration_date_forecasts = [0]*len(fields_tickers) # expiration date for the forecast of a particular asset
      

    # get the historical data to perform the forecasts
    context.historical_data = database_wrapper_zipline.load_data_from_passdb(fields_tickers, source = 'BLP', timezone_indication = 'UTC', start = None, end = None, instruments = [], set_price = 'close', align_dates = True, set_volume_val = 1e6)

    set_commission(commission.PerShare(cost=params.cost, min_trade_cost=params.min_trade_cost))
    set_slippage(slippage.FixedSlippage(spread=params.spread))
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], 0.35, method=1)

    # schedule agent weights updates
    schedule_function(update_agent_weights, date_rules.week_start(),
                      time_rules.market_close())
def initialize(context):
    '''
    Called once at the very beginning of a backtest (and live trading). 
    Use this method to set up any bookkeeping variables.
    
    The context object is passed to all the other methods in your algorithm.

    Parameters

    context: An initialized and empty Python dictionary that has been 
             augmented so that properties can be accessed using dot 
             notation as well as the traditional bracket notation.
    
    Returns None
    '''
    # Turn off the slippage model
    set_slippage(slippage.FixedSlippage(spread=0.0))
    # Set the commission model (Interactive Brokers Commission)
    set_commission(commission.PerShare(cost=0.01, min_trade_cost=1.0))
    context.tick = 0
def initialize(context):
    """
    Called once at the start of the algorithm.
    """
    context.n_longs = N_LONGS
    context.n_shorts = N_SHORTS
    context.min_positions = MIN_POSITIONS
    context.universe = assets

    set_slippage(slippage.FixedSlippage(spread=0.00))
    set_commission(commission.PerShare(cost=0, min_trade_cost=0))

    schedule_function(rebalance, date_rules.every_day(),
                      time_rules.market_open(hours=1, minutes=30))

    schedule_function(record_vars, date_rules.every_day(),
                      time_rules.market_close())

    pipeline = compute_signals()
    attach_pipeline(pipeline, 'signals')
示例#33
0
def initialize(context):
    # Get all tickers in america stock
    tickers = read_table('Ticker.txt')
    context.tickers = transpose(array(tickers)).tolist()[0]

    # Configurations for context object
    context.day = 0
    context.DAYS_CUMULATIVE = 90
    context.REFORM_PERIOD = 5
    context.PORTFOLIO_SIZE = 20
    context.STARTING_CASH = 100000.0
    context.my_portfolio_quantity = {}

    # Setting equity commission model to zero commission
    set_commission(
        us_equities=commission.PerShare(cost=0.000, min_trade_cost=0))

    # Setting slippage model; This is the default slippage, we need to figure out what slippage model suits our case
    set_slippage(us_equities=slippage.FixedBasisPointsSlippage(
        basis_points=5, volume_limit=0.1))
示例#34
0
def initialize(context):
    attach_pipeline(make_pipeline(), 'data_pipeline')

    context.technical_indicator_states = {}
    context.window_length = 7
    context.benchmark = deque(maxlen=context.window_length)
    context.benchmark_asset = symbol('SPY')
    context.benchmark_assets = symbols('QQQ', 'SPY')

    #context.classifier = RandomForestClassifier() # Use a random forest classifier
    context.classifier = DecisionTreeClassifier(max_depth=5, max_leaf_nodes=10)

    # Clasifier training data
    context.features = [
        'RSI', 'EMA', 'MACD', 'SMA_5', 'SMA_10', 'bb_lower', 'bb_middle',
        'bb_upper'
    ]
    context.response = ['Class']
    context.X = pd.DataFrame(
        columns=context.features)  # Independent, or input variables
    context.Y = pd.DataFrame(
        columns=context.response)  # Dependent, or output variable

    context.prediction = {}  # Stores most recent prediction

    context.tick = 0
    context.total_buy = 0
    context.positions = None
    context.position_adjustment_days = 5  # Number of days to wait before adjusting positions
    context.min_data_points = 500
    context.max_data_points = 1500

    schedule_function(rebalance, date_rules.every_day(),
                      time_rules.market_open(minutes=1))
    schedule_function(record_vars, date_rules.every_day(),
                      time_rules.market_close())
    set_benchmark(symbol('SPY'))
    # Turn off the slippage model
    set_slippage(slippage.FixedSlippage(spread=0.0))
    # Set the commission model (Interactive Brokers Commission)
    set_commission(commission.PerShare(cost=0.01, min_trade_cost=1.0))
def initialize(context):
    '''
    Called once at the very beginning of a backtest (and live trading). 
    Use this method to set up any bookkeeping variables.
    
    The context object is passed to all the other methods in your algorithm.

    Parameters

    context: An initialized and empty Python dictionary that has been 
             augmented so that properties can be accessed using dot 
             notation as well as the traditional bracket notation.
    
    Returns None
    '''
    # Register history container to keep a window of the last 100 prices.
    add_history(10, '1d', 'price')
    # Turn off the slippage model
    set_slippage(slippage.FixedSlippage(spread=0.0))
    # Set the commission model (Interactive Brokers Commission)
    set_commission(commission.PerShare(cost=0.0, min_trade_cost=0.0))
    context.tick = 0
示例#36
0
def initialize(context):
    context.has_ordered = False
    set_commission(commission.PerShare(cost=0.0, min_trade_cost=0.0))
    set_slippage(slippage.FixedSlippage(spread=0.0)) 
    schedule_function(func0, date_rules.week_end())
def initialize (context) :
   
    # Algorithm parameters are defined here
    if ENVIRONMENT == 'RESEARCH':    
        context.benchmark = symbols('SPY')
        context.risk_free = symbols('SHY')
        context.cash_proxy = symbols('SHY')
    elif ENVIRONMENT == 'ZIPLINE' or ENVIRONMENT == 'IDE':
        context.benchmark = symbol('SPY')
        context.risk_free = symbol('SHY')
        context.cash_proxy = symbol('SHY')

    context.algo_transforms = [(['price'], ROCP, ['mom_63'], 62), 
                               (['price'], ROCP, ['mom_21'], 20), 
                               (['price'], average_historic_volatility, ['hvol_20'], 63, 20),
                               (['price'], SMA, ['short_mavg'], 21),
                               (['price'], SMA, ['long_mavg'], 63)
                               ]
    
    if ENVIRONMENT == 'ZIPLINE' or ENVIRONMENT == 'IDE':
        context.strategies = {
                                'strategy1': {
                                    'strat_wt' : 1.0,
                                    'strat_type' : 'SIGNAL',
                                    'alloc_type' : 'EQWT',   
                                    'portfolios' : [
                                                    symbols('VHT', 'VGT', 'VCR', 'IBB', 'EDV', 'VB', 'VAW', 
                                                            'TLT')
                                                    ],
                                    'weights' : [1.0],
                                    'n_top' : [1],
                                    'zscore_data' : ['+mom_63', '+mom_21', '-hvol_20'],
                                    'zscore_weights' : [0.7, 0.0, 0.3],
    #                                 'filter' : [lambda x,y: (x > y) & (x > 0), 'mom_21', ('mom_21', context.risk_free)]
    #                                 'filter' : []
                                    'filter' : [lambda x,y: x > y, 'mom_63', ('mom_63', context.risk_free)],
                                    'buy_trigger' : [lambda x,y: (x > y) and (context.buy_flag==False), 
                                                     'short_mavg', 'long_mavg'], 
                                    'sell_trigger' : [lambda x,y : (x < y) and (context.buy_flag==True), 
                                                      'short_mavg', 'long_mavg']
                                    }
                                }
        
    elif ENVIRONMENT == 'RESEARCH':
        context.strategies = {
                                'strategy1': {
                                    'strat_wt' : 1.0,
                                    'strat_type' : 'SIGNAL',
                                    'alloc_type' : 'EQWT',
                                    'portfolios' : [
                                                    [symbols('VHT'), symbols('VGT'), symbols('VCR'), 
                                                     symbols('IBB'), symbols('EDV'), symbols('VB'), 
                                                     symbols('VAW'), symbols('TLT')]
                                                    ],
                                    'weights' : [1.0],
                                    'n_top' : [1],
                                    'zscore_data' : ['+mom_63', '+mom_21', '-hvol_20'],
                                    'zscore_weights' : [0.7, 0.0, 0.3],
                                    # 'filter' : [lambda x,y: (x > y) & (x > 0), 'mom_21', 
                                    #             ('mom_21', context.risk_free)]
    #                                 'filter' : []
                                    'filter' : [lambda x,y: x > y, 'mom_63', ('mom_63', context.risk_free)], 
                                    'buy_trigger' : [lambda x,y: (x > y) and (context.buy_flag==False), 
                                                     'short_mavg', 'long_mavg'],
                                    'sell_trigger' : [lambda x,y : (x < y) and (context.buy_flag==True), 
                                                      'short_mavg', 'long_mavg']
                                    }
                                }

    # have to set this manually 'til I figure how to generate it programmatically from above parameters
    context.max_lookback = 120        # need additional data for recursive indicators like EMA, historic_vol etc.
    
    # set the appropriate time_rule offsets (hours, minutes) for order processing
    context.order_time_rule = time_rules.market_close(hours=0, minutes=1)
    
    context.threshold = 0.0     # only BUY/SELL if asset QTY changes by threshold%
    
    set_commission(commission.PerTrade(cost=0.0))  
    set_slippage(slippage.FixedSlippage(spread=0))
    
    #######################################################################################
    # THE FOLLOWING MUST ALWAYS BE INCLUDED AND MUST PRECEDE ANY SCHEDULED FUNCTIONS!
    # include this check to make sure that the algo parameters are formatted as required
    check_algo_parameters (context)
    # create a set of all the symbols used by the algorithm
    create_symbol_list(context)
    initialize_strat_variables(context)
    if ENVIRONMENT != 'IDE':
        add_strat_history(context)  
    #######################################################################################
    
    schedule_function(generate_strat_data,  
                     # date_rule=date_rules.month_start(days_offset=0),
                     date_rule=date_rules.month_start(days_offset=0),
                     time_rule=time_rules.market_open(hours=0, minutes=1),
                     half_days=True) 

#     schedule_function_by_interval(context, test_signal,  
#                      date_rule=date_rules.every_day(), 
#                      time_rule=time_rules.market_open(hours=0, minutes=5),
#                      half_days=True,
#                      freq=1)    
    
    # the 'freq' parameter is added to the norma schedule_function routine
    # to enable intervals of 'freq' periods - 1,2,3,.....  months
    schedule_function_by_interval(context, rebalance,  
                     date_rule=date_rules.month_start(days_offset=0), 
                     time_rule=time_rules.market_open(hours=0, minutes=5),
                     half_days=True,
                     freq=1)
    
    schedule_function_by_interval(context, handle_orders,  
                     date_rule=date_rules.month_start(days_offset=0), 
                     time_rule=time_rules.market_close(hours=0, minutes=15),
                     half_days=True,
                     freq=1)
示例#38
0
def initialize(context):
    set_commission(commission.PerShare(cost=0.01, min_trade_cost=1.0))
    set_slippage(slippage.FixedSlippage(spread=0.0))
示例#39
0
def initialize_api(context):
    context.incr = 0
    context.sale_price = None
    set_slippage(FixedSlippage())
示例#40
0
def initialize(context, date_limits, verbose, symbols):
    print "in initialize"
    start_time_initialize = time.time()
    context.verbose = verbose
    # Define rebalance frequency
    context.scheduler = SCHEDULER
    if context.scheduler  == 'Daily':
        schedule_function(rebalance)
    if context.scheduler  == 'Weekly':
        schedule_function(rebalance, date_rules.week_end())
    elif context.scheduler  == 'Monthly':
        schedule_function(rebalance, date_rules.month_end())

    context.date_limits = date_limits

    # Set slippage model and commission model
    set_slippage(slippage.FixedSlippage(spread=SLIPPAGE_SPREAD))
    set_commission(commission.PerShare(cost=COMMISSION_COST_PER_SHARE, min_trade_cost=COMMISSION_MIN_TRADE_COST))

    context.symbols = symbols
    symbol_membership = utils.get_underlying(context.symbols, sep = ' / ')
    symbol_membership.index = context.symbols
    context.symbol_membership = symbol_membership
    if REFERENCE_GROUP_SYMBOL['Cash'] in context.symbol_membership.index:
        context.symbol_membership.loc[REFERENCE_GROUP_SYMBOL['Cash']] = 'Cash'
    
    asset_keys = utils.get_asset_keys_db()
    if RETS_READER is not None:
        returns = utils.get_returns_db(RETS_READER, context.symbols, asset_keys)

    else:
        returns = None
    if VOL_READER is not None and CORR_READER is not None:
        volatility = utils.get_volatilities_db(VOL_READER, context.symbols, asset_keys)
        correlations = utils.get_correlations_db(CORR_READER, context.symbols, asset_keys)

    else:
        volatility, correlations = [None]*2
    data_dict = {'returns': returns, 'volatility': volatility, 'correlations': correlations}
    context.data_dict = data_dict


    #print returns
    #print ''
    #print ''
    #print volatility
    #print ''
    #print ''
    #print correlations
    #print asfasfasf

    # Group
    group = "Top"
    if len(sys.argv) >= 3 and 'ASSETTYPE=' in sys.argv[2]:
        group = sys.argv[2].split('=')[-1]
    params_dict = {}
    params_dict.update(data_dict)
    params_dict.update(PARAMS['params'])

    ag_name = 'Top'
    if group == 'Equity':
        ag_name = PARAMS['params']['group']
    context.group = AbstractGroup(context.symbols, name = ag_name, start=START_DATE_BT_STR, end = END_DATE_BT_STR, verbose = verbose, strategy_id = ID, scheduler = SCHEDULER , **params_dict)
    #print "DEBUG [context.group.updated_symbols] : \n", context.group.updated_symbols
    context.round_weights = 4
    context.ref_group_df = pd.DataFrame(pd.Series({v:k for k,v in REFERENCE_GROUP_SYMBOL.items()}))
    context.active_orders = []

    if verbose:
        print "INFO: Initialize took %f s." % (time.time() - start_time_initialize)
示例#41
0
def initialize(context):
    add_history(120, '1d', 'price')
    set_slippage(slippage.FixedSlippage(spread=0.0))
    set_commission(commission.PerShare(cost=0.01, min_trade_cost=1.0))
    context.tick = 0