C# TWS API - get position information

Discussion in 'App Development' started by rodras, Jun 25, 2020.

  1. rodras

    rodras

    Hi,

    I'm building a c# api program to create a montage more friendly than TWS's interface. However, as a novice programmer I'm hitting some walls.

    The latest one is how to get the position information, namely the size and average cost, for the contract selected in the montage (combobox, or textbox).
    I know the IBapi "updateportfolio" and "position" methods returns the data I'm looking for, but I my knowledge only allows me to fetch all contracts (including closed positions) and as strings, instead of returning a value for each data, by the selected contract.

    In youtube there is an example of what I want to achieve, but it using an older TWS API version (AxTWSLib._DtwsEvents_updatePortfolioExEvents).

    For now, I'm getting the data throw a data grid view, but I believe it can be achieved thru a simple code.

    I would appreciate any help on this.

    Thanks.
     
    Last edited: Jun 25, 2020
    Topper16 and pstrusi like this.
  2. RedDuke

    RedDuke

    Why reinvent the wheel. Just use IB gateway and NinjaTrader. Ninja is C# based and you can code anything you want there.
     
  3. SteveH

    SteveH

    See if any of these tutorials help you:

    http://holowczak.com/ib-api-tutorials-by-programming-language/
     
    rodras likes this.
  4. rodras

    rodras

    Thanks Steve,

    I've already run through those tutorials. Unfortunately, they are based on older API versions and I'm not able - do not have the required programming skills - to adapt them to the current API.
     
  5. I'm not sure whether I understand your question correctly. IB can send you a complete portfolio overview, giving you all open positions. But this is not what you want? You want to ask IB's server to give the open position details for a single symbol?
     
  6. rodras

    rodras

    Hi HobbyTradding,

    Yes, I want to get the position just for the (single) symbol selected in the montage.
     
  7. I don't think that such a function/method is available in the API. You can only request all open positions. You would then have to store the returned information and do the filtering to find the desired symbol yourself.

    http://interactivebrokers.github.io...Client.html#ab262cf5601e57d6612d3df5e821fca9e
     
    rodras likes this.
  8. pstrusi

    pstrusi


    I'm just another trader, not a programmer, with some experience developing Algos in Ninjatrader. Despite not being the same platform, here there's some procedure in C# that perhaps may inspire some idea. The basic idea is this:


    foreach (var instrument in BarsArray)
    {
    foreach (Account acct in Cbi.Globals.Accounts)
    {
    if (acct.Positions != null)
    {
    PositionCollection positions = acct.Positions;
    foreach (Position pos in positions)
    {
    if (BarsInProgress == 0 && pos.Account.Name == "XXXXX98" && pos.Instrument.FullName == "MES 03-20")
    {
    if ( Position.MarketPosition == MarketPosition.Flat && Position.Quantity == 0 )
    {
    if ( (pos.MarketPosition != MarketPosition.Flat || pos.Quantity != 0) )
    {
    //Do something
    }
    }
    else
    {
    if ( Position.MarketPosition != pos.MarketPosition || Position.Quantity != pos.Quantity )
    {
    //Do something
    }
    }
    }
    }
    }
    }
    }
     
    rodras likes this.
  9. rodras

    rodras

    Thanks pstrusi for the code.
    After several hours around this issue I ended by settling with a datagridview solution to get the position value I needed.
     
    pstrusi likes this.
  10. Topper16

    Topper16

    Hello Rodras,

    Happy to see this post, I have the same issue, I'm trying to use the function self.reqAccountUpdates(True, self.account) or self.reqPositions() in Python and it always returns all the positions together, I tried to filter the output, but was not successful!!

    Any chance to share your thoughts on how you solved it? my end goal is to return the position of the contract I want, let's say AAPL, I will need the position (qty) and AvgCost variable to use in another function

    I appreciate your help
     
    #10     May 12, 2021