Go Back   NinjaTrader Support Forum > NinjaTrader 7 > General Development

General Development General NinjaScript development questions.

NinjaTrader
Reply
 
Thread Tools Display Modes
Old 04-13-2018, 08:02 AM   #1
newuser
Member
 
Join Date: Aug 2016
Posts: 50
Thanks: 0
Thanked 0 times in 0 posts
Default 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:
//added in variables
private bool dropout = true;
protected override void OnMarketData (MarketDataEventArgs e) { if (e.MarketData == null && dropout == true) { //send alert //send email dropout = false; } if (e.MarketData != null) { dropout = true; } }
newuser is offline  
Reply With Quote
Old 04-13-2018, 08:16 AM   #2
NinjaTrader_Jesse
NinjaTrader Customer Service
 
NinjaTrader_Jesse's Avatar
 
Join Date: Mar 2014
Location: Denver, CO
Posts: 4,635
Thanks: 29
Thanked 1,185 times in 1,108 posts
Default

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.
NinjaTrader_Jesse is offline  
Reply With Quote
Old 04-13-2018, 08:38 AM   #3
newuser
Member
 
Join Date: Aug 2016
Posts: 50
Thanks: 0
Thanked 0 times in 0 posts
Default

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
			}
		}
newuser is offline  
Reply With Quote
Old 04-13-2018, 08:57 AM   #4
NinjaTrader_Jesse
NinjaTrader Customer Service
 
NinjaTrader_Jesse's Avatar
 
Join Date: Mar 2014
Location: Denver, CO
Posts: 4,635
Thanks: 29
Thanked 1,185 times in 1,108 posts
Default

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.
NinjaTrader_Jesse is offline  
Reply With Quote
Old 04-13-2018, 09:22 AM   #5
newuser
Member
 
Join Date: Aug 2016
Posts: 50
Thanks: 0
Thanked 0 times in 0 posts
Default

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.
newuser is offline  
Reply With Quote
Old 04-13-2018, 12:20 PM   #6
NinjaTrader_Jesse
NinjaTrader Customer Service
 
NinjaTrader_Jesse's Avatar
 
Join Date: Mar 2014
Location: Denver, CO
Posts: 4,635
Thanks: 29
Thanked 1,185 times in 1,108 posts
Default

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.
NinjaTrader_Jesse is offline  
Reply With Quote
Old 04-14-2018, 07:12 AM   #7
newuser
Member
 
Join Date: Aug 2016
Posts: 50
Thanks: 0
Thanked 0 times in 0 posts
Default

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.
newuser is offline  
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to find order and check status rozirozi Strategy Development 1 01-30-2017 11:59 AM
Check Trailing Stop status Matheyas5 Strategy Development 7 02-04-2016 03:24 PM
Can you through programming check Order Connection Status? RunnrX Strategy Development 8 11-19-2013 04:03 PM
Yellow connection status? Radical Platform Technical Support 7 11-09-2012 03:57 AM
check status of specific Entry ddurocher Strategy Development 1 06-30-2010 09:04 AM


All times are GMT -6. The time now is 01:09 PM.

Futures, foreign currency and options trading contains substantial risk and is not for every investor. An investor could potentially lose all or more than the initial investment. Risk capital is money that can be lost without jeopardizing ones financial security or lifestyle. Only risk capital should be used for trading and only those with sufficient risk capital should consider trading. Past performance is not necessarily indicative of future results. View Full Risk Disclosure.

CFTC Rules 4.41 - Hypothetical or Simulated performance results have certain limitations, unlike an actual performance record, simulated results do not represent actual trading. Also, since the trades have not been executed, the results may have under-or-over compensated for the impact, if any, of certain market factors, such as lack of liquidity. Simulated trading programs in general are also subject to the fact that they are designed with the benefit of hindsight. No representation is being made that any account will or is likely to achieve profit or losses similar to those shown.

This website is hosted and operated by NinjaTrader, LLC (“NT”), a software development company which owns and supports all proprietary technology relating to and including the NinjaTrader trading platform. NT is an affiliated company to NinjaTrader Brokerage (“NTB”), which is a NFA registered introducing broker (NFA #0339976) providing brokerage services to traders of futures and foreign exchange products. This website is intended for educational and informational purposes only and should not be viewed as a solicitation or recommendation of any product, service or trading strategy. No offer or solicitation to buy or sell securities, securities derivative or futures products of any kind, or any type of trading or investment advice, recommendation or strategy, is made, given, or in any manner endorsed by any NT affiliate and the information made available on this Web site is not an offer or solicitation of any kind. Specific questions related to a brokerage account should be sent to your broker directly. The content and opinions expressed on this website are those of the authors and do not necessarily reflect the official policy or position of NT or any of its affiliates. 

Vendors along with their websites, products and services, collectively referred to as (“Vendor Content”), are independent persons or companies that are in no manner affiliated with NT or any if its affiliates. NT or any of its affiliates are not responsible for, do not approve, recommend or endorse any Vendor Content referenced on this website and it’s your sole responsibility to evaluate Vendor Content. Please be aware that any performance information provided by a vendor should be considered hypothetical and must contain the disclosures required by NFA Rule 2-29(c). If you are interested in learning more about, or investigating the quality of, any such Vendor Content you must contact the vendor, provider or seller of such Vendor Content. No person employed by, or associated with, NT or any of its affiliates is authorized to provide any information about any such Vendor Content.