from_signals module¶
Numba-compiled functions for portfolio simulation based on signals.
dir_signal_func_nb function¶
dir_signal_func_nb(
c,
entries,
exits,
direction,
from_ago,
adjust_func_nb=no_adjust_func_nb,
adjust_args=()
)
Resolve direction-aware signals out of entries, exits, and direction.
The direction of each pair of signals is taken from direction argument:
- True, True,
Direction.LongOnly-> True, True, False, False - True, True,
Direction.ShortOnly-> False, False, True, True - True, True,
Direction.Both-> True, False, True, False
Best to use when the direction doesn't change throughout time.
Prior to returning the signals, calls user-defined adjust_func_nb, which can be used to adjust stop values in the context. Must accept SignalContext and *adjust_args, and return nothing.
dir_to_ls_signals_nb function¶
Convert direction-unaware to direction-aware signals.
from_basic_signals_nb function¶
from_basic_signals_nb(
target_shape,
group_lens,
open=nan,
high=nan,
low=nan,
close=nan,
init_cash=100.0,
init_position=0.0,
init_price=nan,
cash_deposits=0.0,
cash_earnings=0.0,
cash_dividends=0.0,
long_entries=False,
long_exits=False,
short_entries=False,
short_exits=False,
size=inf,
price=inf,
size_type=0,
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,
val_price=inf,
accumulate=0,
upon_long_conflict=0,
upon_short_conflict=0,
upon_dir_conflict=0,
upon_opposite_entry=4,
from_ago=0,
sim_start=None,
sim_end=None,
call_seq=None,
auto_call_seq=False,
ffill_val_price=True,
update_value=False,
save_state=False,
save_value=False,
save_returns=False,
max_order_records=None,
max_log_records=0
)
Simulate given basic signals (no limit or stop orders).
Iterates in the column-major order. Utilizes flexible broadcasting.
Note
Should be only grouped if cash sharing is enabled.
from_signal_func_nb function¶
from_signal_func_nb(
target_shape,
group_lens,
cash_sharing,
index=None,
freq=None,
open=nan,
high=nan,
low=nan,
close=nan,
init_cash=100.0,
init_position=0.0,
init_price=nan,
cash_deposits=0.0,
cash_earnings=0.0,
cash_dividends=0.0,
signal_func_nb=no_signal_func_nb,
signal_args=(),
post_signal_func_nb=no_post_func_nb,
post_signal_args=(),
post_segment_func_nb=no_post_func_nb,
post_segment_args=(),
size=inf,
price=inf,
size_type=0,
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,
val_price=inf,
accumulate=0,
upon_long_conflict=0,
upon_short_conflict=0,
upon_dir_conflict=0,
upon_opposite_entry=4,
order_type=0,
limit_delta=nan,
limit_tif=-1,
limit_expiry=-1,
limit_reverse=False,
limit_order_price=-1,
upon_adj_limit_conflict=0,
upon_opp_limit_conflict=3,
use_stops=True,
stop_ladder=0,
sl_stop=nan,
tsl_stop=nan,
tsl_th=nan,
tp_stop=nan,
td_stop=-1,
dt_stop=-1,
stop_entry_price=-5,
stop_exit_price=-1,
stop_exit_type=0,
stop_order_type=0,
stop_limit_delta=nan,
upon_stop_update=0,
upon_adj_stop_conflict=1,
upon_opp_stop_conflict=1,
delta_format=1,
time_delta_format=1,
from_ago=0,
sim_start=None,
sim_end=None,
call_seq=None,
auto_call_seq=False,
ffill_val_price=True,
update_value=False,
fill_pos_info=True,
max_order_records=None,
max_log_records=0,
in_outputs=None
)
Simulate given a signal function.
Iterates in the column-major order. Utilizes flexible broadcasting.
signal_func_nb is a user-defined signal generation function that is called at each row and column (= element). It must accept the context of the type SignalContext and return 4 signals: long entry, long exit, short entry, and short exit.
post_signal_func_nb is a user-defined post-signal function that is called after an order has been processed. It must accept the context of the type PostSignalContext and return nothing.
post_segment_func_nb is a user-defined post-segment function that is called after each row and group (= segment). It must accept the context of the type SignalSegmentContext and return nothing.
from_signals_nb function¶
from_signals_nb(
target_shape,
group_lens,
index=None,
freq=None,
open=nan,
high=nan,
low=nan,
close=nan,
init_cash=100.0,
init_position=0.0,
init_price=nan,
cash_deposits=0.0,
cash_earnings=0.0,
cash_dividends=0.0,
long_entries=False,
long_exits=False,
short_entries=False,
short_exits=False,
size=inf,
price=inf,
size_type=0,
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,
val_price=inf,
accumulate=0,
upon_long_conflict=0,
upon_short_conflict=0,
upon_dir_conflict=0,
upon_opposite_entry=4,
order_type=0,
limit_delta=nan,
limit_tif=-1,
limit_expiry=-1,
limit_reverse=False,
limit_order_price=-1,
upon_adj_limit_conflict=0,
upon_opp_limit_conflict=3,
use_stops=True,
stop_ladder=0,
sl_stop=nan,
tsl_stop=nan,
tsl_th=nan,
tp_stop=nan,
td_stop=-1,
dt_stop=-1,
stop_entry_price=-5,
stop_exit_price=-1,
stop_exit_type=0,
stop_order_type=0,
stop_limit_delta=nan,
upon_stop_update=0,
upon_adj_stop_conflict=1,
upon_opp_stop_conflict=1,
delta_format=1,
time_delta_format=1,
from_ago=0,
sim_start=None,
sim_end=None,
call_seq=None,
auto_call_seq=False,
ffill_val_price=True,
update_value=False,
save_state=False,
save_value=False,
save_returns=False,
max_order_records=None,
max_log_records=0
)
Simulate given signals.
Iterates in the column-major order. Utilizes flexible broadcasting.
Note
Should be only grouped if cash sharing is enabled.
generate_stop_signal_nb function¶
Generate stop signal and change accumulation if needed.
holding_enex_signal_func_nb function¶
holding_enex_signal_func_nb(
c,
direction,
close_at_end,
adjust_func_nb=no_adjust_func_nb,
adjust_args=()
)
Resolve direction-aware signals for holding.
init_FSInOutputs_nb function¶
init_FSInOutputs_nb(
target_shape,
group_lens,
cash_sharing=False,
save_state=True,
save_value=True,
save_returns=True
)
Initialize FSInOutputs.
ls_signal_func_nb function¶
ls_signal_func_nb(
c,
long_entries,
long_exits,
short_entries,
short_exits,
from_ago,
adjust_func_nb=no_adjust_func_nb,
adjust_args=()
)
Get an element of direction-aware signals.
The direction is already built into the arrays. Best to use when the direction changes frequently (for example, if you have one indicator providing long signals and one providing short signals).
Prior to returning the signals, calls user-defined adjust_func_nb, which can be used to adjust stop values in the context. Must accept SignalContext and *adjust_args, and return nothing.
no_adjust_func_nb function¶
Placeholder adjustment function.
no_signal_func_nb function¶
Placeholder signal function that returns no signal.
order_signal_func_nb function¶
order_signal_func_nb(
c,
size,
price,
size_type,
direction,
min_size,
max_size,
val_price,
from_ago,
adjust_func_nb=no_adjust_func_nb,
adjust_args=()
)
Resolve direction-aware signals out of orders.
You must ensure that size, size_type, min_size, and max_size are writeable non-flexible arrays and accumulation is enabled.
prepare_fs_records_nb function¶
Prepare from-signals records.
resolve_dir_conflict_nb function¶
Resolve any direction conflict between a long entry and a short entry.
resolve_opposite_entry_nb function¶
resolve_opposite_entry_nb(
position_now,
is_long_entry,
is_long_exit,
is_short_entry,
is_short_exit,
upon_opposite_entry,
accumulate
)
Resolve opposite entry.
resolve_pending_conflict_nb function¶
Resolve any conflict between a pending signal and a user-defined signal.
Returns whether to keep the pending signal and execute the user signal.
resolve_signal_conflict_nb function¶
Resolve any conflict between an entry and an exit.
save_post_segment_func_nb function¶
signal_to_size_nb function¶
signal_to_size_nb(
position_now,
val_price_now,
value_now,
is_long_entry,
is_long_exit,
is_short_entry,
is_short_exit,
size,
size_type,
accumulate
)
Translate direction-aware signals into size, size type, and direction.