Only access data when you need it
In Quantopian 1, you had to define the universe of stocks you were interested in and we would pre-load all the data for you. You paid the performance cost of loading that data every minute, regardless of whether you were going to use it or not. In Quantopian 2, all pricing data is always accessible to you, so there is no pre-loading. Instead, you pay the performance cost of accessing the data when you ask for it. Therefore, you should ask for data only when you need it. This is the best way to speed up your algorithms.
Use schedule function liberally
Schedule all of your functions to run only when necessary, as opposed to every
minute. In Quantopian 1, it was common to have much of your algorithm's logic and
handle_data(). Since the data was being loaded regardless of whether
you used it or not, this practice was fine. In Quantopian 2, you should only use
handle_data() for things you really need to happen every
minute. Everything else should be its own function, scheduled to run when
Batch data requests
Whenever possible, you should batch data requests. All of the data functions
accept a list of securities when requesting data. Running these functions once with a
list of securities will be significantly more performant than looping through the
list of securities and calling these functions individually per security.
Record data daily in backtesting
Any data you record in your backtest will record the last data point per day.
If you try to record something in
record more frequently, it will still only record one daily data point.
handle_data() will not give you more data, and
will significantly slow down your algorithm. We recommend creating a
separate record function, and scheduling it to run once a day (or less
frequently) since greater frequency won't give you more data.
The one caveat here is that
record does work minutely
in live trading. You can update your record function to run more frequently
before live trading your algo, or use
to schedule record functions to behave different in backtesting and live trading.
Access account and portfolio data only when needed
Account and portfolio information is calculated daily or on demand.
If you access
this will force the system to calculate your entire portfolio minutely, and slow
down your algo. You should only call context.account or context.portfolio when
you need to use the data. We recommend doing this in a schedule function.
The one caveat here is that your account and portfolio information is calculated
minutely in live trading. You can update your algorithm to access this information
more frequently before live trading if necessary, or use
get_environment() to schedule functions to behave
different in backtesting and live trading.