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 do I track a position by system?

    General Code/Help
    4
    6
    5115
    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.
    • RandyT
      RandyT last edited by

      Realizing that if I have other systems entering positions on the same instrument, if self.position: is going to evaluate true en any system looking/using that instrument to trade.

      How do I constrain the system logic in cerebro.next() to consider only the positions for which this system is running/entering?

      I suspect I could do it with the historical trade .addinfo dict, but it occurs to me, is that info persisted as part of the trade info at the broker?

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

        I would set separate variable in strategy __init__ for each system for each data, and change it on the open/close order execution events. This is kind of parallel calculations.

        • 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
        1 Reply Last reply Reply Quote 0
        • RandyT
          RandyT last edited by

          The information I have about open positions is retrieved from IB when connecting to the live broker. Some of those open positions on IB were opened by different systems.

          So the information about what system opened that position needs to either be persisted through the IB API on order execution, or persisted locally so that restarts would still have some information about the system that opened that position.

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

            After you sent order to IB and it is executed, probably you receive confirmation of this order execution with size (maybe)? On this confirmation event you change your local variables.

            And on the connection to IB you can compare IB positions size on each security with summary of your local numbers for each security in order to avoid incorrect counting.

            Do you use notify_order method in te real time trading?

            • 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
            1 Reply Last reply Reply Quote 0
            • B
              backtrader administrators last edited by

              Although not the core of the discussion: self.position will evaluate to True only for an open position on self.data0. For other data feeds in the system self.getposition(specific_data) can be used.

              With regards to IB positions:

              • Positions carry no information per se.

              • Orders on the other hand have some fields that are intended to convey information and the field will persist in an order

                When creating an order via buy / sell / close, the remaining **kwargs will be used to fill fields in the order. This allows:

                • Overwriting values set by backtrader to for example create an OCO order
                • Set fields not set by the platform (like those conveying persistent information)
              • Trades are seen as a set of orders that have opened/increased a position and then reduced/closed it. The orders in a trade will still carry the persistent information.

                There isn't a get_trades API call (if there is, it has been completely missed) because a Trade is a logical artifact and not a thing

              With all that in mind:

              • Given an open position, there is no direct way to find out who created the position.

              • Given a list of orders (open/closed) with persistent information and setting a start date one could try to:

                • Recreate the trades and understand who opened what and when

              @skytrading54 is also looking into having orders retrieved: specifically the opens one if the system exits when an order is open, obviously to understand if the next trigger has to trigger something or be skipped because an open order is already awaiting execution, cancellation or expiry.

              Things to obviously look into.

              1 Reply Last reply Reply Quote 0
              • K
                kausality last edited by

                This is an old thread and I have bumped into this thing when thinking to take the strategy live.
                Here is a simple idea I can think:

                1. When an order is executed, save the details including the positons in nofity_order(). Position details can be saved in a csv file or database.

                2. When the next time strategy sends an order, see if it is already opened in IB. If it is already opened, then check the position size of the currently generated signal. Send an order only if the position of the current signal is more than that saved in local db.

                What are your thoughts? Has someone has a better alternative to the above sequence? Has something new been added in Backtrader which makes this simpler?

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