Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Help with intrabar strategy

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

    Help with intrabar strategy

    I'm programming a intrabar strategy. The problem is this: if the strategy takes a trade and get stopped out, it will re enter on the same bar immediately if the condition still is the same. How do I prevent this. I want the strat to enter only one time, and not re enter again on the same bar.

    regards, Finn

    #2
    You would then need to limit trade taking with a bool flag in your conditions that you reset to allow trade taking on FirstTickOfBar, you would set it to false then with your entry, so it would reduce activity to one trade per bar.

    Comment


      #3
      Ah, of course. Thanks alot.
      Finn

      Comment


        #4
        Similar Problem - But not resetting BOOL correctly

        Hi there - i have a similar problem, at one time the strategy enter a trade multiple on the same bar, so i used the BOOL approach. Then on FirstTickOfBar the bool should be reset - but the RESET is not working well. Therefore, i miss re-entry opportunities. (I use continuous DrawText of the bool to monitor that it does not reset to 'false' with a new bar.) May i post my code and would you please take a look?? Thanks bunches.

        #region Variables
        privatebool tradedOnceLongAlready = false;


        if(FirstTickOfBar) {
        tradedOnceLongAlready = false;
        tradedOnceShortAlready =
        false; }


        if((Position.MarketPosition == MarketPosition.Flat)
        && (tradedOnceLongAlready == false)
        // Meaning traded IN THIS BAR already
        && (MyCondition1 > Mycondition2
        ) {
        entryOrder = EnterLong(t1Quantity,
        "C1 Long");
        tradedOnceLongAlready =
        true; }

        Comment


          #5
          Julie, the code looks ok to me - which NT version and charttype are you working with here?

          Comment


            #6
            Hi Bertrand,
            I am using NT 7.0.1000.1 with a Range Bar 18 on instrument CL.
            Further, i have tried a similar approach with a User Defined Variable: a) variable must be a "0" to take the trade, b) assigning a "1" at the same time the trade is taken, and then c) attempting to assign a "0" on the FirstTickOfBar. However the reset to "0" does not occur on FirstTickOfBar and according to the DrawText the variable remains "1" during the course of the next bar.
            Either way would be fine, a bool or a User Defined Variable, but I am searching for reliability. Is one method stronger than another?
            Thanks for your help.
            Jules

            Comment


              #7
              Hello,

              Your FirstTickOfBar will work. This looks good. Therefor it is getting set back to 0. However most likely it is being reset back to 1 or set back to true.

              Suggest adding a bunch of Print() statments to print out the value as you have done.

              However add a print at the end of FirstTickOfBar where you set it to 0. Check to make sure it outputs 0 here.

              Then add a print before your entry conditions and then after to find where the logic is causing this to be set back to 1.

              If you are still running into issue here we would need tot take a look at full code to look into. In this case you would need to send it to support at ninjatrader dot com and reference this forum post.

              I look forward to assisting you further.
              BrettNinjaTrader Product Management

              Comment


                #8
                Thanks Brett for your suggestion to use Print() to trouble shoot - the trouble shooting went as you expected. In the FirstTickOfBar the bool is being set to false as it should be.

                The trouble comes from within the "if" statement that generates an entryOrder as well as changes the bool to true. As you suggested, the Print() just before "tradedOnceLongAlready = true;" indicates the bool was "false" at that instant. Then using Print() just after "tradedOnceLongAlready = true;" we find the bool is set to "true."

                That would seem as desired, but the other conditions of the entryOrder generating "if" statement are not met -- i expected the "if" statement to be aborted because the other conditions are not met. i must be missing the obvious. Thanks!

                if(FirstTickOfBar) { //used Print()test=OK
                tradedOnceLongAlready = false; }

                // LONG ENTRY CONTINUATION TYPE TRADE
                if((Position.MarketPosition == MarketPosition.Flat
                && (tradedOnceLongAlready==false)//one trade per bar
                && (yLSMA[1] > yNike[1]) && (yLSMA[2] > yNike[2]) // last 2 Bars meet LONG TREND condition
                && (Close[2] == Low[2]) && (Close[1] == High[1]) // RedBar[2] followed by GreenBar[1]
                // TRIGGERING EVENTS IN ACTION BAR
                && (apV3 > chopThreshold)) { // Unless in CHOP enter LONG at beginning of New Bar
                entryOrder = EnterLong(t1Quantity,"C1 Long");
                // below is where the error seems to be ... somehow getting reset to "true" when other conditions of "if" statement are not met
                tradedOnceLongAlready = true;
                Print(tradedOnceLongAlready);
                // This print shows several bars after a trade is triggered, the status is still true
                // there is no other place in the strategy that sets the value to "true" - only this one place assigns "true"
                }

                Comment


                  #9
                  Hello,

                  in this case I would start Print() these variables in the if statement as we know this is where the issue is.

                  Before the iff statement:

                  Print(Position.MarketPosition);
                  Print(tradedOnceLongAlready);
                  Print(yLSMA[1]);
                  Print(yNike[1]);
                  //etc. etc... Until you find that issue code block. As all conditions are evaluating back to true to allow this to go back to 1.


                  if (((Position.MarketPosition == MarketPosition.Flat //&& (tradedOnceLongAlready==false)
                  && (yLSMA[1] > yNike[1]) && (yLSMA[2] > yNike[2])
                  && (Close[2] == Low[2]) && (Close[1] == High[1])


                  Or send you code in to support at ninjatrader dot com and reference this forum post if you are still stuck.

                  Let me know if I can be of further assistance.

                  BrettNinjaTrader Product Management

                  Comment


                    #10
                    Problems resolved - Thanks so much for your help. Detailed below are solution steps to help others that may run across this same situation.

                    This thread would be best titled "Trouble shooting for a strategy when you can't visually see what is going on."

                    I followed Brett's suggestion and did a Print() on each value. What a pain - but totally necessary. I found one of my Data Series values had been Set with the wrong value.

                    Second, i was looking at bad "evidence" about what the strategy was doing. The evidence i depended upon was a DrawText that printed values of bools onto the screen. That is cool, and conceptually a good plan, but i was calling for the DrawText from inside the
                    protectedoverridevoid OnPositionUpdate(IPosition position)
                    which is not cool. My printed bool information was stale and only updated when my trading position was changed. Once i relocated the DrawText command the information was relevant and accurate.

                    Thanks NT Team - you ROCK!

                    Comment


                      #11
                      Intrabar issues

                      Hi guys. I am having a similar problem to this whereby once I am either stopped out of a trade or targets are met, an order is sometimes processed on the same bar whereas I am trying to get it to process on the next bar. The difference in my strategy as opposed to Julies is I am using an ATM strategy. Everything else in my strategy works fine. I have tried a number of different ways to get around this issue (including inserting lines: 123, 150, and 155) but to no avail so far, so I am trying the method in this post and hopefully I can resolve this issue. As per Julie's example I have inserted:

                      lines 53 and 54

                      private bool tradedoncelong = false;
                      private bool tradedonceshort = false;

                      Line 127

                      && tradedoncelong == false

                      and also 159

                      tradedoncelong = true;

                      Now, I also notice that when I inserted line 126

                      &&GetAtmStrategyMarketPosition(atmStrategyIdL) == Cbi.MarketPosition.Flat

                      I am now getting the following message in the output window:

                      **NT** GetAtmStrategyMarketPosition() method error: Missing atmStrategyId parameter

                      I am unsure if this is causing the same order processing issue or not. I'm also unsure as to why I am getting that error.

                      Any help on this would be appreciated.

                      Thanks. DJ
                      Attached Files

                      Comment


                        #12
                        DJ, I would suggest you simplify your code and print out needed values to investigate - you see the error likely as you reset the strategy id to early in the process and thus it's empty when being accessed.

                        Comment

                        Latest Posts

                        Collapse

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