Announcement

Collapse
No announcement yet.

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.

    https://ninjatrader.com/support/help...ttickofbar.htm

    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:
            public class ExampleBoolStrategy : Strategy
            {
                private bool TradeTaken;
        
        
                protected override void OnStateChange()
                {
                    if (State == State.SetDefaults)
                    {
                        Description                                    = @"Enter the description for your new custom Strategy here.";
                        Name                                        = "ExampleBoolStrategy";
                        Calculate                                    = Calculate.OnEachTick;
                        EntriesPerDirection                            = 5;
                        EntryHandling                                = EntryHandling.AllEntries;
                        IsExitOnSessionCloseStrategy                = true;
                        ExitOnSessionCloseSeconds                    = 30;
                        IsFillLimitOnTouch                            = false;
                        MaximumBarsLookBack                            = MaximumBarsLookBack.TwoHundredFiftySix;
                        OrderFillResolution                            = OrderFillResolution.Standard;
                        Slippage                                    = 0;
                        StartBehavior                                = StartBehavior.WaitUntilFlat;
                        TimeInForce                                    = TimeInForce.Gtc;
                        TraceOrders                                    = false;
                        RealtimeErrorHandling                        = RealtimeErrorHandling.StopCancelClose;
                        StopTargetHandling                            = StopTargetHandling.PerEntryExecution;
                        BarsRequiredToTrade                            = 20;
                        // Disable this property for performance gains in Strategy Analyzer optimizations
                        // See the Help Guide for additional information
                        IsInstantiatedOnEachOptimizationIteration    = true;
                        TradeTaken                    = false;
                    }
                    else if (State == State.Configure)
                    {
                        SetStopLoss("", CalculationMode.Ticks, 10, false);
                        SetProfitTarget("", CalculationMode.Ticks, 20);
                    }
                }
        
                protected override void OnBarUpdate()
                {
                    if (BarsInProgress != 0) 
                        return;
        
                    if (CurrentBars[0] < 1)
                        return;
                    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 Impeesa, Today, 03:17 AM
          0 responses
          7 views
          0 likes
          Last Post Impeesa
          by Impeesa
           
          Started by Impeesa, Today, 12:53 AM
          0 responses
          14 views
          0 likes
          Last Post Impeesa
          by Impeesa
           
          Started by Gaterz, Yesterday, 06:52 PM
          1 response
          13 views
          0 likes
          Last Post NinjaTrader_Marco  
          Started by dmelleady, Yesterday, 10:18 PM
          1 response
          10 views
          0 likes
          Last Post NinjaTrader_ChrisSch  
          Started by arangocj, Yesterday, 09:46 PM
          1 response
          16 views
          0 likes
          Last Post NinjaTrader_ChelseaB  
          Working...
          X