Hello,
I'm still trying to get the hang of quantopian and have a few questions about using external csv files. I've attached the following backtest containing my source code.
Simply, I'm trying to make the following:
- I import a list of stocks in a csv formatted like this:
symbol,start_date,rank
FXCM,01/01/2015,1
IRC,01/01/2015,1
FCS,01/01/2015,1
HSEB,01/01/2015,1
CXW,01/01/2015,1
YOKU,01/01/2015,1
BIN,01/01/2015,1
ADMS,01/01/2015,1
EDE,01/01/2015,1
HR,01/01/2015,1
ROVI,01/01/2015,1
AFFX,01/01/2015,1
SNH,01/01/2015,1
BDBD,01/01/2015,1
PBY,01/01/2015,1
KING,01/01/2015,1
WPP,01/01/2015,1
BMR,01/01/2015,1
From what I've read, you need to define a date field, so in real life use, I would have a PHP script that updates that csv every day with the new stocks and the current date.
- Set these stocks to my current universe
- Loop through them and buy them based on some criteria
I would think something like this would be simple. I've been reading for hours, and am stuck. The confusing thing is that the code that I currently have works only sometimes. If I build the algorithm 20 times, maybe 4 of the times it will come back and show some logs of the csv data, but I can't ever get it to advance to my handle_data method to print out the stocks. Can someone provide some insight as to what I may be missing here?
Thanks a ton!
Bryce
EDIT: It looks like you can't see the backtest source code if there are build errors. Here's the code:
import datetime
import pandas as pd
import numpy as np
# my_universe returns a set of securities that define your universe.
def my_universe(context, fetcher_data):
# fetcher_data is the data resulting from the CSV file from fetcher.
# set my_stocks to be every security in the fetcher_data
my_stocks = set(fetcher_data['sid'])
# log the size of the universe for debugging
context.count = len(my_stocks)
# print 'total universe size: {c}'.format(c=context.count)
# return the securities we identified earlier
return my_stocks
# see a snapshot of your CSV for debugging
def preview(df):
log.info(' %s ' % df.head())
return df
def initialize(context):
log.info("starting")
# import the custom CSV data file
fetch_csv("http://some_csv.csv",
pre_func=preview,
post_func=preview,
date_column='start_date',
universe_func=(my_universe))
def handle_data(context,data):
log.info("now handling data")
for stock in data:
log.info(data[stock])