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/

    Buy and Hold example - monthly cash being added twice?

    General Code/Help
    2
    4
    41
    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
      gretzteam last edited by

      From the buy and hold blog post:
      Buy and Hold

      Why is the self.p.monthly_cash added first to self.broker.add, and then again to calculate the target_value for the current order? Isn't this adding the cash twice?

          def notify_timer(self, timer, when, *args, **kwargs):
              # Add the influx of monthly cash to the broker
              self.broker.add_cash(self.p.monthly_cash)
      
              # buy available cash
              target_value = self.broker.get_value() + self.p.monthly_cash
              self.order_target_value(target=target_value)
      
      run-out 1 Reply Last reply Reply Quote 0
      • run-out
        run-out @gretzteam last edited by

        @gretzteam I believe you are adding the cash twice. First using:

        self.broker.add_cash(self.p.monthly_cash)

        Then here:

        self.broker.get_value() + self.p.monthly_cash

        get_value will have the first cash addition in it.

        G 1 Reply Last reply Reply Quote 0
        • G
          gretzteam @run-out last edited by

          @run-out This is what I thought but being new to backtrader I wasn't sure.
          I took this code from the documentation here, maybe it needs to get updated?

          run-out 1 Reply Last reply Reply Quote 0
          • run-out
            run-out @gretzteam last edited by

            @gretzteam No my error, get_value is returning the value of a datas, or if no datas provide then all the market value of the datas. Backtrader's blog is correct. See the comments here

            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(); }); }