Good afternoon,
Thanks to this paper: http://www.sciencedirect.com/science/article/pii/S1059056016301563
I developed this strategy in which I predict the direction of the overnight SPY and of the first 30 minutes of trading based on the last 30 minutes of the day before.
This is my code:
from quantopian.algorithm import attach_pipeline, pipeline_output
from quantopian.pipeline import Pipeline
from quantopian.pipeline import CustomFactor
from quantopian.pipeline.data.builtin import USEquityPricing
from quantopian.pipeline.data import morningstar
import numpy as np
import pandas as pd
from datetime import date, timedelta
from quantopian.pipeline.filters.morningstar import Q500US
def initialize(context):
#schedule function so that I open positions before market closes
schedule_function(rebalance1, date_rules.every_day(), time_rules.market_close(minutes=1))
#schedule function to change position
#when the market opens the next morning
schedule_function(rebalance2, date_rules.every_day(), time_rules.market_open(minutes=1))
#schedule function to close all positions 30 minutes
#after the market opens everyday
schedule_function(rebalance3, date_rules.every_day(), time_rules.market_open(minutes=31))
#leverage for long positions
context.long_leverage = 1
#leverage for short positions
context.short_leverage = -1
#create pipeline
my_pipe = make_pipeline()
attach_pipeline(my_pipe, 'my_pipeline')
def make_pipeline():
# Base universe set to the Q1500US.
base_universe = Q500US()
# find close minute of trading and
last1min_before_clos = data.history(base_universe, 'close', 1, '1m')
last30min_before_clos = data.history(base_universe, 'close', 30, '1m')
#see where we have a gain in the last 30 minutes of trading
increasing_last30mins = last1min_before_clos > last30min_before_clos
# Filter to select securities to long.
longs_overnight = increasing_last30mins.top(25)
# Filter to select securities to short.
shorts_overnight = increasing_last30mins.bottom(25)
# Get today's open and yesterday's close prices
today_open = data.history(base_universe, 'open', 1, '1d')
yesterday_close = data.history(base_universe,'close', 2, '1d')
#see where we have a gain overnight
increasing_overnight = today_open > yesterday_close
# Filter to select securities to long.
longs_morning = increasing_overnight.top(25)
# Filter to select securities to short.
shorts_morning = increasing_overnight.bottom(25)
# Filter for all securities that we want to trade.
securities_to_trade = (longs_overnight | shorts_overnight | longs_morning | shorts_morning)
pipe = Pipeline(
columns={
'longs_overnight': longs_overnight,
'shorts_overnight': shorts_overnight,
'longs_morning': longs_morning,
'shorts_morning': shorts_morning,
},
screen=securities_to_trade,
)
def before_trading_start(context, data):
# Call pipelive_output to get the output
context.output = pipeline_output('stocks_500')
Olongs_list = context.output[0]
Oshorts_list = context.output[1]
Mlongs_list = context.output[2]
Mshorts_list = context.output[3]
#This function rebalances my portfolio before the market closes
#I go long if the "last 30 minutes of trading" are positive and
#I short otherwise.
#This happens only for the best-performing 5% stocks and
#for the worst-performing 5% stocks.
def rebalance1(context, data):
#Compute weights
long_weight = context.long_leverage / float(len(Olongs_list))
short_weight = context.short_leverage / float(len(Oshorts_list))
#If last "30 minutes of trading" returns are positive I go long
#based on my long list, otherwise I don't invest.
for Olong in Olongs_list:
if Olong > 0:
order_target_percent(pipe.Olongs_list, long_weight)
else:
order_target_percent(pipe.Olongs_list, 0)
#If last "30 minutes of trading" returns are negative I go short
#based on my short list, otherwise I don't invest.
for Oshort in Oshorts_list:
if Oshort < 0:
order_target_percent(pipe.Olongs_list, short_weight)
else:
order_target_percent(pipe.Olongs_list, 0)
#This function rebalances my portfolio when the market opens
#I go long if the "overnight returns" are positive and
#I go short otherwise.
#This happens only for the best-performing 5% stocks and
#for the worst-performing 5% stocks
def rebalance2(context, data):
#compute weights
long_weight = context.long_leverage / float(len(Mlongs_list))
short_weight = context.short_leverage / float(len(Mshorts_list))
#If overnight returns are negative go long based on my
#long list, otherwise I don't invest.
for Mlong in Mlongs_list:
if Mlong < 0:
order_target_percent(pipe.Mlongs_list, long_weight)
else:
order_target_percent(pipe.Mlongs_list, 0)
#If overnight returns are positive go short based on my
#short list, otherwise I don't invest.
for Mshort in Mshorts_list:
if Mshort > 0:
order_target_percent(pipe.Mshorts_list, short_weight)
else:
order_target_percent(pipe.Mshorts_list, 0)
#This function rebalances my portfolio by closing all positions
#30 minutes after the market opens in the next morning
def rebalance3(context, data):
#I close every position 30 minutes after the market opens
for stock in base_universe:
order_target(stock, 0)
I am kind of new to the use of pipeline, but here is where I got stuck:
My issue comes in line 34: I don't understand why, but it looks like the history function doesn't seem to work properly in pipeline.
Am I just mistaking somewhere or is there an alternative method that I am supposed to use to get the prices in pipeline for a specific minute of the day? How can I solve this problem?
Thanks in advance for the help,
Mattia