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 Mindset, 04-21-2026, 06:46 AM
        0 responses
        93 views
        0 likes
        Last Post Mindset
        by Mindset
         
        Started by M4ndoo, 04-20-2026, 05:21 PM
        0 responses
        138 views
        0 likes
        Last Post M4ndoo
        by M4ndoo
         
        Started by M4ndoo, 04-19-2026, 05:54 PM
        0 responses
        68 views
        0 likes
        Last Post M4ndoo
        by M4ndoo
         
        Started by cmoran13, 04-16-2026, 01:02 PM
        0 responses
        123 views
        0 likes
        Last Post cmoran13  
        Started by PaulMohn, 04-10-2026, 11:11 AM
        0 responses
        73 views
        0 likes
        Last Post PaulMohn  
        Working...
        X