Skip to content

alpaca module

Module with AlpacaData.


AlpacaData class

AlpacaData(
    wrapper,
    data,
    single_key=True,
    classes=None,
    level_name=None,
    fetch_kwargs=None,
    returned_kwargs=None,
    last_index=None,
    delisted=None,
    tz_localize=None,
    tz_convert=None,
    missing_index=None,
    missing_columns=None,
    **kwargs
)

Data class for fetching from Alpaca.

See https://github.com/alpacahq/alpaca-py for API.

See AlpacaData.fetch_symbol() for arguments.

Usage

  • Set up the API key globally (optional for crypto):
>>> from vectorbtpro import *

>>> vbt.AlpacaData.set_custom_settings(
...     client_config=dict(
...         api_key="YOUR_KEY",
...         secret_key="YOUR_SECRET"
...     )
... )
  • Pull stock data:
>>> data = vbt.AlpacaData.pull(
...     "AAPL",
...     start="2021-01-01",
...     end="2022-01-01",
...     timeframe="1 day"
... )
  • Pull crypto data:
>>> data = vbt.AlpacaData.pull(
...     "BTCUSD",
...     client_type="crypto",
...     start="2021-01-01",
...     end="2022-01-01",
...     timeframe="1 day"
... )

Superclasses

Inherited members


fetch_symbol class method

AlpacaData.fetch_symbol(
    symbol,
    client=None,
    client_type=None,
    client_config=None,
    start=None,
    end=None,
    timeframe=None,
    tz=None,
    adjustment=None,
    feed=None,
    limit=None
)

Override Data.fetch_symbol() to fetch a symbol from Alpaca.

Args

symbol : str
Symbol.
client : alpaca.common.rest.RESTClient

Client.

See AlpacaData.resolve_client().

client_type : str

Client type.

See AlpacaData.resolve_client().

client_config : dict

Client config.

See AlpacaData.resolve_client().

start : any

Start datetime.

See to_tzaware_datetime().

end : any

End datetime.

See to_tzaware_datetime().

timeframe : str

Timeframe.

Allows human-readable strings such as "15 minutes".

tz : any

Timezone.

See to_timezone().

adjustment : str

Specifies the corporate action adjustment for the returned bars.

Options are: "raw", "split", "dividend" or "all". Default is "raw".

feed : str

The feed to pull market data from.

This is either "iex", "otc", or "sip". Feeds "sip" and "otc" are only available to those with a subscription. Default is "iex" for free plans and "sip" for paid.

limit : int
The maximum number of returned items.

For defaults, see custom.alpaca in data. Global settings can be provided per exchange id using the exchanges dictionary.


list_symbols class method

AlpacaData.list_symbols(
    pattern=None,
    use_regex=False,
    sort=True,
    status=None,
    asset_class=None,
    exchange=None,
    trading_client=None,
    client_config=None
)

List all symbols.

Uses CustomData.key_match() to check each symbol against pattern.

Arguments status, asset_class, and exchange can be strings, such as asset_class="crypto". For possible values, take a look into alpaca.trading.enums.

Note

If you get an authorization error, make sure that you either enable or disable the paper flag in client_config depending upon the account whose credentials you used. By default, the credentials are assumed to be of a live trading account (paper=False).


resolve_client class method

AlpacaData.resolve_client(
    client=None,
    client_type=None,
    **client_config
)

Resolve the client.

If provided, must be of the type alpaca.data.historical.CryptoHistoricalDataClient for client_type="crypto" and alpaca.data.historical.StockHistoricalDataClient for client_type="stocks". Otherwise, will be created using client_config.