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

Surviving disconnects

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

    Surviving disconnects

    I have a mysterious problem with my strategy

    The goal is to have the strategy survive disconnects

    but it doesn't...

    What happens is:

    1. I run my strategy
    2. it enters a position
    3. i manually forced a disconnect from the connections menu
    4. so far so good
    5. I manually reconnect from the connections menu
    6. there is no output from ninjatrader to the output window (I mean nothing to warn anything about connections being lost/reconnected or strategies being disabled / terminated)
    7. my strategy outputs the text below
    8. position is closed because the strategy is terminated

    so the problem is that the strategy is getting terminated upon reconnect.

    How can I avoid this?

    note: I do have several CloseStrategy() calls in my code, however it cannot be one of them because all of them are preceded by print statements and none of those print statements appear in the output

    this is the output (from my print code) I capture upon reconnect:

    ⮑ St EURCHF Terminating...
    which gets fired within:

    Code:
    else if (State == State.Terminated)
    here is my code that I set to try to keep everything running:

    Code:
    else if (State == State.Configure)
    {
      ConnectionLossHandling = ConnectionLossHandling.KeepRunning;
      DisconnectDelaySeconds = 99999;
      NumberRestartAttempts = 999;
    but having this code doesn't prevent the problem.
    Last edited by NinjaCustomer; 05-06-2019, 04:29 AM.

    #2
    I really don't get it...

    I managed to avoid it closing the position (that was from my code when it detected there was some imbalance)

    But

    I just don't understand this terminating thing that happens upon account reconnect

    Code:
    INIT STATE VARIABLES: reason=State.DataLoaded
    Enabling NinjaScript strategy '▲ Buy EURCHF /168353373' : On starting a real-time strategy - StartBehavior=WaitUntilFlat EntryHandling=All entries EntriesPerDirection=5 StopTargetHandling=Per entry execution ErrorHandling=Stop strategy, cancel orders, close positions ExitOnSessionClose=False SetOrderQuantityBy=Strategy ConnectionLossHandling=Keep running DisconnectDelaySeconds=10 CancelEntriesOnStrategyDisable=False CancelExitsOnStrategyDisable=False Calculate=On bar close IsUnmanaged=False MaxRestarts=4 in 5 minutes
    BUY EURCHF Qty 10000
    OnPositionUpdate: quantity=10000 averagePrice=1.14045, marketPosition=10000, instrument='EURCHF' account='Sim101' avgPrice=1.14045 quantity=10,000 marketPosition=Long statementDate='2019-05-05'
    ┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈
    ⮑ fx ▲ Buy EURCHF  Terminating...
    Disabling NinjaScript strategy '▲ ⏳ fx EURCHF/168353373'
    INIT STATE VARIABLES: reason=State.DataLoaded
    Enabling NinjaScript strategy '▲ Buy EURCHF /168353373' : On starting a real-time strategy - StartBehavior=WaitUntilFlat AccountPosition=EURCHF 10000L EntryHandling=All entries EntriesPerDirection=5 StopTargetHandling=Per entry execution ErrorHandling=Stop strategy, cancel orders, close positions ExitOnSessionClose=False SetOrderQuantityBy=Strategy ConnectionLossHandling=Keep running DisconnectDelaySeconds=10 CancelEntriesOnStrategyDisable=False CancelExitsOnStrategyDisable=False Calculate=On bar close IsUnmanaged=False MaxRestarts=4 in 5 minutes
    
    BUY EURCHF Qty 10000
    OnPositionUpdate: quantity=10000 averagePrice=1.14044, marketPosition=10000, instrument='EURCHF' account='Sim101' avgPrice=1.14044 quantity=10,000 marketPosition=Long statementDate='2019-05-05'
    ignore that the strategy has a different name, my code just renames the strategy upon termination
    Last edited by NinjaCustomer; 05-06-2019, 05:40 AM.

    Comment


      #3
      Hello NinjaCustomer,
      Thanks for your post.

      The strategy terminating on reconnect would not be the designed behavior of your selected ConnectionLossHandling property. This would point towards something to do with your custom logic.

      Are there any errors on the Logs tab of your Control Center?

      Does TraceOrders report anything?
      Josh G.NinjaTrader Customer Service

      Comment


        #4
        I don't see any errors in the Logs tab

        except sometimes connection lost to the NinjaTrader Historial Data Server, which makes sense since I disconnect manually to test the problem.

        I turned TraceOrders on now

        However...

        I realize the root of the problem is nothing to do with orders. It is with termination and the strategy being disabled.

        So what could cause a Strategy to be terminated and disabled?

        Some kind of invisible exception?

        this is what happens:

        everything below happens automatically except for what is in the brackets, which is stuff I do.

        Output window:

        (start the strategy by checking the checkbox in the strategies tab)

        Enabling NinjaScript strategy

        (here I do a Manual disconnect)
        (here I do a Manual reconnect)

        Terminating... (my strategy writes this from OnStatusChange Terminated)
        Disabling NinjaScript strategy
        Enabling NinjaScript strategy



        Comment


          #5
          NinjaCustomer,

          Errors, order rejections, and connection loss events would be reasons a strategy would get disabled. I do see that your RealTimeErrorHandling property is set to StopCancelClose. As you suggested, it could also perhaps be some type of exception that is visible in the trace, but we would need to look at the trace file to determine that.

          Do you see the same thing when you set your RealTimeErrorHandling property to IgnoreAllErrors? Please see the following link for more information: https://ninjatrader.com/support/help...orhandling.htm
          Josh G.NinjaTrader Customer Service

          Comment


            #6
            Yes this makes no difference if set to IgnoreAllErrors, but I expected that since I found out that the strategy will terminate even if there are no orders placed yet.

            However, there is one strange (or maybe not strange thing, if this is how ninjatrader is supposed to operate)

            When I first load the workspace, the strategy will also call OnStatusChange - Terminate

            I mean just from loading the workspace, without enabling the strategy yet. and before any message saying that the strategy is "Enabling..." in the output window.

            I'm guessing this is just how ninjatrader operates and nothing I should pay attention to.

            ok... new information.. see next post

            Comment


              #7
              I created a new Strategy to isolate the error (see attached)

              first I removed all indicators from the chart. there is only 1 chart in the workspace. I could see no exceptions or errors in the trace file from today.

              I used the generator and basically just clicked generate

              then I added 3 changes

              my State.Terminated print message

              and IgnoreAllErrors in SetDefaults

              and ConnectionLossHandling = ConnectionLossHandling.KeepRunning; in State.Configure

              here is the output...

              Code:
              Enabling NinjaScript strategy 'TheTerminator/168353407' : On starting a real-time strategy - StartBehavior=WaitUntilFlat EntryHandling=All entries EntriesPerDirection=1 StopTargetHandling=Per entry execution ErrorHandling=Ignore all errors ExitOnSessionClose=True / triggering 30 seconds before close SetOrderQuantityBy=Strategy ConnectionLossHandling=Keep running DisconnectDelaySeconds=10 CancelEntriesOnStrategyDisable=False CancelExitsOnStrategyDisable=False Calculate=On bar close IsUnmanaged=False MaxRestarts=4 in 5 minutes
              
              Terminating...
              
              Disabling NinjaScript strategy 'TheTerminator/168353407'
              
              Enabling NinjaScript strategy 'TheTerminator/168353407' : On starting a real-time strategy - StartBehavior=WaitUntilFlat EntryHandling=All entries EntriesPerDirection=1 StopTargetHandling=Per entry execution ErrorHandling=Ignore all errors ExitOnSessionClose=True / triggering 30 seconds before close SetOrderQuantityBy=Strategy ConnectionLossHandling=Keep running DisconnectDelaySeconds=10 CancelEntriesOnStrategyDisable=False CancelExitsOnStrategyDisable=False Calculate=On bar close IsUnmanaged=False MaxRestarts=4 in 5 minutes

              same methodology as above here... start the strategy.. disconnect... reconnect... then get the terminate and re-enabling messages.


              connections

              FXCM demo... CQG demo also connected but testing on a forex pair.

              Is manually disconnecting and reconnecting not a good way to test what happens when a connection is lost? I want to make sure my strategy runs stable through loss of connection.

              However occasionally in the past the only way to get my connections back quickly enough was to try to manually disconnect and reconnect them. So while that is not something that needs to be done every day... it still can sometimes be needed.

              also I was having unexplained termination of the strategy after connection loss before which is why I've been investigating this.

              I use .17.2
              Attached Files
              Last edited by NinjaCustomer; 05-06-2019, 01:14 PM.

              Comment


                #8
                NinjaCustomer,

                You are correct on the way NinjaTrader uses State.Terminated. Generally speaking, the 'Terminating.." Print() in this script does not really tell us anything.
                That being said, I am not seeing the same behavior on my end with your strategy. I tested using 'TCPView for Windows'.

                Please write in to PlatformSupport(AT)NinjaTrader(DOT)com and reference this forum post with RE:JOSH in the subject line. Please attach your Log and Trace files to your email so that I may investigate this matter further. You may follow the steps below to attach those files.
                • Open your NinjaTrader folder under My Documents.
                • Right click on the 'log' and 'trace' folders and select Send To> Compressed (zipped) Folder.
                • Send the 2 compressed folders as attachments to this email.
                • Once complete, you can delete these compressed folders.

                Josh G.NinjaTrader Customer Service

                Comment


                  #9
                  Code:
                  Enabling NinjaScript strategy 'TheTerminator/168353408' : On starting a real-time strategy - StartBehavior=WaitUntilFlat EntryHandling=All entries EntriesPerDirection=1 StopTargetHandling=Per entry execution ErrorHandling=Ignore all errors ExitOnSessionClose=True / triggering 30 seconds before close SetOrderQuantityBy=Strategy ConnectionLossHandling=Keep running DisconnectDelaySeconds=10 CancelEntriesOnStrategyDisable=False CancelExitsOnStrategyDisable=False Calculate=On bar close IsUnmanaged=False MaxRestarts=4 in 5 minutes
                  I managed to get it (TheTerminator.cs) working now to survive disconnects.I upgraded to 18 also, but it still had the same problems until I got an idea from the trace or logs to remove dlls that I don't use from other indicators. So I removed 2 and now the output looks like above even when I disconnect/reconnect FXCM. I'm not entirely sure which dll it was but since I don't use either of them it doesn't matter.

                  So I think my actual strategy should be ok too as well.

                  thanks for the help.

                  Comment


                    #10
                    I can't really figure it out.. TheTerminator.cs problem is solved but it persists under my actual strategy.

                    But now I'm testing instead of manually disconnecting from the menu, to pull out the ethernet cord.

                    And my actual strategy seems to survive this... which is good news.

                    So I guess I just have to avoid manually disconnecting.

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by timmbbo, Today, 08:59 AM
                    1 response
                    2 views
                    0 likes
                    Last Post NinjaTrader_ChelseaB  
                    Started by KennyK, 05-29-2017, 02:02 AM
                    2 responses
                    1,281 views
                    0 likes
                    Last Post marcus2300  
                    Started by fernandobr, Today, 09:11 AM
                    0 responses
                    3 views
                    0 likes
                    Last Post fernandobr  
                    Started by itrader46, Today, 09:04 AM
                    1 response
                    6 views
                    0 likes
                    Last Post NinjaTrader_Clayton  
                    Started by bmartz, 03-12-2024, 06:12 AM
                    5 responses
                    33 views
                    0 likes
                    Last Post NinjaTrader_Zachary  
                    Working...
                    X