Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Exit afterwards

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

    Exit afterwards

    I would like to limit the strategy to trade at certain times
    Code:
    if ((ToTime(Time[0]) >= 90000 && ToTime(Time[0]) < 120000) || (ToTime(Time[0]) >= 140000 && ToTime(Time[0]) < 151000))
    Also, instead of ExitOnClose I have
    Code:
    if (( ToTime(Time[0]) >= 123200 && Position.MarketPosition != MarketPosition.Flat) || (ToTime(Time[0]) >= 151200 && Position.MarketPosition != MarketPosition.Flat)) 
                {
                    if ( Position.MarketPosition == MarketPosition.Long )
                    {
                        ExitLong();
                    }
                    else
                    {
                        ExitShort();
                    }
                }
    I cannot figure out why I am getting a whole bunch of trades towards the close (attachment). Thanks in advance
    Attached Files

    #2
    Hello 2Look4me,

    Thank you for your inquiry.

    Can you please provide a sample script that replicates this behavior so we may test on our end?
    Zachary G.NinjaTrader Customer Service

    Comment


      #3
      Hi Zachary,

      Thanks for the assistance. I'm enclosing a sample script giving me that problem.
      Attached Files

      Comment


        #4
        Originally posted by 2Look4me View Post
        I would like to limit the strategy to trade at certain times
        Code:
        if ((ToTime(Time[0]) >= 90000 && ToTime(Time[0]) < 120000) || (ToTime(Time[0]) >= 140000 && ToTime(Time[0]) < 151000))
        Also, instead of ExitOnClose I have
        Code:
        if (( ToTime(Time[0]) >= 123200 && Position.MarketPosition != MarketPosition.Flat) || (ToTime(Time[0]) >= 151200 && Position.MarketPosition != MarketPosition.Flat)) 
                    {
                        if ( Position.MarketPosition == MarketPosition.Long )
                        {
                            ExitLong();
                        }
                        else
                        {
                            ExitShort();
                        }
                    }
        I cannot figure out why I am getting a whole bunch of trades towards the close (attachment). Thanks in advance
        Your exit block translates to: "After 1232hrs, exit every trade on the next tick after the position is confirmed."

        The code is doing what you programmed it to do. What did you actually intend it to do?

        Comment


          #5
          Hello 2Look4me,

          You have logic in your strategy that enters trades after 12:32 PM.

          For instance, you have logic that enters long or short when Time[0] time is greater than 2:30 PM and less than 3:10 PM.

          I would suggest utilizing Prints to see what conditions are being called on what bars. We do have a post on our support forum detailing debugging NinjaScript code: http://ninjatrader.com/support/forum...58&postcount=1
          Zachary G.NinjaTrader Customer Service

          Comment


            #6
            koganam/Zachary, thanks for the replies.

            Yes, the logic is to enter trades based on certain conditions between 9:00 and 12:00 pm, then "After 1232hrs, exit every trade on the next tick after the position is confirmed."

            Then again, based on the same previous conditions to enter trades between 2:00 pm and 3:10pm and "After 1512hrs, exit every trade on the next tick after the position is confirmed."

            I did try debugging it as well as TraceOrders with no errors either in the log file. The problem I'm having is that trades placed between 2:00pm and 3:10pm occur on every single bar (screenshot). So if my script is not reflective of the logic how can I rework it?

            Comment


              #7
              Hello 2Look4me,

              I would suggest utilizing signal names on your orders so you can see what order is being placed and what bar number the order is called on.

              I have provided a sample that does this for you.

              As a test on the ES 03-16 on a 1 minute chart:

              14:37 EnterLong2700
              14:38 ExitLong2701
              14:40 EnterLong2703
              14:41 ExitLong2704
              14:43 EnterLong2706
              14:44 ExitLong2707
              14:46 EnterLong2709
              14:47 ExitLong2710

              The logic in your strategy is doing what you have programmed it to do:
              1. Is the current time greater or equal to 12:32 and you're not flat OR the current time is greater or equal to 15:12 and you're not flat?
                FALSE

              2. Is the current time greater or equal to 9:00 and less than 12:30 OR is the current time greater than 14:30 and less that 15:10?
                TRUE

              3. Are you currently in a flat position?
                TRUE

              4. Are the conditions in the flat position check true?
                TRUE, so EnterLong (EnterLong2700)

              5. Are you currently in a long position?
                FALSE (historically, OnBarUpdate() is called only at the close of a bar; the position doesn't exist yet)

              6. Are you currently in a flat position?
                TRUE

              7. Are the conditions in the flat position check true?
                FALSE

              8. Are you currently in a short position?
                FALSE



              Next OnBarUpdate() call:
              1. Is the current time greater or equal to 12:32 and you're not flat OR the current time is greater or equal to 15:12 and you're not flat?
                TRUE

              2. Are you currently in a long position?
                TRUE, so ExitLong (ExitLong2701)

              3. Is the current time greater or equal to 9:00 and less than 12:30 OR is the current time greater than 14:30 and less that 15:10?
                TRUE

              4. Are you currently in a flat position?
                FALSE

              5. Are you currently in a long position?
                TRUE

              6. Is the condition in your long position check true?
                FALSE

              7. Are you currently in a flat position?
                FALSE

              8. Are you currently in a short position?
                FALSE



              The next call of OnBarUpdate() repeats all the checks all over again. The reason why you enter long again is because the conditions to enter long are true.

              If you want to stop trading at a specific time, you will need a way to prevent your second if condition being true so the entries won't be called. A boolean would do the trick.

              Ensure to reset this variable when needed so your entry logic runs again.
              Attached Files
              Zachary G.NinjaTrader Customer Service

              Comment


                #8
                Zachary, thanks for taking the time for the thorough explanation.

                After the first time frame 9:00 am - 12:00 pm all positions are flat at 12:32p(that's working). Then the next time frame 2:00pm - 3:10pm all positions need to be flat by 3:12 pm (not working). But these second time frame trades are all immediately closed upon entry because it is after 12:32pm

                The question I have is how to reset 12:32pm, so the trades during 2:00 pm - 3:10pm are not flat, but until 3:12pm?

                Code:
                if (( ToTime(Time[0]) >= 123200 && Position.MarketPosition != MarketPosition.Flat) || (ToTime(Time[0]) >= 151200 && Position.MarketPosition != MarketPosition.Flat))             
                            {
                              //Do This
                            }

                Comment


                  #9
                  Originally posted by 2Look4me View Post
                  Zachary, thanks for taking the time for the thorough explanation.

                  After the first time frame 9:00 am - 12:00 pm all positions are flat at 12:32p(that's working). Then the next time frame 2:00pm - 3:10pm all positions need to be flat by 3:12 pm (not working). But these second time frame trades are all immediately closed upon entry because it is after 12:32pm

                  The question I have is how to reset 12:32pm, so the trades during 2:00 pm - 3:10pm are not flat, but until 3:12pm?

                  Code:
                  if (( ToTime(Time[0]) >= 123200 && Position.MarketPosition != MarketPosition.Flat) || (ToTime(Time[0]) >= 151200 && Position.MarketPosition != MarketPosition.Flat))             
                              {
                                //Do This
                              }
                  If you do not want your automatic exits between 2PM and 3:10 PM, then you have to exclude that time slot from the process.
                  Code:
                   ToTime(Time[0]) >= 123200 && Position.MarketPosition != MarketPosition.Flat && ToTime(Time[0]) < 140000 ...
                  and so on.

                  Delimit your time slots correctly for what happens to what. It will be much easier if you made your code less procedural and more structured. Namely, separate your time slot code, to enable/disable bool variables, then check the bool variables to process trade action.
                  Last edited by koganam; 12-19-2015, 12:14 PM.

                  Comment


                    #10
                    koganam,

                    Your explanation worked and thanks for breaking it down into a more comprehensive way.

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                    0 responses
                    656 views
                    0 likes
                    Last Post Geovanny Suaza  
                    Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                    0 responses
                    371 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
                    574 views
                    1 like
                    Last Post Geovanny Suaza  
                    Started by RFrosty, 01-28-2026, 06:49 PM
                    0 responses
                    579 views
                    1 like
                    Last Post RFrosty
                    by RFrosty
                     
                    Working...
                    X