Bars

This object contains all pricing data over time, including open, close, high, low, etc prices. You can get the raw pandas DataFrame by using bars.df. The dataframe has the following columns:

  • open

  • high

  • low

  • close

  • volume

  • dividend

  • stock_splits

The dataframe index is of type pd. Timestamp localized at the timezone America/New_York.

Bars objects have the following fields:

  • source: the source of the data e.g. (yahoo, alpaca, …)

  • symbol: the symbol of the bars

  • df: the pandas dataframe containing all the datas

Bars objects has the following helper methods:

  • get_last_price(): Returns the closing price of the last dataframe row

  • get_last_dividend(): Returns the dividend per share value of the last dataframe row

  • get_momentum(start=None, end=None): Calculates the global price momentum of the dataframe.

  • aggregate_bars(frequency): Will convert a set of bars to a different timeframe (eg. 1 min to 15 min) frequency (string): The new timeframe that the bars should be in, eg. “15Min”, “1H”, or “1D”. Returns a new Bars object.

When specified, start and end will be used to filter the daterange for the momentum calculation. If none of start or end are specified the momentum will be calculated from the first row untill the last row of the dataframe.

  • get_total_volume(start = None, end = None): returns the sum of the volume column. When start and/or end is/are specified use them to filter for that given daterange before returning the total volume

  • filter(start = None, end = None): Filter the bars dataframe. When start and/or end is/are specified use them to filter for that given daterange before returning the total volume

When getting historical data from Interactive Brokers, it is important to note that they do not consider themselves a data supplier. If you exceed these data access pacing rates, your data will be throttled. Additionally, with respect to above three mentioned helpers, when using Interactive Brokers live, tick data is called instead of bar data. This allows for more frequent and accurate pricing updates. get_last_dividend are not available in Interactive Brokers. (see [Interactive Brokers’ pacing rules](https://interactivebrokers.github. io/tws-api/historical_limitations.html))

Documentation

class entities.bars.Bars(df, source, asset, quote=None, raw=None)

Bases: object

Pricing and financial data for given Symbol.

The OHLCV, and if available, dividends, stock splits for a given financial instrument. Price change, dividend yield and return are calculated if appropriate.

Parameters:
  • df (Pandas Dataframe) –

    Dataframe with:

    datetime.datetime index time zone aware. columns = [‘open’, ‘high’, ‘low’, ‘close’, ‘volume’] optional columns [‘dividend’, ‘stock_splits’]

  • source – The source of the data e.g. (yahoo, alpaca, …)

  • asset (Asset) –

    The asset for which the bars are holding data.

    source: the source of the data e.g. (yahoo, alpaca, …)

  • symbol (str) – The ticker symbol. eg: “AAPL”. If cryptocurrency, the symbol will be the pair of coins. eg: “ETH/BTC”

  • quote (Asset) – For cryptocurrency only. This is the other asset for trading getting ohlcv quotes.

get_last_price()

Returns the closing price of the last dataframe row

get_last_dividend()

Returns the dividend per share value of the last dataframe row

get_momentum(start=None, end=None)

Calculates the global price momentum of the dataframe.

aggregate_bars(frequency)

Will convert a set of bars to a different timeframe (eg. 1 min to 15 min) frequency (string): The new timeframe that the bars should be in, eg. “15Min”, “1H”, or “1D”. Returns a new Bars object.

get_total_volume(start=None, end=None)

Returns the total volume of the dataframe.

get_total_dividends(start=None, end=None)

Returns the total dividend amount of the dataframe.

get_total_stock_splits(start=None, end=None)

Returns the total stock split amount of the dataframe.

get_total_return(start=None, end=None)

Returns the total return of the dataframe.

get_total_return_pct(start=None, end=None)

Returns the total return percentage of the dataframe.

get_total_return_pct_change(start=None, end=None)

Returns the total return percentage change of the dataframe.

Examples

>>> # Get the most recent bars for AAPL
>>> bars = bars.get_bars("AAPL")
>>> # Get the most recent bars for AAPL between 2018-01-01 and 2018-01-02
>>> bars = bars.get_bars("AAPL", start=datetime(2018, 1, 1), end=datetime(2018, 1, 10))
>>> df = bars.df
>>> self.log_message(df["close"][-1])
>>> # Get the most recent bars for ES futures contract
>>> asset = Asset(symbol="ES", asset_type="future", multiplier=100)
>>> bars = bars.get_bars(asset)
>>> df = bars.df
>>> self.log_message(df["close"][-1])
>>> # Get the most recent bars for Ethereum into Bitcoin.
>>> asset = Asset(symbol="ETH", asset_type="crypto")
>>> quote = Asset(symbol="BTC", asset_type="crypto")
>>> bars = bars.get_bars(asset)
>>> df = bars.df
>>> self.log_message(df["close"][-1])
aggregate_bars(frequency, **grouper_kwargs)

Will convert a set of bars to a different timeframe (eg. 1 min to 15 min) frequency (string): The new timeframe that the bars should be in, eg. “15Min”, “1H”, or “1D” Returns a new bars object.

Parameters:

frequency (str) – The new timeframe that the bars should be in, eg. “15Min”, “1H”, or “1D”

Return type:

Bars object

Examples

>>> # Get the 15 minute bars for the last hour
>>> bars = self.get_historical_prices("AAPL", 60, "minute")
>>> bars_agg = bars.aggregate_bars("15Min")
filter(start=None, end=None)

Return a Bars object with only the bars between start and end

Parameters:
  • start (datetime.datetime) – The start of the range to filter on

  • end (datetime.datetime) – The end of the range to filter on

Return type:

Bars object

get_last_dividend()

Return the last dividend of the last bar

Parameters:

None

Return type:

float

get_last_price()

Return the last price of the last bar

Parameters:

None

Return type:

float

get_momentum(num_periods: int = 1)

Calculate the momentum of the asset over the last num_periods rows. If dividends are provided by the data source, and included in the return calculation, the momentum will be adjusted for dividends.

get_total_volume(start=None, end=None)

Return the total volume of the bars between start and end

Parameters:
  • start (datetime.datetime) – The start of the range to filter on (inclusive) (default: None)

  • end (datetime.datetime) – The end of the range to filter on (inclusive) (default: None)

Return type:

float

classmethod parse_bar_list(bar_list, source, asset)
split()

Return a list of Bars objects, each with a single bar

Parameters:

None

Return type:

list of Bars objects

exception entities.bars.NoBarDataFound(source, asset)

Bases: Exception