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

Hello,

I am having a difficult time writing what is suppose to be a simple algorithm. I cannot determine if it is me not understanding Python or do not have a grasp on properly using the API. Can anyone help me write it, I am a student trying to understand simple market behaviors.

What I want the algorithm to do is buy SPY five trading day before the ex-dividend date and sell it three days after the pay date. I want to buy an arbitrary amount, 5000 shares. These are the dates of interest:

Ex-dividend date Pay date


9/20/13 10/31/13
6/21/13 7/31/13
3/15/13 4/30/13
12/21/12 1/31/13

I would appreciate the help very much. I tried using the get_date() function to access a certain day, but I got errors. I'm going over Python because I believe my problems stem from here.

Thanks

3 responses

Jaime, sometimes it's easiest if you share your existing code, even if it fails. I'd be happy to help.

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.

Hi Dan,

This is what I am trying to do, but I am getting an error. I am not accessing the date time object correctly. Can you help? This is all very new to me, so your help is appreciated.

# Put any initialization logic here.  The context object will be passed to  
# the other methods in your algorithm.  
import datetime  
import pytz  
import bisect  
import cmath  
import collections  
import functools  
import heapq  
import itertools  
import math  
import numpy  
import pandas  
import Queue  
import random  
import re  
import scipy  
import statsmodels  
import sklearn  
import time  
import zipline


def initialize(context):  
    #context.currTime=get_datetime()  
  context.SPY=sid(8554)


# Will be called on every trade event for the securities you specify.  
def handle_data(context, data):  
    day = 20  
    month =9  
    year = 2013  
    current_day = get_datetime()  
   if year == current_day.year and month == current_day.month and day == current_day.day:  #want to buy SPY on ex-dividend date for now.  
        order(context.SPY, 1000)  

Hello Jaime,

I got your code to run (see attached). Note that for minute data, the code will need some tweaking, but for daily data it is o.k.

Some tips:

  • I suggest only importing modules that you need.
  • The day/month/year values for trade execution can be stored in a context variable, using a suitable Python data structure (e.g. a list).
  • The function get_datetime() returns type 'datetime.datetime' so if you put your dates into a datetime type, you should be able to make a direct comparison.
  • You might consider using a Python membership operator (e.g. if current_day in context.days, where context.days is a list of datetime objects).

Grant