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/

    MultiAssets resulting in broker.getposition() == "nan"

    General Discussion
    2
    6
    45
    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.
    • R
      randrade last edited by

      I'm trying to backtest with a large set of assets, which include new securities that go public and start trading after the start date of the simulation (and for which there are prices prior to the IPO date). Conversely, I also have shares that go private (or M&A) and cease to exist before the simulation ends.

      I was leaving the absent prices as "nan", but I realize that the broker cannot propertly calculate the position. So I decided to substitute the "nan"s for zeros, but then I got a division by zero error.

      (1) Is there any workaround to fix this situation?
      (2) On a separate note, how does backtrader treat positions in securities that cease to exist?

      I could not find any related posts, so please apologize me if this issue has already been brought up.

      PS: Eliminating the assets will not work, otherwise I would incur in selection bias. That is, I need to consider allocation within the entire pool of assets that existed at a certain point in time.

      R 1 Reply Last reply Reply Quote 0
      • R
        randrade @randrade last edited by

        @randrade said in MultiAssets resulting in broker.getposition() == "nan":

        (and for which there are prices prior to the IPO date).

        I meant to say: for which there are NO prices prior to the IPO date.

        1 Reply Last reply Reply Quote 0
        • R
          randrade last edited by

          ok, for item (1), apparently I just need to exclude empty data points in the CSVs I am importing. In other words, the first data point will be the first day of trading (or the previous day if I use the IPO price in it).

          Now I just need to figure out how to treat assets that cease to exist (e.g. delisted). In particular, how backtrader treats assets that cease to exist and happen to be allocated to the portfolio.

          1 Reply Last reply Reply Quote 0
          • R
            randrade last edited by

            Actually, I think deleting empty data points messes with the logic that determines the first day of the simulation. So disregard my previous comment.

            I am almost convinced at this point that there is no fix to the issues I mentioned.

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

              You need to remove data with no prices and use both next and prenext to code trade logic. IIRC you need to add longest data feed first,bit will get used as baseline data axis.

              Thus was discussed several times in the forum.

              1 Reply Last reply Reply Quote 0
              • R
                randrade last edited by

                I found 2 or 3 similar discussions, but the answer to them either :

                • pointed to a page that discusses handling of data with different frequencies (e.g. daily and weekly"), which is not exactly the issue at hand -- btw, for some reason frequency seems to be addressed as timeframe in the docs; or
                • were simply "read the documentation"

                I confess I was hoping to find easier instructions on how to procede, particularly since this is a fairly common setup for doing backtests with shares (i.e. some companies IPO during the simulation), and the first page of the documentation advertises its ease of use.

                I will continue doing some tests now that you mentioned that the order that you load the data matters, and will update if I achieve any progress.

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