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

OnMarketData - check connection status

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

    OnMarketData - check connection status

    First up: I'm running NT7.

    I'm having an issue with 240 minute (H4) charts and I want to develop some code that will notify me if my strategy isn't getting any data/working properly.

    I know the problem is not the strategy/code because the same script works just fine on a shorter time frame (so it's also not my account connection).I managed to get the 240 minute strategy working by (a) disabling the strategy, (b) removing the strategy from the strategy window and (c) re-adding it & re-enabling the strategy. A simple disable/reenable didn't seem to cut it.

    I had a look through the manual and I thought OnMarketData should do the trick. I could put some code in OnBarUpdate but then I would have to wait around for several hours to find out if the strategy is working.

    Is the code below going to work? I note there are other options (like e.MarketData.Connection.Status) but the manual does not have detailed information on all the various arguments and their relevant values.

    Note I put the extra condition with the boolean so that I don't get inundated with messages/alerts if there's no market data.

    Code:
    [INDENT]//added in variables[/INDENT][INDENT]private bool dropout = true;[/INDENT]
    
    		protected override void OnMarketData (MarketDataEventArgs e)
    		{
    			if (e.MarketData == null && dropout == true)
    			{
    				//send alert
    				//send email  				
    				dropout = false;
    			}
    			
    			if (e.MarketData != null)
    			{
    				dropout = true;
    			}
    		}

    #2
    Hello newuser,

    Thank you for the post.

    I think the first thing to cover here would be what happens if the strategy is not getting any data. In that case, because the script is event-driven your code would not be run. If there are not any price updates or data coming through to call OnMarketData, the code would never be run.

    If you are wanting to know about the status of the connection specifically, you can use the OnConnectionStatus override: https://ninjatrader.com/support/help...sub=connection
    This would be called by the connection events which would be separate from price data events.

    If you want to know the market data has completely stopped, for example, you are just out of session but still connected. that would require extra logic like a Timer. If OnMarketData is not called, you would need a way to run your code to check if it is still active.

    I look forward to being of further assistance.
    JesseNinjaTrader Customer Service

    Comment


      #3
      Thanks for the prompt reply.

      So if I am understanding you correctly what you are saying is that OnConnectionStatus is like the gatekeeper...if I don't get confirmation of a valid connection using that method then nothing else in the strategy is going to run?

      So going off the manual I would check the connection like so(?):

      Code:
      		protected override void OnConnectionStatus (ConnectionStatus orderStatus, ConnectionStatus priceStatus)
      		{		
      			if(priceStatus == ConnectionStatus.Connected)
      			{
      				//notify that strategy is connected
      			}
      		}

      Comment


        #4
        Hello,

        This would mainly just act like any of the other overrides in the script and would be called when appropriate. If you have bars on the chart, and have this override in the script and you are also connected when the script is enabled, it should report connected. This is only the connection though and does not tell you if there is market data for the instrument you are using, just that you have a data server connection.

        It would be helpful to better understand your question or what your intended goal is. Are you trying to check for a disconnect or just ensure the script reached realtime and started working?

        You noted "if my strategy isn't getting any data/working properly", the getting data part could be as simple as putting a Print in OnMarketData. Do you see the print, if yes then you have data. The working properly part would be more specific to your logic. What is currently not working properly?

        Can you provide more detail on whats not working and what you are trying to do? potentially there is a more simple approach.



        I look forward to being of further assistance.
        JesseNinjaTrader Customer Service

        Comment


          #5
          I'm not entirely sure what is causing the problem so it's difficult to know where to go from here. Here's what I found this week:

          I had the exact same script that I enabled in the strategy window, both for the 240 minute timeframe and a shorter time frame. The script does not place trades, it just sends me an alert when there's a signal (which prompts me to check the market and decide if I want to trade).

          I was getting signals on the shorter time frame no problem, but I noticed I wasn't getting anything on the 240 minute. It's the same script, running on the same machine and using the same account connection (which is an FXCM feed by the way).

          I did try putting in a print command inside OnBarUpdate to literally just print "yes this is working" but I didn't get any prints whatsoever. I tried putting print commands in a couple places (first thing inside OnBarUpdate and after my criteria) but nothing, not a single print, bubkiss.

          Next I thought I'd try a different test, so I created a brand new strategy with nothing at all in it, blank except for a print command inside OnBarUpdate and run the blank new strategy for the 240 minute time frame. To begin with, I still didn't get any prints(!) After some playing around I started getting prints from the blank strategy on the 240 minute, so I went back to my original script to try again.

          I had added OnMarketData inside my basic/empty script with a print command and noticed nothing was printing when it wasn't working, but it did print values when it was working. I disabled my actual strategy script, added the OnMarketData code and nothing - no data printing. That was when I removed the script completely from the strategy window, re-added & re-enabled and then I started getting data prints from OnMarketData.

          I was basically thinking of using OnMarketData as 'the canary in the coal mine.' So rather than putting a print command inside OnBarUpdate, waiting 4 hours to see if something printed, trying a restart then waiting 4 hours again to see if that worked(!), I could at least use OnMarketData to tell straight away if my strategy was getting data into it.

          As I said, the whole time that I did this I had the original strategy running just fine on a shorter time frame - my internet didn't drop out, the account connection didn't disconnect and I didn't restart my computer. I got signals on the shorter timeframe no problem, but for some reason the 240 minute just wouldn't play ball. Once I removed it, re-added and re-started the 240 minute strategy it ran as expected for the rest of the week.

          I'm at a loss to explain it, other than to note that I am using an old machine and I have had a couple other random bugs to deal with.

          Comment


            #6
            Hello newuser,

            Thank you for providing more details.

            Based on your explanation it sounds like you may have more going on than just needing to find if the data connection is active.

            If you have a strategy enabled and then enabled the same strategy on a different timeframe, I would expect that OnBarUpdate/OnMarketData is called for both scripts but may not expect the conditions to become true for both because of differences in timeframe. Based on your further testing that an empty script with just OnMarketData was not printing, this is likely playing into the problem with your existing script.

            You noted after playing around with the script you were able to see prints and resumed with your existing script. What was changed between the empty script not working and when it became working? Was it only removing the instance and reapplying it that caused it to work again?

            Before going into finding ways to check if the data feed is active, I think it would be important to further explore the empty script test and why that wasn't working. Your theory on checking OnMarketData for a print to know if the strategy is receiving data is fine, but if that is not happening there is likely a reason why.

            Could you detail what had changed when you were using the empty script test? That may provide more detail on why the real script didn't work when applied in your previous tests.

            Also to confirm, when you are adding the additional overrides like OnMarketData, are you fully removing the script and re applying it or just refreshing/disable/enable?


            I look forward to being of further assistance.
            JesseNinjaTrader Customer Service

            Comment


              #7
              Unfortunately I don’t remember exactly what I did to get the blank script working. I don’t recall if I fully removed (either) script when I added the overrides or just refreshed/disable/enable. At some point I definitely fully removed the 240 minute script, but thinking back I did at some point remove and re-add the blank script (but I cannot recall the sequence of events and if that action specifically led to it working).

              As it happens the week before last I was having the same issues with the 240 minute chart. In that case (2 weeks ago), I wasn’t using the 240 minute script for any other timeframe, but I did have a different script running on the shorter time frame. At some point in the week the 240 minute script starting working, but I don’t recall if that was based on a refresh or complete remove and re-add.

              I’ll monitor the situation next week and try take more specific notes.

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by kevinenergy, 02-17-2023, 12:42 PM
              115 responses
              2,698 views
              1 like
              Last Post kevinenergy  
              Started by prdecast, Today, 06:07 AM
              1 response
              4 views
              0 likes
              Last Post NinjaTrader_LuisH  
              Started by Christopher_R, Today, 12:29 AM
              1 response
              14 views
              0 likes
              Last Post NinjaTrader_LuisH  
              Started by chartchart, 05-19-2021, 04:14 PM
              3 responses
              577 views
              1 like
              Last Post NinjaTrader_Gaby  
              Started by bsbisme, Yesterday, 02:08 PM
              1 response
              15 views
              0 likes
              Last Post NinjaTrader_Gaby  
              Working...
              X