def initialize(context):
    c = context

    # Some Risk-On universes
    core_etf = symbols('QQQ', 'XLP', 'TLT', 'IEF')
    tact_etf = symbols('XLV', 'XLY', 'TLT', 'GLD')

    VAAG12 = symbols('SPY', 'IWM', 'QQQ', 'VGK', 'EWJ', 'VWO', 'VNQ', 'GSG',
                     'GLD', 'TLT', 'LQD', 'HYG')
    VAAG4 = symbols('SPY', 'VEA', 'VWO', 'BND')

    c.RISK = VAAG4  # Which Riskasset universe to use?

    alletf = symbols('QQQ', 'XLP', 'TLT', 'IEF', 'SPY', 'IWM', 'QQQ', 'VGK',
                     'EWJ', 'VWO', 'VNQ', 'GSG', 'GLD', 'TLT', 'LQD', 'HYG',
                     'XLV', 'XLY', 'GLD', 'VEA')  # 'TLO' repleced by 'TLT'
    # Some Risk Off (Cash) universes
    c.CASH = symbols('SHY', 'IEF', 'LQD')

    schedule_function(define_weights, date_rules.month_end(Month_Offset),
                      time_rules.market_open(minutes=64))
    log.info('define_weights for last trading day of the month')

    schedule_function(trade_sell, date_rules.month_end(Month_Offset),
                      time_rules.market_open(minutes=65))
    log.info('trade_sell for last trading day of the month')

    if TplusX > 0:
        schedule_function(
            trade_buy, date_rules.month_start(max(0,
                                                  TplusX - 1 - Month_Offset)),
            time_rules.market_open(minutes=66))
        log.info('trade_buy for month day ' +
                 str(max(0, TplusX - Month_Offset)))
    else:
        schedule_function(trade_buy, date_rules.month_end(Month_Offset),
                          time_rules.market_open(minutes=66))
        log.info('trade_buy last trading day of the month')

    stocks = core_etf + tact_etf
    context.sell = {}
    context.buy = {}

    for stock in stocks:
        context.sell[stock] = 0
        context.buy[stock] = 0

    context.difference_perc = 0.02

    context.stops = {}
    context.stoploss = 0.30
    context.stoppedout = []
示例#2
0
def get_date_rules(freq):

    if freq == "day":

        start_rule = date_rules.every_day()
        end_rule = date_rules.every_day()

    elif freq == "week":

        start_rule = date_rules.week_start()
        end_rule = date_rules.week_end()

    elif freq == "month":

        start_rule = date_rules.month_start()
        end_rule = date_rules.month_end()

    else:

        start_rule = date_rules.every_day()
        end_rule = date_rules.every_day()

    date_rule = {"start": start_rule, "end": end_rule}
    time_rule = {
        "start": time_rules.market_open(),
        "end": time_rules.market_close()
    }

    return date_rule, time_rule
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))
def initialize(context):
    """
    Called once at the start of the algorithm.
    """
    # Rebalance every day, 1 hour after market open.
    schedule_function(rebalance, date_rules.month_end(),
                      time_rules.market_open(hours=1))

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

    # Create our dynamic stock selector.
    print('ATTACH PIPELINE')
    attach_pipeline(make_pipeline(), 'pipeline')
    print('PIPELINE ATTACHED')
def initialize(context):
    context.universe = StaticAssets(symbols(
        'XLY',  # Select SPDR U.S. Consumer Discretionary
        'XLP',  # Select SPDR U.S. Consumer Staples
        'XLE',  # Select SPDR U.S. Energy
        'XLF',  # Select SPDR U.S. Financials
        'XLV',  # Select SPDR U.S. Healthcare
        'XLI',  # Select SPDR U.S. Industrials
        'XLB',  # Select SPDR U.S. Materials
        'XLK',  # Select SPDR U.S. Technology
        'XLU',  # Select SPDR U.S. Utilities
    ))

    #     my_pipe = Pipeline()
    my_pipe = make_pipeline(context)

    # context.universe = StaticAssets(etfs)
    attach_pipeline(my_pipe, 'my_pipeline')

    schedule_function(func=rebalance,
                      date_rule=date_rules.month_end(days_offset=2),
                      # date_rule=date_rules.every_day(),
                      time_rule=time_rules.market_close(minutes=30))