Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

BarsSinceEntry()

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

    BarsSinceEntry()

    Alright i looked through a lot of past threads and i cannot find a solution to what seems to be a simple problem.

    I have Broken the code down to as simple as possible

    " protected override void OnBarUpdate()
    {
    if (Close[0] > Close[5])
    {
    EnterLong();

    }
    if (BarsSinceEntry() > 5 )
    {
    ExitLong();
    }

    }"

    Why does the code not exit after 5 bars every time? Its erratic at best, sometimes 1 bar, sometimes 3, sometimes 5, sometimes more. I dont understand what is wrong here or if am mis interpreting how this function works. In MC it was simple, if BSE > 2 bars, an exit would happen 2 bars later every time.

    #2
    Hello neb1998, you would need to include proper signal names -

    protected override void OnBarUpdate()
    {
    if (Close[0] > Close[5])
    {
    EnterLong(DefaultQuantity, "");
    }

    if (BarsSinceEntry() > 2)
    {
    ExitLong("", "");
    }
    }

    Comment


      #3
      Ok i had tried this before but i went ahead and added them and its still not working. Random exiting.

      protected override void OnBarUpdate()
      {
      if (Close[0] > Close[5])
      {
      EnterLong(DefaultQuantity,"LE1");

      }
      if (BarsSinceEntry() > 2 )
      {
      ExitLong("LE1","LE1");
      }

      }

      Comment


        #4
        Picture of results

        See Previous Post, i couldnt insert a pic from a reply for some reason.
        Attached Files

        Comment


          #5
          Hello neb1998,

          Is there any improvement when passing in the name of the entry in your BarsSinceEntry() condition?

          if (BarsSinceEntry("LE1") > 2 )
          {
          ExitLong("LE1","LE1");
          }

          It looks like every exit takes place 4 bars after the entry. This is consistent with your code.
          The condition only evaluates true on the third bar after entry, and the order is submitted on the bar following this.
          Ryan M.NinjaTrader Customer Service

          Comment


            #6
            Ok this might be working, thanks! It looks good for now, i have to implement this into my actual strategy but looks good! Thanks.

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by argusthome, 03-08-2026, 10:06 AM
            0 responses
            80 views
            0 likes
            Last Post argusthome  
            Started by NabilKhattabi, 03-06-2026, 11:18 AM
            0 responses
            45 views
            0 likes
            Last Post NabilKhattabi  
            Started by Deep42, 03-06-2026, 12:28 AM
            0 responses
            29 views
            0 likes
            Last Post Deep42
            by Deep42
             
            Started by TheRealMorford, 03-05-2026, 06:15 PM
            0 responses
            32 views
            0 likes
            Last Post TheRealMorford  
            Started by Mindset, 02-28-2026, 06:16 AM
            0 responses
            66 views
            0 likes
            Last Post Mindset
            by Mindset
             
            Working...
            X