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/

    How does the code run in bt.strategy

    General Code/Help
    3
    6
    58
    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.
    • G
      Gleetche last edited by

      I am quite new to programming and backtrader, what I know is code runs from top to the bottom, I have very basic understanding of "calling" classes, loops and ifs conditions.

      My question is down below

      I have a strategy class that contains:

      1. def log(self, txt, dt=None): #Logging Function

      2. def next(self):

        - Self-coded ATR function to define self.atr
        - ```
           self.log('Close: %.2f , ATR:%.2f ,Trailing:%.2f'% (self.dataclose[0],(self.atr*1), 
           (self.dataclose[-1]-(self.atr*1))))
          ```
      

      3. def notify_order(self,order):

              self.log("LONG BUY EXECUTED,Price: %.2f, Quantity: %.2f, Cost: %.2f, Comm%.2f, ATR: %.2f" %
                               (order.executed.price,
                                (order.executed.value/
                                 order.executed.price),
                                order.executed.value,
                                order.executed.comm,
                                self.atr*1))
      

      4. def notify_trade(self, trade): #to log the pnl of the most recent trade completed

      .
      .
      Here is the structure:
      5f18a48d-f2d7-463c-941b-c5ce71a0cd75-image.png

      My problem here is
      why does "3. def notify_order(self,order):" gets executed first before "2. def next(self):", I have a self-coded atr function that calculates the ATR and when there is an ongoing order, it will print out the previous ATR.

      As you can see here the ATR on 2020-02-10 is 1.62,
      an order was created on that day and the order was filled in on the next day whic h is 2020-02-11. But the "LONG BUY EXECU....." prints out the ATR of 02-10, instead of 02-11.
      And you can also see that the actual atr of 02-11 was printed after the notify_order function which is 1.61.
      e96dec6b-de20-4748-b040-964af19436ea-image.png

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

        Please read backtrader docs from the very beginning bro the very end. All your questions are answered their.

        1 Reply Last reply Reply Quote 1
        • G
          Gleetche last edited by

          @ab_trader said in How does the code run in bt.strategy:

          Please read backtrader docs from the very beginning bro the very end. All your questions are answered their.

          I tried to a few times, but a lot of things I can't really understand.
          I've been learning through the quickstart guide and follow-along videos on youtube so I can learn by doing it.

          Can you point me out on which part of the docs will help with my answer?

          run-out A 2 Replies Last reply Reply Quote 0
          • run-out
            run-out @Gleetche last edited by

            @Gleetche said in How does the code run in bt.strategy:

            Can you point me out on which part of the docs will help with my answer?

            At this stage you need to invest the time to learn the software. Start here. https://www.backtrader.com/docu/concepts/ And walk your way forward using the built examples.

            Backtrader assumes a certain level of programming ability and doesn't attempt to teach you that. So you are going to have to grind it out. Be prepared for some stuff not to work, but once you've got the basic elements under your belt, come back and we'll get you going.

            Good luck!

            1 Reply Last reply Reply Quote 3
            • A
              ab_trader @Gleetche last edited by

              @Gleetche said in How does the code run in bt.strategy:

              Can you point me out on which part of the docs will help with my answer?

              Docs - Strategy

              Also Quickstart Guide gives full understanding of the bt workflow steps.

              Answering your question about ATR - notify_order() prints the last delivered ATR value, which is the value from the 02-10.

              1 Reply Last reply Reply Quote 2
              • G
                Gleetche last edited by

                thank you @ab_trader @run-out

                1 Reply Last reply Reply Quote 0
                • 1 / 1
                • First post
                  Last post
                Copyright © 2016, 2017, 2018 NodeBB Forums | Contributors
                $(document).ready(function () { app.coldLoad(); }); }