• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Syncing accounts after Disconnection

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

    Syncing accounts after Disconnection

    Back to NT7, it was not possible to sync Strategy and Position Account after a disconnection, so I had this little snippet to check if all was synced once the full connection was recovered

    Code:
    foreach (Account acct in Cbi.Globals.Accounts)				
    				{				
    					if ( acct.Name == "Uxxxxxx")			
    					{			
    						PositionCollection positions = acct.Positions;		
    						foreach (Position pos in positions)		
    						{		
    							if (pos.Instrument.FullName == "$EURUSD")	
    							{	
    								if ( Position.MarketPosition != pos.MarketPosition || Position.Quantity != pos.Quantity )
    								{
    									SendMail ("[email protected]", "[email protected]", "URGENT: UNSYNCED ACCOUNTS! ", Position.MarketPosition.ToString() + " " + Position.Quantity.ToString()+" Versus IB " +pos.MarketPosition.ToString()+ " " + pos.Quantity.ToString());
    								}
    							}	
    						}		
    					}			
    				}
    So, now in NT8, I've tried to search in this regard but I find nothing, then:

    1. Has NT8 some syncing procedure or method that I'm not aware of to check, or even more, to sync Strategy and Position accounts´after a connection recover?

    2. Is there any snippet substitute for NT8, as I posted above for NT7 ?
    Isthis info reliable as the real position account and is it available everywhere?
    Print("The position is " + PositionAccount.MarketPosition);


    Thanks in advance
    Last edited by pstrusi; 09-21-2017, 12:54 PM.

    #2
    Hello pstrusi,

    After a connection recover, assuming the strategy was not disabled, the strategy should be in sync as it was before the disconnection.

    You could use Position and compare it to PositionAccount to check if the strategy is in sync, see the following
    https://ninjatrader.com/support/help...ionaccount.htm
    https://ninjatrader.com/support/help.../?position.htm

    I do not have a script similar to the one above but I encourage you to look around the forum.

    Please let us know if you need further assistance.
    Alan P.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_AlanP View Post
      Hello pstrusi,

      After a connection recover, assuming the strategy was not disabled, the strategy should be in sync as it was before the disconnection.

      You could use Position and compare it to PositionAccount to check if the strategy is in sync, see the following
      https://ninjatrader.com/support/help...ionaccount.htm
      https://ninjatrader.com/support/help.../?position.htm

      I do not have a script similar to the one above but I encourage you to look around the forum.

      Please let us know if you need further assistance.
      Thanks for the answer.

      Let's assume as you say that the Strategy was not disabled but it had a working order that was filled, then the connection is recovered but obviously, positions are unsynced, so:
      Do you say that NT8 would trigger external orders to get into sync state again? or you meant that only it keeps synced if nothing has changed ?

      Thanks for the links and suggestions

      Comment


        #4
        Hello pstrusi,

        The strategy submits a limit order, connections lost, order is filled, then the connection is restored, the strategy would have a strategy position reflecting entry at that limit order which would match your account positon thus they would be in sync.

        Please see the following link,
        https://ninjatrader.com/support/help..._account_p.htm

        Please let us know if you need further assistance.
        Alan P.NinjaTrader Customer Service

        Comment


          #5
          Whenever I came up with a solution for my initial issue, I'd like to post it here cause it might perhaps help others.

          Code:
          protected override void OnConnectionStatusUpdate(ConnectionStatusEventArgs connectionStatusUpdate)
          		{
          		  	if( connectionStatusUpdate.Status != ConnectionStatus.Connected || connectionStatusUpdate.PriceStatus != ConnectionStatus.Connected )
          			{
          							   	SendMail ("[email protected]", "DISCONNECTION ! ", Position.MarketPosition.ToString() + " " + Position.Quantity.ToString()+" at " +Position.AveragePrice.ToString("0.0000") + "     PnL " + daypnl.ToString("0.00"));
          				dataserver=0;  // internal indicator "OFF" Don't send orders
          			}  
          			if( (connectionStatusUpdate.Status == ConnectionStatus.Connected && connectionStatusUpdate.PriceStatus == ConnectionStatus.Connected) && (connectionStatusUpdate.PreviousStatus != ConnectionStatus.Connected || connectionStatusUpdate.PreviousPriceStatus != ConnectionStatus.Connected) )
          			{
          				if ( Position.MarketPosition != PositionAccount.MarketPosition || Position.Quantity != PositionAccount.Quantity )
          				{	
          					SendMail ("[email protected]", "UNSYNCED ! ", Position.MarketPosition.ToString() + " " + Position.Quantity.ToString()+" at " +Position.AveragePrice.ToString("0.0000") + "     PnL " + daypnl.ToString("0.00"));	
          				}
          				if (ToTime(Time[0]) >= 170010 && ToTime(Time[0]) <= 171450)
          				{	
          					// EOD Reset variables, indicators....etc
          				}
          			   	dataserver=1;  // internal indicator "ON" Ready to send orders
          			}
          		}

          Comment


            #6
            Originally posted by NinjaTrader_AlanP View Post
            Hello pstrusi,

            The strategy submits a limit order, connections lost, order is filled, then the connection is restored, the strategy would have a strategy position reflecting entry at that limit order which would match your account positon thus they would be in sync.

            Please see the following link,
            https://ninjatrader.com/support/help..._account_p.htm

            Please let us know if you need further assistance.

            FOR NT7:

            What if the Order comes as "unknown" ?
            Today happened a weird situation. The Strategy had a working order, then a disconnection occured and lasted for more than 4 minutes, then when all was restored:

            1. NT received the instrument update
            2. NT receives the order execution ( when it should always on the other way )
            3. The old working order comes as "unknown"

            So, what happened is the Broker's account position was flat but wrongly the Strategy Position was Short in this case. So the accounts left unsynced.

            I'm aware that this a very old subject but still I want to ask:

            - Is there any undocumented way to manage the Strategy.Position?
            - Is there any undcocumented way to pull the broker's account info into the ongoing Strategy ?

            Thanks
            Last edited by pstrusi; 11-09-2018, 01:41 PM.

            Comment


              #7
              Hello pstrusi,

              An order state of unknown could be returned if an order is filled when NinjaTrader is disconnected and the order could not be identified when a connection is re-established. Since NinjaTrader did not see the close of the position and if it did not see an update for that order, the order on NinjaTrader's end could then be "unknown."

              There isn't any undocumented way I could share to manage the Position object manually. You could monitor PositionAccount to get the actual position on the instrument from the account, but this could lead to issues where the Strategy logic using Position is out of sync with the strategy when you are trying to correct it later. NinjaTrader 8 offers functionality to better track what the account is doing, and you could also consider creating an AdoptAccountPosition strategy that can be run where you can programmatically manage your positions if such a scenario arises.

              As for NinjaTrader 7, you may wish to have logic that will then begin monitoring PositionAccount after a disconnect is seen.

              OnConnectionStatus - https://ninjatrader.com/support/help...tionstatus.htm

              PositionAccount - https://ninjatrader.com/support/help...ionaccount.htm

              Let us know if we can be of further assistance.
              JimNinjaTrader Customer Service

              Comment


                #8
                Hi Jim, thanks for your attention. As for Ninjatrader 7, How do you pull the Broker's account position? All I can read in the guide is always related to the Strategy Position. I'm aware that this is possible in NT8 but for NT7 I think not. If you have different info, please let me know.
                Thanks!

                Comment


                  #9
                  Hello pstrusi,

                  My recommendation would be to monitor PositionAccount which would be the actual account position on the strategy.

                  Please let me know if you have any further questions.
                  JimNinjaTrader Customer Service

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by chnh1, Yesterday, 11:51 PM
                  0 responses
                  8 views
                  0 likes
                  Last Post chnh1
                  by chnh1
                   
                  Started by Jasper99, Yesterday, 10:29 PM
                  0 responses
                  11 views
                  0 likes
                  Last Post Jasper99  
                  Started by Jasper99, Yesterday, 09:50 PM
                  0 responses
                  6 views
                  0 likes
                  Last Post Jasper99  
                  Started by StevenNelson, Yesterday, 05:57 PM
                  0 responses
                  7 views
                  0 likes
                  Last Post StevenNelson  
                  Started by srbdoc, Yesterday, 03:59 PM
                  1 response
                  19 views
                  0 likes
                  Last Post NinjaTrader_EricB  
                  Working...
                  X