Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
History not working in before_trading_start

Hi, for my algorithm, I'm doing some TA at the beginning of the day to figure which securities I want to focus on for the day. This is the error that I'm getting with the code below:

FunctionCalledOutsideOfTradingStart: 0069 'history' only permitted within before_trading_start function

DAILY_WINDOW = 100

def initialize(context):  
    set_universe(universe.DollarVolumeUniverse(99.0, 100.0))

def before_trading_start(context):  
    high_history = history(DAILY_WINDOW, '1d', 'high')  
    low_history = history(DAILY_WINDOW, '1d', 'low')  
    price_history = history(DAILY_WINDOW, '1d', 'price')

def handle_data(context, data):  
    pass  
9 responses

Here's a work-around. The problem is that the data are not available until before the start of the second day. I wonder if before_trading_starts won't support "warmed up" data via history at the backtest outset?

--Grant

DAILY_WINDOW = 100  
high_history = None  
low_history = None  
price_history = None

def initialize(context):  
    # set_universe(universe.DollarVolumeUniverse(99.0, 100.0))  
    context.spy = sid(8554)

def before_trading_start(context):  
    print high_history

def handle_data(context, data):  
    global high_history, low_history, price_history  
    high_history = history(DAILY_WINDOW, '1d', 'high')  
    low_history = history(DAILY_WINDOW, '1d', 'low')  
    price_history = history(DAILY_WINDOW, '1d', 'price')  

@Grant Kot, history is not currently supported in before_trading_start. We'll work on building that integration!

Disclaimer

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.

Nice to hear that you're working on an integration.
I need this feature, too. It would also help to overcome the 200 securities limit in update_universe().
I'm implementing a algorithm that combines prices with fundamental data and I'd like to screen the universe completly in before-trading-start.

Cool, yeah, I'm working on an algorithm that does some long term daily analysis, and then trades at the minute resolution during the day.

Though I guess this method could be a bit ambiguous. In live trading when does it start? Does it execute after market close or a few minutes before market open? This could affect strategies that rely on news. Or can it be guaranteed to complete execution before market open?

I have also been experimenting on neural networks a little. I could potentially make a strategy where the neural nets are trained overnight and executed during the day without any training overhead.

Here's another work-around that doesn't use globals. So, aside from the first day of trading, before_trading_start can be coaxed into supporting the history API. --Grant

DAILY_WINDOW = 100

def initialize(context):  
    # set_universe(universe.DollarVolumeUniverse(99.0, 100.0))  
    context.spy = sid(8554)  
    context.high_history = None  
    context.low_history = None  
    context.price_history = None

def before_trading_start(context):  
    print context.high_history

def handle_data(context, data):  
    context.high_history = history(DAILY_WINDOW, '1d', 'high')  
    context.low_history = history(DAILY_WINDOW, '1d', 'low')  
    context.price_history = history(DAILY_WINDOW, '1d', 'price')  

@Grant, you remind me of coding the web back in the late 90's. It was not all 2.0'd out, but if you were creative you could build just about anything you could imagine using the tools at hand. You keep asking the tough questions, and then answering them -- yourself. A noble pastime.

Any updates on this integration of history() in before_trading_start()? Just ran into this issue as well...

Antoine,

If you are stuck, there's a work-around, which is simply to copy the output of history() to context and then use it in before_trading_start(). Orders can be handled in the same fashion. Just copy them to context, and then you can pull them in at any point during the trading day.


+1 for this one. One hang-up I see is that before_trading_start() supports update_universe(). So, say I run update_universe() and then call history(). Would I get all of the symbols in my universe, including the ones I just added with update_universe()? Or only the ones from the prior run of initialize() and update_universe()?

Hey Grant,

I was calling history() and passing it in using context before, thought I'd clean up the code (the logic would have that bit in before_trading_start), then ran into the issue...so I reverted.

Can't help you with the 'universe' family of functions, sadly. In all logic, you should get the new symbols...but without knowing what goes on under the hood I don't feel safe using it without testing it. If I do and get an answer I'll let you know - until then, it's all custom universe management :)