Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
A couple questions about the basics- a buy and sell, same day strategy

from pytz import timezone

**# For this strategy I am looking to buy and sell opposing ETF's shortly after the days open and assign them both trailing stop loss orders (percent).  If the a trailing stop is not triggered, then it will sell 45min before the close.  

#### I am not sure how to schedule the functions of buying and selling.  
#### I am not sure how to make a trailing stop order, can you use IF/THEN statements like: If price <= PreviousPrice * 0.99 sell shares, or is there a function I can call?  
#### I am dealing with lots of Undefined name 'context' errors and also an error in line 37 about runtime exception.  

#Can anyone steer me in the right direction on what tools/imports I need to use to get this code working?  Thank you very much**  

def initialize(context):
schedule_function(
func=myfuncOpen,
date_rule=date_rules.every_day(),
time_rule=time_rules.market_close(minutes=1),
half_days=True
) #This is the function to open the positions

schedule_function(  
func=myfuncClose,  
date_rule=date_rules.every_day(),  
time_rule=time_rules.market_close(minutes=1),  
half_days=True  

) #This is the function to close any positions remaining open

#myfuncOpen is to buy after the open  
schedule_function(  
myfuncOpen,  
date_rules.every_day(),  
time_rules.market_open(minutes=15)  

) # Algorithm will call myfuncOpen every day 15 minutes after the market opens

#myfuncClose is to sell before the close  
schedule_function(  
myfuncClose,  
date_rules.every_day(),  
time_rules.market_close(minutes=45)  

) # Algorithm will call myfuncClose every day, 45 minutes before the market closes

set_symbol_lookup_date('2013-10-03')  

context.stocks = sid(42471),sid(42472) #UWTI and DWTI
context.stocks = sid(45570),sid(45571) #JNUG and JDST
context.stocks = sid(40553),sid(40554) #NUGT and DUST
context.stocks = sid(42477),sid(42470) #UGAZ and DGAZ
context.stocks = sid(37515),sid(37133) #TNA and TZA

context.stop_pct = .99
context.stop = 1

context.ugaz = sid(42477)
context.dgaz = sid(42470)

def myfuncOpen(context,data):
pass

def myfuncClose(context,data):
pass

# This initialize function sets any data or variables that you'll use in your algorithm.  For instance, you'll want to define the security (or securities) you want to backtest.  You'll also want to define any parameters or values you're going to use. In our example, we're looking at Apple.  If you re-type this line yourself, you'll see the auto-complete that is available for the security ID.  
# In these two lines, we set the maximum and minimum we want our algorithm  

context.max_notional = 10000.1
context.min_notional = -10000.0

def handle_data(context, data):

priceUWTI = data[context.UWTI].price  
priceDWTI = data[context.DWTI].price  

#UWTI and DWTI  
order(sid(42471), 50) #Buy UWTI at market  
context.stop = data[context.stock].open_price * context.stop_pct #Sell UWTI stop  

order(sid(42472), 50) #Buy DWTI at market  
order(sid(42472), 50, style=StopOrder(price)) #Sell DWTI stop  
#to add later JNUG and JDST  
#NUGT and DUST  
#UGAZ and DGAZ  
#TNA and TZA  

# We need a variable for the current price of the security to compare to the average.  
priceU = data[context.ugaz].price  
priceD = data[context.dgaz].price  

# Another powerful built-in feature of the Quantopian backtester is the  
# portfolio object.  The portfolio ojbect tracks your positions, cash,  
# cost basis of specific holdings, and more.  In this line, we calculate  
# how long or short our position is at this minute.  
notional = context.portfolio.positions[context.aapl].amount * price  

# This is the meat of the algorithm, placed in this if statement.  If the  
# price of the security is .5% less than the 3-day volume weighted average  
# price AND we haven't reached our maximum short, then we call the order  
# command and sell 100 shares.  Similarly, if the stock is .5% higher than  
# the 3-day average AND we haven't reached our maximum long, then we call  
# the order command and buy 100 shares.  
if price < vwap * 0.995 and notional > context.min_notional:  
    order(context.aapl,-100)  
elif price > vwap * 1.005 and notional < context.max_notional:  
    order(context.aapl,+100)