NinjaTrader Support Forum  

Go Back   NinjaTrader Support Forum > Application Technical Support > Automated Trading

Automated Trading Support for automated trading systems using NinjaScript. Support for our ATI (Automated Trading Interface) used to link an external application such as TradeStation and eSignal to NinjaTrader.

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
Old 03-19-2012, 11:39 AM   #1
doculik
Member
 
Join Date: Aug 2011
Posts: 32
Thanks: 9
Thanked 0 times in 0 posts
Question Trading at the close of the bar

Hi All,

I am trying to build a strategy that will reliably trade at the close of the bar, however I'm running into some random... discrepancies, i.e. I'm telling the timer to print each 20 seconds or so a warning, and 2 seconds before the bar close to print out something else.

What happens, is that it sometimes, quite inconsistently, doesn't print the warnings every 20 secods, and skips directly to printing the message at the end of the bar.

Would anyone have any idea why this behavior?

Please find the relevant pieces of code below, inspired greatly by the BarTimer indicator:

Code:
		private DateTime lastTimePlot = Cbi.Globals.MinDate;
		private System.Windows.Forms.Timer timer;
		private int iSecondsLeft = 0;
		private bool bCloseEnough = false;
		private bool bSendWarning = false;

        protected override void Initialize()
        {
            CalculateOnBarClose = false;
        }

		protected override void OnStartUp()
		{
		if (timer == null)
			{
				timer = new System.Windows.Forms.Timer();
				timer.Interval = 1000;
				timer.Tick += new EventHandler(OnTimerTick);
				timer.Enabled = true;
			}
		}
		
		protected override void OnTermination()
        {
            if (timer != null)
            {
                timer.Enabled = false;
                timer = null;
            }
        }

		private void OnTimerTick(object sender, EventArgs e)
		{
			if (Historical) return;
			/*if (DateTime.Now.Subtract(lastTimePlot).Seconds >= 1)
			{*/
				lastTimePlot = DateTime.Now;
				TimeSpan barTimeLeft = Bars.GetTime(Bars.Count - 1).Subtract(Now);
				//iSecondsLeft = (barTimeLeft.Ticks < 0 ? 0 : Convert.ToInt16(barTimeLeft.Minutes * 60 + barTimeLeft.Seconds));
				iSecondsLeft = Convert.ToInt16(barTimeLeft.Minutes * 60 + barTimeLeft.Seconds);
				bCloseEnough = (Math.Abs(iSecondsLeft) < 2 ? true : false);
				bSendWarning = (iSecondsLeft > 20 && iSecondsLeft % 20 < 5 ? true : false);
				
			if (bCloseEnough) Print("Do the trade in the 4 seconds of the bar around the close! --> " + iSecondsLeft + "//" + Time[0] + "|| The time now is = " + DateTime.Now);
			if (bSendWarning) Print("Send a warning, every 20 seconds or so --> " + iSecondsLeft + "//" + Time[0] + "|| The time now is = " + DateTime.Now);
			//}
		}
		
		private DateTime Now
		{
			get 
			{ 
				DateTime now = (Bars.MarketData.Connection.Options.Provider == Cbi.Provider.Replay ? Bars.MarketData.Connection.Now : DateTime.Now); 

				if (now.Millisecond > 0)
					now = Cbi.Globals.MinDate.AddSeconds((long) Math.Floor(now.Subtract(Cbi.Globals.MinDate).TotalSeconds));

				return now;
			}
		}
The OnBarUpdate proc is empty.

Please also find the content of the output window below:
Code:
Send a warning, every 20 seconds or so --> 44//19/03/2012 16:52:00|| The time now is = 19/03/2012 16:51:16
Send a warning, every 20 seconds or so --> 43//19/03/2012 16:52:00|| The time now is = 19/03/2012 16:51:17
Send a warning, every 20 seconds or so --> 42//19/03/2012 16:52:00|| The time now is = 19/03/2012 16:51:18
Send a warning, every 20 seconds or so --> 41//19/03/2012 16:52:00|| The time now is = 19/03/2012 16:51:19
Send a warning, every 20 seconds or so --> 40//19/03/2012 16:52:00|| The time now is = 19/03/2012 16:51:20
Send a warning, every 20 seconds or so --> 24//19/03/2012 16:52:00|| The time now is = 19/03/2012 16:51:36
Send a warning, every 20 seconds or so --> 23//19/03/2012 16:52:00|| The time now is = 19/03/2012 16:51:37
Send a warning, every 20 seconds or so --> 22//19/03/2012 16:52:00|| The time now is = 19/03/2012 16:51:38
Send a warning, every 20 seconds or so --> 21//19/03/2012 16:52:00|| The time now is = 19/03/2012 16:51:39
Do the trade in the 4 seconds of the bar around the close! --> 1//19/03/2012 16:52:00|| The time now is = 19/03/2012 16:51:59
Do the trade in the 4 seconds of the bar around the close! --> 0//19/03/2012 16:52:00|| The time now is = 19/03/2012 16:52:00
Do the trade in the 4 seconds of the bar around the close! --> -1//19/03/2012 16:52:00|| The time now is = 19/03/2012 16:52:01
Send a warning, every 20 seconds or so --> 42//19/03/2012 16:54:00|| The time now is = 19/03/2012 16:53:18
Send a warning, every 20 seconds or so --> 41//19/03/2012 16:54:00|| The time now is = 19/03/2012 16:53:19
Send a warning, every 20 seconds or so --> 40//19/03/2012 16:54:00|| The time now is = 19/03/2012 16:53:20
Send a warning, every 20 seconds or so --> 24//19/03/2012 16:54:00|| The time now is = 19/03/2012 16:53:36
Send a warning, every 20 seconds or so --> 23//19/03/2012 16:54:00|| The time now is = 19/03/2012 16:53:37
Send a warning, every 20 seconds or so --> 22//19/03/2012 16:54:00|| The time now is = 19/03/2012 16:53:38
Send a warning, every 20 seconds or so --> 21//19/03/2012 16:54:00|| The time now is = 19/03/2012 16:53:39
Do the trade in the 4 seconds of the bar around the close! --> 1//19/03/2012 16:54:00|| The time now is = 19/03/2012 16:53:59
Do the trade in the 4 seconds of the bar around the close! --> 0//19/03/2012 16:54:00|| The time now is = 19/03/2012 16:54:00
Do the trade in the 4 seconds of the bar around the close! --> -1//19/03/2012 16:54:00|| The time now is = 19/03/2012 16:54:01
Do the trade in the 4 seconds of the bar around the close! --> 1//19/03/2012 16:55:00|| The time now is = 19/03/2012 16:54:59
Do the trade in the 4 seconds of the bar around the close! --> 0//19/03/2012 16:55:00|| The time now is = 19/03/2012 16:55:00
Do the trade in the 4 seconds of the bar around the close! --> -1//19/03/2012 16:55:00|| The time now is = 19/03/2012 16:55:01
Send a warning, every 20 seconds or so --> 24//19/03/2012 16:56:00|| The time now is = 19/03/2012 16:55:36
Send a warning, every 20 seconds or so --> 23//19/03/2012 16:56:00|| The time now is = 19/03/2012 16:55:37
Send a warning, every 20 seconds or so --> 22//19/03/2012 16:56:00|| The time now is = 19/03/2012 16:55:38
Send a warning, every 20 seconds or so --> 21//19/03/2012 16:56:00|| The time now is = 19/03/2012 16:55:39
Do the trade in the 4 seconds of the bar around the close! --> 1//19/03/2012 16:56:00|| The time now is = 19/03/2012 16:55:59
Do the trade in the 4 seconds of the bar around the close! --> 0//19/03/2012 16:56:00|| The time now is = 19/03/2012 16:56:00
Do the trade in the 4 seconds of the bar around the close! --> -1//19/03/2012 16:56:00|| The time now is = 19/03/2012 16:56:01
Send a warning, every 20 seconds or so --> 44//19/03/2012 16:57:00|| The time now is = 19/03/2012 16:56:16
Send a warning, every 20 seconds or so --> 43//19/03/2012 16:57:00|| The time now is = 19/03/2012 16:56:17
Send a warning, every 20 seconds or so --> 42//19/03/2012 16:57:00|| The time now is = 19/03/2012 16:56:18
Send a warning, every 20 seconds or so --> 41//19/03/2012 16:57:00|| The time now is = 19/03/2012 16:56:19
Send a warning, every 20 seconds or so --> 40//19/03/2012 16:57:00|| The time now is = 19/03/2012 16:56:20
Send a warning, every 20 seconds or so --> 24//19/03/2012 16:57:00|| The time now is = 19/03/2012 16:56:36
Send a warning, every 20 seconds or so --> 22//19/03/2012 16:57:00|| The time now is = 19/03/2012 16:56:38
Send a warning, every 20 seconds or so --> 21//19/03/2012 16:57:00|| The time now is = 19/03/2012 16:56:39
Thanks for your help,
D.
doculik is offline  
Reply With Quote
 

Tags
strategies, timer, trading at close

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
Calculate On Bar Close/Open-Low-High-Close relationship jkmott59 Indicator Development 6 04-10-2012 10:47 AM
Calculate on bar close = false but only signal on close of bar gyoung Indicator Development 3 10-26-2011 11:15 AM
How can I enter at close of bar one and exit at close of session? stevep3 Strategy Analyzer 5 10-15-2010 04:49 AM
Does NT simulator place orders at the Close of current bar, or Open of next bar? tomace Strategy Development 1 03-02-2009 12:43 PM
Trading at close of a bar jptrder Automated Trading 3 10-24-2008 02:38 PM


All times are GMT -6. The time now is 01:58 AM.