Distinguish open position in IB for different bots (strategies)

Discussion in 'Order Execution' started by ac02nwt, May 25, 2022.

  1. ac02nwt

    ac02nwt

    Hi all,

    I am building a bot to send order to IB for trade with Python. In order to run multiple strategies, i am running multiple bots in which every bot will be assigned different OrderRef to place order to IB. But when i want to square the open position, how can i identify which open position belongs to which strategies? I tried reqPositions but the information is insufficient to differentiate. It seems that there is reqPositionsMulti, but i don't know how to use it. (What do reqId, modelCode mean? Appreciate if someone can share an example to illustrate) and I am not sure if this "reqPositionsMulti" can help.

    def positionMulti(self, reqId: int, account: str, modelCode: str,contract: Contract, pos: float, avgCost: float): super().positionMulti(reqId, account, modelCode, contract, pos, avgCost)
     
  2. M.W.

    M.W.

    You can attach a ref string, forgot the parameter name. Check out the documentation.

     

  3. Place order with Order(.... modelCode = 'model_name', ....)

    Get positions back with positionMulti(...., modelCode = 'model name', ....)


    GAT
     
    M.W. likes this.
  4. M.W.

    M.W.

    Right, apologies, I did not read the question carefully, it was about positions not the orders...

     
  5. ac02nwt

    ac02nwt

    Thanks for your advice. Let me try.
     
  6. M.W.

    M.W.

    @globalarbtrader,

    Could you help me please with an issue I experience with reqPositionsMulti?

    I do not seem to be able to retrieve positions with different model codes. I submit orders via API and attach a string to the modelCode parameter. When I request reqPositionsMulti with an empty modelCode string I get all positions back but modelCode is an empty string for each returned position. When I query reqPositionsMulti with the same modelCode used when I submitted orders then I get an error message 321 "Error Code: 321 - Message: Error validating request.-'bj' : cause - Model name 'ALL' is incorrect. Valid model name: ''; 'Core'; "

    My goal is to tag different orders to be allocated to different positions even though the symbol is the same. That, I thought, is the purpose of reqPositionsMulti. One strategy goes long 100 asset1 and another position goes short 100 asset1. My goal is to get positions (with their avgWeightedCost) back for each strategy, not just a net 0 position.

    Am I misunderstanding reqPositionsMulti?