Hi Eray,
Like Ryan mentioned, Quandl provides data at the daily level in this instance which you can load into your algorithm by using the 'fetch_csv' method. Here's an example of how to use it:
'''
This algorithm uses the new universe_func callback inside of fetch_csv to define an investible
universe of sids based on an externally sourced list of index constituents.
In this example we are using a csv file containing the list of SP500 constituents and GICS
sector codes from Quandl. This file is a static snapshot -- but you can point to a csv file
with varying universe composition over time and fetcher will correctly update the universe
with the current constituents.
'''
import datetime
import pandas as pd
import numpy as np
def preview(df):
log.info(' \n %s ' % df.head())
df['date'] = '2011-01-04'
df = df.rename(columns={'Ticker': 'symbol'})
return df
# Function for returning a set of SIDs from fetcher_data
def my_universe(context, fetcher_data):
# Grab just the SIDs for the Financials sector w/in the SP500:
financials = fetcher_data[fetcher_data['GICS Sector'] == 'Financials']
sids = set(financials['sid'])
symbols = [s.symbol for s in sids]
context.count = len(symbols)
print "total universe size: {c}".format(c=context.count)
# Compute target equal-weight for each stock in the SP500 Financials universe
context.target_weight = 1.0/context.count
return sids
def initialize(context):
# Fetch the SP500 constituents -- sourced from Quandl
# https://s3.amazonaws.com/quandl-static-content/Ticker+CSV%27s/Indicies/SP500.csv
# static snapshot of the SP500 constituents as of 10/2013, along with GICS sectors
# I'm grabbing the data from a file on my dropbox folder which I modified by adding
# a date column, alternatively you could add the date inside of a more complicated
# pre_func and use the csv file as is.
fetch_csv(
"https://s3.amazonaws.com/quandl-static-content/Ticker+CSV%27s/Indicies/SP500.csv",
pre_func=preview,
date_column='date',
universe_func=(my_universe))
context.target_weight = 0.01
def handle_data(context,data):
# Loop over every stock in the Financials sector and make sure we have an equal-weight
# exposure using the order_target_percent() method.
for stock in data:
# Guard for missing stock data
if 'price' in data[stock]:
order_target_percent(stock,context.target_weight)
else:
log.warn("No price for {s}".format(s=stock))
Seong
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.