Skip to content

core module

Core Numba-compiled functions for portfolio simulation.


adj_size_granularity_nb function

adj_size_granularity_nb(
    size,
    size_granularity
)

Whether to adjust the size with the size granularity.


approx_buy_value_nb function

approx_buy_value_nb(
    position,
    debt,
    locked_cash,
    val_price,
    size,
    direction
)

Approximate value of a buy operation.

Positive value means spending (for sorting reasons).


approx_long_buy_value_nb function

approx_long_buy_value_nb(
    val_price,
    size
)

Approximate value of a long-buy operation.

Positive value means spending (for sorting reasons).


approx_long_sell_value_nb function

approx_long_sell_value_nb(
    position,
    debt,
    val_price,
    size
)

Approximate value of a long-sell operation.

Positive value means spending (for sorting reasons).


approx_order_value_nb function

approx_order_value_nb(
    exec_state,
    size,
    size_type=0,
    direction=2
)

Approximate the value of an order.

Assumes that cash is infinite.

Positive value means spending (for sorting reasons).


approx_sell_value_nb function

approx_sell_value_nb(
    position,
    debt,
    val_price,
    size,
    direction
)

Approximate value of a sell operation.

Positive value means spending (for sorting reasons).


approx_short_buy_value_nb function

approx_short_buy_value_nb(
    position,
    debt,
    locked_cash,
    val_price,
    size
)

Approximate value of a short-buy operation.

Positive value means spending (for sorting reasons).


approx_short_sell_value_nb function

approx_short_sell_value_nb(
    val_price,
    size
)

Approximate value of a short-sell operation.

Positive value means spending (for sorting reasons).


buy_nb function

buy_nb(
    account_state,
    size,
    price,
    direction=2,
    fees=0.0,
    fixed_fees=0.0,
    slippage=0.0,
    min_size=nan,
    max_size=nan,
    size_granularity=nan,
    leverage=1.0,
    leverage_mode=0,
    price_area_vio_mode=0,
    allow_partial=True,
    percent=nan,
    price_area=PriceArea(open=nan, high=nan, low=nan, close=nan),
    is_closing_price=False
)

Buy.


cast_account_state_nb function

cast_account_state_nb(
    account_state
)

Cast account state to float.


check_adj_price_nb function

check_adj_price_nb(
    adj_price,
    price_area,
    is_closing_price,
    price_area_vio_mode
)

Check whether adjusted price is within price boundaries.


check_dt_stop_hit_nb function

check_dt_stop_hit_nb(
    i,
    stop=-1,
    time_delta_format=1,
    index=None,
    freq=None
)

Check whether DT stop was hit by comparing the current index with the initial index.

Returns whether the stop was hit already on open, and whether the stop was hit during this bar.


check_group_lens_nb function

check_group_lens_nb(
    group_lens,
    n_cols
)

Check group_lens.


check_limit_expired_nb function

check_limit_expired_nb(
    creation_idx,
    i,
    tif=-1,
    expiry=-1,
    time_delta_format=1,
    index=None,
    freq=None
)

Check whether limit is expired by comparing the current index with the creation index.

Returns whether the limit expires already on open, and whether the limit expires during this bar.


check_limit_hit_nb function

check_limit_hit_nb(
    open,
    high,
    low,
    close,
    price,
    size,
    direction=2,
    limit_delta=nan,
    delta_format=1,
    limit_reverse=False,
    can_use_ohlc=True,
    check_open=True,
    hard_limit=False
)

Resolve the limit price using resolve_limit_price_nb() and check whether it was hit.

Returns the limit price, whether it was hit before open, and whether it was hit during this bar.

If can_use_ohlc and check_open is True and the stop is hit before open, returns open.


check_price_hit_nb function

check_price_hit_nb(
    open,
    high,
    low,
    close,
    price,
    hit_below=True,
    can_use_ohlc=True,
    check_open=True,
    hard_price=False
)

Check whether a target price was hit.

If hard_price is False, and can_use_ohlc and check_open are True and the target price is hit by open, returns open. Otherwise, returns the actual target price.

Returns the stop price, whether it was hit by open, and whether it was hit during this bar.


check_stop_hit_nb function

check_stop_hit_nb(
    open,
    high,
    low,
    close,
    is_position_long,
    init_price,
    stop,
    delta_format=1,
    hit_below=True,
    can_use_ohlc=True,
    check_open=True,
    hard_stop=False
)

Resolve the stop price using resolve_stop_price_nb() and check whether it was hit.

See check_price_hit_nb().


check_td_stop_hit_nb function

check_td_stop_hit_nb(
    init_idx,
    i,
    stop=-1,
    time_delta_format=1,
    index=None,
    freq=None
)

Check whether TD stop was hit by comparing the current index with the initial index.

Returns whether the stop was hit already on open, and whether the stop was hit during this bar.


check_tsl_th_hit_nb function

check_tsl_th_hit_nb(
    is_position_long,
    init_price,
    peak_price,
    threshold,
    delta_format=1
)

Resolve the TSL threshold price using resolve_stop_price_nb() and check whether it was hit.


clear_limit_info_nb function

clear_limit_info_nb(
    limit_info
)

Clear limit order information.


clear_sl_info_nb function

clear_sl_info_nb(
    sl_info
)

Clear SL order information.


clear_time_info_nb function

clear_time_info_nb(
    time_info
)

Clear time order information.


clear_tp_info_nb function

clear_tp_info_nb(
    tp_info
)

Clear TP order information.


clear_tsl_info_nb function

clear_tsl_info_nb(
    tsl_info
)

Clear TSL/TTP order information.


close_position_nb function

close_position_nb(
    price=inf,
    fees=0.0,
    fixed_fees=0.0,
    slippage=0.0,
    min_size=nan,
    max_size=nan,
    size_granularity=nan,
    leverage=1.0,
    leverage_mode=0,
    reject_prob=0.0,
    price_area_vio_mode=0,
    allow_partial=True,
    raise_reject=False,
    log=False
)

Close the current position.


execute_order_nb function

execute_order_nb(
    exec_state,
    order,
    price_area=PriceArea(open=nan, high=nan, low=nan, close=nan),
    update_value=False
)

Execute an order given the current state.

Args

exec_state : ExecState
See ExecState.
order : Order
See Order.
price_area : OrderPriceArea
See PriceArea.
update_value : bool
Whether to update the value.

Error is thrown if an input has value that is not expected. Order is ignored if its execution has no effect on the current balance. Order is rejected if an input goes over a limit or against a restriction.


fill_init_pos_info_nb function

fill_init_pos_info_nb(
    record,
    col,
    position_now,
    price
)

Fill position record for an initial position.


fill_log_record_nb function

fill_log_record_nb(
    records,
    r,
    group,
    col,
    i,
    price_area,
    exec_state,
    order,
    order_result,
    new_exec_state,
    order_id
)

Fill a log record.


fill_order_record_nb function

fill_order_record_nb(
    records,
    r,
    col,
    i,
    order_result
)

Fill an order record.


get_diraware_size_nb function

get_diraware_size_nb(
    size,
    direction
)

Get direction-aware size.


get_limit_info_target_price_nb function

get_limit_info_target_price_nb(
    limit_info
)

Get target price from limit order information.


get_sl_info_target_price_nb function

get_sl_info_target_price_nb(
    sl_info,
    position_now
)

Get target price from SL order information.


get_stop_ladder_exit_size_nb function

get_stop_ladder_exit_size_nb(
    stop_,
    step,
    col,
    init_price,
    init_position,
    position_now,
    ladder=0,
    delta_format=1,
    hit_below=True
)

Get the exit size corresponding to the current step in the ladder.


get_time_stop_ladder_exit_size_nb function

get_time_stop_ladder_exit_size_nb(
    stop_,
    step,
    col,
    init_idx,
    init_position,
    position_now,
    ladder=0,
    time_delta_format=1,
    index=None
)

Get the exit size corresponding to the current step in the ladder.


get_tp_info_target_price_nb function

get_tp_info_target_price_nb(
    tp_info,
    position_now
)

Get target price from TP order information.


get_trade_stats_nb function

get_trade_stats_nb(
    size,
    entry_price,
    entry_fees,
    exit_price,
    exit_fees,
    direction
)

Get trade statistics.


get_tsl_info_target_price_nb function

get_tsl_info_target_price_nb(
    tsl_info,
    position_now
)

Get target price from TSL/TTP order information.


is_grouped_nb function

is_grouped_nb(
    group_lens
)

Check if columm,ns are grouped, that is, more than one column per group.


is_limit_active_nb function

is_limit_active_nb(
    init_idx,
    init_price
)

Check whether a limit order is active.


is_limit_info_active_nb function

is_limit_info_active_nb(
    limit_info
)

Check whether information record for a limit order is active.


is_stop_active_nb function

is_stop_active_nb(
    init_idx,
    stop
)

Check whether a stop order is active.


is_stop_info_active_nb function

is_stop_info_active_nb(
    stop_info
)

Check whether information record for a stop order is active.


is_stop_info_ladder_active_nb function

is_stop_info_ladder_active_nb(
    info
)

Check whether information record for a stop ladder is active.


is_time_stop_active_nb function

is_time_stop_active_nb(
    init_idx,
    stop
)

Check whether a time stop order is active.


is_time_stop_info_active_nb function

is_time_stop_info_active_nb(
    time_stop_info
)

Check whether information record for a time stop order is active.


long_buy_nb function

long_buy_nb(
    account_state,
    size,
    price,
    fees=0.0,
    fixed_fees=0.0,
    slippage=0.0,
    min_size=nan,
    max_size=nan,
    size_granularity=nan,
    leverage=1.0,
    leverage_mode=0,
    price_area_vio_mode=0,
    allow_partial=True,
    percent=nan,
    price_area=PriceArea(open=nan, high=nan, low=nan, close=nan),
    is_closing_price=False
)

Open or increase a long position.


long_sell_nb function

long_sell_nb(
    account_state,
    size,
    price,
    fees=0.0,
    fixed_fees=0.0,
    slippage=0.0,
    min_size=nan,
    max_size=nan,
    size_granularity=nan,
    price_area_vio_mode=0,
    allow_partial=True,
    percent=nan,
    price_area=PriceArea(open=nan, high=nan, low=nan, close=nan),
    is_closing_price=False
)

Decrease or close a long position.


order_nb function

order_nb(
    size=inf,
    price=inf,
    size_type=0,
    direction=2,
    fees=0.0,
    fixed_fees=0.0,
    slippage=0.0,
    min_size=nan,
    max_size=nan,
    size_granularity=nan,
    leverage=1.0,
    leverage_mode=0,
    reject_prob=0.0,
    price_area_vio_mode=0,
    allow_partial=True,
    raise_reject=False,
    log=False
)

Create an order.

See Order for details on arguments.


order_not_filled_nb function

order_not_filled_nb(
    status,
    status_info
)

Return OrderResult for order that hasn't been filled.


order_nothing_nb function

order_nothing_nb()

Convenience function to order nothing.


prepare_last_cash_nb function

prepare_last_cash_nb(
    target_shape,
    group_lens,
    cash_sharing,
    init_cash
)

Prepare last_cash.


prepare_last_pos_info_nb function

prepare_last_pos_info_nb(
    target_shape,
    init_position,
    init_price,
    fill_pos_info=True
)

Prepare last_pos_info.


prepare_last_position_nb function

prepare_last_position_nb(
    target_shape,
    init_position
)

Prepare last_position.


prepare_last_value_nb function

prepare_last_value_nb(
    target_shape,
    group_lens,
    cash_sharing,
    init_cash,
    init_position,
    init_price
)

Prepare last_value.


prepare_records_nb function

prepare_records_nb(
    target_shape,
    max_order_records=None,
    max_log_records=0
)

Prepare records.


prepare_sim_out_nb function

prepare_sim_out_nb(
    order_records,
    order_counts,
    log_records,
    log_counts,
    cash_deposits,
    cash_earnings,
    call_seq=None,
    in_outputs=None,
    sim_start=None,
    sim_end=None
)

Prepare simulation output.


process_order_nb function

process_order_nb(
    group,
    col,
    i,
    exec_state,
    order,
    price_area=PriceArea(open=nan, high=nan, low=nan, close=nan),
    update_value=False,
    order_records=None,
    order_counts=None,
    log_records=None,
    log_counts=None
)

Process an order by executing it, saving relevant information to the logs, and returning a new state.


raise_rejected_order_nb function

raise_rejected_order_nb(
    order_result
)

Raise an RejectedOrderError.


resolve_dyn_limit_price_nb function

resolve_dyn_limit_price_nb(
    val_price,
    price,
    limit_price
)

Resolve price dynamically.

Uses the valuation price as the left bound and order price as the right bound.


resolve_dyn_stop_entry_price_nb function

resolve_dyn_stop_entry_price_nb(
    val_price,
    price,
    stop_entry_price
)

Resolve stop entry price dynamically.

Uses the valuation/open price as the left bound and order price as the right bound.


resolve_hl_nb function

resolve_hl_nb(
    open,
    high,
    low,
    close
)

Resolve the current high and low.


resolve_limit_order_price_nb function

resolve_limit_order_price_nb(
    limit_price,
    close,
    limit_order_price
)

Resolve the limit order price of a limit order.


resolve_limit_price_nb function

resolve_limit_price_nb(
    init_price,
    limit_delta=nan,
    delta_format=1,
    hit_below=True
)

Resolve the limit price.


resolve_size_nb function

resolve_size_nb(
    size,
    size_type,
    position,
    val_price,
    value,
    target_size_type=0,
    as_requirement=False
)

Resolve size into an absolute amount of assets and percentage of resources.

Percentage is only set if the option SizeType.Percent(100) is used.


resolve_stop_exit_price_nb function

resolve_stop_exit_price_nb(
    stop_price,
    close,
    stop_exit_price
)

Resolve the exit price of a stop order.


resolve_stop_price_nb function

resolve_stop_price_nb(
    init_price,
    stop,
    delta_format=1,
    hit_below=True
)

Resolve the stop price.


sell_nb function

sell_nb(
    account_state,
    size,
    price,
    direction=2,
    fees=0.0,
    fixed_fees=0.0,
    slippage=0.0,
    min_size=nan,
    max_size=nan,
    size_granularity=nan,
    leverage=1.0,
    price_area_vio_mode=0,
    allow_partial=True,
    percent=nan,
    price_area=PriceArea(open=nan, high=nan, low=nan, close=nan),
    is_closing_price=False
)

Sell.


set_limit_info_nb function

set_limit_info_nb(
    limit_info,
    signal_idx,
    creation_idx=None,
    init_idx=None,
    init_price=-inf,
    init_size=inf,
    init_size_type=0,
    init_direction=2,
    init_stop_type=-1,
    delta=nan,
    delta_format=1,
    tif=-1,
    expiry=-1,
    time_delta_format=1,
    reverse=False,
    order_price=-1
)

Set limit order information.

See limit_info_dt.


set_sl_info_nb function

set_sl_info_nb(
    sl_info,
    init_idx,
    init_price=-inf,
    init_position=nan,
    stop=nan,
    exit_price=-1,
    exit_size=nan,
    exit_size_type=-1,
    exit_type=0,
    order_type=0,
    limit_delta=nan,
    delta_format=1,
    ladder=0,
    step=-1,
    step_idx=-1
)

Set SL order information.

See sl_info_dt.


set_time_info_nb function

set_time_info_nb(
    time_info,
    init_idx,
    init_position=nan,
    stop=-1,
    exit_price=-1,
    exit_size=nan,
    exit_size_type=-1,
    exit_type=0,
    order_type=0,
    limit_delta=nan,
    delta_format=1,
    time_delta_format=1,
    ladder=0,
    step=-1,
    step_idx=-1
)

Set time order information.

See time_info_dt.


set_tp_info_nb function

set_tp_info_nb(
    tp_info,
    init_idx,
    init_price=-inf,
    init_position=nan,
    stop=nan,
    exit_price=-1,
    exit_size=nan,
    exit_size_type=-1,
    exit_type=0,
    order_type=0,
    limit_delta=nan,
    delta_format=1,
    ladder=0,
    step=-1,
    step_idx=-1
)

Set TP order information.

See tp_info_dt.


set_tsl_info_nb function

set_tsl_info_nb(
    tsl_info,
    init_idx,
    init_price=-inf,
    init_position=nan,
    peak_idx=None,
    peak_price=None,
    stop=nan,
    th=nan,
    exit_price=-1,
    exit_size=nan,
    exit_size_type=-1,
    exit_type=0,
    order_type=0,
    limit_delta=nan,
    delta_format=1,
    ladder=0,
    step=-1,
    step_idx=-1
)

Set TSL/TTP order information.

See tsl_info_dt.


short_buy_nb function

short_buy_nb(
    account_state,
    size,
    price,
    fees=0.0,
    fixed_fees=0.0,
    slippage=0.0,
    min_size=nan,
    max_size=nan,
    size_granularity=nan,
    price_area_vio_mode=0,
    allow_partial=True,
    percent=nan,
    price_area=PriceArea(open=nan, high=nan, low=nan, close=nan),
    is_closing_price=False
)

Decrease or close a short position.


short_sell_nb function

short_sell_nb(
    account_state,
    size,
    price,
    fees=0.0,
    fixed_fees=0.0,
    slippage=0.0,
    min_size=nan,
    max_size=nan,
    size_granularity=nan,
    leverage=1.0,
    price_area_vio_mode=0,
    allow_partial=True,
    percent=nan,
    price_area=PriceArea(open=nan, high=nan, low=nan, close=nan),
    is_closing_price=False
)

Open or increase a short position.


should_update_stop_nb function

should_update_stop_nb(
    new_stop,
    upon_stop_update
)

Whether to update stop.


should_update_time_stop_nb function

should_update_time_stop_nb(
    new_stop,
    upon_stop_update
)

Whether to update time stop.


update_open_pos_info_stats_nb function

update_open_pos_info_stats_nb(
    record,
    position_now,
    price
)

Update statistics of an open position record using custom price.


update_pos_info_nb function

update_pos_info_nb(
    record,
    i,
    col,
    position_before,
    position_now,
    order_result,
    order_id
)

Update position record after filling an order.


update_value_nb function

update_value_nb(
    cash_before,
    cash_now,
    position_before,
    position_now,
    val_price_before,
    price,
    value_before
)

Update valuation price and value.