Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
context.portfolio.positions_value always = 0.0

Hi everyone,

It's me again. I was experimenting with a sample TRIX algorithm and trying to track the outstanding positions. However, somehow the number returned by 'context.portfolio.positions_value' always equals to zero.

import talib  

def initialize(context):  
    context.futures = continuous_future('CL', offset=0, roll='volume', adjustment=None)  
    schedule_function(rebalance, date_rules.every_day(), time_rules.market_open(minutes = 60))

def rebalance(context, data):  

    spot = data.current(context.futures, 'contract')  
    positions = context.portfolio.positions_value  
    log.info(positions)  
    prices = data.history(context.futures, 'price', 250, '1d')  
    trixs = talib.TRIX(prices, 15)  
    trix = talib.TRIX(prices, 30)[-1]  
    trix_signal = talib.EMA(trixs, 9)[-1]

    if trix > trix_signal and trix > 0 and data.can_trade(spot):  
        order_target_percent(spot, 1.0)  
    elif trix < trix_signal and trix < 0 and data.can_trade(spot):  
        order_target_percent(spot, -1.0)  
    record(Positions=positions, Trix=trix, Signal=trix_signal)  

Appreciate if anyone can tell me what I did wrong. Thanks in advance.