Zipline
Quick Reference
User-Implemented Functions
The following table describes functions that are treated specially when defined in the IDE.
Algorithms are required to implement one method: Initialize(self).
An optional scheduled event can be defined to behave like before_trading_start(). OnData() is also optional but is treated especially if defined.
Quantopian | QuantConnect |
---|---|
initialize (context) | Initialize (self) |
Required function called once at the start of a backtest. | |
before_trading_start (context, data) | self.Schedule.On(self.DateRules.EveryDay(), self.TimeRules.AfterMarketOpen(symbol, 10), self.before_trading_start) |
Optional function called prior to market open on every trading day. | |
handle_data (context, data) | OnData (self, slice) |
Optional function called at the end of each market minute. |
Interface Classes
The following table describes classes that are passed to initialize()
, handle_data()
, before_trading_start()
and any scheduled with schedule_function()
.
In QuantConnect/Lean, the portfolio state and the positions are class attributes of QCAlgorithm, available via self keyword. The scheduled function is parameterless and can algorithms can self.CurrentSlice.
Quantopian | QuantConnect |
---|---|
quantopian.algorithm.interface.AlgorithmContext | QCAlgorithm |
Shared object for storing custom state. | |
quantopian.algorithm.interface.BarData | Slice |
Provides methods for accessing minutely and daily price/volume data from Algorithm API functions. | |
quantopian.algorithm.interface.Portfolio | self.Portfolio |
Object providing read-only access to current portfolio state. | |
quantopian.algorithm.interface.Positions | self.Transactions |
A dict-like object containing the algorithm's current positions. |
The features from the BarData methods are found in the Slice object (current data), History method (past data) and class attribute Securities.
Quantopian | QuantConnect |
---|---|
quantopian.algorithm.interface.BarData.current | Slice[symbol] |
Returns the "current" value of the given fields for the given assets at the current simulation time. | |
quantopian.algorithm.interface.BarData.history | self.History(symbols, bar_count) |
Returns a trailing window of length bar_count containing data for the given assets, fields, and frequency. | |
quantopian.algorithm.interface.BarData.can_trade | self.Securities[symbol].IsTradable |
For the given asset or iterable of assets, returns True if all of the following are true: | |
quantopian.algorithm.interface.BarData.is_stale | not Slice[symbol].IsFillForward |
For the given asset or iterable of assets, returns True if the asset is alive and there is no trade data for the current simulation time. |
Pipeline
The following table describes functions used to schedule and retrieve results of Universe selection. For more information on the Universe Selection API, see the Univeres API Reference.
Quantopian | QuantConnect |
---|---|
quantopian.algorithm.attach_pipeline (...) | universe = self.AddUniverse (...)name = universe.Configuration.Symbol |
Register a pipeline to be computed at the start of each day. | |
quantopian.algorithm.pipeline_output (name) | self.UniverseManager [name] |
Get results of the pipeline attached by with name . |
Scheduling Functions
The following tables describes functions that can be used to schedule functions to run periodically during your algorithm.
Quantopian | QuantConnect |
---|---|
quantopian.algorithm.schedule_function (func) | self.Schedule.On(IDateRule, ITimeRule, func) |
Schedule a function to be called repeatedly in the future. |
Date Rules
The following table contains functions that can be used with the date_rule
parameter of schedule_function()
.
Quantopian | QuantConnect |
---|---|
quantopian.algorithm.date_rules.every_day () | self.DateRules.EveryDay(symbol) |
Create a rule that triggers every day. | |
quantopian.algorithm.date_rules.month_start ([...]) | self.DateRules.MonthStart(symbol, daysOffset) |
Create a rule that triggers a fixed number of trading days after the start of each month. | |
quantopian.algorithm.date_rules.month_end ([...]) | self.DateRules.MonthEnd(symbol, daysOffset) |
Create a rule that triggers a fixed number of trading days before the end of each month. | |
quantopian.algorithm.date_rules.week_start ([...]) | self.DateRules.WeekStart(symbol, daysOffset) |
Create a rule that triggers a fixed number of trading days after the start of each week. | |
quantopian.algorithm.date_rules.week_end ([...]) | self.DateRules.WeekEnd(symbol, daysOffset) |
Create a rule that triggers a fixed number of trading days before the end of each week. |
Time Rules
The following table contains functions that can be used with the time_rule
parameter of schedule_function()
.