Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
layering an intraday function over daily functions?

Hi All,

Sorry if this is a niave question...Im juststarting out - anyway the algo I have so far works by having a schedule function that performs at the opening of everydays trading and every subsequent piece of logic so far speaks to that. Now, I want to "overlay" a second intraday function that allows me to have an intraday trailing stop or intraday conditional order while at the same time keeping the existing opening trade function intact. Any ideas on how I do this? I know it sounds simple and it probably is - Is it as simple as having a second schedule function that speaks to the intraday condition, say at the end of the algo? How would I set it up? If anyone could provide me with a few lines of code to point me in the right direction it would be most appreciated! Sorry if Im insulting anyones intelligence with the question :-)

Best Regards, Steph

1 response

Steph -

On the help page, you'll see:

handle_data(context, data)

Called every minute.
Parameters

context: Same context object in initialize, stores any state you've defined, and stores portfolio object.

data: An object that provides methods to get price and volume data, check whether a security exists, and check the last time a security traded.
Returns

None
Example

def handle_data(context, data):  
    # all your algorithm logic here  
    # ...  
    order_target_percent(symbol('AAPL'), 1.0)  
    # ...

It is actually optional to have in your code (and probably shouldn't be used unless you need it, since it'll add overhead that slows down backtests).

Note that handle_data has access to context, which can be used as a kind of global (e.g. context.do_something or context.my_state_variables).

So, if you need to perform a minutely operation (e.g. monitor for a condition on a continuous basis and take action), you could use handle_data.

Or, as you say, you could schedule a second function (e.g. one that runs 30 minutes before the market close), that would close out the day, for example.