Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

If Conditions Not Working

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

    If Conditions Not Working

    I have completed enough of my strategy to at least put it on a backtest to see how it is progressing. It still has a very long way to go before being finished. That being said, it is clear that there are some issues with the code that need to be dealt with.

    In the backtest, way too many trades are being placed and it does not line up with what I expect my logic to say. Below are the if statements:

    Code:
    			/* Conditions for Long Entry */
                if (Close[0] > Open[0]
                    && Close[0] > MAEnvelopes(MAEPerc, 3, 25).Upper[0]
                    && Aroon(14).Up[0] == 100
                    && Close[0] > ParabolicSAR(0.02, 0.2, 0.02)[0]
                    && Close[0] > Ichimoku(9, 26, 52).KijunSen[0]
                    && Close[0] > SMA(SMAPeriod)[0]
    				&& !barResetUp)
                {	
    				SignalCandleLong = true;
    			}
    			
    			if (SignalCandleLong == true)
    			{			
    				signalHigh = High[0];
    				signalLow = Low[0];
    				
    				//Order Entries A & B as well as Order for Initial Stop Loss
                    EnterLongStop(DefaultQuantity, TrendPilotPricesLong(EntryPerc, StopPerc, TargetPerc).LongEntry[0], "LongEntryA");
                    EnterLongStop(DefaultQuantity, TrendPilotPricesLong(EntryPerc, StopPerc, TargetPerc).LongEntry[0], "LongEntryB");
    				ExitLongLimit(TargetPriceLong,"LongLimit","LongEntryA");
    				ExitLongStop(StopPriceLong,"LongStopA","LongEntryA");
    				ExitLongStop(StopPriceLong,"LongStopB","LongEntryB");
    				barResetUp = true;
                }
    Here is a link to show you the chart created with the backtest.



    I have placed an Aroon indicator on the chart. It is very clear that several orders are being placed even if the AroonUp does not have a value of 100. This tells me that there is something I have done wrong in the logic somehow.

    Could somebody please take a look at this and see what may be going wrong?

    Thank you for your help.

    #2
    jg123,

    You are setting the SignalCandleLong to true, and you are using an IF statement.
    However, you don't reset your bool back to false, meaning it will always remain true and the IF statement will always become true.

    You may want to reset the bool to false after you have placed your trades so that it is not true on the next bar update.
    Cal H.NinjaTrader Customer Service

    Comment


      #3
      Thank you, Cal

      That definitely changed the results. I am still getting entries when I would not expect it.

      Here is another picture of what I am getting now. It is a closeup just to make it easier to see the signal. Again, it is clear that the AroonUp does not have a value of 100.

      Are you able to see other things wrong with this portion of the code?

      Here is what it looks like now that I have made the modification that you mentioned (assuming I understood you correctly)

      I have added a little bit more to the segment of code that I am sharing, just to give you a bit more clarity if needed.

      Code:
              protected override void OnBarUpdate()
              {
      			/* This can not be in Initialize - it will cause errors.
      			Sets the variables for initial stop loss and initial Entry A target */
      			TargetPriceLong = High[0]+((High[0]-Low[0])*TargetPerc);
      			StopPriceLong = SMA(sMAPeriod)[0]-((High[0]-Low[0])*StopPerc);
      			LongEntryA = EnterLongStop(DefaultQuantity, TrendPilotPricesLong(EntryPerc, StopPerc, TargetPerc).LongEntry[0], "LongEntryA");
      			LongEntryB = EnterLongStop(DefaultQuantity, TrendPilotPricesLong(EntryPerc, StopPerc, TargetPerc).LongEntry[0], "LongEntryB");
      			LongStopA = ExitLongStop(StopPriceLong,"LongStopA","LongEntryA");
      			LongStopB = ExitLongStop(StopPriceLong,"LongStopB","LongEntryB");
      			
      			
      			/* Conditions for Long Entry */
                  if (Close[0] > Open[0]
                      && Close[0] > MAEnvelopes(MAEPerc, 3, 25).Upper[0]
                      && Aroon(14).Up[0] == 100
                      && Close[0] > ParabolicSAR(0.02, 0.2, 0.02)[0]
                      && Close[0] > Ichimoku(9, 26, 52).KijunSen[0]
                      && Close[0] > SMA(SMAPeriod)[0]
      				&& !barResetUp)
                  {	
      				SignalCandleLong = true;
      			}
      			
      			if (SignalCandleLong == true)
      			{			
      				signalHigh = High[0];
      				signalLow = Low[0];
      				
      				//Order Entries A & B as well as Order for Initial Stop Loss
                      EnterLongStop(DefaultQuantity, TrendPilotPricesLong(EntryPerc, StopPerc, TargetPerc).LongEntry[0], "LongEntryA");
                      EnterLongStop(DefaultQuantity, TrendPilotPricesLong(EntryPerc, StopPerc, TargetPerc).LongEntry[0], "LongEntryB");
      				ExitLongLimit(TargetPriceLong,"LongLimit","LongEntryA");
      				ExitLongStop(StopPriceLong,"LongStopA","LongEntryA");
      				ExitLongStop(StopPriceLong,"LongStopB","LongEntryB");
      				barResetUp = true;
      				SignalCandleLong = false;
                  }
      			
      
      			// Required reset in order to establish a new trend
      			if (Close[0] < Ichimoku(9,26,52).KijunSen[0])
      			{
      				// Allows for a new trend to be determined after it has closed below the KijunSen
      				barResetUp = false;
      				// ***NEED TO SET IT TO CANCEL ANY PENDING ORDERS AND CLOSE ANY OPEN ORDERS***
      			}

      Comment


        #4
        jg123,

        I don't see the link for the second picture.
        Cal H.NinjaTrader Customer Service

        Comment


          #5
          Whoops..Sorry.

          Here it is:

          Comment


            #6
            And here is a broader picture of the same screenshot posted in the last post:

            Comment


              #7
              jg123,

              I would run TraceOrders = true; in your Initialize and see what time those orders were submitted.
              Since you are placing Limit and Stop orders, they may be placed before the actual execution took place and just now getting filled at those times.
              Cal H.NinjaTrader Customer Service

              Comment


                #8
                I am running it on a daily chart - therefore every order was placed at 11:00 pm (Euro Time).

                I will work on trying to get my Cancel Orders logic corrected. I am pretty confident that this logic is not correct anyway, and therefore your theory could definitely be correct.

                I will open those questions in a new thread.

                Comment


                  #9
                  Originally posted by jg123 View Post
                  Thank you, Cal

                  That definitely changed the results. I am still getting entries when I would not expect it.

                  Here is another picture of what I am getting now. It is a closeup just to make it easier to see the signal. Again, it is clear that the AroonUp does not have a value of 100.

                  Are you able to see other things wrong with this portion of the code?

                  Here is what it looks like now that I have made the modification that you mentioned (assuming I understood you correctly)

                  I have added a little bit more to the segment of code that I am sharing, just to give you a bit more clarity if needed.

                  Code:
                          protected override void OnBarUpdate()
                          {
                              /* This can not be in Initialize - it will cause errors.
                              Sets the variables for initial stop loss and initial Entry A target */
                              TargetPriceLong = High[0]+((High[0]-Low[0])*TargetPerc);
                              StopPriceLong = SMA(sMAPeriod)[0]-((High[0]-Low[0])*StopPerc);
                  [COLOR=Red]            LongEntryA = EnterLongStop(DefaultQuantity, TrendPilotPricesLong(EntryPerc, StopPerc, TargetPerc).LongEntry[0], "LongEntryA");
                              LongEntryB = EnterLongStop(DefaultQuantity, TrendPilotPricesLong(EntryPerc, StopPerc, TargetPerc).LongEntry[0], "LongEntryB");
                              LongStopA = ExitLongStop(StopPriceLong,"LongStopA","LongEntryA");
                              LongStopB = ExitLongStop(StopPriceLong,"LongStopB","LongEntryB");[/COLOR]
                              
                              
                              /* Conditions for Long Entry */
                              if (Close[0] > Open[0]
                                  && Close[0] > MAEnvelopes(MAEPerc, 3, 25).Upper[0]
                                  && Aroon(14).Up[0] == 100
                                  && Close[0] > ParabolicSAR(0.02, 0.2, 0.02)[0]
                                  && Close[0] > Ichimoku(9, 26, 52).KijunSen[0]
                                  && Close[0] > SMA(SMAPeriod)[0]
                                  && !barResetUp)
                              {    
                                  SignalCandleLong = true;
                              }
                              
                              if (SignalCandleLong == true)
                              {            
                                  signalHigh = High[0];
                                  signalLow = Low[0];
                                  
                                  //Order Entries A & B as well as Order for Initial Stop Loss
                                  EnterLongStop(DefaultQuantity, TrendPilotPricesLong(EntryPerc, StopPerc, TargetPerc).LongEntry[0], "LongEntryA");
                                  EnterLongStop(DefaultQuantity, TrendPilotPricesLong(EntryPerc, StopPerc, TargetPerc).LongEntry[0], "LongEntryB");
                                  ExitLongLimit(TargetPriceLong,"LongLimit","LongEntryA");
                                  ExitLongStop(StopPriceLong,"LongStopA","LongEntryA");
                                  ExitLongStop(StopPriceLong,"LongStopB","LongEntryB");
                                  barResetUp = true;
                                  SignalCandleLong = false;
                              }
                              
                  
                              // Required reset in order to establish a new trend
                              if (Close[0] < Ichimoku(9,26,52).KijunSen[0])
                              {
                                  // Allows for a new trend to be determined after it has closed below the KijunSen
                                  barResetUp = false;
                                  // ***NEED TO SET IT TO CANCEL ANY PENDING ORDERS AND CLOSE ANY OPEN ORDERS***
                              }
                  See what I have emphasized in red. Those are unconditional orders that you are issuing on every bar update. They will be filled on any and every candle whenever your processing gates (EntriesPerDirection and EntryHandling) allow.

                  Comment


                    #10
                    Originally posted by koganam View Post
                    See what I have emphasized in red. Those are unconditional orders that you are issuing on every bar update. They will be filled on any and every candle whenever your processing gates (EntriesPerDirection and EntryHandling) allow.
                    Thank you very much.

                    I did an experiment to figure out how to fix it, but that also did not work. This is what I now have and now there are no trades being placed. haha

                    Code:
                            protected override void OnBarUpdate()
                            {
                    			/* Conditions for Long Entry */
                                if (Close[0] > Open[0]
                                    && Close[0] > MAEnvelopes(MAEPerc, 3, 25).Upper[0]
                                    && Aroon(14).Up[0] == 100
                                    && Close[0] > ParabolicSAR(0.02, 0.2, 0.02)[0]
                                    && Close[0] > Ichimoku(9, 26, 52).KijunSen[0]
                                    && Close[0] > SMA(SMAPeriod)[0]
                    				&& !barResetUp)
                                {	
                    				SignalCandleLong = true;
                    			}
                    			
                    			if (SignalCandleLong == true)
                    			{			
                    				signalHigh = High[0];
                    				signalLow = Low[0];
                    				
                    				//Order Entries A & B as well as Order for Initial Stop Loss
                                    LongEntryA = EnterLongStop(DefaultQuantity, TrendPilotPricesLong(EntryPerc, StopPerc, TargetPerc).LongEntry[0], "LongEntryA");
                                    LongEntryB = EnterLongStop(DefaultQuantity, TrendPilotPricesLong(EntryPerc, StopPerc, TargetPerc).LongEntry[0], "LongEntryB");
                    				LongLimit = ExitLongLimit(TargetPriceLong,"LongLimit","LongEntryA");
                    				LongStopA = ExitLongStop(StopPriceLong,"LongStopA","LongEntryA");
                    				LongStopB = ExitLongStop(StopPriceLong,"LongStopB","LongEntryB");
                    				barResetUp = true;
                    				SignalCandleLong = false;
                                }

                    Comment


                      #11
                      I tried running the test again and it worked this time. I didn't do anything different so I don't know why it didn't work the first time, maybe just a glitch.

                      Comment


                        #12
                        Originally posted by jg123 View Post
                        Thank you very much.

                        I did an experiment to figure out how to fix it, but that also did not work. This is what I now have and now there are no trades being placed. haha

                        Code:
                                protected override void OnBarUpdate()
                                {
                                    /* Conditions for Long Entry */
                                    if (Close[0] > Open[0]
                                        && Close[0] > MAEnvelopes(MAEPerc, 3, 25).Upper[0]
                                        && Aroon(14).Up[0] == 100
                                        && Close[0] > ParabolicSAR(0.02, 0.2, 0.02)[0]
                                        && Close[0] > Ichimoku(9, 26, 52).KijunSen[0]
                                        && Close[0] > SMA(SMAPeriod)[0]
                                        && !barResetUp)
                                    {    
                                        SignalCandleLong = true;
                                    }
                                    
                                    if (SignalCandleLong == true)
                                    {            
                                        signalHigh = High[0];
                                        signalLow = Low[0];
                                        
                                        //Order Entries A & B as well as Order for Initial Stop Loss
                                        LongEntryA = EnterLongStop(DefaultQuantity, TrendPilotPricesLong(EntryPerc, StopPerc, TargetPerc).LongEntry[0], "LongEntryA");
                                        LongEntryB = EnterLongStop(DefaultQuantity, TrendPilotPricesLong(EntryPerc, StopPerc, TargetPerc).LongEntry[0], "LongEntryB");
                                        LongLimit = ExitLongLimit(TargetPriceLong,"LongLimit","LongEntryA");
                                        LongStopA = ExitLongStop(StopPriceLong,"LongStopA","LongEntryA");
                                        LongStopB = ExitLongStop(StopPriceLong,"LongStopB","LongEntryB");
                                        barResetUp = true;
                                        SignalCandleLong = false;
                                    }
                        Which most probably means that your entry conditions are so restrictive that they are never satisfied. Print() your filter values to the Output Window, and verify that those conditions can ever be met. (As a quick test, take out the Aroon indicator filter completely and see what happens: it looks like the most restrictive filter to me).

                        Comment


                          #13
                          Thank you Koganam.

                          I was able to fix that problem. There definitely always runs the issue that the entry conditions are too restrictive. If that were the case with this, then it would just be based on my own ability to code as my conditions are the way that I trade every single day, day in and day out.

                          Comment

                          Latest Posts

                          Collapse

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