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

Holidays + Session End

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

    Holidays + Session End

    Hi guys-

    Came across something today that's curious. I have some logic that closes positions at session end (this supplants the ExitOnClose setting, which I leave as false). The code looks like this:

    Code:
    if (BacktestExitOnClose && 					Positions[BarsInProgress].MarketPosition != MarketPosition.Flat)
    //sessionEnd.DayOfWeek == DayOfWeek.Friday)
    	{
    		int start = ToTime(sessionEnd.AddSeconds(-1*BacktestExitOnCloseSeconds));
    		int end = ToTime(sessionEnd);
    					
    		if (ToTime(Time[0]) > start &&
    			ToTime(Time[0]) < end &&
    			!tradingClosed &&
    			!closingOrderSubmitted)
    		{
    			// Close the position
    			Positions[BarsInProgress].Close();
    						
    			tradingClosed = true;  // tradingClosed is here to ensure that no trades occur after specified close.
    			closingOrderSubmitted = true; // This flag ensures that only one closing order will be submitted.
    								
    			// Need to add this here to prevent a new entry into a position.
    			barNumberOfOrder = CurrentBar;
    					
    			if (DebugOutput)
    				DebugMessage(BarsInProgress, CurrentBar, Instrument, "PositionCloser():  Position Closed on bar #" + barNumberOfOrder);
    
    			if (TradingOutput)
    				TradingMessage(BarsInProgress, CurrentBar, Instrument, "Session Close Submitted.");	
    							}
    					
    		// Need code to turn things back on
    		if (ToTime(Time[0]) < start &&
    			ToTime(Time[0]) > end)
    		{
    			tradingClosed = false;  // tradingClosed is here to ensure that no trades occur after specified close.
    			closingOrderSubmitted = false; // This flag ensures that only one closing order will be submitted.
    		}
    							
    		break;
    }
    Now, this code works just fine - until I came across July 4. In backtesting this code went right through an early afternoon 1PM close on the CME as if the session didn't end.

    However, when I use NT's backtesting "ExitOnClose," this early close is correctly resolved.

    I can't find anything related to holidays in NT's help file. The session in this case was for the default "Nymex Metals / Energy ETH" session, using UTC-05:00.

    Any thoughts on what I might be missing here?

    FYI I do initialize sessionStart and sessionEnd using:

    Code:
    // Get the session begin/end
    Bars.Session.GetNextBeginEnd(BarsArray[BarsInProgress], 0, out sessionBegin, out sessionEnd);
    Last edited by cgeorgan; 08-07-2016, 12:47 PM.

    #2
    Hello,
    In NinjaTrader 7 you will need to account for the holidays and their early closes or halts. You could do this by chekcing for what day it is and then changing the close for your strategy.
    In NinjaTrader 8 holiday hours are added in. For more information on using holidays in trading hours in NinjaTrader 8 please see the following link: http://ninjatrader.com/support/helpG...radingHolidays

    There is not a set release date for NinjaTrader 8. NinjaTrader 8 is currently in a beta phase.

    You can install the beta at the following link: http://ninjatrader.com/support/forum...ad.php?t=87190

    Documents to help you get started with NinjaTrader 8 Beta:

    Installation Guide
    Migration Guide
    Connections Guides
    Existing User FAQ
    User Help Guide
    NinjaScript Code Breaking Changes

    Please also make sure to review the general beta information page and what to expect from our Help Guide

    If we can be of any other assistance please let us know.
    Cody B.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_CodyB View Post
      Hello,
      In NinjaTrader 7 you will need to account for the holidays and their early closes or halts. You could do this by chekcing for what day it is and then changing the close for your strategy.


      If we can be of any other assistance please let us know.
      Yes - however, this doesn't answer my question. That is, NT knows what these days are; is this information exposed to users and - if so - how? If not, why? Somewhere NT does a check to see if it's July 4 (or another holiday), yet there are no references to holidays in NT7's documentation.

      Comment


        #4
        Hello,
        NinjaTrader 7 does not know when a holiday is. You can see this as when you backtested NinjaTrader continued through the session as if it did not close early. In NinjaTrader 8 holidays are checked in the Trading Hours template.
        Cody B.NinjaTrader Customer Service

        Comment


          #5
          Originally posted by NinjaTrader_CodyB View Post
          Hello,
          NinjaTrader 7 does not know when a holiday is. You can see this as when you backtested NinjaTrader continued through the session as if it did not close early. In NinjaTrader 8 holidays are checked in the Trading Hours template.
          That's what I'm saying - NT did in fact close at the correct session time on backtesting.

          I've attached two screenshots. The first shows NT's "ExitOnClose = true" logic, and the trade was exited correctly before the 1PM EST close on 7.4.16.

          The second shows the logic I've used - using sessionStart and sessionEnd - and the position goes right through the 1PM EST close on 7.4.16.

          So how did NT know of this early close? No special logic was used in the first example outside of "ExitOnClose = true".
          Attached Files

          Comment


            #6
            Originally posted by cgeorgan View Post
            FYI I do initialize sessionStart and sessionEnd using:

            Code:
            // Get the session begin/end
            Bars.Session.GetNextBeginEnd(BarsArray[BarsInProgress], 0, out sessionBegin, out sessionEnd);
            I think what support is saying,
            1. In NT7, GetNextBeginEnd() does not return correct hours for holiday sessions, whether in live trading or backtest.
            2. Unlike NT7, NT8 has a facility to get correct hours for holiday sessions.

            Nevertheless, cgeorgan, you have a highly interesting question.

            Comment


              #7
              My guess was that ExitOnClose processing cheated by looking at the timestamp of historical Day bars, since each bar on a 1-Day chart should be timestamped when the bar closes.

              Well, on a holiday, such as 07/04/16, you'd think that particular 'CL 09-16' candlestick would have an adjusted timestamp of 10:00 PST, but it does not.

              On my day chart, the 07/04/16 candlestick shows 14:00 PST, which is the same time as all the other bars before and after.

              Well, that theory is out.

              In backtest, everything is historical data, so you'd think ExitOnClose is somehow using the benefit of hindsight to get it right.

              But if ExitOnClose is doing that, then the how & why is indeed interesting -- because it doesn't seem to be using the timestamp of historical day bars trick.

              Comment


                #8
                Originally posted by bltdavid View Post
                My guess was that ExitOnClose processing cheated by looking at the timestamp of historical Day bars, since each bar on a 1-Day chart should be timestamped when the bar closes.

                Well, on a holiday, such as 07/04/16, you'd think that particular 'CL 09-16' candlestick would have an adjusted timestamp of 10:00 PST, but it does not.

                On my day chart, the 07/04/16 candlestick shows 14:00 PST, which is the same time as all the other bars before and after.

                Well, that theory is out.

                In backtest, everything is historical data, so you'd think ExitOnClose is somehow using the benefit of hindsight to get it right.

                But if ExitOnClose is doing that, then the how & why is indeed interesting -- because it doesn't seem to be using the timestamp of historical day bars trick.
                I've honestly struggled to understand this - at least from NinjaTrader_CodyB's perspective. It's clear that an adjustment was made for an early close on July 4. I did not tell NT that the session ended early that day - some how it knew, for whatever reason. So somewhere in NT7 there's some indicator that a session is ending early.

                Comment


                  #9
                  Originally posted by cgeorgan View Post
                  I've honestly struggled to understand this - at least from NinjaTrader_CodyB's perspective. It's clear that an adjustment was made for an early close on July 4 inside the Strategy Analyzer.
                  What behavior does Market Replay show for July 4th?

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by judysamnt7, 03-13-2023, 09:11 AM
                  4 responses
                  59 views
                  0 likes
                  Last Post DynamicTest  
                  Started by ScottWalsh, Today, 06:52 PM
                  4 responses
                  36 views
                  0 likes
                  Last Post ScottWalsh  
                  Started by olisav57, Today, 07:39 PM
                  0 responses
                  7 views
                  0 likes
                  Last Post olisav57  
                  Started by trilliantrader, Today, 03:01 PM
                  2 responses
                  21 views
                  0 likes
                  Last Post helpwanted  
                  Started by cre8able, Today, 07:24 PM
                  0 responses
                  10 views
                  0 likes
                  Last Post cre8able  
                  Working...
                  X