Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Bars since entry as an exit condition

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

    Bars since entry as an exit condition

    What if I want to exit a position if a condition apply after X bars since entry..

    if (Medians[0][0] < SMA (BarsArray[0],SMACycle)[0] && XXX)
    {
    ExitLong();
    }

    what would be XXX?

    #2
    You would use
    BarsSinceEntry() > X

    The other overloads that might be of use when dealing with signal names or multi series scripts can be found here:


    Let me know if I can further assist.
    LanceNinjaTrader Customer Service

    Comment


      #3
      If I insert on my strategy:
      if (BarsSinceEntry() >= 60 )
      {
      ExitLong();
      }

      The strategy does not generate any trades.. If I take this part out, it generates several trades,,

      Any clue?

      Comment


        #4
        Unfortunately that bit of code alone is not enough to determine why it would not generate trades.

        When you look at the start of the chart, are you seeing a trade that's entered but never closed out? Do you have more than 60 bars on the chart?

        Try adding print statements or drawing tools to figure out how your code is being processed: http://www.ninjatrader.com/support/f...ead.php?t=3418

        Let me know how I can further assist.
        LanceNinjaTrader Customer Service

        Comment


          #5
          Attached in the code..
          if you take the '/*' out you will see that any trade will be generate.

          /*if ((Medians[1][0] < SMA (BarsArray[1],SMACycle)[0]) && BarsSinceEntry() > 30)
          {
          ExitShort();
          }*/
          Attached Files

          Comment


            #6
            If you go to tools -> output window you'll see an error message when running the script

            **NT** Error on calling 'OnBarUpdate' method for strategy 'sp500testingBarssinceEntry/080a137567be4da6922a79c3efa40da3': You must use the overload that has a 'BarsInProgress' parameter when calling the BarsSinceEntry() method in the context of a multi-time frame and instrument strategy.

            When using a multi series script you will need to identify the correct BIP.

            BarsSinceEntry(int barsInProgressIndex, string signalName, int entriesAgo)

            Let me know if I can further assist.
            LanceNinjaTrader Customer Service

            Comment


              #7
              Nice to know!
              but what would be signalName? (I read about it on guide but I did not understand what is it?)


              BarsSinceEntry(int barsInProgressIndex, string signalName, int entriesAgo)

              Thank you!

              Comment


                #8
                Looking at your script you're using this to enter

                EnterLong(0,positionTaken,priorClosePrice.ToString());

                The String you have on the end is the signal name

                This will need to match. Because you're dynamically changing your signal name each new order you would need to either store store the signal name to a variable or hard code the string signal name.
                LanceNinjaTrader Customer Service

                Comment


                  #9
                  Just one more question:
                  The strategy generates all orders in inst [0], should I have to use bas=rsInProgressIndex =0???

                  BarsSinceEntry(int barsInProgressIndex, string signalName, int entriesAgo)

                  Comment


                    #10
                    Yes you'll want to use BarsInProgressIndex 0 any time you're not using a multi series script as the primary series will always be 0.

                    Let me know if I can further assist.
                    LanceNinjaTrader Customer Service

                    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