DEV Community

Danglewood
Danglewood

Posted on

Building an Historical Prices Object With the OpenBB SDK

The objective is to build an object for historical price data, containing the attributes of multiple resolutions, wrapped as a one-liner function. The purpose of the functionality is to reduce the amount of time required to toggle between various time-intervals, for downstream calculations or charting.

For those interested in both, open source and finance, OpenBB is a great rabbit hole to dive down; and it's free. Clone the repo from GitHub here.

The first code block is for the import statements.

# Import the SDK

from typing import Optional
from openbb_terminal.sdk import openbb
from IPython.utils import io
Enter fullscreen mode Exit fullscreen mode

The next step is to define the object that will be returned when called by the function.

# Define the object to be returned.


class HistoricalPrices:
    def __init__(
        self,
        symbol: str = "",
        start_date: Optional[str] = "1990-01-01",
        end_date: Optional[str] = None,
        prepost: Optional[bool] = False,
        source: str = "YahooFinance",
    ) -> None:
        self.one_min = openbb.stocks.load(
            symbol=symbol,
            start_date=start_date,
            end_date=end_date,
            interval=1,
            prepost=prepost,
            source=source,
        )

        self.five_min = openbb.stocks.load(
            symbol=symbol,
            start_date=start_date,
            end_date=end_date,
            interval=5,
            prepost=prepost,
            source=source,
        )

        self.fifteen_min = openbb.stocks.load(
            symbol=symbol,
            start_date=start_date,
            end_date=end_date,
            interval=15,
            prepost=prepost,
            source=source,
        )

        self.thirty_min = openbb.stocks.load(
            symbol=symbol,
            start_date=start_date,
            end_date=end_date,
            interval=30,
            prepost=prepost,
            source=source,
        )

        self.sixty_min = openbb.stocks.load(
            symbol=symbol,
            start_date=start_date,
            end_date=end_date,
            interval=60,
            prepost=prepost,
            source=source,
        )

        self.daily = openbb.stocks.load(
            symbol=symbol,
            start_date=start_date,
            end_date=end_date,
            interval=1440,
            prepost=prepost,
            source=source,
        )

        self.weekly = openbb.stocks.load(
            symbol=symbol,
            start_date=start_date,
            end_date=end_date,
            interval=1440,
            prepost=prepost,
            weekly=True,
            source=source,
        )

        self.monthly = openbb.stocks.load(
            symbol=symbol,
            start_date=start_date,
            end_date=end_date,
            interval=1440,
            prepost=prepost,
            monthly=True,
            source=source,
        )
Enter fullscreen mode Exit fullscreen mode

Then, make the function to call the object.

# Create the function to return the data as an object.


def historical_prices(
    symbol: str = "SPY",
    start_date: Optional[str] = "1990-01-01",
    end_date: Optional[str] = None,
    prepost: Optional[bool] = True,
    source: str = "YahooFinance",
) -> HistoricalPrices:
    # capture_output() is used to silence the console output.
    with io.capture_output():
        historical = HistoricalPrices(symbol, start_date, end_date, prepost, source)

    return historical
Enter fullscreen mode Exit fullscreen mode

Finally, assign the function to a variable.

#Use the variables, [symbol, start_date, end_date, prepost, source], to modify the call.

historical = historical_prices()
Enter fullscreen mode Exit fullscreen mode

The attributes returned are:

historical.one_min
historical.five_min
historical.fifteen_min
historical.thirty_min
historical.sixty_min
historical.daily
historical.weekly
historical.monthly
Enter fullscreen mode Exit fullscreen mode
historical.one_min.tail(5)
Enter fullscreen mode Exit fullscreen mode
date Open High Low Close Adj Close Volume
2023-03-07 19:55:00 398.15 398.19 398.14 398.18 398.18 0
2023-03-07 19:56:00 398.15 398.19 398.15 398.175 398.175 0
2023-03-07 19:57:00 398.17 398.28 398.17 398.25 398.25 0
2023-03-07 19:58:00 398.25 398.27 398.23 398.24 398.24 0
2023-03-07 19:59:00 398.26 398.27 398.18 398.25 398.25 0
historical.monthly.head(5)
Enter fullscreen mode Exit fullscreen mode
date Open High Low Close Adj Close Volume Dividends Stock Splits
1993-02-01 00:00:00 25.2362 25.8998 24.5725 25.4873 25.4873 5.4176e+06 0 0
1993-03-01 00:00:00 25.577 26.3123 25.3797 25.9357 25.9357 3.0192e+06 0.213 0
1993-04-01 00:00:00 26.0942 26.0942 24.9589 25.3914 25.3914 2.6972e+06 0 0
1993-05-01 00:00:00 25.4274 26.3285 25.2833 26.0762 26.0762 1.808e+06 0 0
1993-06-01 00:00:00 26.1663 26.4186 25.4995 25.9861 25.9861 3.438e+06 0.318 0

Download a notebook with this code, here

Top comments (0)