Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

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 Tim-c, Today, 03:54 AM
          0 responses
          3 views
          0 likes
          Last Post Tim-c
          by Tim-c
           
          Started by FrancisMorro, Today, 03:24 AM
          0 responses
          2 views
          0 likes
          Last Post FrancisMorro  
          Started by Segwin, 05-07-2018, 02:15 PM
          10 responses
          1,771 views
          0 likes
          Last Post Leafcutter  
          Started by Rapine Heihei, 04-23-2024, 07:51 PM
          2 responses
          31 views
          0 likes
          Last Post Max238
          by Max238
           
          Started by Shansen, 08-30-2019, 10:18 PM
          24 responses
          945 views
          0 likes
          Last Post spwizard  
          Working...
          X