records module¶
Numba-compiled functions for portfolio records.
apply_weights_to_orders_nb function¶
Apply weights to order records.
best_price_idx_nb function¶
best_price_idx_nb(
records,
open,
high,
low,
close,
entry_price_open=False,
exit_price_close=False,
max_duration=None,
relative=True
)
Get index of best price by applying trade_best_worst_price_nb on each trade.
best_price_nb function¶
best_price_nb(
records,
open,
high,
low,
close,
entry_price_open=False,
exit_price_close=False,
max_duration=None
)
Get best price by applying trade_best_worst_price_nb on each trade.
copy_trade_record_nb function¶
Copy a trade record.
edge_ratio_nb function¶
edge_ratio_nb(
records,
col_map,
open,
high,
low,
close,
volatility,
entry_price_open=False,
exit_price_close=False,
max_duration=None
)
Get edge ratio of each column.
expanding_best_price_nb function¶
expanding_best_price_nb(
records,
open,
high,
low,
close,
entry_price_open=False,
exit_price_close=False,
max_duration=None
)
Get expanding best price of each trade.
expanding_mae_nb function¶
Get expanding MAE of each trade.
expanding_mfe_nb function¶
Get expanding MFE of each trade.
expanding_worst_price_nb function¶
expanding_worst_price_nb(
records,
open,
high,
low,
close,
entry_price_open=False,
exit_price_close=False,
max_duration=None
)
Get expanding worst price of each trade.
expectancy_reduce_nb function¶
Expectancy of a PnL array.
fill_entry_trades_in_position_nb function¶
fill_entry_trades_in_position_nb(
order_records,
col_map,
col,
sim_start,
sim_end,
first_c,
last_c,
init_price,
first_entry_size,
first_entry_fees,
exit_idx,
exit_size_sum,
exit_gross_sum,
exit_fees_sum,
direction,
status,
parent_id,
new_records,
r
)
Fill entry trades located within a single position.
Returns the next trade id.
fill_position_record_nb function¶
Fill a position record by aggregating trade records.
fill_trade_record_nb function¶
fill_trade_record_nb(
new_records,
r,
col,
size,
entry_order_id,
entry_idx,
entry_price,
entry_fees,
exit_order_id,
exit_idx,
exit_price,
exit_fees,
direction,
status,
parent_id
)
Fill a trade record.
get_entry_trades_nb function¶
get_entry_trades_nb(
order_records,
close,
col_map,
init_position=0.0,
init_price=nan,
sim_start=None,
sim_end=None
)
Fill entry trade records by aggregating order records.
Entry trade records are buy orders in a long position and sell orders in a short position.
Usage
>>> from vectorbtpro import *
>>> close = order_price = np.array([
... [1, 6],
... [2, 5],
... [3, 4],
... [4, 3],
... [5, 2],
... [6, 1]
... ])
>>> size = np.array([
... [1, -1],
... [0.1, -0.1],
... [-1, 1],
... [-0.1, 0.1],
... [1, -1],
... [-2, 2]
... ])
>>> target_shape = close.shape
>>> group_lens = np.full(target_shape[1], 1)
>>> init_cash = np.full(target_shape[1], 100)
>>> sim_out = vbt.pf_nb.from_orders_nb(
... target_shape,
... group_lens,
... init_cash=init_cash,
... size=size,
... price=close,
... fees=np.asarray([[0.01]]),
... slippage=np.asarray([[0.01]])
... )
>>> col_map = vbt.rec_nb.col_map_nb(sim_out.order_records['col'], target_shape[1])
>>> entry_trade_records = vbt.pf_nb.get_entry_trades_nb(sim_out.order_records, close, col_map)
>>> pd.DataFrame.from_records(entry_trade_records)
id col size entry_order_id entry_idx entry_price entry_fees \
0 0 0 1.0 0 0 1.01 0.01010
1 1 0 0.1 1 1 2.02 0.00202
2 2 0 1.0 4 4 5.05 0.05050
3 3 0 1.0 5 5 5.94 0.05940
4 0 1 1.0 0 0 5.94 0.05940
5 1 1 0.1 1 1 4.95 0.00495
6 2 1 1.0 4 4 1.98 0.01980
7 3 1 1.0 5 5 1.01 0.01010
exit_order_id exit_idx exit_price exit_fees pnl return \
0 3 3 3.060000 0.030600 2.009300 1.989406
1 3 3 3.060000 0.003060 0.098920 0.489703
2 5 5 5.940000 0.059400 0.780100 0.154475
3 -1 5 6.000000 0.000000 -0.119400 -0.020101
4 3 3 3.948182 0.039482 1.892936 0.318676
5 3 3 3.948182 0.003948 0.091284 0.184411
6 5 5 1.010000 0.010100 0.940100 0.474798
7 -1 5 1.000000 0.000000 -0.020100 -0.019901
direction status parent_id
0 0 1 0
1 0 1 0
2 0 1 1
3 1 0 2
4 1 1 0
5 1 1 0
6 1 1 1
7 0 0 2
get_exit_trades_nb function¶
get_exit_trades_nb(
order_records,
close,
col_map,
init_position=0.0,
init_price=nan,
sim_start=None,
sim_end=None
)
Fill exit trade records by aggregating order records.
Exit trade records are sell orders in a long position and buy orders in a short position.
Usage
- Building upon the example in get_exit_trades_nb:
>>> exit_trade_records = vbt.pf_nb.get_exit_trades_nb(sim_out.order_records, close, col_map)
>>> pd.DataFrame.from_records(exit_trade_records)
id col size entry_order_id entry_idx entry_price entry_fees \
0 0 0 1.0 0 0 1.101818 0.011018
1 1 0 0.1 0 0 1.101818 0.001102
2 2 0 1.0 4 4 5.050000 0.050500
3 3 0 1.0 5 5 5.940000 0.059400
4 0 1 1.0 0 0 5.850000 0.058500
5 1 1 0.1 0 0 5.850000 0.005850
6 2 1 1.0 4 4 1.980000 0.019800
7 3 1 1.0 5 5 1.010000 0.010100
exit_order_id exit_idx exit_price exit_fees pnl return \
0 2 2 2.97 0.02970 1.827464 1.658589
1 3 3 3.96 0.00396 0.280756 2.548119
2 5 5 5.94 0.05940 0.780100 0.154475
3 -1 5 6.00 0.00000 -0.119400 -0.020101
4 2 2 4.04 0.04040 1.711100 0.292496
5 3 3 3.03 0.00303 0.273120 0.466872
6 5 5 1.01 0.01010 0.940100 0.474798
7 -1 5 1.00 0.00000 -0.020100 -0.019901
direction status parent_id
0 0 1 0
1 0 1 0
2 0 1 1
3 1 0 2
4 1 1 0
5 1 1 0
6 1 1 1
7 0 0 2
get_long_view_orders_nb function¶
get_long_view_orders_nb(
order_records,
close,
col_map,
init_position=0.0,
init_price=nan,
sim_start=None,
sim_end=None
)
Get view of orders in long positions only.
get_position_feature_nb function¶
get_position_feature_nb(
order_records,
close,
col_map,
feature=0,
init_position=0.0,
init_price=nan,
fill_closed_position=False,
fill_exit_price=True,
sim_start=None,
sim_end=None
)
Get the position's feature at each time step.
For the list of supported features see PositionFeature.
If fill_exit_price is True and a part of the position is not closed yet, will fill the exit price as if the part was closed using the current close.
If fill_closed_position is True, will forward-fill missing values with the prices of the previously closed position.
get_positions_nb function¶
Fill position records by aggregating trade records.
Trades can be entry trades, exit trades, and even positions themselves - all will produce the same results.
Usage
- Building upon the example in get_exit_trades_nb:
>>> col_map = vbt.rec_nb.col_map_nb(exit_trade_records['col'], target_shape[1])
>>> position_records = vbt.pf_nb.get_positions_nb(exit_trade_records, col_map)
>>> pd.DataFrame.from_records(position_records)
id col size entry_order_id entry_idx entry_price entry_fees \
0 0 0 1.1 0 0 1.101818 0.01212
1 1 0 1.0 4 4 5.050000 0.05050
2 2 0 1.0 5 5 5.940000 0.05940
3 0 1 1.1 0 0 5.850000 0.06435
4 1 1 1.0 4 4 1.980000 0.01980
5 2 1 1.0 5 5 1.010000 0.01010
exit_order_id exit_idx exit_price exit_fees pnl return \
0 3 3 3.060000 0.03366 2.10822 1.739455
1 5 5 5.940000 0.05940 0.78010 0.154475
2 -1 5 6.000000 0.00000 -0.11940 -0.020101
3 3 3 3.948182 0.04343 1.98422 0.308348
4 5 5 1.010000 0.01010 0.94010 0.474798
5 -1 5 1.000000 0.00000 -0.02010 -0.019901
direction status parent_id
0 0 1 0
1 0 1 1
2 1 0 2
3 1 1 0
4 1 1 1
5 0 0 2
get_short_view_orders_nb function¶
get_short_view_orders_nb(
order_records,
close,
col_map,
init_position=0.0,
init_price=nan,
sim_start=None,
sim_end=None
)
Get view of orders in short positions only.
mae_nb function¶
Apply trade_mae_nb on each trade.
mfe_nb function¶
Apply trade_mfe_nb on each trade.
price_status_nb function¶
Return the status of the order's price related to high and low.
See OrderPriceStatus.
profit_factor_reduce_nb function¶
Profit factor of a PnL array.
records_within_sim_range_nb function¶
records_within_sim_range_nb(
target_shape,
records,
col_arr,
idx_arr,
sim_start=None,
sim_end=None
)
Return records within simulation range.
running_edge_ratio_nb function¶
running_edge_ratio_nb(
records,
col_map,
open,
high,
low,
close,
volatility,
entry_price_open=False,
exit_price_close=False,
max_duration=None,
incl_shorter=False
)
Get running edge ratio of each column.
sqn_reduce_nb function¶
SQN of a PnL array.
trade_best_worst_price_nb function¶
trade_best_worst_price_nb(
trade,
open,
high,
low,
close,
entry_price_open=False,
exit_price_close=False,
max_duration=None,
idx_relative=True,
cont_idx=-1,
one_iteration=False,
vmin=nan,
vmax=nan,
imin=-1,
imax=-1
)
Best price, worst price, and their indices during a trade.
trade_losing_streak_nb function¶
Return the current losing streak of each trade.
trade_mae_nb function¶
Compute Maximum Adverse Excursion (MAE).
trade_mfe_nb function¶
Compute Maximum Favorable Excursion (MFE).
trade_winning_streak_nb function¶
Return the current winning streak of each trade.
weighted_price_reduce_meta_nb function¶
Size-weighted price average.
win_rate_reduce_nb function¶
Win rate of a PnL array.
worst_price_idx_nb function¶
worst_price_idx_nb(
records,
open,
high,
low,
close,
entry_price_open=False,
exit_price_close=False,
max_duration=None,
relative=True
)
Get worst price by applying trade_best_worst_price_nb on each trade.
worst_price_nb function¶
worst_price_nb(
records,
open,
high,
low,
close,
entry_price_open=False,
exit_price_close=False,
max_duration=None
)
Get worst price by applying trade_best_worst_price_nb on each trade.