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

ConnectionLossHandling for account interruption

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

    ConnectionLossHandling for account interruption

    I have defined the following settings for strategy:

    // Keeps the strategy running as if no disconnect occurred
    ConnectionLossHandling = ConnectionLossHandling.KeepRunning;


    If i interrupt account connection of IB-Gateway to simulate reconnection behaviour on every week, bad server restart, internet interrption etc., strategy shows error message and stopped immediately!?

    How can i prevent from this behaviour?
    What does (ConnectionLossHandling.KeepRunning) really means for what kind of service?

    My expectation is while (ConnectionLossHandling.KeepRunning) is set, that strategy ignores any kind of connection problem...


    May someone can help me out - give me a hint? :-)


    Cheers,
    Phil

    Attached Files

    #2
    By (https://ninjatrader.com/support/foru...ccountposition) i have followed instructions from Chris.

    Following scenario:
    1.) Connection for Account is lost

    2.) NT stopps strategy and prompt error message (see above)

    3.) User re-established Account-Connection (Connection from NT to IB-Gateway is green)

    4.) User started related strategy again
    Parameters to adopt account position is set / defined by strategy as followed:
    IsAdoptAccountPositionAware = true;
    StartBehavior = StartBehavior.AdoptAccountPosition;

    5.) By advice from Chris, User sync all strategies by forceing related function from context-menu
    ------------------------------------------------------------

    Result:
    Optical everything seems running and connecting fine, BUT strategy doesn't do anything with related account position. (see image01)
    (For example close position if minute is (05, 15, 25, 35 etc...))

    Also on chart there is a position that is labeled as virtual!? (see image02)


    How can i be sure, that strategy takes over account position for following management after re-established account-connection?

    Attached Files

    Comment


      #3
      Hello PWidera,

      Thanks for your post.

      We recommend using ImmediatelySubmit to have strategy calculate historical data and resume position.

      Paul H.NinjaTrader Customer Service

      Comment


        #4
        Hi Paul,

        thanks for your feedback!

        I have tested your advice on realtime for (StartBehavior.ImmediatelySubmit) as well as (StartBehavior.ImmediatelySubmitSynchronizeAccount ) while (IsAdoptAccountPositionAware = true).
        This makes no sence for me and brings no positive result!? :-(

        1.) If connection to account is lost, strategy will stop.
        2.) AccountPosition is still 1 contract.
        3.) After re-establishing account-connection (Connection from NT to IB-Gateway is green), strategy would be startet by hand.
        4.) Current contract / AccountPosition would directly be closed for both StartBehaviorTypes (StartBehavior.ImmediatelySubmit) & (StartBehavior.ImmediatelySubmitSynchronizeAccount ).
        Current AccountPosition would not be respected while IsAdoptAccountPositionAware = true!!!!?

        How can be a connection problem re-established and also current AccountPosition respected by re-started strategy?
        I don't want to close / lose current AccountPosition merely a connection problem comes up for some minutes!



        Cheers,
        Phil
        Attached Files

        Comment


          #5
          Hello PWidera,

          Thanks for your reply.

          I've attached a simplified example that may be helpful for you to review/test.

          AdoptAccountPositionTestManaged (1).zip

          Paul H.NinjaTrader Customer Service

          Comment


            #6
            Hi Paul,

            i have seen this script already.
            With this recommendation, we leave completely managed approach and most of NT-Servicefunctions!?

            I tried to make this script working on analyzer-side / historical-data but can't open any position if it is not realtime!?

            Comment


              #7
              Hello PWidera,

              Thanks for your reply.

              Here is an unmanaged apprach if that is what you are looking for:

              AdoptAccountPositionTestUnmanaged (1).zip


              I may not be understanding correctly but you would not be able to test start-up behaviors in the strategy analyzer.




              Paul H.NinjaTrader Customer Service

              Comment


                #8
                Hello Pwidera,

                The examples I posted in (#5) uses the Managed Approach and (#7 unmanaged approach). They cannot be backtested because they skips processing historical data with if (Stater == State.Historical) return;

                I would recommend using ImmediatelySubmit without SynchronizeAccount to re-enable the strategy and have it attempt to resume the position. ImmediatelySubmit will have the strategy process historical data to determine the strategy position and any active strategy orders and if the live orders match the historical orders, they will be resumed. If they do not the live orders will be replaced with the historical orders."
                Paul H.NinjaTrader Customer Service

                Comment


                  #9
                  Related to my post #2 and the problems / observed behavoir i have written down, how should me your provided script (AdoptAccountPositionTestManaged) help out?


                  On methode (OnStateChange) there are only AccountPosition adoption by parameters:
                  IsAdoptAccountPositionAware = true;
                  StartBehavior = StartBehavior.AdoptAccountPosition;


                  On section State.Realtime there is a definition for stop- and target order.
                  But this was not the problem, because till yet i don't use the functions for simple test strategy.

                  Result is always the same:
                  Optical everything seems running and connecting fine, BUT strategy doesn't do anything with related account position.

                  And on code side i can't see anything else than setting mentioned parameters for adopting AccountPosition.


                  Here is your code....

                  protected override void OnStateChange()
                  {
                  if (State == State.SetDefaults)
                  {
                  Description = @"Enter the description for your new custom Strategy here.";
                  Name = "AdoptAccountPositionTestManaged";
                  Calculate = Calculate.OnBarClose;
                  EntriesPerDirection = 1;
                  EntryHandling = EntryHandling.AllEntries;
                  IsExitOnSessionCloseStrategy = true;
                  ExitOnSessionCloseSeconds = 30;
                  IsFillLimitOnTouch = false;
                  MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
                  OrderFillResolution = OrderFillResolution.Standard;
                  Slippage = 0;
                  TimeInForce = TimeInForce.Gtc;
                  TraceOrders = false;
                  RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
                  StopTargetHandling = StopTargetHandling.PerEntryExecution;
                  BarsRequiredToTrade = 20;
                  // Disable this property for performance gains in Strategy Analyzer optimizations
                  // See the Help Guide for additional information
                  IsInstantiatedOnEachOptimizationIteration = true;

                  IsAdoptAccountPositionAware = true;
                  StartBehavior = StartBehavior.AdoptAccountPosition;

                  }
                  else if (State == State.Realtime)
                  {
                  if (Position.MarketPosition == MarketPosition.Long)
                  {
                  oco = GetAtmStrategyUniqueId() + "LongExits";
                  stopOrder = ExitLongStopMarket(0, true, Position.Quantity, Position.AveragePrice - 10 * TickSize, "StopLossLong", "");
                  targetOrder = ExitLongLimit(0, true, Position.Quantity, Position.AveragePrice + 10 * TickSize, "TargetLong", "");

                  }
                  }
                  }


                  I really need to get this resilient working!!!!

                  Comment


                    #10
                    Hello PWidera,

                    Thanks for your reply.

                    Here is a quick review of the start-up behaviors:

                    When a strategy is enabled, it processes historical data to determine trades that the strategy would have made on the data that is already on the PC/chart and to determine what position the strategy is in. (Strategy positions are separate from actual Account positions.)

                    Wait Until Flat will wait until this virtual/historical position is closed before live orders can be submitted. It ensures that the strategy starts trading live from a flat position. If you enable the strategy when the account is flat, the strategy will waiting until this position calculated from historical data is closed so it is logically making trades starting from an entry signal.

                    Immediately Submit automatically submits working orders from when the strategy processed historical data, and assumes the strategy position and account position are where you want it when you enable the strategy. This is typically used to have a strategy resume a position after disabling/enabling. If the strategy already had live orders running, the orders will resume with the new enablement of the strategy if they match the historically calculated orders. If the orders calculated from historical data do not match the live working orders, the live working orders will be cancelled and replaced by those calculated from historical data.

                    Sync Account Positions is an additional option that has NinjaTrader submit an order to sync the account position to the position calculated by the strategy. (Not the other way around.)

                    Adopt Account Position would be used if you want the strategy to inherit the Account Position on enablement. This requires additional programming.
                    If you do not want the strategy to calculate a position from processing historical data. Simply add if (State == State.Historical) return; to the top of your strategy logic so historical processing is skipped. The strategy will then always start from a flat position because it has not calculated any orders.

                    Strategy vs. Account Position — https://ninjatrader.com/support/help..._account_p.htm

                    Start Behaviors — https://ninjatrader.com/support/help..._positions.htm

                    Here is a short video to demonstrate the AdoptaccountpositiontestManaged: https://paul-ninjatrader.tinytake.co...OF8xNjk0NjQxNA
                    Paul H.NinjaTrader Customer Service

                    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