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

There's no market data handler to unsubscribe

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

    There's no market data handler to unsubscribe

    I have made a custom indicator that I'm using to gather position info on for all accounts. When a new trade is entered or the chart is refreshed, it will subscribe to market data via OnMarketDataUpdate either in State.DataLoaded or OnExecutionUpdate. I remove this subscription handler in State.Terminated. I keep track of which Instruments I have subscribed to but I am still getting a Fatal exception "There's no market data handler to unsubscribe". Is there a way to verify the current Instrument subscriptions before detaching the handler to avoid this error? I tried using a try / catch but that fails to catch it.

    Also, right now the market's are closed and it is consistently throwing the error when refreshing the chart

    #2
    Hello habibalex,

    Thanks for your notes.

    So I may accurately assist, how exactly are you defining the code in your script?

    To clarify, are you simply using OnMarketData() within your script as seen in the NinjaTrader help guide?

    OnMarketData(): https://ninjatrader.com/support/help...marketdata.htm

    Please create and share with me a reduced exported script that demonstrates the behavior in question so that I may investigate this matter further.

    Note that a reduced copy refers to a copy of the script that contains the minimum amount of code needed to reproduce the issue. All other code is commented out or removed. To create a copy of your script to modify, open a New > NinjaScript Editor, select your script, right-click in the Editor, select 'Save as', name the script, and click OK.

    To export the script, go to Tools > Export > NinjaScript AddOn.
    Brandon H.NinjaTrader Customer Service

    Comment


      #3
      Here is a script w/ the relevant parts, but it's not reproducing right now, however this other serilization error is also occurring
      Here is the output window:
      subscribeES 12-23
      subscribeHLGN
      removeES 12-23
      removeHLGN
      subscribeES 12-23
      subscribeHLGN
      Could not save indicator 'MyCustomIndicator:' There was an error reflecting type 'NinjaTrader.NinjaScript.Indicators.MyCustomIndica tor'.​
      Attached Files

      Comment


        #4
        Hello habibalex,

        Thanks for your notes.

        To investigate the behavior further, we would need a sample script that reproduces the error and the exact steps and settings you use to consistently reproduce the behavior in question.

        If you are able to consistently reproduce the behavior using the indicator you shared, please send us the exact steps and settings you use to reproduce the behavior so we may look into this further.

        In regard to the other error message you noted, the error you have presented would generally happen if the public properties or structure of the script cannot be serialized. It sounds like this is the case based on the details you provided.

        The easiest way to find out what is causing the problem is to quickly debug the script using the following steps.
        1. Open the script in the NinjaScript editor
        2. Comment out all the logic in OnBarUpdate so the script essentially does nothing. Ensure to comment out other areas you used logic such as OnRender.
        3. Comment out the SetDefaults for any public properties you created, you can leave the scripts default settings.
        4. Re apply the script on a chart, and check that it still fails to save.
        5. Go back to the script, comment out all public properties or anything you have specifically created with the public modifier
        6. Remove the script from where it was applied, re apply it and re test.

        This will allow you to find the point in the code which toggles the error on and off. You may need to use the above steps to gradually uncomment parts of the file to find the breaking point.

        The solution is very likely going to be appending the [XmlIgnore] attribute to the property causing the problem​

        That said, I see you are using a HashSet<T> in your script and using this in the script would go beyond the support we would be able to provide you with in the Support Department at NinjaTrader. If the error is regarding the use of HashSet<T> then you would need to do further research on how to implement a HashSet<T> in the script to resolve the error.

        You could do a quick Google search for something like "Use HashSet<T> C#" or "HashSet<T> C#" to find more information about working with HashSet<T>.
        Brandon H.NinjaTrader Customer Service

        Comment


          #5
          The script I sent does not have any public properties, can you check on your end whether the same thing happens? To test, open a chart, enter into a position in the Sim101 account, refresh the chart. Then select the chart tab and choose duplicate in new window

          edit: nm, looks like setting it to private from public fixes the issue. This seems like a bug though since it is not a property, just a field.
          Last edited by habibalex; 10-09-2023, 03:16 PM.

          Comment


            #6
            Hello habibalex,

            Thanks for your notes.

            Yes, the behavior occurs when trying to save the indicator you shared as a template.

            It seems that the error is likely being caused by the public HashSet<> in your script.

            When commenting out the HashSet<> code in the script the indicator can be successfully saved as an indicator template or open when duplicating a chart window. Changing public HashSet<> to private HashSet<> allows the indicator to successfully save as an indicator template or open when duplicating a chart window.

            As noted in my previous post, using C# HashSet<> goes beyond the scope of support we would be able to provide you with in the Support Department at NinjaTrader.

            Since it is a C# topic that goes outside our scope of support and is not supported NinjaScript-specific code, it would be up to your discretion to use C# HashSet<> in you code and it would be up to you to debug your code and resolve any error revolving around the use of HashSet<> in your code.

            This forum thread will also be open for other community members to share their insights on using C# HashSet<> in a NinjaScript.
            Last edited by NinjaTrader_BrandonH; 10-09-2023, 03:28 PM.
            Brandon H.NinjaTrader Customer Service

            Comment


              #7
              Regarding the original topic of this thread, it looks like I need to subscribe / unsubscribe to OnMarketDataUpdate using the Dispatcher.InvokeAsync(() => Instrument.MarketDataUpdate += OnMarketDataUpdate). Is this correct?

              So for each position, ie: NQ, ES, ZN etc, I subscribe and handle them all in the OnMarketDataUpdate function. Then based on the instrument I update the appropriate data. However, it seemed like some of the updates are not occurring on the same threads and I'm missing data. I tried the following and it seems to be a little more consistent:
              Code:
              if(Dispatcher.CheckAccess()){
                  UpdateAllPositions(e.Instrument, e.Last, nextUpdates[e.Instrument.Id]);
              }else{
                   Dispatcher.InvokeAsync(() => UpdateAllPositions(e.Instrument, e.Last, nextUpdates[e.Instrument.Id]));
               }

              Comment


                #8
                Hello habibalex,

                Thanks for your notes.

                Yes, you could use instrument.Dispatcher.InvokeAsync(() => instrument.MarketData.Update += OnMarketData); to subscribe to OnMarketData for the instrument which could be seen in the MarketData help guide page linked below.

                Note: "instrument" is a placeholder in this example, you will need to replace with a valid Instrument object through various methods or properties available depending on the NinjaScript type you are working with (e.g., Bars.Instrument or Instrument.GetInstrument()

                Before unsubscribing, make sure to check if instrument != null before trying to unsubscribe to OnMarketData.

                See this help guide page for more information about MarketData: https://ninjatrader.com/support/help...marketdata.htm
                Brandon H.NinjaTrader Customer Service

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by rhyminkevin, Today, 04:58 PM
                3 responses
                48 views
                0 likes
                Last Post Anfedport  
                Started by iceman2018, Today, 05:07 PM
                0 responses
                5 views
                0 likes
                Last Post iceman2018  
                Started by lightsun47, Today, 03:51 PM
                0 responses
                7 views
                0 likes
                Last Post lightsun47  
                Started by 00nevest, Today, 02:27 PM
                1 response
                14 views
                0 likes
                Last Post 00nevest  
                Started by futtrader, 04-21-2024, 01:50 AM
                4 responses
                50 views
                0 likes
                Last Post futtrader  
                Working...
                X