For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See:

How do I track a position by system?

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

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

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

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

  • administrators

    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.

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

Log in to reply