Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Help to develop a simple algorithm

Hello,

I am student trying to learn to program, develop simple trading algorithms to understand price behavior during certain events. I am trying to develop an algorithm that will show me how the price of SPY changes before the ex-dividend date and after the pay date.

I looked at the API, but I can't find something that will help me buy a certain amount of shares around the ex-dividend date and sell it after the pay date. As I understood it, it is not yet implemented. Is there anything I can do to write an algorithm that will accomplish what I need? I would also like to plot my position during that time period. I hope what I said made sense.

Any help that would lead me to getting started would be appreciated.

Thank you,

Jaime

2 responses

Hello Jamie,

You need to find the dividend data and import to Quantopian via 'fetcher'. The following may give you some ideas. This data if from http://www.quandl.com/SEC-U-S-Securities-and-Exchange-Commission/DIV_SPY-SPDR-S-P-500-ETF-SPY-Dividends

''' The CSV file contains data in the format:

Date    Dividend  
15/03/2002  0.331  
21/06/2002  0.353  
20/09/2002  0.378  
20/12/2002  0.436  
21/03/2003  0.354

'''

def initialize(context):  
    context.SPY = [sid(8554)]  
    fetch_csv('https://raw.github.com/pcawthron/StockData/master/SPY%20Dividends.csv',  
              symbol      = 'SPY',  
              date_column = 'Date',  
              date_format = '%d/%m/%Y',  
              pre_func    = show_csv)  

def show_csv(df):  
    print df.head()  
    return df

def handle_data(context, data):  
    if 'Dividend' in data['SPY'] and data['SPY'].datetime == get_datetime():  
        print "Dividend: " + str(data['SPY']['Dividend'])  
        record(Dividend=data['SPY']['Dividend'])  

Log:

1970-01-01 PRINT Date Dividend  
0 15/03/2002 0.331  
1 21/06/2002 0.353  
2 20/09/2002 0.378  
3 20/12/2002 0.436  
4 21/03/2003 0.354  
2002-03-15 PRINT Dividend: 0.331  
2002-06-21 PRINT Dividend: 0.353  
2002-09-20 PRINT Dividend: 0.378  
2002-12-20 PRINT Dividend: 0.436  
2003-03-21 PRINT Dividend: 0.354  
End of logs.  

Thank you, Peter, that will help me very much.