Financial Series#
Financial Series is an abstract class representing financial time series.
Concrete implementations include Prices, Returns, CumulativeReturns
- class portfolio_plan.financialseries.Prices(data: DataFrame, name: str)#
Bases:
FinancialSeries- Parameters:
data (pd.DataFrame) – The data including financial prices.
name (str) – The name of this set of financial prices.
Note
It is recommended to derive Prices instance from a resource rather than from direct instantiation. A resource, can be a file, an in memory parquet buffer, a network resource. Refer to Resource
- plot(scale_x: bool = True)#
Plot all prices candle chart
- plot_close(scale_x: bool = True) ggplot#
Plot all price close price
- plot_high(scale_x: bool = True) ggplot#
Plot all prices high price
- plot_low(scale_x: bool = True) ggplot#
Plot all prices low price
Example
>>> from portfolio_plan.financialseries import Prices, Frequency >>> from portfolio_plan.resource import File >>> from portfolio_plan.data import example_prices_path
>>> resource = File(path=example_prices_path(), frequency=Frequency.BDAILY) >>> prices = resource.fetch( ... ["A", "B"], ... start_date="2025-01-01", ... end_date="2025-01-31" ) >>> price.plot_low()
- plot_open(scale_x: bool = True) ggplot#
Plot all prices open price
- class portfolio_plan.financialseries.Returns(data: DataFrame, name: str)#
Bases:
FinancialSeries- Parameters:
data (pd.DataFrame) – A dataframe containing the OHLC returns
- avg(weights: Weights, portfolio_name: str = 'Portfolio') Returns#
Compute the average return by date
- cumulative(name: str, starting_value: float | List[float] = 1.0, keepna: bool = True, check_base: bool = True, periods: List[Period] | None = None) CumulativeReturns#
- Parameters:
name (str) – See
portfolio_plan.financialseries.CumulativeReturns()starting_value (int | List[int]) – See
empyrical.cum_returns()If starting_value is a list, it must be of length 4. Each element is mapped (in order) to: - open - close - high - low
Warning
I recommend setting keepna to True. When False, inconsistencies between the frequencies of the scale of the plotted dates and the actual frequency of the data could lead to misinterpretation.
- plot(scale_x: bool = True)#
Plot all returns candle chart
- plot_close(scale_x: bool = True) ggplot#
Plot all returns close price
- plot_high(scale_x: bool = True) ggplot#
Plot all returns high price
- plot_low(scale_x: bool = True) ggplot#
Plot all returns low price
Example
>>> from portfolio_plan.financialseries import Returns, Frequency >>> from portfolio_plan.resource import File >>> from portfolio_plan.data import example_prices_path >>> from portfolio_plan.utils import returns_from_prices
>>> resource = File(path=example_prices_path(), frequency=Frequency.BDAILY) >>> prices = resource.fetch( ... ["A", "B"], ... start_date="2025-01-01", ... end_date="2025-01-31" ... ) >>> returns = Returns(returns_from_prices(prices.data)) >>> returns.plot_low()
>>> returns.cumulative().plot_low()
- plot_open(scale_x: bool = True) ggplot#
Plot all returns open price
from portfolio_plan import Frequency
from portfolio_plan.resource import File
from portfolio_plan.utils import returns_from_prices
from portfolio_plan.data import example_prices_path
from datetime import datetime
START_DATE = datetime.strptime("2025-01-30", "%Y-%m-%d")
END_DATE = datetime.strptime("2025-03-01", "%Y-%m-%d")
prices = File(path=example_prices_path(), frequency=Frequency.BDAILY).fetch(
["A"],
start_date=START_DATE,
end_date=END_DATE,
name="Prices"
)
returns_from_prices(prices.data)
| Returns | open | close | high | low | |
|---|---|---|---|---|---|
| symbol | date | ||||
| A | 2025-01-30 | NaN | NaN | NaN | NaN |
| 2025-01-31 | 0.000000 | 0.000000 | 0.000000 | 0.000000 | |
| 2025-02-03 | 0.000918 | 0.000471 | 0.000239 | 0.000317 | |
| 2025-02-04 | 0.001116 | 0.000573 | 0.000290 | 0.000386 | |
| 2025-02-05 | 0.000000 | 0.000000 | 0.000000 | 0.000000 | |
| 2025-02-06 | -0.000054 | -0.000028 | -0.000014 | -0.000019 | |
| 2025-02-07 | -0.001246 | -0.000640 | -0.000324 | -0.000431 | |
| 2025-02-10 | 0.000000 | 0.000000 | 0.000000 | 0.000000 | |
| 2025-02-11 | 0.002883 | 0.001480 | 0.000750 | 0.000996 | |
| 2025-02-12 | -0.000906 | -0.000466 | -0.000236 | -0.000313 | |
| 2025-02-13 | 0.000000 | 0.000000 | 0.000000 | 0.000000 | |
| 2025-02-14 | 0.002698 | 0.001386 | 0.000703 | 0.000933 | |
| 2025-02-17 | -0.000675 | -0.000347 | -0.000176 | -0.000234 | |
| 2025-02-18 | 0.000000 | 0.000000 | 0.000000 | 0.000000 | |
| 2025-02-19 | 0.001734 | 0.000892 | 0.000452 | 0.000600 | |
| 2025-02-20 | 0.001595 | 0.000821 | 0.000417 | 0.000553 | |
| 2025-02-21 | 0.000000 | 0.000000 | 0.000000 | 0.000000 | |
| 2025-02-24 | -0.001556 | -0.000801 | -0.000407 | -0.000540 | |
| 2025-02-25 | 0.000529 | 0.000273 | 0.000138 | 0.000184 | |
| 2025-02-26 | 0.001125 | 0.000579 | 0.000294 | 0.000390 | |
| 2025-02-27 | 0.000000 | 0.000000 | 0.000000 | 0.000000 | |
| 2025-02-28 | 0.001043 | 0.000537 | 0.000273 | 0.000362 |
- class portfolio_plan.financialseries.CumulativeReturns(data: DataFrame, name: str, check_base: bool = True, periods: List[Period] | None = None, check_frequency: bool = True)#
Bases:
FinancialSeries- Parameters:
data (pd.DataFrame) – A dataframe containing cumulative OHLC returns
name (str) – Name of the cumulative returns
check_base (bool) – Whether to enforce first value to be equal across open, close, high, low. Defaults to True
periods (List[Period] | None) – See
portfolio_plan.utils.Period()
- plot(scale_x: bool = True)#
Plot all cumulative returns candle chart
- plot_close(scale_x: bool = True) ggplot#
Plot all cumulative returns close price
- plot_high(scale_x: bool = True) ggplot#
Plot all cumulative returns high price
- plot_low(scale_x: bool = True) ggplot#
Plot all cumulative returns high price
- plot_open(scale_x: bool = True) ggplot#
Plot all cumulative returns open price