Skip to content

logs module

Base class for working with log records.

Order records capture information on simulation logs. Logs are populated when simulating a portfolio and can be accessed as Portfolio.logs.

>>> import pandas as pd
>>> import numpy as np
>>> from datetime import datetime, timedelta
>>> import vectorbt as vbt

>>> np.random.seed(42)
>>> price = pd.DataFrame({
...     'a': np.random.uniform(1, 2, size=100),
...     'b': np.random.uniform(1, 2, size=100)
... }, index=[datetime(2020, 1, 1) + timedelta(days=i) for i in range(100)])
>>> size = pd.DataFrame({
...     'a': np.random.uniform(-100, 100, size=100),
...     'b': np.random.uniform(-100, 100, size=100),
... }, index=[datetime(2020, 1, 1) + timedelta(days=i) for i in range(100)])
>>> pf = vbt.Portfolio.from_orders(price, size, fees=0.01, freq='d', log=True)
>>> logs = pf.logs

>>> logs.filled.count()
a    88
b    99
Name: count, dtype: int64

>>> logs.ignored.count()
a    0
b    0
Name: count, dtype: int64

>>> logs.rejected.count()
a    12
b     1
Name: count, dtype: int64

Stats

>>> logs['a'].stats()
Start                             2020-01-01 00:00:00
End                               2020-04-09 00:00:00
Period                              100 days 00:00:00
Total Records                                     100
Status Counts: None                                 0
Status Counts: Filled                              88
Status Counts: Ignored                              0
Status Counts: Rejected                            12
Status Info Counts: None                           88
Status Info Counts: NoCashLong                     12
Name: a, dtype: object

StatsBuilderMixin.stats() also supports (re-)grouping:

>>> logs.stats(group_by=True)
Start                             2020-01-01 00:00:00
End                               2020-04-09 00:00:00
Period                              100 days 00:00:00
Total Records                                     200
Status Counts: None                                 0
Status Counts: Filled                             187
Status Counts: Ignored                              0
Status Counts: Rejected                            13
Status Info Counts: None                          187
Status Info Counts: NoCashLong                     13
Name: group, dtype: object

Plots

This class does not have any subplots.


logs_attach_field_config Config

Config of fields to be attached to Logs.

Config({
    "res_side": {
        "attach_filters": true
    },
    "res_status": {
        "attach_filters": true
    },
    "res_status_info": {
        "attach_filters": true
    }
})

logs_field_config Config

Field config for Logs.

Config({
    "dtype": {
        "id": "int64",
        "group": "int64",
        "col": "int64",
        "idx": "int64",
        "cash": "float64",
        "position": "float64",
        "debt": "float64",
        "free_cash": "float64",
        "val_price": "float64",
        "value": "float64",
        "req_size": "float64",
        "req_price": "float64",
        "req_size_type": "int64",
        "req_direction": "int64",
        "req_fees": "float64",
        "req_fixed_fees": "float64",
        "req_slippage": "float64",
        "req_min_size": "float64",
        "req_max_size": "float64",
        "req_size_granularity": "float64",
        "req_reject_prob": "float64",
        "req_lock_cash": "bool",
        "req_allow_partial": "bool",
        "req_raise_reject": "bool",
        "req_log": "bool",
        "new_cash": "float64",
        "new_position": "float64",
        "new_debt": "float64",
        "new_free_cash": "float64",
        "new_val_price": "float64",
        "new_value": "float64",
        "res_size": "float64",
        "res_price": "float64",
        "res_fees": "float64",
        "res_side": "int64",
        "res_status": "int64",
        "res_status_info": "int64",
        "order_id": "int64"
    },
    "settings": {
        "id": {
            "title": "Log Id"
        },
        "group": {
            "title": "Group"
        },
        "cash": {
            "title": "Cash"
        },
        "position": {
            "title": "Position"
        },
        "debt": {
            "title": "Debt"
        },
        "free_cash": {
            "title": "Free Cash"
        },
        "val_price": {
            "title": "Val Price"
        },
        "value": {
            "title": "Value"
        },
        "req_size": {
            "title": "Request Size"
        },
        "req_price": {
            "title": "Request Price"
        },
        "req_size_type": {
            "title": "Request Size Type",
            "mapping": {
                "Amount": 0,
                "Value": 1,
                "Percent": 2,
                "TargetAmount": 3,
                "TargetValue": 4,
                "TargetPercent": 5
            }
        },
        "req_direction": {
            "title": "Request Direction",
            "mapping": {
                "LongOnly": 0,
                "ShortOnly": 1,
                "Both": 2
            }
        },
        "req_fees": {
            "title": "Request Fees"
        },
        "req_fixed_fees": {
            "title": "Request Fixed Fees"
        },
        "req_slippage": {
            "title": "Request Slippage"
        },
        "req_min_size": {
            "title": "Request Min Size"
        },
        "req_max_size": {
            "title": "Request Max Size"
        },
        "req_size_granularity": {
            "title": "Request Size Granularity"
        },
        "req_reject_prob": {
            "title": "Request Rejection Prob"
        },
        "req_lock_cash": {
            "title": "Request Lock Cash"
        },
        "req_allow_partial": {
            "title": "Request Allow Partial"
        },
        "req_raise_reject": {
            "title": "Request Raise Rejection"
        },
        "req_log": {
            "title": "Request Log"
        },
        "new_cash": {
            "title": "New Cash"
        },
        "new_position": {
            "title": "New Position"
        },
        "new_debt": {
            "title": "New Debt"
        },
        "new_free_cash": {
            "title": "New Free Cash"
        },
        "new_val_price": {
            "title": "New Val Price"
        },
        "new_value": {
            "title": "New Value"
        },
        "res_size": {
            "title": "Result Size"
        },
        "res_price": {
            "title": "Result Price"
        },
        "res_fees": {
            "title": "Result Fees"
        },
        "res_side": {
            "title": "Result Side",
            "mapping": {
                "Buy": 0,
                "Sell": 1
            }
        },
        "res_status": {
            "title": "Result Status",
            "mapping": {
                "Filled": 0,
                "Ignored": 1,
                "Rejected": 2
            }
        },
        "res_status_info": {
            "title": "Result Status Info",
            "mapping": {
                "SizeNaN": 0,
                "PriceNaN": 1,
                "ValPriceNaN": 2,
                "ValueNaN": 3,
                "ValueZeroNeg": 4,
                "SizeZero": 5,
                "NoCashShort": 6,
                "NoCashLong": 7,
                "NoOpenPosition": 8,
                "MaxSizeExceeded": 9,
                "RandomEvent": 10,
                "CantCoverFees": 11,
                "MinSizeNotReached": 12,
                "PartialFill": 13
            }
        },
        "order_id": {
            "title": "Order Id"
        }
    }
})

Logs class

Logs(
    wrapper,
    records_arr,
    col_mapper=None,
    **kwargs
)

Extends Records for working with log records.

Superclasses

Inherited members


buy method

Records filtered by res_side == 0.


cant_cover_fees method

Records filtered by res_status_info == 11.


cash method

Mapped array of the field cash.


col method

Mapped array of the field col.


debt method

Mapped array of the field debt.


field_config class variable

Field config of Logs.

Config({
    "dtype": {
        "id": "int64",
        "group": "int64",
        "col": "int64",
        "idx": "int64",
        "cash": "float64",
        "position": "float64",
        "debt": "float64",
        "free_cash": "float64",
        "val_price": "float64",
        "value": "float64",
        "req_size": "float64",
        "req_price": "float64",
        "req_size_type": "int64",
        "req_direction": "int64",
        "req_fees": "float64",
        "req_fixed_fees": "float64",
        "req_slippage": "float64",
        "req_min_size": "float64",
        "req_max_size": "float64",
        "req_size_granularity": "float64",
        "req_reject_prob": "float64",
        "req_lock_cash": "bool",
        "req_allow_partial": "bool",
        "req_raise_reject": "bool",
        "req_log": "bool",
        "new_cash": "float64",
        "new_position": "float64",
        "new_debt": "float64",
        "new_free_cash": "float64",
        "new_val_price": "float64",
        "new_value": "float64",
        "res_size": "float64",
        "res_price": "float64",
        "res_fees": "float64",
        "res_side": "int64",
        "res_status": "int64",
        "res_status_info": "int64",
        "order_id": "int64"
    },
    "settings": {
        "id": {
            "name": "id",
            "title": "Log Id"
        },
        "col": {
            "name": "col",
            "title": "Column",
            "mapping": "columns"
        },
        "idx": {
            "name": "idx",
            "title": "Timestamp",
            "mapping": "index"
        },
        "group": {
            "title": "Group"
        },
        "cash": {
            "title": "Cash"
        },
        "position": {
            "title": "Position"
        },
        "debt": {
            "title": "Debt"
        },
        "free_cash": {
            "title": "Free Cash"
        },
        "val_price": {
            "title": "Val Price"
        },
        "value": {
            "title": "Value"
        },
        "req_size": {
            "title": "Request Size"
        },
        "req_price": {
            "title": "Request Price"
        },
        "req_size_type": {
            "title": "Request Size Type",
            "mapping": {
                "Amount": 0,
                "Value": 1,
                "Percent": 2,
                "TargetAmount": 3,
                "TargetValue": 4,
                "TargetPercent": 5
            }
        },
        "req_direction": {
            "title": "Request Direction",
            "mapping": {
                "LongOnly": 0,
                "ShortOnly": 1,
                "Both": 2
            }
        },
        "req_fees": {
            "title": "Request Fees"
        },
        "req_fixed_fees": {
            "title": "Request Fixed Fees"
        },
        "req_slippage": {
            "title": "Request Slippage"
        },
        "req_min_size": {
            "title": "Request Min Size"
        },
        "req_max_size": {
            "title": "Request Max Size"
        },
        "req_size_granularity": {
            "title": "Request Size Granularity"
        },
        "req_reject_prob": {
            "title": "Request Rejection Prob"
        },
        "req_lock_cash": {
            "title": "Request Lock Cash"
        },
        "req_allow_partial": {
            "title": "Request Allow Partial"
        },
        "req_raise_reject": {
            "title": "Request Raise Rejection"
        },
        "req_log": {
            "title": "Request Log"
        },
        "new_cash": {
            "title": "New Cash"
        },
        "new_position": {
            "title": "New Position"
        },
        "new_debt": {
            "title": "New Debt"
        },
        "new_free_cash": {
            "title": "New Free Cash"
        },
        "new_val_price": {
            "title": "New Val Price"
        },
        "new_value": {
            "title": "New Value"
        },
        "res_size": {
            "title": "Result Size"
        },
        "res_price": {
            "title": "Result Price"
        },
        "res_fees": {
            "title": "Result Fees"
        },
        "res_side": {
            "title": "Result Side",
            "mapping": {
                "Buy": 0,
                "Sell": 1
            }
        },
        "res_status": {
            "title": "Result Status",
            "mapping": {
                "Filled": 0,
                "Ignored": 1,
                "Rejected": 2
            }
        },
        "res_status_info": {
            "title": "Result Status Info",
            "mapping": {
                "SizeNaN": 0,
                "PriceNaN": 1,
                "ValPriceNaN": 2,
                "ValueNaN": 3,
                "ValueZeroNeg": 4,
                "SizeZero": 5,
                "NoCashShort": 6,
                "NoCashLong": 7,
                "NoOpenPosition": 8,
                "MaxSizeExceeded": 9,
                "RandomEvent": 10,
                "CantCoverFees": 11,
                "MinSizeNotReached": 12,
                "PartialFill": 13
            }
        },
        "order_id": {
            "title": "Order Id"
        }
    }
})

filled method

Records filtered by res_status == 0.


free_cash method

Mapped array of the field free_cash.


group method

Mapped array of the field group.


id method

Mapped array of the field id.


idx method

Mapped array of the field idx.


ignored method

Records filtered by res_status == 1.


max_size_exceeded method

Records filtered by res_status_info == 9.


metrics class variable

Metrics supported by Logs.

Config({
    "start": {
        "title": "Start",
        "calc_func": "<function Logs.<lambda> at 0x17506b100>",
        "agg_func": null,
        "tags": "wrapper"
    },
    "end": {
        "title": "End",
        "calc_func": "<function Logs.<lambda> at 0x17506b1a0>",
        "agg_func": null,
        "tags": "wrapper"
    },
    "period": {
        "title": "Period",
        "calc_func": "<function Logs.<lambda> at 0x17506b240>",
        "apply_to_timedelta": true,
        "agg_func": null,
        "tags": "wrapper"
    },
    "total_records": {
        "title": "Total Records",
        "calc_func": "count",
        "tags": "records"
    },
    "res_status_counts": {
        "title": "Status Counts",
        "calc_func": "res_status.value_counts",
        "incl_all_keys": true,
        "post_calc_func": "<function Logs.<lambda> at 0x17506b2e0>",
        "tags": [
            "logs",
            "res_status",
            "value_counts"
        ]
    },
    "res_status_info_counts": {
        "title": "Status Info Counts",
        "calc_func": "res_status_info.value_counts",
        "post_calc_func": "<function Logs.<lambda> at 0x17506b380>",
        "tags": [
            "logs",
            "res_status_info",
            "value_counts"
        ]
    }
})

Returns Logs._metrics, which gets (deep) copied upon creation of each instance. Thus, changing this config won't affect the class.

To change metrics, you can either change the config in-place, override this property, or overwrite the instance variable Logs._metrics.


min_size_not_reached method

Records filtered by res_status_info == 12.


new_cash method

Mapped array of the field new_cash.


new_debt method

Mapped array of the field new_debt.


new_free_cash method

Mapped array of the field new_free_cash.


new_position method

Mapped array of the field new_position.


new_val_price method

Mapped array of the field new_val_price.


new_value method

Mapped array of the field new_value.


no_cash_long method

Records filtered by res_status_info == 7.


no_cash_short method

Records filtered by res_status_info == 6.


no_open_position method

Records filtered by res_status_info == 8.


order_id method

Mapped array of the field order_id.


partial_fill method

Records filtered by res_status_info == 13.


plots_defaults property

Defaults for PlotsBuilderMixin.plots().

Merges Records.plots_defaults and logs.plots from settings.


position method

Mapped array of the field position.


price_nan method

Records filtered by res_status_info == 1.


random_event method

Records filtered by res_status_info == 10.


rejected method

Records filtered by res_status == 2.


req_allow_partial method

Mapped array of the field req_allow_partial.


req_direction method

Mapped array of the field req_direction.


req_fees method

Mapped array of the field req_fees.


req_fixed_fees method

Mapped array of the field req_fixed_fees.


req_lock_cash method

Mapped array of the field req_lock_cash.


req_log method

Mapped array of the field req_log.


req_max_size method

Mapped array of the field req_max_size.


req_min_size method

Mapped array of the field req_min_size.


req_price method

Mapped array of the field req_price.


req_raise_reject method

Mapped array of the field req_raise_reject.


req_reject_prob method

Mapped array of the field req_reject_prob.


req_size method

Mapped array of the field req_size.


req_size_granularity method

Mapped array of the field req_size_granularity.


req_size_type method

Mapped array of the field req_size_type.


req_slippage method

Mapped array of the field req_slippage.


res_fees method

Mapped array of the field res_fees.


res_price method

Mapped array of the field res_price.


res_side method

Mapped array of the field res_side.


res_size method

Mapped array of the field res_size.


res_status method

Mapped array of the field res_status.


res_status_info method

Mapped array of the field res_status_info.


sell method

Records filtered by res_side == 1.


size_nan method

Records filtered by res_status_info == 0.


size_zero method

Records filtered by res_status_info == 5.


stats_defaults property

Defaults for StatsBuilderMixin.stats().

Merges Records.stats_defaults and logs.stats from settings.


subplots class variable

Subplots supported by Logs.

Config({})

Returns Logs._subplots, which gets (deep) copied upon creation of each instance. Thus, changing this config won't affect the class.

To change subplots, you can either change the config in-place, override this property, or overwrite the instance variable Logs._subplots.


val_price method

Mapped array of the field val_price.


val_price_nan method

Records filtered by res_status_info == 2.


value method

Mapped array of the field value.


value_nan method

Records filtered by res_status_info == 3.


value_zero_neg method

Records filtered by res_status_info == 4.