@@ -102,13 +102,17 @@ class Execution(NamedTuple):
102102 execution of ``fn``.
103103 indicator_names: Names of :class:`pybroker.indicator.Indicator`\ s
104104 used for execution of ``fn``.
105+ args: Additional positional arguments for ``fn``.
106+ kwargs: Additional keyword arguments for ``fn``.
105107 """
106108
107109 id : int
108110 symbols : frozenset [str ]
109111 fn : Optional [Callable [[ExecContext ], None ]]
110112 model_names : frozenset [str ]
111113 indicator_names : frozenset [str ]
114+ args : tuple [Any , ...] = tuple ()
115+ kwargs : tuple [tuple [str , Any ], ...] = tuple ()
112116
113117
114118class BacktestMixin :
@@ -186,6 +190,8 @@ def backtest_executions(
186190 pending_order_scope = PendingOrderScope ()
187191 exec_ctxs : dict [str , ExecContext ] = {}
188192 exec_fns : dict [str , Callable [[ExecContext ], None ]] = {}
193+ exec_args : dict [str , tuple [Any , ...]] = {}
194+ exec_kwargs : dict [str , tuple [tuple [str , Any ], ...]] = {}
189195 for sym in test_syms :
190196 for exec in executions :
191197 if sym not in exec .symbols :
@@ -203,6 +209,8 @@ def backtest_executions(
203209 sym_end_index = sym_end_index ,
204210 session = sessions [sym ],
205211 )
212+ exec_args [sym ] = exec .args
213+ exec_kwargs [sym ] = exec .kwargs
206214 if exec .fn is not None :
207215 exec_fns [sym ] = exec .fn
208216 sym_exec_dates = {
@@ -311,7 +319,11 @@ def backtest_executions(
311319 before_exec_fn (active_ctxs )
312320 for sym , ctx in active_ctxs .items ():
313321 if sym in exec_fns :
314- exec_fns [sym ](ctx )
322+ exec_fns [sym ](
323+ ctx ,
324+ * exec_args .get (sym , ()),
325+ ** dict (exec_kwargs .get (sym , ())),
326+ )
315327 if after_exec_fn is not None and active_ctxs :
316328 after_exec_fn (active_ctxs )
317329 for ctx in active_ctxs .values ():
@@ -896,6 +908,8 @@ def add_execution(
896908 symbols : Union [str , Iterable [str ]],
897909 models : Optional [Union [ModelSource , Iterable [ModelSource ]]] = None ,
898910 indicators : Optional [Union [Indicator , Iterable [Indicator ]]] = None ,
911+ * args : Any ,
912+ ** kwargs : Any ,
899913 ):
900914 r"""Adds an execution to backtest.
901915
@@ -910,6 +924,8 @@ def add_execution(
910924 indicators: :class:`Iterable` of
911925 :class:`pybroker.indicator.Indicator`\ s to compute for
912926 backtesting.
927+ args: Positional arguments passed to ``fn``.
928+ kwargs: Keyword arguments passed to ``fn``.
913929 """
914930 symbols = (
915931 frozenset ((symbols ,))
@@ -978,6 +994,8 @@ def add_execution(
978994 fn = fn ,
979995 model_names = model_names ,
980996 indicator_names = ind_names ,
997+ args = args ,
998+ kwargs = tuple (sorted (kwargs .items ())),
981999 )
9821000 )
9831001
0 commit comments