• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

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
    Paul H.NinjaTrader Customer Service

    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.

        Paul H.NinjaTrader Customer Service

        Comment


          #5
          Great Paul, thank you very much.

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by swcooke, Today, 09:20 AM
          5 responses
          14 views
          0 likes
          Last Post NinjaTrader_Jesse  
          Started by chartish, Today, 08:21 AM
          2 responses
          7 views
          0 likes
          Last Post chartish  
          Started by xbionicx, Today, 01:12 AM
          2 responses
          14 views
          0 likes
          Last Post xbionicx  
          Started by NinjaCustomer, Today, 07:52 AM
          2 responses
          6 views
          0 likes
          Last Post NinjaCustomer  
          Started by tradr, Today, 07:45 AM
          2 responses
          9 views
          0 likes
          Last Post tradr
          by tradr
           
          Working...
          X