Hi,
I am confused regarding one aspect of the behavior of moving average.
Since the moving average is calculated per day, I believe (please correct me if I'm wrong) that the value of moving average doesn't change during the day.
If that is correct, the back test should be same in both cases, calculating moving average in handle_data and also in a scheduled function rebalance running once a day 1 hour after the market opens.
However, it is not so.
I am getting different backtest results for:
1.
def initialize(context):
context.stocks = symbols('XLY','XLF','XLK','XLE','XLV','XLI','XLP','XLB','XLU')
#schedule_function(rebalance, date_rules.every_day(), time_rules.market_open(hours = 1))
def handle_data(context,data):
for stock in context.stocks:
k1 = data.history(stock,'price',50,'1d')
ma1 = k1.mean()
k2 = data.history(stock,'price',200,'1d')
ma2 = k2.mean()
price = data.current(stock,'price')
if ma1 > ma2:
log.info('Buying')
log.info(ma1)
log.info(ma2)
order_target_percent(stock,0.11)
elif ma1 < ma2:
log.info('Shorting')
log.info(ma1)
log.info(ma2)
order_target_percent(stock,-0.11)
2.
def initialize(context):
context.stocks = symbols('XLY','XLF','XLK','XLE','XLV','XLI','XLP','XLB','XLU')
schedule_function(rebalance, date_rules.every_day(), time_rules.market_open(hours = 1))
def rebalance(context,data):
for stock in context.stocks:
k1 = data.history(stock,'price',50,'1d')
ma1 = k1.mean()
k2 = data.history(stock,'price',200,'1d')
ma2 = k2.mean()
price = data.current(stock,'price')
if ma1 > ma2:
log.info('Buying')
log.info(ma1)
log.info(ma2)
order_target_percent(stock,0.11)
elif ma1 < ma2:
log.info('Shorting')
log.info(ma1)
log.info(ma2)
order_target_percent(stock,-0.11)
Could someone please help me understand why?