[docs]classBuyAndHoldStrategy(BaseStrategy):""" A simple Buy and Hold trading strategy. This strategy buys the specified symbol once at the first available market event and holds it for the duration of the backtest. :param symbol: The financial instrument symbol this strategy will trade. :param kwargs: Arbitrary keyword arguments passed to the base strategy. """def__init__(self,symbol:str,**kwargs:Any):super().__init__(symbol,**kwargs)self._bought_initial_position=Falselogging.info(f"BuyAndHoldStrategy initialized for {self.symbol}")
[docs]defon_market_event(self,event:MarketEvent):ifevent.symbol!=self.symbol:returnifnotself._bought_initial_position:self._put_signal_event(event.timestamp,Signal.BUY)self._bought_initial_position=Truelogging.info(f"BuyAndHoldStrategy: Initial BUY signal for {self.symbol} at {event.timestamp.date()}")else:# Hold the positionpass