Quantopian 1
Quantopian 2
The data Object

data was a dictionary keyed by securities in your universe for the given bar. This was used to obtain pricing and volume data for the specific securities in a given bar. Iterating over data was equivalent to iterating over all securities in your trading universe.

The data Object

data is an object with functions that allow us to look up data for any/multiple securities. The functions available in data are:

data.current(security_list, field)
data.history(security_list, field, bar_count, frequency)
data.can_trade(security)
data.is_stale(security)
data.fetcher_assets

data is no longer iterable. For more information, read below or see the documentation.

Last Known Value Lookup

Get the last known value for the specified field for asset with data[asset].field.

current_price = data[asset].price
Last Known Value Lookup

Gets the current value for the specified field(s) for asset with data.current(asset, field).

current_price = data.current(asset, 'price')

Note: asset can be a single Asset object or a list of Asset objects. field can be a string or a list of strings. The input types determine the output type of data.current, which will either be a pandas Series or DataFrame. See the documentation for more information.

History

history(bar_count, frequency, field)

Gets a pandas DataFrame storing a trailing window of data for the specified field for each security in your trading universe.

hist = history(10, '1d', 'volume')
History

data.history(assets, field, bar_count, frequency)

Gets a trailing window of data for the specified field(s) for each security in assets, which is assumed to be a list of securities that you maintain.

hist = data.history(assets, 'volume', 10, '1d')

Note: assets can be a single Equity object or a list of Equity objects. field can be a string or a list of strings. The input types determine the output type of data.history, which will either be a pandas Series, DataFrame, or Panel. See the documentation for more information.

Checking If a Security Is In data

Previously, it was possible to check if a security was tradeable by checking if it existed as a key in data. This was possible because the keys of data were made up of securities from your trading universe, where securities without trade volume were removed.

if asset in data:
    order(asset)
Checking If a Security Can Be Traded

data.can_trade(asset)

Returns True if the asset has a known last price and is currently listed on a supported exchange.

if data.can_trade(asset):
    order(asset)

Note: data.can_trade returning true does not guarantee that an order placed during that bar will be filled. The order is filled depending on the size of the order, the trade volume in the subsequent bar, and the specified slippage model.

Set/Update Universe

update_universe(), set_universe()

update_universe([sid(24), sid(8554), sid(5061)])
...
history(5, '1d', 'low')
No More Universe, Explicitly Reference Securities

There is no more trading universe. Instead, securities should be stored in a variable on context. For example, they can be stored in a list:

context.assets = [sid(24), sid(8554), sid(5061)]
data.history(context.assets, 'low', 5, '1d')

Daily Mode Backtest

def initialize(context):
    context.asset = sid(24)
 
def handle_data(context, data):
    order_target_percent(context.asset, 0.5)

Scheduled Functions

Run a minute-mode backtest using schedule_function() to run a function once per day. handle_data() no longer needs to be implemented.

def initialize(context):
    context.asset = sid(24)
    # rebalance once per day, at market open
    schedule_function(rebalance, date_rule=date_rules.every_day())
 
def rebalance(context, data):
    order_target_percent(context.asset, 0.5)

Note: Orders are now canceled at end of day in backtesting (as well as live trading) so placing orders at market close will not fill. Large orders and orders of illiquid securities will take many minutes to fill; these orders should be placed well before market close.

Accessing Fetcher Data
def initialize(context):
    fetch_csv('https://some.path.com/fetcher_file.csv',
              date_column='Settlement Date',
              date_format='%m/%d/%y')
 
    context.security = symbol('NFLX')
 
def handle_data(context, data):
    if 'Days To Cover' in data[context.security]:
        record(
            Short_Interest=data[context.security]['Days To Cover']
        )
Accessing Fetcher Data
def initialize(context):
    fetch_csv('https://some.path.com/fetcher_file.csv',
              date_column='Settlement Date',
              date_format='%m/%d/%y')
 
    context.security = symbol('NFLX')
 
def handle_data(context, data):
    if context.security in data.fetcher_assets:
        record(Short_Interest=data.current(context.security,
                                           'Days To Cover'))
Moving Average
mavg = data[asset].mavg(10)
Moving Average
price_hist = data.history(asset, 'price', 10, '1d')
mavg = price_hist.mean()

Note: When getting the moving average for multiple assets, it's most efficient to use the above example, replacing asset with a list of assets.
VWAP
data[asset].vwap(10)
VWAP
vwap_hist = data.history(asset, ['price', 'volume'], 10, '1d')[:-1]
vwap = (vwap_hist['price'] * vwap_hist['volume']).sum() / vwap_hist['volume'].sum()

Note: When getting the VWAP for multiple assets, it's most efficient to use the above example, replacing asset with a list of assets.
Standard Deviation
data[asset].stddev(10)
Standard Deviation
stddev_hist = data.history(asset, 'price', 10, '1d')[:-1]
stddev = stddev_hist.std()

Note: When getting the standard deviation for multiple assets, it's most efficient to use the above example, replacing asset with a list of assets.
Returns
data[asset].returns(10)
Returns
returns_hist = data.history(asset, 'price', 10, '1d')[:-1]
returns = (returns_hist.iloc[-1] - returns_hist.iloc[0]) / returns_hist.iloc[0]

Note: When getting the returns for multiple assets, it's most efficient to use the above example, replacing asset with a list of assets.
Default Slippage
slippage.VolumeShareSlippage(volume_limit=0.25, price_impact=0.1)
Default Slippage
slippage.VolumeShareSlippage(volume_limit=0.025, price_impact=0.1)
Default Commissions
commission.PerShare(cost=0.03, min_trade_cost=1)
Default Commissions
commission.PerShare(cost=0.0075, min_trade_cost=1)

Get Started on Quantopian