Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
record function double writing data points (Zipline)

I started writing some code in zipline some time ago with the intention of training a neural net on the data.
My 'target' was created as a simple "if the price tomorrow is 2 higher than today, we should buy today"
However, my portfolio value dropped no matter what. Even if i saved the raw target curve and bought according to that!

So i made this small test function to see what happens. Here, i first record the prices 1 minute before close each day. and save them to a file called "oracle". The next time i run the code, i compare the current price with the corresponding position in the previously recorded prices.
The first 200 or such samples the comparison is true. Then the comparison is true for a delayed version of the saved prices, and so on..

the test code can be seen below.

#!/usr/bin/env python -W ignore::DeprecationWarning  
#

from datetime import datetime  
import pandas as pd  
import pytz  
import progressbar  
import zipline  
from zipline.api import order, record, symbol, history, schedule_function, date_rules, time_rules  
import matplotlib.pyplot as plt  
import numpy as np  
import scipy as sp  
import talib #http://www.tadoc.org/

def initialize(context):  
    context.i = 0  
    context.firstRun = False  
    if not context.firstRun:  
        context.oracle = np.load('oracle.npy')  
    schedule_function(buyOrLog,date_rules.every_day(), time_rules.market_close(minutes = 1), half_days =True)


def handle_data(context, data):  
    #glob_bar.update(context.i)  
    just_something = True


def buyOrLog(context, data):  
    pclose = data.current(symbol('AAPL'), 'close')  
    record(close=pclose)

    if not context.firstRun:   #not logging.. buy stuff..  
        print str(pclose == context.oracle[context.i]) + '\t' + str(pclose == context.oracle[context.i+1]) + '\t' + str(pclose == context.oracle[context.i+2]) + ' sample ' + str(context.i)

    context.i += 1



def analyze(context=None, results=None):  
    if(context.firstRun):  
        oracle = np.array(results.close)  
        oracle = np.append(oracle,0)  
        np.save('oracle.npy', oracle)



if __name__ == "__main__":  
    base_capital = 600  
    #start = datetime(2000, 1, 1, 0, 0, 0, 0, pytz.utc)  
    start = datetime(2002, 1, 1, 0, 0, 0, 0, pytz.utc)  
    end = datetime(2010, 1, 1, 0, 0, 0, 0, pytz.utc)

    #global glob_bar  
    #glob_bar = progressbar.ProgressBar(max_value = np.busday_count(start,end))  
    zipline.run_algorithm(start, end, initialize=initialize, capital_base=base_capital, handle_data=handle_data, analyze=analyze, data_frequency='daily', bundle='quantopian-quandl')