Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

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, 01: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



    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



      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,


        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, 02: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 CortexZenUSA, Today, 12:53 AM
                  0 responses
                  1 view
                  0 likes
                  Last Post CortexZenUSA  
                  Started by CortexZenUSA, Today, 12:46 AM
                  0 responses
                  1 view
                  0 likes
                  Last Post CortexZenUSA  
                  Started by usazencortex, Today, 12:43 AM
                  0 responses
                  5 views
                  0 likes
                  Last Post usazencortex  
                  Started by sidlercom80, 10-28-2023, 08:49 AM
                  168 responses
                  2,265 views
                  0 likes
                  Last Post sidlercom80  
                  Started by Barry Milan, Yesterday, 10:35 PM
                  3 responses
                  11 views
                  0 likes
                  Last Post NinjaTrader_Manfred  
                  Working...
                  X