Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Stop Trading Activity 1 Hour before end of trading day

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

    Stop Trading Activity 1 Hour before end of trading day

    Hello NT Team,

    I want to stop trading activity 1 hour prior to market day trading closing time, and I came across this code in your NT8 DOC, but when I added it is shows me several erros

    This is the code I found : https://ninjatrader.com/support/helpGuides/nt8/

    Code:
    private SessionIterator sessionIterator;
    
    protected override void OnStateChange()
    {
      if (State == State.DataLoaded)
      {
        //stores the sessions once bars are ready, but before OnBarUpdate is called
        sessionIterator = new SessionIterator(Bars);
      }
    }
    
    protected override void OnBarUpdate()
    {
      // Only process strategy logic up until three hours prior to the end of the trading day at the exchange
      if (DateTime.Now <= sessionIterator.GetTradingDa yEndLocal(Bars.SessionIterator.ActualTradingDayExc hange).AddHours(-3))
      {
          // Strategy logic here
      }
    }
    I did all what I'm supposed to do, declarations, initiations and everything just as mentioned here, but same error which I attached here

    Can you please tell me how to do it the right way ?

    This is the code snippet I'm using now :


    Code:
    if (DateTime.Now <= sessionIterator.GetTradingDayEndLocal(Bars.Session Iterator.ActualTradingDayExchange).AddHours(-1))
    {
       TimeUp = true;
    }
    Thank you very much

    #2
    Hello MohammedAmine,

    The error is specifically because of this line:

    Bars.SessionIterator.ActualTradingDayExchange

    You would need to remove the Bars.S and make it just lowercase sessionIterator.

    Code:
    sessionIterator.ActualTradingDayExchange
    Alternatively you could just use the ToTime method to make a time window.


    I look forward to being of further assistance.

    Comment


      #3
      Hello Jesse,

      Thanks for your reply, effectively when i removed the Bars. and turned s into lower case it removed the error, but still not working because when I printed this :
      Code:
      Print("Today Trading Will end at : " + sessionIterator.GetTradingDayEndLocal(sessionIterator.ActualTradingDayExchange));
      it printed an Old date of year 1800, u can check the attached image

      so how to know the REAL DayTrading End Date and Time ?

      Thank you

      Comment


        #4
        Never mind, I solved it, here was the missing line :

        Code:
        protected override void OnBarUpdate()
        {
          // on new bars session, find the next trading session
          if (Bars.IsFirstBarOfSession)
          {
            // use the current bar time to calculate the next session
            sessionIterator.GetNextSession(Time[0], true); // This was the missing line :)
        
            Print("The current session end of day is " + sessionIterator.ActualTradingDayEndLocal);
          }
        }

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by CarlTrading, 03-31-2026, 09:41 PM
        1 response
        45 views
        0 likes
        Last Post NinjaTrader_ChelseaB  
        Started by CarlTrading, 04-01-2026, 02:41 AM
        0 responses
        21 views
        0 likes
        Last Post CarlTrading  
        Started by CaptainJack, 03-31-2026, 11:44 PM
        0 responses
        31 views
        1 like
        Last Post CaptainJack  
        Started by CarlTrading, 03-30-2026, 11:51 AM
        0 responses
        50 views
        0 likes
        Last Post CarlTrading  
        Started by CarlTrading, 03-30-2026, 11:48 AM
        0 responses
        42 views
        0 likes
        Last Post CarlTrading  
        Working...
        X