Exemple #1
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,
        time_rules.market_open(minutes=1),
    )

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

    # Set up universe, alphas and ML pipline
    context.universe = QTradableStocksUS()

    ml_pipeline = make_ml_pipeline(
        context.universe,
        n_forward_days=PRED_N_FORWARD_DAYS,
        window_length=ML_TRAINING_WINDOW,
    )
    # Create our dynamic stock selector.
    attach_pipeline(ml_pipeline, 'alpha_model')

    context.past_predictions = {}
    context.hold_out_accuracy = 0
    context.hold_out_log_loss = 0
    context.hold_out_returns_spread_bps = 0
Exemple #2
0
def initialize(context):
    """
    Called once at the start of the algorithm.
    """
    set_slippage(slippage.VolumeShareSlippage(volume_limit=0.025, price_impact=0.1)) # Default
    set_commission(commission.PerShare(cost=0.005, min_trade_cost=1.0)) # FSC for IB

    schedule_function(
        rebalance,
        TRADE_FREQ,
        time_rules.market_open(minutes=1),
    )

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

    # Set up universe, alphas and ML pipline
    context.universe = Q1500US()
    # if you are using IsAnnouncedAcqTarget, uncomment the next line
    # context.universe &= IsAnnouncedAcqTarget()

    ml_pipeline = make_ml_pipeline(
        context.universe,
        n_forward_days=PRED_N_FORWARD_DAYS,
        window_length=ML_TRAINING_WINDOW,
    )
    # Create our dynamic stock selector.
    attach_pipeline(ml_pipeline, 'alpha_model')

    context.past_predictions = {}
    context.hold_out_accuracy = 0
    context.hold_out_log_loss = 0
    context.hold_out_returns_spread_bps = 0
Exemple #3
0
def initialize(context):
    """
    Called once at the start of the algorithm.
    """
    # Rebalance
    algo.schedule_function(
        rebalance,
        algo.date_rules.month_end(),  # SHOULD BE MONTH
        algo.time_rules.market_close(minutes=30))

    # Record tracking variables at the end of each day.
    algo.schedule_function(record_vars, algo.date_rules.every_day(),
                           algo.time_rules.market_close(minutes=15))

    # Create our dynamic stock selector.
    algo.attach_pipeline(make_pipeline(), 'pipeline')

    # Set commissions
    algo.set_commission(commission.PerTrade(cost=19.0))
    #algo.set_commission(commission.NoCommission())
    #algo.set_commission(commission.PerShare(cost=.05))

    context.use_weights = True  # If weights should be used in optimization

    context.filters = True  # use filtering?
    context.TF_lookback = 63  # How many days of SPY to look at
    context.can_buy_stocks = False  # Triggered by bullish market
    context.can_buy = True

    context.bonds = [symbol('IEF'), symbol('SHY'), symbol('TLT')]

    context.num_stocks_to_trade = 20
    context.roe_top_n = 50  # How many top roe companies to keep
    context.momentum_days = 126  # How many days to check momentum
    context.score_to_go = 30  # Minimum Momentum score to consider a share
    context.days_to_skip = 10  # days before today to ignore in momentum