Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

PSAR flips not working correctly

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

    PSAR flips not working correctly

    Does anyone have any idea why this code makes my trades exit early? I just want it to buy & exit short when PSAR flips down and sell & exit long when psar flips up.

    Code:
            protected override void Initialize()
            {
    
                CalculateOnBarClose = true;
            }
    
            /// <summary>
            /// Called on each bar update event (incoming tick)
            /// </summary>
            protected override void OnBarUpdate()
            {
                // Condition set 1
                if (ParabolicSAR(0.02, 0.2, 0.02)[2] > Close[2]
                    && ParabolicSAR(0.02, 0.2, 0.02)[1] < Close[1])
                {
                    ExitShort("", "");
                    EnterLong(Lots, "");
                }
    
                // Condition set 2
                if (ParabolicSAR(0.02, 0.2, 0.02)[2] < Close[2]
                    && ParabolicSAR(0.02, 0.2, 0.02)[1] > Close[1])
                {
                    ExitLong("", "");
                    EnterShort(Lots, "");
                }
            }
    Attached Files

    #2
    Looks correct to me. You are checking 1 bar ago and 2 bars ago for the flip. When the flip is identified it places the trades. The trades execute on the next bar because the signal bar is already closed in backtesting.
    Josh P.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_Josh View Post
      Looks correct to me. You are checking 1 bar ago and 2 bars ago for the flip. When the flip is identified it places the trades. The trades execute on the next bar because the signal bar is already closed in backtesting.
      Josh,

      But if you look at the screenshot, it shows the trades closing well before a PSAR flip occurs. I don't understand what's wrong with the exits.

      Comment


        #4
        Texasnomad, what Josh was saying is that you're checking the PSAR values from one bar back and two bars back, NOT the current bar and one bar back. Changing the values from 2 and 1 to 1 and 0 would fix this.

        In addition, the early exits are caused by ExitOnClose being set to true as indicated by "Exit on close" on the chart.

        You can try changing these settings and see if the exits/entries are closer to where they should be.
        AustinNinjaTrader Customer Service

        Comment


          #5
          Originally posted by NinjaTrader_Austin View Post
          Texasnomad, what Josh was saying is that you're checking the PSAR values from one bar back and two bars back, NOT the current bar and one bar back. Changing the values from 2 and 1 to 1 and 0 would fix this.
          I don't want the strategy to use the current bar. I want to see 2 bars to the left with PSAR above the price, 1 bar to the left with PSAR below the price, then I'll buy at the open of the current bar. This is what this code does, correct?

          In addition, the early exits are caused by ExitOnClose being set to true as indicated by "Exit on close" on the chart.
          How do I eliminate ExitOnClose? I don't see any variables or functions of that name in my code.

          Comment


            #6
            Nevermind... I just found exitonclose in the strategy options. I didn't realize it was an external parameter to the code.

            Comment


              #7
              Originally posted by texasnomad View Post
              I don't want the strategy to use the current bar. I want to see 2 bars to the left with PSAR above the price, 1 bar to the left with PSAR below the price, then I'll buy at the open of the current bar. This is what this code does, correct?
              Correct. Sorry for not understanding what you wanted to do. Did changing Exit on close resolve the early exit issue?
              AustinNinjaTrader Customer Service

              Comment


                #8
                Originally posted by NinjaTrader_Austin View Post
                Correct. Sorry for not understanding what you wanted to do. Did changing Exit on close resolve the early exit issue?
                Yes - everything works as designed now. Thanks for your timely help.

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                0 responses
                558 views
                0 likes
                Last Post Geovanny Suaza  
                Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                0 responses
                324 views
                1 like
                Last Post Geovanny Suaza  
                Started by Mindset, 02-09-2026, 11:44 AM
                0 responses
                101 views
                0 likes
                Last Post Mindset
                by Mindset
                 
                Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                0 responses
                545 views
                1 like
                Last Post Geovanny Suaza  
                Started by RFrosty, 01-28-2026, 06:49 PM
                0 responses
                547 views
                1 like
                Last Post RFrosty
                by RFrosty
                 
                Working...
                X