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

One Order Entry Per Bar

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

    One Order Entry Per Bar

    hi,
    please i am in the early stages of putting together a simple crossover strategy where a momentum indicator has influence on.

    The challenge is that when the market is in a deadlock where momentum keeps oscillating within a single bar, my strategy keeps opening and closing trades within the same bar. sometimes opening trades in the opposite direction and closing depending on how the momentum indicator moves.

    this problem is somewhat solved when i set my strategy to 'On Bar Close' but this is not an option for me.

    please can you help in executing one trade per bar and ignore any other conditions that may be satisfied to open or close a trade within the same bar?

    i have attached a photo of what i am trying to explain

    my code so far is as below, but doesn't seem to work;

    // Set 1
    if ((EMA1[0] > EMA2[0])
    && (MQMomentum1.Fast[0] > MQMomentum1.Slow[0]))
    {
    BarBrush = Brushes.DarkBlue;
    EnterLong(Convert.ToInt32(DefaultQuantity), @"");
    }
    if (IsFirstTickOfBar);
    {
    doOnce = true;
    }

    // Set 2
    if ((EMA1[0] < EMA2[0])
    && (MQMomentum1.Fast[0] < MQMomentum1.Slow[0]))
    {
    BarBrush = Brushes.Azure;
    EnterShort(Convert.ToInt32(DefaultQuantity), @"");

    if (IsFirstTickOfBar);
    doOnce = true;
    }

    // Set 3
    if ((MQMomentum1.Fast[0] < MQMomentum1.Slow[0])
    && (EMA1[0] > EMA2[0]))
    {
    ExitLong(Convert.ToInt32(DefaultQuantity), "", "");
    }




    // Set 4
    if ((MQMomentum1.Fast[0] > MQMomentum1.Slow[0])
    && (EMA1[0] < EMA2[0]))
    {
    ExitShort(Convert.ToInt32(DefaultQuantity), "", "");
    }
    }
    Attached Files

    #2
    Hello odeifuor,

    Thank you for your post.

    I would suggest using a bool that gets reset on FirstTickOfBar that controls whether or not a trade can be entered. So for example, I could have a bool called TradeTaken and set that to false on the first tick of the bar. Then, check in your conditions for entry that the bool is false before a trade can be entered, and after you've submitted the trade set the bool to true right after that. that way you wouldn't be able to make another entry until the bool resets on the next FirstTickOfBar.



    Please let us know if we may be of further assistance to you.
    Kate W.NinjaTrader Customer Service

    Comment


      #3
      Thanks so much for your answer NinjaTrader_Kate the thing is I have very limited knowledge when it comes to coding.

      Will I have to toggle manually between true or false every time?
      Thanks
      ​​​​​

      Comment


        #4
        Hello odeifuor,

        Thank you for your reply.

        No, the code iterating through OnBarUpdate each time will reset it for you on the first tick of the bar if you structure the code correctly. Here's a very simple example:

        Code:
        private bool TradeTaken;
        
        protected override void OnBarUpdate()
        {
            if (IsFirstTickOfBar)
            {
                TradeTaken = false;
            }
        
            // Set 1
            if (Close[0] > Open[0] && TradeTaken == false )
            {
                EnterLong(Convert.ToInt32(DefaultQuantity), "");
        
                TradeTaken = true;
            }
        }
        Please let us know if we may be of further assistance to you.
        Kate W.NinjaTrader Customer Service

        Comment


          #5
          thank you so much NinjaTrader_Kate

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by itrader46, Today, 09:04 AM
          0 responses
          3 views
          0 likes
          Last Post itrader46  
          Started by timmbbo, Today, 08:59 AM
          0 responses
          1 view
          0 likes
          Last Post timmbbo
          by timmbbo
           
          Started by bmartz, 03-12-2024, 06:12 AM
          5 responses
          33 views
          0 likes
          Last Post NinjaTrader_Zachary  
          Started by Aviram Y, Today, 05:29 AM
          4 responses
          14 views
          0 likes
          Last Post Aviram Y  
          Started by algospoke, 04-17-2024, 06:40 PM
          3 responses
          28 views
          0 likes
          Last Post NinjaTrader_Jesse  
          Working...
          X