Navigation

    Backtrader Community

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    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

    General Code/Help
    2
    2
    861
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Y
      yjy last edited by

      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!

      1 Reply Last reply Reply Quote 0
      • B
        backtrader administrators last edited by

        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.

        1 Reply Last reply Reply Quote 0
        • 1 / 1
        • First post
          Last post
        Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors