As amateur Python hacker, I must admit it took me a while to come to grips with Batch Transforms and Fetcher. This little test helped me understand them better... Maybe it will help someone else too!
As amateur Python hacker, I must admit it took me a while to come to grips with Batch Transforms and Fetcher. This little test helped me understand them better... Maybe it will help someone else too!
I think that's a pretty nifty little demo, thank you.
I threw in a "cash" line to show why the two prices aren't the same - the Quantopian version is getting dividends as a cash payment, not a price adjustment like Yahoo Finance does it.
I also changed the date range to run to yesterday. If you scroll all the way to the bottom of the logs, you'll see that the prices finally match up for yesterday, which you'd expect - there is no price adjustment on yesterday's price.
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.
On a related note, I'm having a lot of trouble importing dividend history into the IDE. I'm getting the error: AttributeError: 'SIDData' object has no attribute 'datetime'. What am I doing incorrectly here?
def rename_col(df):
df = df.rename(columns={'Dividends': 'div'})
df = df.fillna(method='ffill')
#df = df[['price', 'sid']]
return df
@batch_transform(window_length=20)
def get_past_prices(data):
prices = data['div']
# Yprices = data['spy']
return prices
def initialize(context):
# this will add a column 'ibm' to data containing Yahoo dividend yields
fetch_csv('http://ichart.finance.yahoo.com/table.csv?s=IBM&a=00&b=2&c=1962&d=06&e=9&f=2013&g=v&ignore=.csv',
date_column='Date',
date_format='%Y-%m-%d',
symbol='ibm',
usecols=['Dividends'],
post_func=rename_col)
context.IBM = sid(3766);
context.day = 0
def handle_data(context, data):
ibm_data = data['ibm']
if context.day == 0 :
order (context.IBM, 10)
context.day = 1
# batch prices
all_prices = get_past_prices(data)
if all_prices is None :
return
else :
log.info('\n all_prices\n %s' % all_prices)
# plot it
record(YSPY=ibm_data['div'], QSPY=data[context.IBM]['price'])
log.info('\nY IBM = %s' % ibm_data['div'] + ' Q IBM = %s' % data[context.IBM]['price'])
Jim, next time feel free to create a new thread ;)
Your file only has two columns in it, Date and Dividends. You need to have another column, one that includes the symbol (docs here). I see that you're trying to indicate that the symbol is IBM, but you need to get IBM loaded into the datapanel. I bet there is a way to do that in your pre_func, but I'm not enough of a pandas expert to say how to do that.
Also, you understand that the Quantopian backtester already includes dividends in the backtest returns?
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.