Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How do I read IB Account and set NT Account for Strategy startup?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    How do I read IB Account and set NT Account for Strategy startup?

    Hi. I have an Interactive Brokers account. I have live positions there. I am constantly either restarting IB TWS or my strategy each day

    I cringe every time I do this because

    1) either NT sells my position, and then repurchases it
    2) I lose all my real time trades (on startup NT NEVER puts the trades back in the right time).
    3) it does something else I didn't want it to do and it is costing me money.

    ISSUE: I need better control over what NT does on start up. I really don't care what NT THINKS my position is internally, what my ACTUAL position is is what is in the IB Account - period, full stop.

    ACTION I NEED HELP WITH:
    1) NT needs to be able to read the IB Account on start up and setup some internal strategy variables for open trades
    a) Strategy tells the NT Account what the position REALLY is - IE: Long SPXL, 5000 shares, at 88.33 purchase price
    b) Strategy read the stop information if there is a stop in place, then set that up in NT Account
    c) Strategy read the current profit or loss on the existing trade, then set that up in NT Account
    d) read the current account profit or loss on the DAY in IB of CLOSED TRADES, then set that up in NT Account

    QUESTION 1: How do I do this?
    QUESTION 2: What settings in NT/Strategy will allow me to do this? IE: Wait until flat, sync = false?

    NOTE: I want to use MANAGED trading
    Last edited by DaFish; 12-31-2015, 01:26 PM.

    #2
    Are you familiar with the new Adopt Account Position feature in NinjaTrader 8? I believe this may solve the issue you've been running into.

    First, let's separate the issue into two parts. The first part is the need for NinjaTrader to recognize and adopt your current position and open orders when connecting to your IB account. This should happen automatically. Assuming you have placed an order and entered a position in TWS, then when you launch NinjaTrader, you should see the position on your account, and the order in the Orders tab. Please let me know if you find either of these things is not working as expected.

    The second part would be your strategy actually recognizing and adopting what NinjaTrader has pulled from IB in terms of open positions and resting orders. This is where the Adopt Account Position feature comes into play. First, you must "enable" this by toggling a boolean property in your code: IsAdoptAccountPositionAware.


    http://ninjatrader.com/support/helpGuides/nt8/en-us/?isadoptaccountpositionaware.htm


    Then, in the Strategies window, you will see a new option for "Start Behavior" labeled "Adopt Account Position." This should do exactly what you are looking for -- it will adopt your account position as your strategy position when the strategy is enabled.
    Dave I.NinjaTrader Product Management

    Comment


      #3
      Hi Dave. I am testing this today on the IB paper trading account.

      I am getting inconsistent results. It thinks I am long (which the account is NOT), then I get an exit signal, so it SHORTS the market.

      Can you assist?

      Comment


        #4
        Adopt account position

        This setting should be used if you would like your strategy to disregard the historical virtual Strategy Position and to start in the same position as the real-world Account Position.


        Right, I don't care about NT Strategy, or the past buys/sells... I only care about what is in the account and having my strategy read it and manage the exit on startup. If my strategy at REAL TIME has a buy order, I want that to be either MATCHED to the account, or a new order generated to buy.

        On startup the strategy will begin executing orders immediately.

        Perfect

        • Any active orders on the account previously generated by the strategy that does not match* an active strategy order will be cancelled.

        Does this mean that any order on the ACCOUNT that doesn't match the strategy will be SOLD automatically? So If I had 1000 shares of SPY on the account, the strategy is trying to trade 100 shares, it would sell all 1000?

        I want the strategy to manage 100 SPYs if that is what I put in the strategy parameters.

        I don't want NT to CANCEL ANY orders or SELL ANY orders in the account. I'll do that if I want to.

        Should the strategy be unable to cancel and receive confirmation on the cancellation of these orders within 40 seconds the strategy will not start and an alert will be issued.

        Well, the strategy should NOT CANCEL any thing...


        • The matching active orders on the account will then be mapped to the active strategy orders

        OK. If my strategy decided it was going LONG, then it should match the order. If it doesn't match, then it shouldn't just quit.

        QUESTION: HOW DO WE READ what is there, and then manage it in my code on startup ?


        • Any remaining active strategy orders that cannot be successfully paired will be submitted live and the strategy will then try to sync your Account Position to your Strategy Position through the process below.

        • Only one strategy with this setting can be started at a time for an individual account and instrument.


        • The account and instrument the strategy is started on must not have any working orders which were submitted outside of the strategy. If an order is detected, the strategy can not be started until these orders have been manually managed.

        So if I have a limit buy order, I must remove those or the strategy won't start? I could live with that.

        Note: Adopt account position will only be available if the developer of a strategy has programmed the strategy to be aware of the real-world account position.

        Can you provide a sample strategy that will read the values and SYNC the Strategy to the account?

        I have to tell you, I am spending SO MUCH TIME fighting with NT to get it to do what I want I am not focusing on the market to make money....

        SIMPLY PUT - HERE IS WHAT I NEED
        1) the ACCOUNT is real. IF I select to Sync, then I want some sample code to read the current ACCOUNT positions and then I can decide to manage it or leave it on strategy start - that way I can read all the internal values like position average price and put that in my strategy.

        - I need a way to get my strategy in SYNC.

        2) If the ACCOUNT is long, and the strategy says BUY, then MATCH the order ONLY, if the strategy says 100 shares, and there are 1000, then match 100 (DON't sell it then buy the same thing again)

        3) If the ACCOUT is LONG and the strategy says SELL - and I selected SYNC - then SELL it, if I said wait for flat, then leave it and I'll manage it

        4) If I am restarting - which I do very often it seems - I only care about the current trade - whether it is going long or short and what to do with it.

        5) I would like to download the trade history on the ACCOUNT to build the performance data

        Comment


          #5
          Hello,


          Does this mean that any order on the ACCOUNT that doesn't match the strategy will be SOLD automatically? So If I had 1000 shares of SPY on the account, the strategy is trying to trade 100 shares, it would sell all 1000?
          It would depend, are you referring to a Position of 1000 or an Order for 1000? There would be a difference as a working Order for 1000 would prevent the strategy from starting per your later note: The account and instrument the strategy is started on must not have any working orders which were submitted outside of the strategy. If an order is detected, the strategy can not be started until these orders have been manually managed.
          If this was a manual Order of 1000, that would not be cancelled but would prevent the strategy from starting, an actual Position of 1000 would instead be assumed by the strategy.

          I want the strategy to manage 100 SPYs if that is what I put in the strategy parameters
          Are you referring to the Entries per direction or Quantity? The Entries PerDirection would control the total entries allowed per direction, otherwise if you are referring to Quantity, this would be controlled by the quantities you use in your Entry statements. Can you clarify this question?
          I don't want NT to CANCEL ANY orders or SELL ANY orders in the account. I'll do that if I want to.
          Orders would only be cancelled if they directly apply to the requirements in the sync settings page or if your logic calls for it. If the order is outside of that the order should remain working based on the order type/settings/broker/market.

          Should the strategy be unable to cancel and receive confirmation on the cancellation of these orders within 40 seconds the strategy will not start and an alert will be issued.
          Well, the strategy should NOT CANCEL any thing...
          This would apply in general to all of the sync settings or in the case a strategy in general cannot cancel the orders within 40 seconds, it would be disabled. In the case the strategy cannot communicate or get a response back from the exchange on the state of the orders in a timely manner, there would not be a good reason for the strategy to continue working if it cannot successfully modify orders.

          QUESTION: HOW DO WE READ what is there, and then manage it in my code on startup ?
          What specifically are you referring to reading? I am not sure what specifically you are trying to access or read the values of, could you provide more details on this?

          Can you provide a sample strategy that will read the values and SYNC the Strategy to the account?
          I don’t believe we have a fully documented sample of this at this point, the overall structure and usage would be no different than a normal strategy though. The main differences would be that you need to specify this strategy can use AdoptAccountPosition: http://ninjatrader.com/support/helpG...itionaware.htm and that you choose a start behavior: http://ninjatrader.com/support/helpG...unt%2Bposition

          SIMPLY PUT - HERE IS WHAT I NEED
          1) the ACCOUNT is real. IF I select to Sync, then I want some sample code to read the current ACCOUNT positions and then I can decide to manage it or leave it on strategy start - that way I can read all the internal values like position average price and put that in my strategy.
          What you are asking for would not be a capability of the AdoptAccountPosition, this only works initially and is an internal check, what you are asking here would be for a check to happen after the strategy is already enabled or has reached your code. For this you would likely need to use undocumented approaches related to accessing the Account object instead of using sync options. This is not a topic I could provide much detail on as there is no documentation in using the account in this way.

          2) If the ACCOUNT is long, and the strategy says BUY, then MATCH the order ONLY, if the strategy says 100 shares, and there are 1000, then match 100 (DON't sell it then buy the same thing again)
          If you are using the account sync, there should not be a point in which the account is Long but the strategy is flat or sees you need to be in a long position. In the case you are not using any syncing this would need to be accounted for in your logic as there are no sync after the start options.

          3) If the ACCOUT is LONG and the strategy says SELL - and I selected SYNC - then SELL it, if I said wait for flat, then leave it and I'll manage it
          In this case, if you select WaitForFlat the strategy could not trade until your account was flat, so this seems to already do what you are asking here. Could you provide more details on this question as it seems this is already a feature?

          Code:
          4) If I am restarting - which I do very often it seems - I only care about the current trade - whether it is going long or short and what to do with it.
          I am not certain on what the question was here; could you provide more detail on what you are asking specifically?

          5) I would like to download the trade history on the ACCOUNT to build the performance data
          This would not be a option of downloading an entire accounts history, you can access the strategies trades though using the TradeCollection.


          Please let me know if I may be of further assistance.
          JesseNinjaTrader Customer Service

          Comment


            #6
            Hi Jesse. Thanks for the detailed reply. In Short.

            I can't use NT8 Beta 8 to autotrade for a variety of reasons. I am not getting the correct action and I don't know what to set things too to fix this.

            How do I do this:

            1) I don't want the strategy on start up to do anything. Once I get to REAL TIME, then I want to do the following:

            QUESTION: Where in my code do I program these events to do this? On Real time transition? Should I make sure my code has made it to that point before issuing buys?

            2) READ the ACCOUNT value - grab the current positions on the ACCOUNT - of the security that is on the chart I have the strategy on

            3) SET the INTERNAL Ninjatrader strategy positions to what I want them to be. Currently, these are NOT correct in the Control Panel - Positions - this is an ERROR see picture attached. I need to fix this or the strategy will do all sorts of wacky things.

            I had a private support ticket in (not a forum discussion thread), but NT8 Beta 8 Control Panel ->Strategy positions thought I was long and short the same security, but when I read the Positions inside the strategy, it only sees the Long position. NT8 is out of sync with reality, specifically the ACCOUNT position. This is a major issue and the reason I can't use NT8 to trade automatically right now. I have tried all sorts of settings to try to fix that, but Adopt Account position, Sync, don't sync, wait until flat... all seem to get out of sync with reality and I can't use the product.


            QUESTION 1: What settings do I have to make on the NT Options and on the strategy to do this ?

            Originally posted by NinjaTrader_Jesse View Post
            Are you referring to the Entries per direction or Quantity? The Entries PerDirection would control the total entries allowed per direction, otherwise if you are referring to Quantity, this would be controlled by the quantities you use in your Entry statements. Can you clarify this question?
            See item #3 above. There is an error where NT gets out of sync with itself and the ACCOUNT.

            Originally posted by NinjaTrader_Jesse View Post
            What specifically are you referring to reading? I am not sure what specifically you are trying to access or read the values of, could you provide more details on this?
            I am trying to READ the ACCOUNT positions and SET the NT Strategy positions correctly. Right now, they are wacked and out of sync.


            Originally posted by NinjaTrader_Jesse View Post
            I don’t believe we have a fully documented sample of this at this point, the overall structure and usage would be no different than a normal strategy though. The main differences would be that you need to specify this strategy can use AdoptAccountPosition:
            I think we need to work on a sample together with this community. This is far too complicated to not have a sample

            Originally posted by NinjaTrader_Jesse View Post
            If you are using the account sync, there should not be a point in which the account is Long but the strategy is flat or sees you need to be in a long position. In the case you are not using any syncing this would need to be accounted for in your logic as there are no sync after the start options.
            Well, I am seeing this happen - see pic attached

            Originally posted by NinjaTrader_Jesse View Post
            In this case, if you select WaitForFlat the strategy could not trade until your account was flat, so this seems to already do what you are asking here. Could you provide more details on this question as it seems this is already a feature?
            Wait until FLAT doesn't work well either because I check for Market positions being LONG or FLAT, and the control panel has INACCURATE values for positions and it is throwing my strategy in to a tizzy.


            Originally posted by NinjaTrader_Jesse View Post
            This would not be a option of downloading an entire accounts history, you can access the strategies trades though using the TradeCollection.
            TradeCollection is not correct. If I reload my strategy, it will get different results than when I run it live due to intraday data. I really need to read the ACCOUNT trade history and build that. Obviously NT8 can read the IB Account, why can't we get some methods to do that? Even if it was just for the day's trading.
            Attached Files

            Comment


              #7
              I have put the following code in my strategy in OnBarUpdate to ignore historical trades:

              if (State >= State.Realtime)
              {
              OkToEnterTrades = true;

              // below in my BIP1, 2... it looks for this flag to enter trades. This way I am only looking to open trades at real time.
              }

              I now want to read the following in State.Transition to setup my strategy for what the ACCOUNT has, which SHOULD be reflected in what Control Center->Positions has listed for all the accounts that it can read in IB Broker account.

              if (State >= State.Transition)
              {
              // read account that was SELECTED in the strategy drop down (See pic)
              HOW DO I DO THIS....? I want to programmatically read what I inputted

              // then once I have that, I want to look up what the Control Center - Positions has in it's database.
              HOW DO I DO THIS?

              // Then I want look up the ACCOUNT value - go directly to IB TWS like NT8 is doing, and read the values direct from the account.
              HOW DO I DO THIS?

              // THen I want to correct any Strategy positions that are not correct
              HOW DO I DO THIS?


              }

              Thanks
              Attached Files

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by DJ888, 04-16-2024, 06:09 PM
              6 responses
              18 views
              0 likes
              Last Post DJ888
              by DJ888
               
              Started by Jon17, Today, 04:33 PM
              0 responses
              1 view
              0 likes
              Last Post Jon17
              by Jon17
               
              Started by Javierw.ok, Today, 04:12 PM
              0 responses
              6 views
              0 likes
              Last Post Javierw.ok  
              Started by timmbbo, Today, 08:59 AM
              2 responses
              10 views
              0 likes
              Last Post bltdavid  
              Started by alifarahani, Today, 09:40 AM
              6 responses
              41 views
              0 likes
              Last Post alifarahani  
              Working...
              X