def rebalance(context, data):
    # Calculate slopes for each futures
    prediction = calc_slopes(context, data)

    # Get target weights to futures contracts based on slopes
    target_weights = get_target_weights(context, data, prediction)

    # Exposure is noted for logging and record() plotting
    context.exposure = {}
    text = ''
    for contract in target_weights:
        context.exposure[contract.root_symbol] = target_weights[contract]
        if target_weights[contract] != 0:
            text += "\n%+3.1f%% \t%s \t(%s)" % (target_weights[contract] * 100,
                                                contract.symbol,
                                                contract.asset_name)
    if text == '':
        text = '\nNo positions to take'
    log.info('Target position of today:' + text)

    # Rebalance portfolio using optimaize API
    order_optimal_portfolio(opt.TargetPortfolioWeights(target_weights),
                            constraints=[
                                opt.MaxGrossLeverage(context.maxleverage),
                            ],
                            universe=target_weights)
Ejemplo n.º 2
0
def rebalance_pairs(context, data):
    # equilibrium(平衡) から現在のスプレッドが離れているか計算
    zscore = calc_spread_zscore(context, data)
    # weightを計算
    target_weights = get_target_weight(context, data, zscore)

    if target_weights:
        # TargetPortfolioWeights https://www.evernote.com/Home.action#b=177d468e-bf46-408c-8f29-97655b654be5&st=p&x=TargetPortfolioWeights&n=b9413a7e-db9e-4a8b-81c7-cb3929d925f2
        order_optimal_portfolio(opt.TargetPortfolioWeights(target_weights),
                                constraints=[])
Ejemplo n.º 3
0
def allocate(context, data):
    # Set objective to match target weights as closely as possible, given constraints
    objective = opt.TargetPortfolioWeights(context.target_weights)

    # Define constraints
    constraints = []
    constraints.append(opt.MaxGrossLeverage(MAX_GROSS_LEVERAGE))

    algo.order_optimal_portfolio(objective=objective,
                                 constraints=constraints,
                                 universe=context.stocks)
Ejemplo n.º 4
0
def rebalance_pairs(context, data):

    # Calculate how far away the current spread is from its equilibrium
    zscore = calc_spread_zscore(context, data)

    # Get target weights to rebalance portfolio
    target_weights = get_target_weights(context, data, zscore)

    if target_weights:
        # If we have target weights, rebalance portfolio
        order_optimal_portfolio(opt.TargetPortfolioWeights(target_weights),
                                constraints=[])
Ejemplo n.º 5
0
def rebalance(context, data):
    context.short1 = data.current(context.continues[0][0], 'contract')
    context.short2 = data.current(context.continues[1][0], 'contract')
    context.long1 = data.current(context.continues[1][1], 'contract')
    targets = {
        context.short1: -0.33,
        context.short2: -0.33,
        context.long1: 0.33
    }

    for x in range(len(targets)):
        order_optimal_portfolio(objective=opt.TargetPortfolioWeights(targets),
                                constraints=[])
Ejemplo n.º 6
0
def check_signal(context, data):
    if context.counter != 0:  #We only want to check after the first day
        sentiment = context.sentiment

        scores = {}  #Holds the scores for the stock to invest in
        for i in range(len(context.names)):
            name = context.names[i]
            symbol = context.symbols[i]

            s = sentiment[name]
            if s > 7:
                scores[symbol] = s

        weights = get_weights(scores)
        print weights

        objective = opt.TargetPortfolioWeights(weights)
        order_optimal_portfolio(objective, [])

    context.sentiment = data[
        "Sentiment"]  #Pull in todays sentiment for use on the next day
    context.counter += 1