factory module¶
Factory for building signal generators.
The signal factory class SignalFactory extends IndicatorFactory to offer a convenient way to create signal generators of any complexity. By providing it with information such as entry and exit functions and the names of inputs, parameters, and outputs, it will create a stand-alone class capable of generating signals for an arbitrary combination of inputs and parameters.
SignalFactory class¶
A factory for building signal generators.
Extends IndicatorFactory with place functions.
Generates a fixed number of outputs (depending upon mode). If you need to generate other outputs, use in-place outputs (via in_output_names).
See FactoryMode for supported generation modes.
Other arguments are passed to IndicatorFactory.
Superclasses
Inherited members
- Base.chat
- Base.find_api
- Base.find_assets
- Base.find_docs
- Base.find_examples
- Base.find_messages
- Cacheable.get_ca_setup
- Chainable.chain
- Chainable.pipe
- Configured.copy
- Configured.equals
- Configured.get_writeable_attrs
- Configured.prettify
- Configured.replace
- Configured.resolve_merge_kwargs
- Configured.update_config
- HasSettings.get_path_setting
- HasSettings.get_path_settings
- HasSettings.get_setting
- HasSettings.get_settings
- HasSettings.has_path_setting
- HasSettings.has_path_settings
- HasSettings.has_setting
- HasSettings.has_settings
- HasSettings.reset_settings
- HasSettings.resolve_setting
- HasSettings.resolve_settings_paths
- HasSettings.set_settings
- IndicatorFactory.Indicator
- IndicatorFactory.attr_settings
- IndicatorFactory.class_docstring
- IndicatorFactory.class_name
- IndicatorFactory.config
- IndicatorFactory.custom_indicators
- IndicatorFactory.deregister_custom_indicator
- IndicatorFactory.find_smc_indicator
- IndicatorFactory.find_ta_indicator
- IndicatorFactory.find_technical_indicator
- IndicatorFactory.from_custom_techcon
- IndicatorFactory.from_expr
- IndicatorFactory.from_pandas_ta
- IndicatorFactory.from_smc
- IndicatorFactory.from_ta
- IndicatorFactory.from_talib
- IndicatorFactory.from_techcon
- IndicatorFactory.from_technical
- IndicatorFactory.from_wqa101
- IndicatorFactory.get_custom_indicator
- IndicatorFactory.get_indicator
- IndicatorFactory.in_output_names
- IndicatorFactory.input_names
- IndicatorFactory.lazy_outputs
- IndicatorFactory.list_builtin_locations
- IndicatorFactory.list_custom_indicators
- IndicatorFactory.list_custom_locations
- IndicatorFactory.list_indicators
- IndicatorFactory.list_locations
- IndicatorFactory.list_pandas_ta_indicators
- IndicatorFactory.list_smc_indicators
- IndicatorFactory.list_ta_indicators
- IndicatorFactory.list_talib_indicators
- IndicatorFactory.list_techcon_indicators
- IndicatorFactory.list_technical_indicators
- IndicatorFactory.list_vbt_indicators
- IndicatorFactory.list_wqa101_indicators
- IndicatorFactory.match_location
- IndicatorFactory.metrics
- IndicatorFactory.module_name
- IndicatorFactory.output_flags
- IndicatorFactory.output_names
- IndicatorFactory.param_names
- IndicatorFactory.parse_pandas_ta_config
- IndicatorFactory.parse_smc_config
- IndicatorFactory.parse_ta_config
- IndicatorFactory.parse_technical_config
- IndicatorFactory.plots_defaults
- IndicatorFactory.prepend_name
- IndicatorFactory.rec_state
- IndicatorFactory.register_custom_indicator
- IndicatorFactory.short_name
- IndicatorFactory.split_indicator_name
- IndicatorFactory.stats_defaults
- IndicatorFactory.subplots
- IndicatorFactory.with_apply_func
- IndicatorFactory.with_custom_func
- Pickleable.decode_config
- Pickleable.decode_config_node
- Pickleable.dumps
- Pickleable.encode_config
- Pickleable.encode_config_node
- Pickleable.file_exists
- Pickleable.getsize
- Pickleable.load
- Pickleable.loads
- Pickleable.modify_state
- Pickleable.resolve_file_path
- Pickleable.save
- Prettified.pprint
mode class property¶
Factory mode.
with_place_func method¶
SignalFactory.with_place_func(
entry_place_func_nb=None,
exit_place_func_nb=None,
generate_func_nb=None,
generate_ex_func_nb=None,
generate_enex_func_nb=None,
cache_func=None,
entry_settings=None,
exit_settings=None,
cache_settings=None,
jit_kwargs=None,
jitted=None,
**kwargs
)
Build signal generator class around entry and exit placement functions.
A placement function is simply a function that places signals. There are two types of it: entry placement function and exit placement function. Each placement function takes broadcast time series, broadcast in-place output time series, broadcast parameter arrays, and other arguments, and returns an array of indices corresponding to chosen signals. See generate_nb.
Args
entry_place_func_nb:callable-
place_func_nbthat returns indices of entries.Defaults to first_place_nb for
FactoryMode.Chain. exit_place_func_nb:callableplace_func_nbthat returns indices of exits.generate_func_nb:callable-
Entry generation function.
Defaults to generate_nb.
generate_ex_func_nb:callable-
Exit generation function.
Defaults to generate_ex_nb.
generate_enex_func_nb:callable-
Entry and exit generation function.
Defaults to generate_enex_nb.
cache_func:callable-
A caching function to preprocess data beforehand.
All returned objects will be passed as last arguments to placement functions.
entry_settings:dict- Settings dict for
entry_place_func_nb. exit_settings:dict- Settings dict for
exit_place_func_nb. cache_settings:dict- Settings dict for
cache_func. jit_kwargs:dict-
Keyword arguments passed to
@njitdecorator of the parameter selection function.By default, has
nogilset to True. jitted:any-
Gets applied to generation functions only. If the respective generation function is not jitted, then the apply function won't be jitted as well.
**kwargs- Keyword arguments passed to
IndicatorFactory.with_custom_func.
Note
Choice functions must be Numba-compiled.
Which inputs, parameters and arguments to pass to each function must be explicitly indicated in the function's settings dict. By default, nothing is passed.
Passing keyword arguments directly to the placement functions is not supported. Use pass_kwargs in a settings dict to pass keyword arguments as positional.
Settings dict of each function can have the following keys:
Attributes
pass_inputs:listofstr-
Input names to pass to the placement function.
Defaults to []. Order matters. Each name must be in
input_names. pass_in_outputs:listofstr-
In-place output names to pass to the placement function.
Defaults to []. Order matters. Each name must be in
in_output_names. pass_params:listofstr-
Parameter names to pass to the placement function.
Defaults to []. Order matters. Each name must be in
param_names. pass_kwargs:dict,listofstrorlistoftuple-
Keyword arguments from
kwargsdict to pass as positional arguments to the placement function.Defaults to []. Order matters.
If any element is a tuple, must contain the name and the default value. If any element is a string, the default value is None.
Built-in keys include:
input_shape: Input shape if no input time series passed. Default is provided by the pipeline ifpass_input_shapeis True.wait: Number of ticks to wait before placing signals. Default is 1.until_next: Whether to place signals up to the next entry signal. Default is True. Applied ingenerate_ex_func_nbonly.skip_until_exit: Whether to skip processing entry signals until the next exit. Default is False. Applied ingenerate_ex_func_nbonly.pick_first: Whether to stop as soon as the first exit signal is found. Default is False withFactoryMode.Entries, otherwise is True.temp_idx_arr: Empty integer array used to temporarily store indices. Default is an automatically generated array of shapeinput_shape[0]. You can also passtemp_idx_arr1,temp_idx_arr2, etc. to generate multiple.
pass_cache:bool-
Whether to pass cache from
cache_functo the placement function.Defaults to False. Cache is passed unpacked.
The following arguments can be passed to run and run_combs methods:
Args
*args- Can be used instead of
place_args. place_args:tuple- Arguments passed to any placement function (depending on the mode).
entry_place_args:tuple- Arguments passed to the entry placement function.
exit_place_args:tuple- Arguments passed to the exit placement function.
entry_args:tuple- Alias for
entry_place_args. exit_args:tuple- Alias for
exit_place_args. cache_args:tuple- Arguments passed to the cache function.
entry_kwargs:tuple- Settings for the entry placement function. Also contains arguments passed as positional if in
pass_kwargs. exit_kwargs:tuple- Settings for the exit placement function. Also contains arguments passed as positional if in
pass_kwargs. cache_kwargs:tuple- Settings for the cache function. Also contains arguments passed as positional if in
pass_kwargs. return_cache:bool- Whether to return only cache.
use_cache:any- Cache to use.
**kwargs- Default keyword arguments (depending on the mode).
For more arguments, see IndicatorBase.run_pipeline.
Usage
- The simplest signal indicator that places True at the very first index:
>>> from vectorbtpro import *
>>> @njit
... def entry_place_func_nb(c):
... c.out[0] = True
... return 0
>>> @njit
... def exit_place_func_nb(c):
... c.out[0] = True
... return 0
>>> MySignals = vbt.SignalFactory().with_place_func(
... entry_place_func_nb=entry_place_func_nb,
... exit_place_func_nb=exit_place_func_nb,
... entry_kwargs=dict(wait=1),
... exit_kwargs=dict(wait=1)
... )
>>> my_sig = MySignals.run(input_shape=(3, 3))
>>> my_sig.entries
0 1 2
0 True True True
1 False False False
2 True True True
>>> my_sig.exits
0 1 2
0 False False False
1 True True True
2 False False False
- Take the first entry and place an exit after waiting
nticks. Find the next entry and repeat. Test three differentnvalues.
>>> from vectorbtpro.signals.factory import SignalFactory
>>> @njit
... def wait_place_nb(c, n):
... if n < len(c.out):
... c.out[n] = True
... return n
... return -1
>>> # Build signal generator
>>> MySignals = SignalFactory(
... mode='chain',
... param_names=['n']
... ).with_place_func(
... exit_place_func_nb=wait_place_nb,
... exit_settings=dict(
... pass_params=['n']
... )
... )
>>> # Run signal generator
>>> entries = [True, True, True, True, True]
>>> my_sig = MySignals.run(entries, [0, 1, 2])
>>> my_sig.entries # input entries
custom_n 0 1 2
0 True True True
1 True True True
2 True True True
3 True True True
4 True True True
>>> my_sig.new_entries # output entries
custom_n 0 1 2
0 True True True
1 False False False
2 True False False
3 False True False
4 True False True
>>> my_sig.exits # output exits
custom_n 0 1 2
0 False False False
1 True False False
2 False True False
3 True False True
4 False False False
- To combine multiple iterative signals, you would need to create a custom placement function. Here is an example of combining two random generators using "OR" rule (the first signal wins):
>>> from vectorbtpro.indicators.configs import flex_elem_param_config
>>> from vectorbtpro.signals.factory import SignalFactory
>>> from vectorbtpro.signals.nb import rand_by_prob_place_nb
>>> # Enum to distinguish random generators
>>> RandType = namedtuple('RandType', ['R1', 'R2'])(0, 1)
>>> # Define exit placement function
>>> @njit
... def rand_exit_place_nb(c, rand_type, prob1, prob2):
... for out_i in range(len(c.out)):
... if np.random.uniform(0, 1) < prob1:
... c.out[out_i] = True
... rand_type[c.from_i + out_i] = RandType.R1
... return out_i
... if np.random.uniform(0, 1) < prob2:
... c.out[out_i] = True
... rand_type[c.from_i + out_i] = RandType.R2
... return out_i
... return -1
>>> # Build signal generator
>>> MySignals = SignalFactory(
... mode='chain',
... in_output_names=['rand_type'],
... param_names=['prob1', 'prob2'],
... attr_settings=dict(
... rand_type=dict(dtype=RandType) # creates rand_type_readable
... )
... ).with_place_func(
... exit_place_func_nb=rand_exit_place_nb,
... exit_settings=dict(
... pass_in_outputs=['rand_type'],
... pass_params=['prob1', 'prob2']
... ),
... param_settings=dict(
... prob1=flex_elem_param_config, # param per frame/row/col/element
... prob2=flex_elem_param_config
... ),
... rand_type=-1 # fill with this value
... )
>>> # Run signal generator
>>> entries = [True, True, True, True, True]
>>> my_sig = MySignals.run(entries, [0., 1.], [0., 1.], param_product=True)
>>> my_sig.new_entries
custom_prob1 0.0 1.0
custom_prob2 0.0 1.0 0.0 1.0
0 True True True True
1 False False False False
2 False True True True
3 False False False False
4 False True True True
>>> my_sig.exits
custom_prob1 0.0 1.0
custom_prob2 0.0 1.0 0.0 1.0
0 False False False False
1 False True True True
2 False False False False
3 False True True True
4 False False False False
>>> my_sig.rand_type_readable
custom_prob1 0.0 1.0
custom_prob2 0.0 1.0 0.0 1.0
0
1 R2 R1 R1
2
3 R2 R1 R1
4