For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

Setting holding period



  • Hi, I'm new to backtrader and have been looking for solutions on preset holding period while backtesting.

    Here is what I'm looking to achieve:

    Suppose I have a strategy that says, go long a stock if condition A happens and go short a stock if condition B happens. For each position, the holding period of the trade is 1 month.

    Is there an easy way to do that?

    Thanks!


  • administrators

    The logic in the strategy has to remember when a position was entered, probably in notify_order by checking the date from the notified order

    • order.executed.dt which can be translated to a datetime instance like this for example: order.data.num2date(order.executed.dt)`

    And then checking when the data hits a month later self.data.datetime, where you can get instances with self.data.datetime.datetime() for example.