IB API Close all Positions

Discussion in 'App Development' started by Mike805, Aug 21, 2012.

  1. crmorris

    crmorris


    use the activex/excel page for positions in the morning, collect opening day positions to insert in system, then use ONLY ats state positions (incrementing and decrementing during the day on fills, not querying ib server). if you have accurate updated positions, set up a vba clock in your main function:

    If time > #3:59:00 PM# Then
    #turn off enableevents, screenupdating, calculation
    closePositions
    turnATSOFF
    End If

    this routine grabs your orders, cancels them, and submits the orders you're talking about. if your system already submits orders, this should be trivially easy to implement. the key, however, is to make sure the system shuts off BEFORE you run your mass cancel / modify. you'd hate to have the system recursively react to your own shutdown actions.
     
    #11     Aug 24, 2012
  2. I'd say that the demo account is unlikely to work at all for this. You should log into your paper trading account. If you don't have an IB account, it's a little more difficult, ie not possible. Assuming you have an account, if you use paper trading account, the portfolio updates should work as expected.

    As for the functionality you talk about, it's all about knowing the positions you have and doing the opposite.
    This is what you need to know how to do:
    -Track your positions in quantity (via updatePortfolio, my previous code)
    -Send market and limit orders
    -Track your sent orders via order ID
    -Cancel your order with the assigned order ID

    Which of the above do you need help with?
     
    #12     Aug 25, 2012
  3. 2rosy

    2rosy

    how much would you pay?
     
    #13     Aug 25, 2012
  4. Thanks, I incorrectly said that its a demo account, it is a paper trading account.

    Sending orders is not an issue, I've been able to do that reliably for some time. What has been occuring over the last several years has been "timing" problems with the portfolio update.

    Let me explain better: the process will check a signal directory (on a timer) for any new signals. It will check for allocation. These two steps are done using VBA & cells in a separate excel sheet. Then, I will call accountUpdate (which should update the portfolio). Occasionally, I get nothing back, i.e. an empty portfolio, so I send no closing orders... hence my questions here.

    While I am gaining some understanding of the callback/event handling nature via this discussion (thanks everyone), I am at a loss as to how to guarantee the portfolio update event comes back with reliable data *before* I call the sendOrder() functions.

    I know the callbacks are reliable if done properly, so the problem lies behind the keyboard :)

    I wrote some test code to try to narrow down the issue, here is the code I am working with:

    Sub marketonclose()
    If Not (objTWSControl Is Nothing) Then
    If objTWSControl.m_isConnected Then
    Dim accountName As String
    accountName = Worksheets("Account").Range("A6").value
    ' clear account values
    Worksheets("Account").Range("A15:D300").ClearContents
    ' clear Portfolio sheet
    Call ThisWorkbook.Sheets(SHEET_NAME_PORTFOLIO).ClearPortfolioData_Click
    Call objTWSControl.m_TWSControl.reqAccountUpdates(True, accountName)
    End If
    End If
    Call sendclosing
    End Sub

    Sub sendclosing()
    Dim openPos, i As Integer
    Worksheets("Portfolio").Range("B3").Calculate
    openPos = Worksheets("Portfolio").Range("B3")
    For i = 1 To openPos - 1
    'send orders
    Next i
    End Sub


    The openPos variable will often times come back as 0 even though there are positions written in the Portfolio page. After I run just sendclosing() again, openPos is correct.

    So it seems that Worksheets("Portfolio").Range("B3").Calculate executes before all portfolio updates have occured.

    Mike
     
    #14     Aug 25, 2012
  5. Well, depends on the skills of the programmer. I do have an engineering back and understand S/W enough to be dangerous as can be seen by my questions on this thread :) .

    The key here would be integrating this functionality into the current VBA that has some decent functionality already built up; without breaking anything that already works.

    An experienced programmer should be able to code this out, test and debug it in under 2 hours at most. I'd pay 100/hour for that, but, the project would have to fully debugged.

    Another option would be to redo all I've built up properly in a better language (C# or something)... I'd do a project quote on that. You can PM me if you want more details, but, I'm not sure I'd want to rebuild all that's been done already just yet.
     
    #15     Aug 25, 2012
  6. 2rosy

    2rosy

    so you would pay $200 bucks. its not worth anyone's time to even consider it
     
    #16     Aug 25, 2012
  7. Lol...
    You obviously haven't checked odesk or other programmer sites. I can get a guy for 12/ hour from Hungary or elsewhere.

    100/ hour is a lot for something this small.That's being generous.
     
    #17     Aug 25, 2012
  8. neke

    neke

    Yes, you can get $12/hr for a programmer offshore, but they will need at least 2 hrs to have you explain what you want, talkless of getting IBs API and developing/debugging and getting used to them. Don't assume they have been working with the TWS and the API for ages.
     
    #18     Aug 25, 2012
  9. Exactly. My point is, 200 is a "test bid". Because I know what to look for, this small amount of risk will tell me what someone is capable of. I would never commit to a project without first seeing some work get done. No one would given the amount of money that might be involved and the future support required.

    This isn't a solicitation btw. I have too much to do and we are looking to eventually work with a good reliable programmer. Spending a couple hundred bucks to find out what someone can do is how I will approach it.

    The other option is to hire a cheaper guy, spend the 20 hours or so, and bring him up to speed as you mentioned.do note that so much has already been done and documented with tws, the excel api spreadsheet has all the functionality built and is free...

    We're not there yet though. For the moment i'm looking to debug a fairly straightforward issue.
     
    #19     Aug 25, 2012
  10. Hmm this is interesting.

    Im already running a fully automated system for IB TWS Excel (search my name for posts) of many strats, yet at times, i do see some of these orders not getting closed problems, though my system had already fired the order.

    It took me 9 months to build up this system, yet when I see the requirements u posted, it would take me at least, 2 months on a part time basis to be bug free. Not even $5000 i would consider spending, not spending but rather "devoting" my time to this (my system's turn around /day $1m)

    Anyway thanks for your idea and those who suggested the "get positions for acct". Befor markets close, I probably do a simple "Get positions" call, run thru the list and see which positions aren't closed yet and close them.

    :)
     
    #20     Sep 9, 2012