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



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


Log in to reply
 

Looks like your connection to Backtrader Community was lost, please wait while we try to reconnect.