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):
    """
        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))
Beispiel #3
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())
Beispiel #4
0
def initialize_bband(context):
    set_commission(commission.PerDollar(cost = COMMISSION))
    context.i = 0
    context.investment = False
    context.buy_price = 0
    context.position = 0.0
    add_history(20, '1d', 'price')
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))
Beispiel #6
0
def initialize(context):

    # List of Major World Indices Yahoo tickers - https://finance.yahoo.com/world-indices
    with open('tickers.pickle', 'rb') as handle:
        indices_tickers = pickle.load(handle)  # load in tickers from pickle

    os.remove('tickers.pickle')  # delete tickers pickle file

    context.indices = [symbol(ticker) for ticker in indices_tickers
                       ]  # create list of ticker symbols
    context.days_of_correction = [
        0 for _ in indices_tickers
    ]  # create list of days since correction has begun
    set_benchmark(symbol('^GSPC'))
    set_commission(
        commission.PerTrade(cost=15.0)
    )  # commission for IBKR, UK for Stocks, ETF's & Warrants - https://www.interactivebrokers.co.uk/en/index.php?f=39753&p=stocks1
    '''-----------------------------PARAMETERS TO BE OPTIMISED--------------------------------'''
    context.correction_margin = 0.1  # the percentage drawdown considered a correction
    print('Drawdown percentage range from peak for correction: ' +
          str(round(context.correction_margin * 100)) + '%')
    context.upturn_coefficient = 0.24  # the ratio upturn from trough indicating end of correction - used in calculate_required_upturn function
    print('Upturn Coefficient: ' + str(round(context.upturn_coefficient, 4)))
    context.min_gain = 0.05  # the highest the price can be from peak and still be considered for ordering
    print('Mainimum potential gain from peak to be considered: ' +
          str(round(context.min_gain * 100, 0)) + '%')
    context.state_threshold = -10.0  # 0.0002 threshold between bull and bear markets
    print('Market state threshhold: ' +
          str(round(context.state_threshold, 4)) + '%')
Beispiel #7
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')
Beispiel #8
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))
Beispiel #9
0
def initialize_magc(context):
    set_commission(commission.PerDollar(cost = COMMISSION))
    add_history(20, '1d', 'price')
    add_history(60, '1d', 'price')
    context.i = 0
    context.investment = False
    context.buy_price = 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')
Beispiel #11
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
Beispiel #12
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
Beispiel #13
0
def initialize(context):
    ''' Initialize global vars'''
    context.long_leverage = 0.1
    context.short_leverage = -0.9
    context.returns_lookback = 16
    context.pct_per_stock = 0.5
    
    context.fastperiod = 12
    context.slowperiod = 26
    context.signalperiod = 9
    context.bar_count = 90

    set_commission(commission.PerShare(cost=0.0014, min_trade_cost=1))
    
    # Rebalance on the first trading day of each week at 12AM.
    schedule_function(rebalance, date_rules.week_start(days_offset=0),time_rules.market_open(hours=0.5))
    
    # Rebalance mid-week
    schedule_function(cut_losses, date_rules.week_start(days_offset=2),time_rules.market_open(hours=0.5))

    # Record tracking variables at the end of each day.
    schedule_function(record, date_rules.every_day(),time_rules.market_open(minutes=1))


    # Create and attach our pipeline (dynamic stock selector), defined below.
    attach_pipeline(make_pipeline(context),
                    'mean_reversion_macd_learning')
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
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))
Beispiel #16
0
def load(universe, context):
    context.input_var = T.matrix('inputs')
    context.network = build_network(context.input_var)
    context.price_diffs = context.input_var[:, N_ASSETS:]
    context.target = T.ivector("targets")

    context.predictions = lasagne.layers.get_output(context.network)
    context.loss = lasagne.objectives.categorical_crossentropy(context.predictions, context.target)
    context.loss = context.loss.mean()

    context.all_params = lasagne.layers.get_all_params(context.network, trainable=True)
    context.updates = lasagne.updates.nesterov_momentum(context.loss, context.all_params,
                                                        learning_rate=lr, momentum=0.9)

    # As a bonus, also create an expression for the classification accuracy:
    # test_acc = T.mean(T.eq(T.argmax(predictions, axis=1), target), dtype=theano.config.floatX)
    context.train_fn = theano.function([context.input_var, context.target], context.loss,
                                       updates=context.updates, allow_input_downcast=True)

    # Compile a second function computing the validation loss and accuracy:
    context.val_fn = theano.function([context.input_var], context.predictions, allow_input_downcast=True)
    context.check = 0
    context.old_prices = np.zeros(N_ASSETS)
    context.portf = np.zeros(N_ASSETS)
    set_commission(commission.PerDollar(0.0006))
    context.action = np.zeros(5)
    context.pred = pd.read_csv(universe.load_file, header=None).values
    context.cnt = 0
Beispiel #17
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
Beispiel #18
0
    def zipline_initialize(context: TradingAlgorithm):
        """Zipline backtest initialization method override.

        Initializes context namespace variables used during the portfolio
        optimization, and sets zipline configuration options for the simulation.
        
        Arguments:
            context {TradingAlgorithm} -- Context variable for the algorithm
        """

        # Zipline context namespace variables
        context.first_run = True  # First run flag
        context.synthetics = dict(
        )  # Dictionary to store synthetic ETF objects
        context.port = MinimumVariance()  # Initializing portfolio

        # Enforcing long trades only
        # NOTE: This is commented out because of rounding bugs; i.e. when
        #       certain positions were reduced to 0, some of the rounding caused
        #       some positions to go to -1 shares. This is neglegible, and as
        #       the current optimization enforces no shorts, this
        #       condition is relaxed here.
        # set_long_only()

        # Setting the per-trade commission from config
        set_commission(PerDollar(cost=config.trade_commission))

        # Initializing utilities module
        context.util = Utilities()

        # Initializing bookkeeping module
        context.books = Bookkeeping()
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))
def initialize(context):
    """
    Initialization and trading logic
    """
    # Set comission and slippage
    if enable_commission:
        comm_model = PerDollar(cost=commission_pct)
    else:
        comm_model = PerDollar(cost=0.0)

    set_commission(comm_model)

    if enable_slippage:
        slippage_model = VolumeShareSlippage(
            volume_limit=slippage_volume_limit, price_impact=slippage_impact)
    else:
        slippage_model = FixedSlippage(spread=0.0)

    # Used only for progress output
    context.last_month = initial_portfolio

    # Store index membership
    context.index_members = pd.read_csv('./data/sp500_members.csv',
                                        index_col=0,
                                        parse_dates=[0])

    # Schedule rebalance monthly
    schedule_function(func=rebalance,
                      date_rule=date_rules.month_start(),
                      time_rule=time_rules.market_open())
Beispiel #21
0
def initialize(context):
    set_commission(commission.PerDollar(cost=0.0055))
    # context.strategy_name = "FOF_{mdd}"
    context.mdd = context.namespace["mdd"]
    context.fof_code = context.namespace["fof_code"]
    context.save2mysql = context.namespace.get("save_method", 0)
    context.i = 0
    schedule_function(scheduled_rebalance,
                      date_rules.month_start(days_offset=0),
                      time_rules.market_open())
    context.init_value = pd.Series()  # 记录自最近一次调仓以来,组合内各基金所达到的最大净值
    context.adjust_date = 0  # 记录距离最近一次季调的时间
    context.stop_date = 0  # 记录距离最近一次止损的时间
    # 基金池
    stock = [
        '000478.OF', '050002.OF', '110006.OF', '161120.OF', '000217.OF',
        "501018.OF", '000071.OF', '070031.OF', '000342.OF', 'B00.IPE',
        "HSCI.HI", '037.CS', "em_bond", "reit", "SPTAUUSDOZ.IDC", '096001.OF',
        'SPX.GI', '000905.SH'
    ]
    context.stocks = []
    for sym in stock:
        context.stocks.append(symbol(sym))
    #  指数与基金的对应关系 fixed
    index = [
        "HSCI.HI", '037.CS', "EM_BOND", "REIT", "SPTAUUSDOZ.IDC", "000905.SH",
        "B00.IPE", "NDX.GI", 'SPX.GI'
    ]
    index_etf = ["000071.OF","161120.OF","000342.OF",\
                 "070031.OF","000217.OF","000478.OF",\
                 "501018.OF","160213.OF",'096001.OF']
    context.df_index = pd.DataFrame(
        data=index_etf,
        index=index,
    )
Beispiel #22
0
def initialize(context):
    # Register 2 histories that track daily prices,
    # one with a 100 window and one with a 300 day window
    add_history(25, '1d', 'price')
    set_commission(commission.PerDollar(0.0003))

    context.i = 0
    context.pct = 0.1
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
Beispiel #24
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))
Beispiel #25
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))
Beispiel #26
0
def initialize(context):
    context.has_ordered = False
    set_commission(
        OrderCost(open_tax=0,
                  close_tax=0.001,
                  open_commission=0.0003,
                  close_commission=0.0003,
                  close_today_commission=0,
                  min_commission=5))
    set_long_only()
Beispiel #27
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 = {}
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))
Beispiel #29
0
    def initialize(self, context):

        comm = commission.PerShare(cost=self.cost,
                                   min_trade_cost=self.min_trade_cost)
        set_commission(comm)

        # schedule train and review functions

        schedule_function(self.train_agent, self.train_date, self.train_time)
        schedule_function(self.review_performance, self.review_date,
                          self.review_time)
Beispiel #30
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()
Beispiel #31
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
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
Beispiel #33
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())
Beispiel #34
0
    def initialize_commission(self, country='US', platform='IB'):
        """Sets commissions

        See https://www.quantopian.com/help#ide-commission and
            https://www.quantopian.com/docs/api-reference/algorithm-api-reference#zipline.finance.commission.PerDollar
        """
        if (country == 'SG'):
            set_commission(SGCommission(platform=platform))
        else:
            # IB broker for US is the top stock broker in US
            # https://brokerchooser.com/best-brokers/best-stock-brokers-in-the-us
            # Typical commission is USD 0.005 per share, minimum per order USD 1.00
            # https://www1.interactivebrokers.com/en/index.php?f=1590&p=stocks
            set_commission(commission.PerShare(cost=0.005, min_trade_cost=1))
def initialize(context):
    """
    Called once at the start of the algorithm.
    """
    context.assets = [
        (symbol('IEF'), 0.40),  # bond etf
        (symbol('QQQ'), 0.60)
    ]  # Nasdaq etf

    context.first_rebalance = 0

    set_commission(commission.PerShare(cost=0.005, min_trade_cost=1))
    # Rebalance every month, at market open.
    schedule_function(my_rebalance, date_rules.month_end(days_offset=0),
                      time_rules.market_open(minutes=1))
Beispiel #36
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
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):
    '''
    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
Beispiel #39
0
def initialize(context):
    context.i = 0
    context.sym = symbol('GS')
    set_commission(commission.PerDollar(cost=0.00165))
Beispiel #40
0
def initialize(context):
    set_commission(commission.PerDollar(cost=0.00165))
    add_history(5, '1d', 'price')
    add_history(20, '1d', 'price')
    context.i = 0
    context.investment = False
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)
Beispiel #43
0
def initialize(context):
    set_commission(commission.PerShare(cost=0.01, min_trade_cost=1.0))
    set_slippage(slippage.FixedSlippage(spread=0.0))
Beispiel #44
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)
Beispiel #45
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