Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Inconsistent variable data in debugger

I am seeing inconsistent variable data within the debugger - has anyone else run into this? ```

from collections import deque  
import pandas as pd  
import numpy as np  
from datetime import date  
def initialize(context):  
    #set_universe(universe.DollarVolumeUniverse(97.5, 98.0))  
    context.stocks = {sid(6413)}  
    context.historicals = {}  
    context.high_mark = 0  
    context.start = None  
    context.spikeStarts = {}  
    context.purchase_size = 10000  
    log.info("Testing")  
def calculateVols(r, dt):  
    interDev = r['C-C'].tail(20).std()  
    overnightDev = r['C-O'].tail(20).std()  
    intraDev = r['O-C'].tail(20).std()  
    interVol = interDev*15.87  
    overnightVol = overnightDev*18.59  
    intraVol = intraDev*30.5  
    r['interDev'][dt]=interDev  
    r['overnightDev'][dt]=overnightDev  
    r['intraDev'][dt]=intraDev  
    r['interVol'][dt]=interVol  
    r['overnightVol'][dt]=overnightVol  
    r['intraVol'][dt]=intraVol  
    intraAvg = r.intraVol.tail(20).mean()  
    overAvg = r.overnightVol.tail(20).mean()  
    if overAvg==0:  
        r['volRatio'][dt] = 0  
    else:  
        r['volRatio'][dt] = r.intraVol.tail(20).mean()/r.overnightVol.tail(20).mean()  
def clearPortfolio(context, stock):  
    if context.portfolio.positions[stock].amount > 0:  
        if stock in context.spikeStarts:  
            spikeDif = get_datetime() - context.spikeStarts[stock]  
            if spikeDif.days > 20:  
                log.info("Closing trade in {0} after {1}".format(stock.symbol, spikeDif.days))  
                order(stock, -1*context.portfolio.positions[stock].amount)  
        else:  
            log.info("Closing trade in {0} - could not tell how long it was open".format(stock.symbol))  
            order(stock, -1*context.portfolio.positions[stock].amount)  
def executeTrades(context, stock):  
    #log.info("Checking on trade")  
    pos = context.portfolio.positions[stock]  
    if pos.amount == 0.0:  
        #log.info("Placing order")  
        order(stock, context.purchase_size)  
def evalTrade(context, stock, hist, dt):  
    vr = hist.volRatio.tail(1)  
    interSpike = hist.interSpike.tail(1)  
    movAvg = hist.volRatio.tail(5).mean()  
    if movAvg==0:  
        x=0  
    else:  
        x = (vr[dt]-movAvg)/movAvg  
    dif = get_datetime() - context.start  
    #log.info(str(dif) + " days since start")  
    shift = 0.0  
    if (int(dif.days)>33):  
        pos = context.portfolio.positions[stock]  
        if interSpike > 2 or interSpike < -2:  
            if context.portfolio.positions[stock].amount > 0:  
                #log.info("Big spike of {0} happened. Covering".format(interSpike[dt]))  
                spikeDif = get_datetime() - context.spikeStarts[stock]  
                log.info("Spike of {0} happened on {1} after {2} days.".format(interSpike[dt], stock.symbol, spikeDif.days))  
                order(stock, -1*pos.amount)  
            else:  
                log.info("Missed spike of {0} on {1}.".format(interSpike, stock.symbol))  
        if (context.high_mark!=0):  
            shift = x/context.high_mark  
        #log.info(str(vr[dt]) + "," + str(movAvg))  
        #log.info(str(context.high_mark) + "," + str(x) + "," + str(shift))  
        threshold = .75 * -1  
        if (shift<=threshold):  
            #log.info("Big swing")  
            context.high_mark = x  
            log.info("Expecting big swing in {0} on {1}".format(stock.symbol, dt))  
            context.spikeStarts[stock] = dt  
            executeTrades(context, stock)  
        elif (np.absolute(x)>np.absolute(context.high_mark)):  
            #log.info("New high mark: " + str(x))  
            context.high_mark = x  
# Will be called on every trade event for the securities you specify.  
def handle_data(context, data):  
    for stock in data:  
        #log.info(stock)  
        quote = data[stock]  
        dt = quote['dt']  
        if stock not in context.historicals:  
            context.start = get_datetime()  
            opens = history(21, '1d', 'open_price')  
            closes = history(21, '1d', 'close_price')  
            d = {'open':opens[stock], 'last':closes[stock]}  
            stock_history = pd.DataFrame(d)  
            stock_history['C-C'] = np.log(stock_history['last']/stock_history['last'].shift(1))  
            stock_history['C-O'] = np.log(stock_history['open']/stock_history['last'].shift(1))  
            stock_history['O-C'] = np.log(stock_history['last']/stock_history['open'])  
            stock_history['interDev'] = float()  
            stock_history['overnightDev'] = float()  
            stock_history['intraDev'] = float()  
            stock_history['interVol'] = None  
            stock_history['overnightVol'] = None  
            stock_history['intraVol'] = None  
            stock_history['interSpike'] = float()  
            stock_history['overnightSpike'] = float()  
            stock_history['intraSpike'] = float()  
            stock_history['volRatio'] = None  
            calculateVols(stock_history, dt)  
            context.historicals[stock] = stock_history  
            #log.info(context.historicals)  
        else:  
            stock_history = context.historicals[stock]  
            open = quote['open_price']  
            last = quote['close_price']  
            prev_close = stock_history['last'].tail(1).item()  
            #prev_open = stock_history.open.tail(1).item()  
            prev_dev = stock_history.interDev.tail(1).item()  
            oneDev = prev_close*prev_dev  
            interday = np.log(last/prev_close)  
            intraday = np.log(last/open)  
            overnight = np.log(open/prev_close)  
            interSpike = oneDev * (last-prev_close)  
            intraSpike = oneDev * (last-open)  
            overnightSpike = oneDev * (open-prev_close)  
            row = pd.Series({'last':last, 'open':open, 'C-C': interday, 'C-O': overnight, 'O-C': intraday, 'interDev': 0.0, 'overnightDev': 0.0, 'intraDev': 0.0, 'interVol': 0.0, 'overnightVol': 0.0, 'intraVol': 0.0, 'interSpike': interSpike, 'overnightSpike': overnightSpike, 'intraSpike': intraSpike, 'volRatio': 0.0})  
            row.name = dt  
            stock_history = stock_history.append(row)  
            calculateVols(stock_history, dt)  
            #log.info(row)  
            context.historicals[stock] = stock_history  
            #log.info(context.historicals)  
        evalTrade(context, stock, context.historicals[stock], dt)  
        clearPortfolio(context, stock)  
    #log.info("Debug")  

At the breakpoint at line 74 (in evalTrade()), I am evaluating the DataSeries "hist", and I get this when I look into the data series itself:

https://www.dropbox.com/s/2wfe5sz4gctm4qv/Screenshot%202015-01-26%2022.48.19.png?dl=0

When I ask for the TimeSeries property within "hist.interDev", I get different data:

https://www.dropbox.com/s/16bilkolwd0bkov/Screenshot%202015-01-26%2022.48.47.png?dl=0

It seems to me these two should be equivalent. Am I missing something?

1 response

This looks to be a data display issue in the debugger with history. We'll get that fixed!

In the meantime, you can look at hist[column_name] to look at individual column's data in the debugger.

Disclaimer

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.