scubamut
2/9/2019 - 1:40 PM

QUANTOPIAN-QUANDL CHECKED AGAINST PORTFOLIOVISUALIZER (?)

# checked against Portfoliovisualizer (different)
# https://www.quantopian.com/algorithms/581b0739ca188f3011000203

from zipline import run_algorithm
from zipline.api import get_datetime, symbols, schedule_function, date_rules, time_rules
from datetime import datetime
import pytz

def initialize(context):
    context.AAPL = symbols('AAPL')[0]
    schedule_function(
        get_monthly_returns,
        date_rules.month_end(),
        time_rules.market_close()
    )    
 
def get_monthly_returns(context, data):
    
    h = data.history(context.AAPL, ['open', 'high', 'low', 'close', 'volume', 'price'], 252, '1d')
    h_m = h.resample('M', how='last').price
    m_returns = h_m.pct_change() * 100
    print ('\n\n{} MONTHLY RETURNS\n\n{}'.format(get_datetime(), m_returns))
    
capital_base = 10000
start = datetime(2010, 12, 30, 0, 0, 0, 0, pytz.utc)
end = datetime(2011, 12, 4, 0, 0, 0, 0, pytz.utc)

result = run_algorithm(start = start, end = end, initialize=initialize,\
                capital_base=capital_base, \
                bundle = 'quantopian-quandl')