Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Exit at specific time

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

    Exit at specific time

    Hi - I need my strategy to stop trading at a specific time. I have the following code.

    Code:
                if (ToTime(Time[0]) <= 93000 || ToTime(Time[0]) >= 153000)
                {
                    if (Position.MarketPosition == MarketPosition.Long)
                    {
                        Print("Exit Long at the end of the day");
                        ExitLong();
                    }
                    
                    if (Position.MarketPosition == MarketPosition.Short)
                    {
                        Print("Exit Short at the end of the day");
                        ExitShort();    
                    }
    
                    return;
                }
    It generally works but, when I run a backtest in strategy analyzer, I see that my strategy is exiting at 15:35 and not at 15:30. I have on bar close set to false. I am using 5 minute bars.

    Not sure if this is something specific to running a backtest or something I am goind wrong.

    Any ideas?

    Onn

    #2
    Onn, the rule would trigger correct, but be filled on the next bar then in backtesting - CalculateOnBarClose is always 'true' here. So to exit earlier, you would need to submit the exit then already on the 1525 bar by modifying your time condition.

    Comment


      #3
      I see - just to make sure I understand. In simulated/live mode, connected to a feed, I'd set the exit to 1530 and it would exit at 1530 - right?

      Comment


        #4
        Correct - as long as you're CalculateOnBarClose = false.
        MatthewNinjaTrader Product Management

        Comment


          #5
          I see - just to make sure I understand. In simulated/live mode, connected to a feed, I'd set the exit to 1530 and it would exit at 1530 - right?
          In BT it exits at 15:30 plus some seconds (as would be in live with CalculateOnBarClose = true). It shows you an exit at the beginning of 15:35 bar!
          p.s.
          Becuase of this behavior of BT its better to have CalculateOnBarClose = true.

          Comment


            #6
            aha - thanks for that!

            Comment


              #7
              exit long position at specific time

              Trying to exit long positions at 9am or 9:05am with 5 min bars.

              Have the following code yet it stays in open positions until the close. Any suggestions?

              {
              if ((ToTime(Time[0]) >= 085500) || (ToTime(Time[0]) <= 090000))
              { if (Position.MarketPosition == MarketPosition.Long)


              {
              ExitLong("MACDlongexit");
              }

              Comment


                #8
                Hello kennyb59,

                Thanks for posting and your question.

                I'm not sure of your use of the {} and () so I have modified your code to make things look how {} and () are typically used.

                Code:
                if ((ToTime(Time[0]) >= 085500) || (ToTime(Time[0]) <= 090000) && if (Position.MarketPosition == MarketPosition.Long) 
                {
                ExitLong("MACDlongexit");
                }
                The statement now reads, if the current bars time is greater than or equal to 8:55 OR if the Current bar is Less than or equal to 9:00 AND if you are in a long position, exit "Maclongexit". By using the logic || between the two time evaluations it will always immediately exit because either condition will be true at any time.

                I think you want to say if the current bar time is greater than or equal to 8:55 AND if the Current bar is Less than or equal to 9:00 AND if you are in a long position, exit "Maclongexit".

                Code:
                 if ((ToTime(Time[0]) >= 085500) [COLOR="Red"]&&[/COLOR] (ToTime(Time[0]) <= 090000) && if (Position.MarketPosition == MarketPosition.Long) 
                {
                ExitLong("MACDlongexit");
                }
                As a good practice you may also want to use print statements when your code is not executing as expected. In this example I would have used: Print ("BarTime: "+Time[0]+" Exiting") ; // just before the exitLong. That way you know the logic processed to that point. The output of the print statement is to the "Output" window found under Tool>output Window.

                Another tool to use for strategies is TraceOrders Please see this link for implementation: http://www.ninjatrader.com/support/h...raceorders.htm and this link for a good explanation http://www.ninjatrader.com/support/f...ead.php?t=3627

                Please let me know if I can be of further assistance.

                Comment


                  #9
                  Paul,

                  Thx for the reply. I am still getting an error for the if statement before Market Position.

                  Ken

                  Comment


                    #10
                    Hello Ken,

                    My mistake, here is the corrected code:

                    Code:
                    if ((ToTime(Time[0]) >= 085500) || (ToTime(Time[0]) <= 090000) && (Position.MarketPosition == MarketPosition.Long)) 
                    {
                    ExitLong("MACDlongexit");
                    }
                    Please let me know if you need further assistance.

                    Comment


                      #11
                      Paul,

                      Thx again. Your written logic is correct. The coding now compiles Because of the or || statement, it is exiting on the next bar after entry. If I put in a &&, it does not exit.

                      Ken

                      Comment


                        #12
                        Hi Ken,

                        Thanks for your reply.

                        I copied the incorrect section of the code and meant to have you use the &&. I am glad you changed it to that. With the OR logic I would expect it to exit on the next bar. I understand when you use the AND logic that it is not exiting.

                        Part of the answer may be with the state of CalculateOnBarClose. If it is set true, then you will only get one chance to exit when the bar closes at 9:00:00. If CalculateOnbarClose is set false, you will surely get many chances within the time to to exit. Other options you can explore are to lengthen out the exit time span or change the time to 1 minute bars.

                        Have you applied the Print statements and the TraceOrders? Using those tools you could determine if the time logic is processing down to the exit statement and the TraceOrder would help you see if the Exit order is being accepted or rejected.

                        Please let me know if I can be of further assistance.

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                        0 responses
                        648 views
                        0 likes
                        Last Post Geovanny Suaza  
                        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                        0 responses
                        369 views
                        1 like
                        Last Post Geovanny Suaza  
                        Started by Mindset, 02-09-2026, 11:44 AM
                        0 responses
                        109 views
                        0 likes
                        Last Post Mindset
                        by Mindset
                         
                        Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                        0 responses
                        573 views
                        1 like
                        Last Post Geovanny Suaza  
                        Started by RFrosty, 01-28-2026, 06:49 PM
                        0 responses
                        576 views
                        1 like
                        Last Post RFrosty
                        by RFrosty
                         
                        Working...
                        X