Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Delayed trade entry after N bars after trade signal

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

    Delayed trade entry after N bars after trade signal

    Hi,
    I would like to ask how to code delayed trade entry. After the trade signal is confirmed, wait N bars and then enter (long or short,doesnt matter) I would also like to be able to optimize the number of bars to enter in strategy analyzer - so I can chose between 1,2,3,4,5 or more bars to enter with best results.

    My current code for which I intend to do it is as follows, but I quess it should be applicable to other strategies too :


    if (Open[0]<Close[0] && Math.Abs(Close[0]-Open[0]) <= ((High[0]-Low[0])*0,5) && Math.Abs(Open[0]-Low[0]) >= ((High[0]-Low[0])*0,3)


    EnterLong(Convert.ToInt32(DefaultQuantity), "");



    So what code and where to put it to apply the delayed entry after specific number of bars?

    Thank you in advance for any help.

    #2
    Hello ExNihilon,

    Thanks for your post.

    One way to accomplish this would be to save the current bar number when the condition is true and then create another condition where the difference between the CurrentBar and the saved bar number is equal to the number of bars you wish to wait. for example:

    if (Open[0]<Close[0] && Math.Abs(Close[0]-Open[0]) <= ((High[0]-Low[0])*0,5) && Math.Abs(Open[0]-Low[0]) >= ((High[0]-Low[0])*0,3)
    {
    savedBar = CurrentBar; // saved the bar number when the condition is true
    }

    if (CurrentBar - savedBar == myBarsToWait)
    {
    EnterLong();
    }


    savedBar would be a previously declared int variable that you would create.
    CurrentBar is a system bar counter: https://ninjatrader.com/support/help...currentbar.htm

    Comment


      #3
      Originally posted by NinjaTrader_PaulH View Post
      Hello ExNihilon,

      Thanks for your post.

      One way to accomplish this would be to save the current bar number when the condition is true and then create another condition where the difference between the CurrentBar and the saved bar number is equal to the number of bars you wish to wait. for example:

      if (Open[0]<Close[0] && Math.Abs(Close[0]-Open[0]) <= ((High[0]-Low[0])*0,5) && Math.Abs(Open[0]-Low[0]) >= ((High[0]-Low[0])*0,3)
      {
      savedBar = CurrentBar; // saved the bar number when the condition is true
      }

      if (CurrentBar - savedBar == myBarsToWait)
      {
      EnterLong();
      }


      savedBar would be a previously declared int variable that you would create.
      CurrentBar is a system bar counter: https://ninjatrader.com/support/help...currentbar.htm
      Paul,

      thank you very much. Just one follow up - is it sufficient to define the savedBar like this and only in this one line:

      public class Hammer : Strategy

      {

      private int SavedBar;


      protected override void OnStateChange()

      {

      or should it be defined also somewhere else in the script to work properly,for example here:


      else if (State == State.Configure)

      {


      }


      Thank you again.

      Comment


        #4
        Hello ExNihilon,

        Thanks for your reply.

        Yes, declaring the variable at the class level would make it available in all methods of the class.

        Comment


          #5
          Great Paul, thank you very much.

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by NullPointStrategies, Today, 05:17 AM
          0 responses
          50 views
          0 likes
          Last Post NullPointStrategies  
          Started by argusthome, 03-08-2026, 10:06 AM
          0 responses
          126 views
          0 likes
          Last Post argusthome  
          Started by NabilKhattabi, 03-06-2026, 11:18 AM
          0 responses
          69 views
          0 likes
          Last Post NabilKhattabi  
          Started by Deep42, 03-06-2026, 12:28 AM
          0 responses
          42 views
          0 likes
          Last Post Deep42
          by Deep42
           
          Started by TheRealMorford, 03-05-2026, 06:15 PM
          0 responses
          46 views
          0 likes
          Last Post TheRealMorford  
          Working...
          X