Backtrader Community

    • 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/

    call a function every day before the trading section begin

    General Code/Help
    strategy
    3
    7
    2467
    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.
    • T
      Ted last edited by

      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

      1 Reply Last reply Reply Quote 0
      • A
        ab_trader last edited by

        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.

        • If my answer helped, hit reputation up arrow at lower right corner of the post.
        • Python Debugging With Pdb
        • New to python and bt - check this out
        T 1 Reply Last reply Reply Quote 0
        • B
          backtrader administrators last edited by

          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.

          T 1 Reply Last reply Reply Quote 0
          • T
            Ted @backtrader last edited by

            @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.

            1 Reply Last reply Reply Quote 0
            • T
              Ted @ab_trader last edited by

              @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.

              1 Reply Last reply Reply Quote 0
              • A
                ab_trader last edited by ab_trader

                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 the strategy.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 in strategy.next ().

                • If my answer helped, hit reputation up arrow at lower right corner of the post.
                • Python Debugging With Pdb
                • New to python and bt - check this out
                T 1 Reply Last reply Reply Quote 0
                • T
                  Ted @ab_trader last edited by

                  @ab_trader Many Thanks

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