Back to Community
code for getting minute data into before_trading_start

Here's code for getting a trailing window of minute data into before_trading_start, including the closing prices from the prior day. Seems sorta circuitous, but this is the only way I could code it. Maybe pipeline could be used to pull in the closing price from the prior day, in a more elegant fashion (e.g. a numpy vector)?

Clone Algorithm
34
Loading...
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
import numpy as np

def initialize(context):
    
    context.stocks = [
        sid(19662),  # XLY Consumer Discrectionary SPDR Fund   
        sid(19656),  # XLF Financial SPDR Fund  
        sid(19658),  # XLK Technology SPDR Fund  
        sid(19655),  # XLE Energy SPDR Fund  
        sid(19661),  # XLV Health Care SPRD Fund  
        sid(19657),  # XLI Industrial SPDR Fund  
        sid(19659),  # XLP Consumer Staples SPDR Fund   
        sid(19654),  # XLB Materials SPDR Fund  
        sid(19660),  # XLU Utilities SPDR Fund
        ]
    
    context.prices = None
    context.first_day = True
    
    schedule_function(copy_history,date_rule=date_rules.every_day(),time_rule=time_rules.market_close())

def before_trading_start(context, data):
    
    if context.first_day:
        return
    
    close_prices = []
    for stock in context.stocks:
        close_prices.append(data[stock].price)
        
    prices = np.vstack([context.prices,close_prices])
    
    print prices

def handle_data(context, data):
    pass
                      
def copy_history(context, data):
    
    context.first_day = False
    
    context.prices = history(5,'1m','price').as_matrix(context.stocks)
    
    
There was a runtime error.
4 responses

Here's an incremental improvement. Arrays are pre-allocated, and by inspection of the output, one can confirm that it is working.

Clone Algorithm
34
Loading...
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
import numpy as np

def initialize(context):
    
    context.stocks = [
        sid(19662),  # XLY Consumer Discrectionary SPDR Fund   
        sid(19656),  # XLF Financial SPDR Fund  
        sid(19658),  # XLK Technology SPDR Fund  
        sid(19655),  # XLE Energy SPDR Fund  
        sid(19661),  # XLV Health Care SPRD Fund  
        sid(19657),  # XLI Industrial SPDR Fund  
        sid(19659),  # XLP Consumer Staples SPDR Fund   
        sid(19654),  # XLB Materials SPDR Fund  
        sid(19660),  # XLU Utilities SPDR Fund
        ]
    
    context.prices = None
    context.first_day = True
    
    schedule_function(show_history,date_rule=date_rules.every_day(),time_rule=time_rules.market_open())
    schedule_function(copy_history,date_rule=date_rules.every_day(),time_rule=time_rules.market_close())

def before_trading_start(context, data):
    
    if context.first_day:
        return
    
    prices = np.roll(context.prices, -1, axis=0)
    
    for k,stock in enumerate(context.stocks):
        prices[-1,k] = data[stock].price
    
    print 'before_trading_start'
    print prices

def handle_data(context, data):
    pass
                      
def copy_history(context, data):
    
    context.first_day = False
    
    context.prices = history(5,'1m','price').as_matrix(context.stocks)
    
def show_history(context, data):
    
    print 'show_history'
    print history(6,'1m','price').as_matrix(context.stocks)[0:-1,:]
There was a runtime error.

Here's another incremental improvement (thanks to Alan Coppola on https://www.quantopian.com/posts/get-data-as-vector-instead-of-scalar).

Clone Algorithm
34
Loading...
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
import numpy as np

def initialize(context):
    
    context.stocks = [
        sid(19662),  # XLY Consumer Discrectionary SPDR Fund   
        sid(19656),  # XLF Financial SPDR Fund  
        sid(19658),  # XLK Technology SPDR Fund  
        sid(19655),  # XLE Energy SPDR Fund  
        sid(19661),  # XLV Health Care SPRD Fund  
        sid(19657),  # XLI Industrial SPDR Fund  
        sid(19659),  # XLP Consumer Staples SPDR Fund   
        sid(19654),  # XLB Materials SPDR Fund  
        sid(19660),  # XLU Utilities SPDR Fund
        ]
    
    context.prices = None
    context.first_day = True
    
    schedule_function(show_history,date_rule=date_rules.every_day(),time_rule=time_rules.market_open())
    schedule_function(copy_history,date_rule=date_rules.every_day(),time_rule=time_rules.market_close())

def before_trading_start(context, data):
    
    if context.first_day:
        return
    
    prices = np.roll(context.prices, -1, axis=0)
    prices[-1,:] = [data[stock].price for stock in context.stocks]
    
    print 'before_trading_start'
    print prices

def handle_data(context, data):
    pass
                      
def copy_history(context, data):
    
    context.first_day = False
    
    context.prices = history(5,'1m','price').as_matrix(context.stocks)
    
def show_history(context, data):
    
    print 'show_history'
    print history(6,'1m','price').as_matrix(context.stocks)[0:-1,:]
There was a runtime error.

Wow, that is clean code. Thank you for sharing Grant. Following and testing (found from your post here: https://www.quantopian.com/posts/pipeline-classifiers-are-here#5704dbaaf3b71d2097000158).

Thanks Marc,

There is a more general case with a dynamic universe. I haven't sorted that one out yet. The fundamental problem, I think, is that the analyses would be done in before_trading_start on the prior day's universe, then the universe would be updated, doing an add/drop for the current day. But the analyses would have been done on the prior universe, so orders opening new positions for stocks no longer in data would fail. But maybe I haven't thought everything through carefully enough...

Grant