History

History

class dxlib.history.History(df: DataFrame | dict | None = None, schema: Schema | None = None)

Bases: object

add(data: History | DataFrame | Series | tuple | dict)

Add historical data to history

Parameters:

data – pandas DataFrame or History object

Examples

>>> bars = {
        ('2024-01-01', 'AAPL'): Bar(close=155, open=150, high=160, low=140, volume=1000000, vwap=150),
        ('2024-01-01', 'MSFT'): Bar(close=255, open=250, high=260, low=240, volume=2000000, vwap=250)
    }
>>> history = History(data)
>>> history.add({
        ('2024-01-02', 'AAPL'): Bar(close=160, open=155, high=165, low=145, volume=1000000, vwap=155),
        ('2024-01-02', 'MSFT'): Bar(close=260, open=255, high=265, low=245, volume=2000000, vwap=255)
    })
>>> history.get(securities='AAPL', fields='close', dates='2024-01-02')
# Output:
# date        security
# 2024-01-02  AAPL      160
# Name: close, dtype: int64
apply(func: Dict[SchemaLevel, callable] | callable, schema: Schema | None = None, *args, **kwargs)
apply_df(func: Dict[SchemaLevel, callable] | callable, *args, **kwargs)
apply_on(other: History, func: callable, schema: Schema | None = None)
apply_on_df(other: DataFrame, func: callable)
convert_index(index: MultiIndex) MultiIndex
copy()
classmethod from_df(df: DataFrame, schema: Schema | None = None)
classmethod from_dict(serialized=False, **kwargs)
classmethod from_list(history: List[Series], schema: Schema | None = None)
classmethod from_tuple(history: tuple, schema: Schema | None = None)
get(levels: Dict[SchemaLevel, list] | None = None, fields: List[str] | None = None) History

Get historical data for a given security, field and date

Args:

Returns:

pandas DataFrame with multi-index and fields as columns

get_df(levels: Dict[SchemaLevel, list] | None = None, fields: List[str] | None = None) DataFrame
level_unique(level: SchemaLevel = SchemaLevel.SECURITY)
levels_unique(levels: List[SchemaLevel] | None = None) Dict[SchemaLevel, list]
property schema
set(fields: List[str] | None = None, values: DataFrame | dict | None = None)

Set historical data for a given security, field and date

Parameters:
  • fields – list of bar fields

  • values – pandas DataFrame or dict with multi-index and bar fields as columns

Examples

>>> history = History()
>>> history.set(
        fields=['close'],
        values={
            ('2024-01-01', 'AAPL'): 155,
            ('2024-01-01', 'MSFT'): 255
        }
    )
>>> history.get(securities='AAPL', fields='close', dates='2024-01-01')
date        security
2024-01-01  AAPL      155
Name: close, dtype: int64
set_df(levels: Dict[SchemaLevel, list] | None = None, fields: List[str] | None = None, values: DataFrame | dict | None = None)
property shape
to_dict(serializable=False)

Schema

class dxlib.history.Schema(levels: List[SchemaLevel] | None = None, fields: List[str] | None = None, security_manager: SecurityManager | None = None)

Bases: object

apply_deserialize(df: DataFrame)
copy()
classmethod deserialize(obj: any)
extend(other: Schema) Schema
fields: List[str]
classmethod from_dict(**kwargs) Schema
levels: List[SchemaLevel]
security_manager: SecurityManager
classmethod serialize(obj: any)
to_dict() dict

SchemaLevel

class dxlib.history.SchemaLevel(value)

Bases: Enum

An enumeration.

DATE = 'date'
SECURITY = 'security'
classmethod from_dict(**kwargs)
to_dict()