I am new to quantopian and still familiarize the code.
I was trying to test out trade on open and trade on close. The following codes are cloned from "Ivy Portfolio 200-SMA". The only difference between the following to codes is the schedule_function part. I changed market_close() to market_open(). But when I back-test it, both algo give me the same transaction prices for each asset classes. Can anyone help me on this? I just want both algo to check the signal on the same day using the previous 250 days data and trade at open using opening price or trade at close using closing price ( couple minutes after open or before close are fine too.)
trade on close
# Put any initialization logic here. The context object will be passed to
# the other methods in your algorithm.
from pytz import timezone
def initialize(context):
#This variable set whether you want to allocate your entire portfolio to the best performing assets, or do you want to put the underperforming piece os pie towards cash?
context.nocash = False
set_symbol_lookup_date('2015-11-30')
# list of all raa asset classes
context.stocks = symbols('SPY',
'EFA',
'VNQ',
'DBC',
'IEF')
schedule_function(rebalance,
date_rule = date_rules.month_end(),
time_rule = time_rules.market_close())
def rebalance(context, data):
buylist = []
#find which asset classes are above their 200 day moving average
for s in context.stocks:
if s in data and data[s].price > data[s].mavg(250):
buylist.append(s)
for s in context.portfolio.positions:
if s not in buylist:
order_target_percent(s, 0)
#if nothing is worth buying, don't open a black hold by dividing by zero
if len(buylist) == 0:
return
#Do you allocae all your portfolio to the best performing asset classes or do you allocate only 1/n?
if context.nocash:
weight = 0.995/len(buylist)
else:
weight = 0.995/len(context.stocks)
for s in buylist:
order_target_percent(s, weight)
def handle_data(context, data):
for stock in context.stocks:
print stock, data[stock].price
trade on open
# Put any initialization logic here. The context object will be passed to
# the other methods in your algorithm.
from pytz import timezone
def initialize(context):
#This variable set whether you want to allocate your entire portfolio to the best performing assets, or do you want to put the underperforming piece os pie towards cash?
context.nocash = False
set_symbol_lookup_date('2015-11-30')
# list of all raa asset classes
context.stocks = symbols('SPY',
'EFA',
'VNQ',
'DBC',
'IEF')
schedule_function(rebalance,
date_rule = date_rules.month_end(),
time_rule = time_rules.market_open())
def rebalance(context, data):
buylist = []
#find which asset classes are above their 200 day moving average
for s in context.stocks:
if s in data and data[s].price > data[s].mavg(250):
buylist.append(s)
for s in context.portfolio.positions:
if s not in buylist:
order_target_percent(s, 0)
#if nothing is worth buying, don't open a black hold by dividing by zero
if len(buylist) == 0:
return
#Do you allocae all your portfolio to the best performing asset classes or do you allocate only 1/n?
if context.nocash:
weight = 0.995/len(buylist)
else:
weight = 0.995/len(context.stocks)
for s in buylist:
order_target_percent(s, weight)
def handle_data(context, data):
for stock in context.stocks:
print stock, data[stock].price