• 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 simjs, Yesterday, 08:11 PM
          0 responses
          3 views
          0 likes
          Last Post simjs
          by simjs
           
          Started by mrlucky1x, Yesterday, 06:01 PM
          0 responses
          3 views
          0 likes
          Last Post mrlucky1x  
          Started by dedouglas, Yesterday, 03:17 PM
          1 response
          9 views
          0 likes
          Last Post NinjaTrader_Jesse  
          Started by Jugado, Yesterday, 02:22 PM
          2 responses
          30 views
          0 likes
          Last Post Jugado
          by Jugado
           
          Started by login_dejavu, 04-21-2019, 04:58 AM
          2 responses
          24 views
          0 likes
          Last Post login_dejavu  
          Working...
          X