Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Strategy placing entry on wrong bar

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

    Strategy placing entry on wrong bar

    Hi all,

    I'm having an issue with a strategy I'm backtesting and I need some help troubleshooting. The strategy is placing the entry stop order on a bar I don't want it to, usually several bars after the bar where I want it to be placed. See the attached screenshot for an example.

    I've tried troubleshooting by taking out individual conditions, printing indicator values and orders placement history in the output window, and by just walking through all the conditions one by one and comparing them to the backtested trades on the chart. I'm flummoxed.

    Here is the code:

    Code:
                // Long trades
                if (EMA(BarsArray[1],8)[0] > EMA(BarsArray[1],13)[0]
                && ToTime(Time[0])> ToTime(08,30,00)
                && ToTime(Time[0])< ToTime(14,0,00)
                && High[Math.Min(100000,Swing(SwingStrength).SwingHighBar(SwingBarsAgo,1,SwingBarsBack))] > High[Math.Min(100000,Swing(SwingStrength).SwingHighBar(SwingBarsAgo,2,SwingBarsBack))]
                && Low[Math.Max(0,Swing(SwingStrength).SwingLowBar(SwingBarsAgo,1,SwingBarsBack))] < Low[Math.Max(0,Swing(SwingStrength).SwingLowBar(SwingBarsAgo,2,SwingBarsBack))]  
                && Close[1] > (Close[Math.Max(0,Swing(SwingStrength).SwingHighBar(SwingBarsAgo,1,SwingBarsBack))] - Open[Math.Min(100000,Swing(2).SwingHighBar(SwingBarsAgo,1,SwingBarsBack))])*.382 + Open[Math.Min(100000,Swing(SwingStrength).SwingHighBar(SwingBarsAgo,1,SwingBarsBack))]
                && Close[0] >= Open[1]
                && Close[0] >= Close[Math.Max(0,Swing(3).SwingHighBar(SwingBarsAgo,1,SwingBarsBack))]   //Optional? Either this OR condition 2
                && (Close[0] - Open[0])/(High[0] - Low[0]) >= 0.6
                && Close[0]-Low[0] >= TickSize*10
                )
                {
                    EnterLongStop(DefaultQuantity, Close[0] + 1 * TickSize, "LongEntry1");
                    SetStopLoss("LongEntry1", CalculationMode.Price, Low[0]-1*TickSize, false);
                    SetProfitTarget("LongEntry1", CalculationMode.Price, (Close[0]-Low[0])*1.55+Close[0]);
                    StopMoveTriggerLong = (Close[0]-Low[0])*0.9+Close[0];
                }
    Thanks in advance for any help.
    Attached Files

    #2
    Originally posted by PhillyD View Post
    Hi all,

    I'm having an issue with a strategy I'm backtesting and I need some help troubleshooting. The strategy is placing the entry stop order on a bar I don't want it to, usually several bars after the bar where I want it to be placed. See the attached screenshot for an example.

    I've tried troubleshooting by taking out individual conditions, printing indicator values and orders placement history in the output window, and by just walking through all the conditions one by one and comparing them to the backtested trades on the chart. I'm flummoxed.

    Here is the code:

    Code:
                // Long trades
                if (EMA(BarsArray[1],8)[0] > EMA(BarsArray[1],13)[0]
                && ToTime(Time[0])> ToTime(08,30,00)
                && ToTime(Time[0])< ToTime(14,0,00)
                && High[Math.Min(100000,Swing(SwingStrength).SwingHighBar(SwingBarsAgo,1,SwingBarsBack))] > High[Math.Min(100000,Swing(SwingStrength).SwingHighBar(SwingBarsAgo,2,SwingBarsBack))]
                && Low[Math.Max(0,Swing(SwingStrength).SwingLowBar(SwingBarsAgo,1,SwingBarsBack))] < Low[Math.Max(0,Swing(SwingStrength).SwingLowBar(SwingBarsAgo,2,SwingBarsBack))]  
                && Close[1] > (Close[Math.Max(0,Swing(SwingStrength).SwingHighBar(SwingBarsAgo,1,SwingBarsBack))] - Open[Math.Min(100000,Swing(2).SwingHighBar(SwingBarsAgo,1,SwingBarsBack))])*.382 + Open[Math.Min(100000,Swing(SwingStrength).SwingHighBar(SwingBarsAgo,1,SwingBarsBack))]
                && Close[0] >= Open[1]
                && Close[0] >= Close[Math.Max(0,Swing(3).SwingHighBar(SwingBarsAgo,1,SwingBarsBack))]   //Optional? Either this OR condition 2
                && (Close[0] - Open[0])/(High[0] - Low[0]) >= 0.6
                && Close[0]-Low[0] >= TickSize*10
                )
                {
                    EnterLongStop(DefaultQuantity, Close[0] + 1 * TickSize, "LongEntry1");
                    SetStopLoss("LongEntry1", CalculationMode.Price, Low[0]-1*TickSize, false);
                    SetProfitTarget("LongEntry1", CalculationMode.Price, (Close[0]-Low[0])*1.55+Close[0]);
                    StopMoveTriggerLong = (Close[0]-Low[0])*0.9+Close[0];
                }
    Thanks in advance for any help.
    This looks like a backtest mode. Is that correct?

    Comment


      #3
      PhillyD, what's the highest swing strength here used in your script?

      Comment


        #4
        Yes sir. I'm backtesting this.

        Comment


          #5
          The swing strength is 3 throughout. Variable SwingStrength is set to 3, and then there is that one instance on the 8th line of code where I have it hardcoded to 3.

          edit: SwingBarsAgo is set to 0 and SwingBarsBack is set to 20.
          Last edited by PhillyD; 02-05-2015, 10:31 AM. Reason: Additional info

          Comment


            #6
            Originally posted by PhillyD View Post
            Hi all,

            I'm having an issue with a strategy I'm backtesting and I need some help troubleshooting. The strategy is placing the entry stop order on a bar I don't want it to, usually several bars after the bar where I want it to be placed. See the attached screenshot for an example.

            I've tried troubleshooting by taking out individual conditions, printing indicator values and orders placement history in the output window, and by just walking through all the conditions one by one and comparing them to the backtested trades on the chart. I'm flummoxed.

            Here is the code:

            Code:
                        // Long trades
                        if (EMA(BarsArray[1],8)[0] > EMA(BarsArray[1],13)[0]
                        && ToTime(Time[0])> ToTime(08,30,00)
                        && ToTime(Time[0])< ToTime(14,0,00)
                        && High[Math.Min(100000,Swing(SwingStrength).SwingHighBar(SwingBarsAgo,1,SwingBarsBack))] > High[Math.Min(100000,Swing(SwingStrength).SwingHighBar(SwingBarsAgo,2,SwingBarsBack))]
                        && Low[Math.Max(0,Swing(SwingStrength).SwingLowBar(SwingBarsAgo,1,SwingBarsBack))] < Low[Math.Max(0,Swing(SwingStrength).SwingLowBar(SwingBarsAgo,2,SwingBarsBack))]  
                        && Close[1] > (Close[Math.Max(0,Swing(SwingStrength).SwingHighBar(SwingBarsAgo,1,SwingBarsBack))] - Open[Math.Min(100000,Swing(2).SwingHighBar(SwingBarsAgo,1,SwingBarsBack))])*.382 + Open[Math.Min(100000,Swing(SwingStrength).SwingHighBar(SwingBarsAgo,1,SwingBarsBack))]
                        && Close[0] >= Open[1]
                        && [COLOR="Red"][B]Close[0] >= Close[Math.Max(0,Swing(3).SwingHighBar(SwingBarsAgo,1,SwingBarsBack))] [/B][/COLOR]//Optional? Either this OR condition 2
                        && (Close[0] - Open[0])/(High[0] - Low[0]) >= 0.6
                        && Close[0]-Low[0] >= TickSize*10
                        )
                        {
                            EnterLongStop(DefaultQuantity, Close[0] + 1 * TickSize, "LongEntry1");
                            SetStopLoss("LongEntry1", CalculationMode.Price, Low[0]-1*TickSize, false);
                            SetProfitTarget("LongEntry1", CalculationMode.Price, (Close[0]-Low[0])*1.55+Close[0]);
                            StopMoveTriggerLong = (Close[0]-Low[0])*0.9+Close[0];
                        }
            Thanks in advance for any help.
            Look at the line that I have marked in red.

            Comment


              #7
              Hi Koganam,

              As I understand it, that line says "if the close of the current bar is higher than or equal to the close of the most recent swing high bar".

              I already tried removing that line of code, and it didn't make a difference to that backtest example trade.

              I also inserted a Print(Close[Math.Max(0,Swing(3).SwingHighBar(SwingBarsAgo,1,Sw ingBarsBack))]) and the value that was shown in the output window was 2017, which is indeed the closing value of that bar. The closing price of the green bar where I want the entry order placed is higher than 2017, so this condition should be OK.

              Comment


                #8
                Originally posted by PhillyD View Post
                Hi Koganam,

                As I understand it, that line says "if the close of the current bar is higher than or equal to the close of the most recent swing high bar".

                I already tried removing that line of code, and it didn't make a difference to that backtest example trade.

                I also inserted a Print(Close[Math.Max(0,Swing(3).SwingHighBar(SwingBarsAgo,1,Sw ingBarsBack))]) and the value that was shown in the output window was 2017, which is indeed the closing value of that bar. The closing price of the green bar where I want the entry order placed is higher than 2017, so this condition should be OK.
                Yes, it is close to close. I shall have to take another look.

                Comment


                  #9
                  Hi Bertrand,

                  Any thoughts on this?

                  Comment


                    #10
                    PhillyD, the strategy will place the entry exactly when all of your entry conditions to do so are evaluating to 'true'. What you now need to better understand via debugging is why the entry condition is only true that late, or later as you had expected. What you would need to keep in mind that Swing indicator will have a delay of Strength bars generally to detect it's signals, as the # of Strength bars has to occur to the right side of the pivot found as well to confirm.

                    Comment


                      #11
                      Hi PhillyD,

                      I have a suggestion, To know for sure which bar you order is going in on, add code similar to this in with the orders when they are triggered.

                      BackColor = Color.PaleGreen;

                      This will color the bar background that has the order. Then you can work from there.

                      RJay

                      Originally posted by PhillyD View Post
                      Hi all,

                      I'm having an issue with a strategy I'm backtesting and I need some help troubleshooting. The strategy is placing the entry stop order on a bar I don't want it to, usually several bars after the bar where I want it to be placed. See the attached screenshot for an example.

                      I've tried troubleshooting by taking out individual conditions, printing indicator values and orders placement history in the output window, and by just walking through all the conditions one by one and comparing them to the backtested trades on the chart. I'm flummoxed.

                      Here is the code:

                      Code:
                                  // Long trades
                                  if (EMA(BarsArray[1],8)[0] > EMA(BarsArray[1],13)[0]
                                  && ToTime(Time[0])> ToTime(08,30,00)
                                  && ToTime(Time[0])< ToTime(14,0,00)
                                  && High[Math.Min(100000,Swing(SwingStrength).SwingHighBar(SwingBarsAgo,1,SwingBarsBack))] > High[Math.Min(100000,Swing(SwingStrength).SwingHighBar(SwingBarsAgo,2,SwingBarsBack))]
                                  && Low[Math.Max(0,Swing(SwingStrength).SwingLowBar(SwingBarsAgo,1,SwingBarsBack))] < Low[Math.Max(0,Swing(SwingStrength).SwingLowBar(SwingBarsAgo,2,SwingBarsBack))]  
                                  && Close[1] > (Close[Math.Max(0,Swing(SwingStrength).SwingHighBar(SwingBarsAgo,1,SwingBarsBack))] - Open[Math.Min(100000,Swing(2).SwingHighBar(SwingBarsAgo,1,SwingBarsBack))])*.382 + Open[Math.Min(100000,Swing(SwingStrength).SwingHighBar(SwingBarsAgo,1,SwingBarsBack))]
                                  && Close[0] >= Open[1]
                                  && Close[0] >= Close[Math.Max(0,Swing(3).SwingHighBar(SwingBarsAgo,1,SwingBarsBack))]   //Optional? Either this OR condition 2
                                  && (Close[0] - Open[0])/(High[0] - Low[0]) >= 0.6
                                  && Close[0]-Low[0] >= TickSize*10
                                  )
                                  {
                                      EnterLongStop(DefaultQuantity, Close[0] + 1 * TickSize, "LongEntry1");
                                      SetStopLoss("LongEntry1", CalculationMode.Price, Low[0]-1*TickSize, false);
                                      SetProfitTarget("LongEntry1", CalculationMode.Price, (Close[0]-Low[0])*1.55+Close[0]);
                                      StopMoveTriggerLong = (Close[0]-Low[0])*0.9+Close[0];
                                  }
                      Thanks in advance for any help.
                      RJay
                      NinjaTrader Ecosystem Vendor - Innovative Trading Solutions

                      Comment


                        #12
                        Hi Bertrand,

                        Regarding the strength of bars comment: I have this set to 3. In the screenshot in my first post you can see that the bar on which I want the entry order is 3 bars after the swing high, so at the time that bar closes, that particular swing high should be in place. However, the strategy is instead placing the order on the 5th bar after the swing high.

                        I've tried walking through each condition in the code to see what is/is not evaluating as TRUE. For both my desired entry bar and the actual entry bar, as far as I can tell all conditions evaluate to TRUE. What I don't understand is, why doesn't the strategy then place the order on the first bar to evaluate all conditions as TRUE (my desired bar)?

                        Comment


                          #13
                          Hi RJay,

                          I tried your suggestion and the strategy is indeed placing the entry order on the (incorrect) bar that I thought it was placing it on. Not sure how this will help me debug?
                          Attached Files

                          Comment


                            #14
                            PhillyD, now you would that that's ultimately the bar the renders your condition true to allow placement of the order.

                            'What I don't understand is, why doesn't the strategy then place the order on the first bar to evaluate all conditions as TRUE (my desired bar)?'

                            That's exactly what is happening here.

                            Another thought - I see your order would expire since not be placed by liveUntilCancelled then. Could it perhaps be that your order would expire unfilled on an earlier bar and then be resubmitted each bar that your condition would call for order placement?

                            Comment


                              #15
                              Hi PhillyD,

                              Looks like my post on Saturday got lost in the web site maintenance update.

                              I posted that I believe that if you test your strategy in market replay for the same timeframe as the backtest, the orders may actually have a better entry.

                              If you would like to discuss this directly, feel free to contact me directly.

                              RJay

                              Originally posted by PhillyD View Post
                              Hi RJay,

                              I tried your suggestion and the strategy is indeed placing the entry order on the (incorrect) bar that I thought it was placing it on. Not sure how this will help me debug?
                              RJay
                              NinjaTrader Ecosystem Vendor - Innovative Trading Solutions

                              Comment

                              Latest Posts

                              Collapse

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