Good evening,
I have been trying to implement my first simple pipeline algorithm: I want to trade the first 30 minutes of trading based on the overnight returns.
Unfortunately I haven't been able to run my backtest because I keep receiving the error:
UnsupportedOrderParameters: Passing non-Asset argument to 'order()' is not supported. Use 'sid()' or 'symbol()' methods to look up an Asset.
There was a runtime error on line 76.
This is my code that is giving me troubles:
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
class get_open_price(CustomFactor):
inputs = [USEquityPricing.open]
window_length = 1
def compute(self, today, assets, out, open):
out[:] = open[0]
class get_close_price(CustomFactor):
inputs = [USEquityPricing.close]
window_length = 1
def compute(self, today, assets, out, close):
out[:] = close[0]
class get_overnight_returns(CustomFactor):
inputs = [USEquityPricing.close, USEquityPricing.open]
window_length = 1
def compute(self, today, assets, out, close, open):
out[:] = (open[0]-close[0])/close[0]
def initialize(context):
#schedule function to change position
#when the market opens the next morning
schedule_function(rebalance, 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(rebalance2, 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
# Base universe set to the Q500US.
base_universe=Q500US()
#Create pipeline and attach it to algorithm.
pipe = Pipeline()
attach_pipeline(pipe, 'my_pipeline')
today_open = get_open_price()
pipe.add(today_open, 'today_open')
yesterday_close = get_close_price()
pipe.add(yesterday_close, 'yesterday_close')
returns = get_overnight_returns()
pipe.add(returns,'overnight_returns')
returns_rank = returns.rank(mask=base_universe)
pipe.add(returns_rank,'returns_rank')
pipe.set_screen(base_universe)
def before_trading_start(context, data):
# Call pipelive_output to get the output
context.output = pipeline_output('my_pipeline')
context.Mlongs_list = context.output['returns_rank'].iloc[-25:]
context.Mshorts_list = context.output['returns_rank'].iloc[:25]
context.universe = context.Mlongs_list.index.union(context.Mshorts_list.index)
#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 rebalance(context, data):
#compute weights
long_weight = context.long_leverage / float(len(context.Mlongs_list))
short_weight = context.short_leverage / float(len(context.Mshorts_list))
#If overnight returns are negative go long based on my
#long list, otherwise I don't invest.
for Mlong in context.Mlongs_list.index:
if data.can_trade(Mlong):
order_target_percent(context.Mlongs_list, long_weight)
#If overnight returns are positive go short based on my
#short list, otherwise I don't invest.
for Mshort in context.Mshorts_list.index:
if data.can_trade(Mshort):
order_target_percent(context.Mshorts_list, short_weight)
#This function rebalances my portfolio by closing all positions
#30 minutes after the market opens in the next morning
def rebalance2(context, data):
#I close every position 30 minutes after the market opens
for stock in context.universe:
order_target(stock, 0)
Can anybody please give me a hand in fixing this issue?
Thanks a lot in advance,
Mattia