Hello all,
I have been trying to figure out a way to load data from before the start date so there is no data lag for the first days of trading. I would like to load a csv with the prior few months of prices so I only need it the one time to set some initial parameters. My problem is that the fetcher will go get the same old data daily which seems like a waste and there doesn't seem to be a way around this.
Questions:
- Is there a way to call fetch_csv once?
- can the fetcher be given several urls or be called several times per cycle?
- Is zipline.utils.factory.load_from_yahoo supported???? I get Runtime exception: TypeError: a float is required
- Will there be URL support outside of the fetcher?? I assume not for security reasons.
Im following this template because I think it's a great step in the right direction.
import datetime
import math
import numpy as np
import pandas as pd
# I'd like to use the fetcher in this sort of way.
class Thing():
def __init__(self, context):
self.context = context
def pre_fetcher(self, df):
# modify df and update context here
log.info(str(df))
return df
def post_fetcher(self, df):
# And/or here
log.info(str(df))
return df
class YahooCSV():
"""
This is a tool to get the url for the current quote of a company
or the historical data over a time period. They have sector and
industry data as well.
To extend this there's more info here,
http://code.google.com/p/yahoo-finance-managed/wiki/CSVAPI
"""
def quote(self, symbol, stat='all'):
url = "http://download.finance.yahoo.com/d/quotes.csv?s=%s&f=" %(symbol)
all_stats = 'l1c1va2xj1b4j4dyekjm3m4rr5p5p6s7a0b0'
if not stat == 'all':
url += stat
else:
url += all_stats
return url
def history(self, sym, start_date, end_date):
#
# Date format must be 'YYYY-MM-DD'
# str(data[sym].datetime) works for this
#
url = 'http://ichart.yahoo.com/table.csv?'
url += 's={}&a={}&b={}&c={}&d={}&e={}&f={}'.format(
sym,
str(int(start_date[5:7]) - 1),
str(int(start_date[8:10])),
str(int(start_date[0:4])),
str(int(end_date[5:7]) - 1),
str(int(end_date[8:10])),
str(int(end_date[0:4]))
)
url += '&g=d&ignore=.csv'
return url
def initialize(context):
context.stocks = [sid(16841), sid(8229)]
start_date = '2012-06-01'
end_date = '2013-01-01'
url = YahooCSV().history('AMZN', start_date, end_date)
fetch_csv(
url,
date_column = 'Date',
symbol = 'AMZN',
pre_func = Thing(context).pre_fetcher,
post_func = Thing(context).post_fetcher,
# one_time_fetch = True this would be awesome to have!!
)
########################################################
# ALTERNATIVE
########################################################
def initialize(context):
#
#
context.handle_back_data = True
context.back_data_start_date = datetime.datetime(2012,1,1)
context.back_data_end_date = datetime.datetime(2012,6,1)
#
#Build the Universe
#Add SPDR SPY
context.SPY = sid(8554)
context.SHY = sid(23911)
#Set constraints on borrowing
context.pct_invested_threshold = 0.95 #Set limit on percent invested (as a decimal)
context.init_margin=1.50 #Set initial margin requirement
context.maint_margin=1.25 #Set the maintenance margin requirement
def handle_data(context, data):
if context.handle_back_data:
handle_back_data(context)
context.handle_back_data = False
# Update new frame
update_newFrame(context, data)
#Apply Trade Logic
trade_logic(context,data)
def handle_back_data(context):
start = context.back_data_start_date
end = context.back_data_end_date
back_data = load_from_yahoo(
stocks=['SPY', 'SHY'],
indexes={},
start=start,
end=end,
adjusted=False
)
# Mess with back data here:
I actually like the second way more because load_from_yahoo takes a list of stocks.