call a function every day before the trading section begin
-
Dear Sir / Madam,
I want to achieve a goal like: call a function (e.g, stock selection function) every day before trading and also call another function every day after the trading section. This is a common feature in some trading platform
How can I achieve this goal in backtrader?
Thanks
Ted Zeng
-
Regular
strategy.next()
method is called after each bar close. If it will be the last bar of the current session, then this method is called after trading session. Also at the same time this call can be considered as a before the the next trading session call. -
Germans say: "Nach dem Spiel ist vor dem Spiel" (which literally translated means: "After the game is before the game"), which basically summarizes the behavior described by @ab_trader.
The real question here is whether the question can be answered literally or part of the original question has been lost in translation. For example
... every day before trading ...
Does this mean "before each bar is going to be evaluated" or "every day before the trading session begins"?
The difference being that the second option will not be triggered on each bar, but only before session starts. (This will be the same if the bars are daily, but won't be if one is using intraday bars)
.. .call another function every day after the trading section ...
Is this really section or it was meant as session? The implications would be the same as in the question before.
-
@backtrader Sorry for the unclear describtion. I mean "every day before the trading session begins" when using the intraday bars.
Actually, I want the fuction as in zipline.
before_trading_start(context, data)
Optional. Called daily prior to the open of market. Orders cannot be placed inside this method. The primary purpose of this method is to use Pipeline to create a set of securities that your algorithm will use.Parameters
context: Same context object as in handle_data.
data: Same data object in handle_data.Thanks a lot.
-
@ab_trader
Actually, I want the fuction as in zipline when using the intraday bars.before_trading_start(context, data)
Optional. Called daily prior to the open of market. Orders cannot be placed inside this method. The primary purpose of this method is to use Pipeline to create a set of securities that your algorithm will use. -
Understand. I am not really familiar with zipline. It looks complex for me. Probably @backtrader can add such function.
To simulate such behavior now I would set
if
condition in thestrategy.next ()
to check date and time of the bar. If the bar date and time corresponds to time of the last bar of the previous day, than you perform your stock selection actions. It can be a separate function inside strategy, which is called instrategy.next ()
. -
@ab_trader Many Thanks