Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Trading at the close of the bar

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

    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.

    #2
    Hello doculik,
    NinjaTrader is an event based application and a new bar will be formed only when a new ticks comes in besides the time conditions (if it is a time based chart). For example if you are watching a 1 minute chart then the 1:01:00PM bar can form at 1:00:03 PM instead of 1:00:00 PM if there is no trade between 1:00:00 and 1:00:02. As such Bars.GetTime(Bars.Count - 1) will return the value of the previous bar and you will not get the desired value.

    Please use the Print function and see what value you are getting for the iSecondsLeft field.

    Please let me know if I can assist you any further.
    JoydeepNinjaTrader Customer Service

    Comment


      #3
      Roger that. I figured out things will happen only when new ticks come in, so I'll look up the timer functionality so that it triggers without waiting for new ticks to come in.

      Thanks,
      Dan M.

      Comment


        #4
        Hello doculik,
        Glad you could figure it out.

        If you take any reference from Bars or Time[0] then you will have the issue.

        Please let me know if I can assist you any further.
        JoydeepNinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by algospoke, Yesterday, 06:40 PM
        2 responses
        19 views
        0 likes
        Last Post algospoke  
        Started by ghoul, Today, 06:02 PM
        3 responses
        14 views
        0 likes
        Last Post NinjaTrader_Manfred  
        Started by jeronymite, 04-12-2024, 04:26 PM
        3 responses
        45 views
        0 likes
        Last Post jeronymite  
        Started by Barry Milan, Yesterday, 10:35 PM
        7 responses
        20 views
        0 likes
        Last Post NinjaTrader_Manfred  
        Started by AttiM, 02-14-2024, 05:20 PM
        10 responses
        180 views
        0 likes
        Last Post jeronymite  
        Working...
        X