Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Prevent multiple entries

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

    Prevent multiple entries

    Is it possible to prevent multiple orders on the same bar? I have a ninjascript that generates the same order repeatedly from time to time, not under every signal condition is met though.

    I have sorted out the issue on other strats by adding FirstTickofBar but on a LineBreak Chart it doesn't seem to help.

    Here is my entry signal and ATM call code;
    PHP Code:
    // Long entry signal if cross above daily PP
                            if (Position.MarketPosition == MarketPosition.Flat
                                && orderIdL.Length == 0  
                                && atmStrategyIdL.Length == 0
                                && FirstTickOfBar
                                //&& Close[0] >= Open[0]
                                //&& Close[1] > GetCurrentAsk()
                                && BarsSinceEntry() > 1
                                && CrossAbove(Close, Pivots(PivotRange.Daily, HLCCalculationMode.CalcFromIntradayData, 0, 0, 0, 20).PP, 1)
                                || CrossAbove(Close, Pivots(PivotRange.Daily, HLCCalculationMode.CalcFromIntradayData, 0, 0, 0, 20).R1, 1)
                          
                                
                                //&& Close[1] + prevBarsPlusTicks*TickSize > GetCurrentAsk()
                                //&& Close[1]  < GetCurrentBid()
                                )
                                
                                   
                            {
                                    atmStrategyIdL = GetAtmStrategyUniqueId();
                                    orderIdL = GetAtmStrategyUniqueId();
                                    orderBarL = CurrentBar;
                                    AtmStrategyCreate(Cbi.OrderAction.Buy, OrderType.Market, 0, High[1], TimeInForce.Day, orderIdL, "TF_2_Ren_DPP_Long", atmStrategyIdL);    
                    
                            } 
    
    My questions are:
    1. Is it possible to implement && BarsSinceEntry() > 1 in an ATM strat somehow? If so, where do you place it?
    2. Is there a master instrument reference I could check so if a contract in that class is open then no further signals will be taken? I have Position.MarketPosition == MarketPosition.Flat in the entry signal section to prevent multiple orders but as the order is passed to the ATM function the strat is under the impression that there are no open orders. Do I need to start using IOorders or arrays?

    #2
    MrTicks, you can track the entries by storing the bar of the entry, and then ignore the second entry if it is on the same bar.

    Code:
    if (entryConditions == true && entryBar != CurrentBar)
    {
       entryBar = CurrentBar
       EnterLong(...)
    }
    Unfortunately there are no master instrument references available. A strategy only knows about positions opened by itself.
    AustinNinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_Austin View Post
      MrTicks, you can track the entries by storing the bar of the entry, and then ignore the second entry if it is on the same bar.

      Code:
      if (entryConditions == true && entryBar != CurrentBar)
      {
         entryBar = CurrentBar
         EnterLong(...)
      }
      Unfortunately there are no master instrument references available. A strategy only knows about positions opened by itself.
      Hi Austin,

      That wouldn't compile for me and gave the following error,
      The name 'entryBar' does not exist in the current context

      so I'm trying && BarsSinceEntry() > 1

      which looks like this when coded,

      PHP Code:
      // Long entry signal with ATM attached.
                              if (orderIdL.Length == 0  
                                      && atmStrategyIdL.Length == 0
                                      && Position.MarketPosition == MarketPosition.Flat
                                      && Close [1] <= Open [1]
                                      && Open[1] - PrevBarsMinusTicks*TickSize < GetCurrentBid() 
                                      && BarsSinceEntry() > 1
                                      //&& entryBar != CurrentBar
                                  )
                              
                              {
                                      //entryBar = CurrentBar;
                                      atmStrategyIdL = GetAtmStrategyUniqueId();
                                      orderIdL = GetAtmStrategyUniqueId();
                                      orderBarL = CurrentBar;
                                      AtmStrategyCreate(Cbi.OrderAction.Sell, OrderType.Stop, 0, Close[1], TimeInForce.Day, orderIdL, "FDAX_1_LB_short", atmStrategyIdL);    
                      
                              } 
      

      Would that do the same thing? Or will it allow additional entries on further incoming ticks on the same bar?

      Comment


        #4
        Hello MrTicks,

        BarsSinceEntry() wouldn't apply to ATM generated orders. You would have to manually track this value. The compile error you receive should be fixed by adding a variable declaration, in variables region of code.

        private int entryBar;
        Ryan M.NinjaTrader Customer Service

        Comment


          #5
          Originally posted by NinjaTrader_RyanM View Post
          Hello MrTicks,

          BarsSinceEntry() wouldn't apply to ATM generated orders. You would have to manually track this value. The compile error you receive should be fixed by adding a variable declaration, in variables region of code.

          private int entryBar;
          Thanks, it compiled OK but I still get multiple entries on a line break chart. Would you know if there is another method I could test?

          Comment


            #6
            Just saw this post on the forum and am testing it now, http://www.ninjatrader.com/support/f...t=32875&page=2

            This is what I'm currently testing;
            private bool OkToTrade = true;
            && OkToTrade == true

            AtmStrategyCreate(Cbi.OrderAction.Buy, OrderType.Market, 0, High[1], TimeInForce.Day, orderIdL, "TF_2_Ren_DPP_Long", atmStrategyIdL);
            OkToTrade = false;


            Here is my latest code;

            Code:
                public class TF_Pivot_ATM_cancel : Strategy
                {
                    #region Variables
                            private string  atmStrategyIdL          = string.Empty;
                            private string  atmStrategyIdS          = string.Empty;
                           
                            private int prevBarsPlusTicks = 1; // Default setting for PrevBarsPlusTicks
                            private int prevBarsMinusTicks = 1; // Default setting for PrevBarsPlusTicks
                           
                            private string  orderIdL                                = string.Empty;
                            private string  orderIdS                                = string.Empty;
                           
                            private int orderBarL                           = 0;
                            private int orderBarS                           = 0;
                            private int entryBarL                           = 0;
                            private int entryBarS                           = 0;
                            private int cancelBars                          = 1;
                            private int entryBar; 
                            private bool OkToTrade                     = true;
                            #endregion
            
                    
                    protected override void Initialize()
                    {
                        EntriesPerDirection = 1;
                        EntryHandling = EntryHandling.AllEntries; 
                        CalculateOnBarClose = false;
                        TraceOrders = true;
                        TimeInForce = Cbi.TimeInForce.Day;
                    }
                           
                   
                    protected override void OnBarUpdate()
                    {
                                    // Make sure this strategy does not execute against historical data
                                    if (Historical)
                                            return;
            
                                    // Submits an entry limit order at the current low price to initiate an ATM Strategy if both order id and strategy id are in a reset state
                                    
                                    // Long entry signal if cross above daily PP
                                    if (Position.MarketPosition == MarketPosition.Flat
                                        && orderIdL.Length == 0  
                                        && atmStrategyIdL.Length == 0
                                        && FirstTickOfBar
                                        //&& Close[0] >= Open[0]
                                        //&& Close[1] > GetCurrentAsk()
                                        && entryBar != CurrentBar
                                        && OkToTrade == true
                                        //&& BarsSinceEntry() > 1
                                        && CrossAbove(Close, Pivots(PivotRange.Daily, HLCCalculationMode.CalcFromIntradayData, 0, 0, 0, 20).PP, 1)
                                        || CrossAbove(Close, Pivots(PivotRange.Daily, HLCCalculationMode.CalcFromIntradayData, 0, 0, 0, 20).R1, 1)
                                        //&& Close[1] + prevBarsPlusTicks*TickSize > GetCurrentAsk()
                                        //&& Close[1]  < GetCurrentBid()
                                        )
                                        
                                           
                                    {
                                            atmStrategyIdL = GetAtmStrategyUniqueId();
                                            orderIdL = GetAtmStrategyUniqueId();
                                            orderBarL = CurrentBar;
                                            entryBar = CurrentBar;
            
                                            AtmStrategyCreate(Cbi.OrderAction.Buy, OrderType.Market, 0, High[1], TimeInForce.Day, orderIdL, "TF_2_Ren_DPP_Long", atmStrategyIdL);    
                                            OkToTrade = false; 
                                    }
                                    
                                    
                        
                                    
                                    // Short entry signal cross below PP
                                    if (Position.MarketPosition == MarketPosition.Flat
                                        && orderIdS.Length == 0  
                                        && atmStrategyIdS.Length == 0  
                                        && FirstTickOfBar
                                        && entryBar != CurrentBar
                                        && OkToTrade == true
                                        //&& BarsSinceEntry() > 1
                                        //&& Close[1] <= Open[1]
                                        && CrossBelow(Close, Pivots(PivotRange.Daily, HLCCalculationMode.CalcFromIntradayData, 0, 0, 0, 20).PP, 1)
                                        || CrossBelow(Close, Pivots(PivotRange.Daily, HLCCalculationMode.CalcFromIntradayData, 0, 0, 0, 20).R1, 1)
                                        //&& Close[0] > High[1]
                                        //&& Close[1]  > GetCurrentAsk()  
                                         )
                                            
                                        // && Low   [1] - PrevBarsMinusTicks * TickSize < GetCurrentAsk())
                                        
                                           
                                    {
                                            atmStrategyIdS = GetAtmStrategyUniqueId();
                                            orderIdS = GetAtmStrategyUniqueId();
                                            orderBarS = CurrentBar;
                                            entryBar = CurrentBar;
                                            AtmStrategyCreate(Cbi.OrderAction.Sell, OrderType.Market, 0, Low[1], TimeInForce.Day, orderIdS, "TF_2_Ren_DPP_Short", atmStrategyIdS);
                                             OkToTrade = false; 
                                    }
                                    
                                    
                                   
            
                                    // Check for a pending entry order
                                    if (orderIdL.Length > 0)
                                    {
                                        string[] statusL = GetAtmStrategyEntryOrderStatus(orderIdL);
                                         
                                        // If the status call can't find the order specified, the return array length will be zero otherwise it will hold elements
                                        if (statusL.GetLength(0) > 0)
                                            
                                            //Cancel long order after n bars
                                            if ((CurrentBar - orderBarL) >= cancelBars)
                                                
                                            
                                            {
                                                    AtmStrategyCancelEntryOrder(orderIdL);
                                                   
                                                    orderIdL = string.Empty;
                                            }
                                            
                                            {
                                                    // Print out some information about the order to the output window
                                                    Print("The entry order average fill price is:\t" + statusL[0]);
                                                  
            
                                                    // If the order state is terminal, reset the order id value
                                                    if (statusL[2] == "Filled" || statusL[2] == "Cancelled" || statusL[2] == "Rejected")
                                                            orderIdL = string.Empty;
                                            }
                                    } // If the strategy has terminated reset the strategy id
                                    else if (atmStrategyIdL.Length > 0 &&
                                            GetAtmStrategyMarketPosition(atmStrategyIdL) == Cbi.MarketPosition.Flat)
                                            atmStrategyIdL = string.Empty;
            ////////////////////////////////////////////////////////////////////////////////
                                    // Check for a pending entry order
                                    if (orderIdS.Length > 0)
                                        
                                    {
                                        string[] statusS = GetAtmStrategyEntryOrderStatus(orderIdS);
                                        
                                        // If the status call can't find the order specified, the return array length will be zero otherwise it will hold elements
                                            if (statusS.GetLength(0) > 0)
                                                
                                            //Cancel short order after n bars
                                            if ((CurrentBar - orderBarS) >= cancelBars)
                                            
                                            {
                                                    AtmStrategyCancelEntryOrder(orderIdS);
                                                   
                                                    orderIdS = string.Empty;
                                            }
                                            
                                            {
                                                    // Print out some information about the order to the output window
                                                    Print("The entry order average fill price is:\t" + statusS[0]);
                                                   
            
                                                    // If the order state is terminal, reset the order id value
                                                    if (statusS[2] == "Filled" || statusS[2] == "Cancelled" || statusS[2] == "Rejected")
                                                            orderIdS = string.Empty;
                                            }
                                    } // If the strategy has terminated reset the strategy id
                                    else if (atmStrategyIdS.Length > 0 &&
                                            GetAtmStrategyMarketPosition(atmStrategyIdS) == Cbi.MarketPosition.Flat)
                                            atmStrategyIdS = string.Empty;
            ////////////////////////////////////////////////////////////////////////////////
            Will let you know if it works.
            Last edited by MrTicks; 10-05-2010, 06:59 AM. Reason: Left out the changes I made

            Comment


              #7
              Originally posted by NinjaTrader_Austin View Post
              MrTicks, you can track the entries by storing the bar of the entry, and then ignore the second entry if it is on the same bar.

              Code:
              if (entryConditions == true && entryBar != CurrentBar)
              {
                 entryBar = CurrentBar
                 EnterLong(...)
              }
              Unfortunately there are no master instrument references available. A strategy only knows about positions opened by itself.
              Hi Austin,

              Does COBC = True have to be set for the above sample to work or would it work with COBC = False?

              Comment


                #8
                That didn't work either, I just tried the following;

                under variables;
                Code:
                private bool tradeLong                            = false;
                private bool tradeShort                            = false;
                under protected override void OnBarUpdate()
                Code:
                && tradeLong == false
                Code:
                 {
                    atmStrategyIdL = GetAtmStrategyUniqueId();
                    orderIdL = GetAtmStrategyUniqueId();
                    orderBarL = CurrentBar;
                    entryBar = CurrentBar;
                
                    AtmStrategyCreate(Cbi.OrderAction.Buy, OrderType.Market, 0, High[1], TimeInForce.Day, orderIdL, "TF_2_Ren_DPP_Long", atmStrategyIdL);    
                    //OkToTrade = false; 
                    tradeLong = true;
                  }
                It still repeats multiple entries though. Is there any chance you could run the attached strat and see why it happens? I was testing this on a Renko chart set at 2 bricks.
                Attached Files

                Comment


                  #9
                  Hello Mr Ticks,

                  This suggestion works best with COBC = true. If set to false, the condition can be evaluated multiple times on the same bar and could return true multiple times.
                  Ryan M.NinjaTrader Customer Service

                  Comment


                    #10
                    Originally posted by NinjaTrader_RyanM View Post
                    Hello Mr Ticks,

                    This suggestion works best with COBC = true. If set to false, the condition can be evaluated multiple times on the same bar and could return true multiple times.
                    Thanks, will test with COBC = true.

                    Comment


                      #11
                      Originally posted by NinjaTrader_RyanM View Post
                      Hello Mr Ticks,

                      This suggestion works best with COBC = true. If set to false, the condition can be evaluated multiple times on the same bar and could return true multiple times.
                      Works a treat with COBC = true.

                      Thank you very much!!!!!!!!!!

                      Comment


                        #12
                        Glad to hear it's working for you, MrTicks. Thanks for the update.
                        Ryan M.NinjaTrader Customer Service

                        Comment


                          #13
                          I spoke to soon! It all looked OK today until it was doing repeat orders every now and again, albeit on a much smaller scale. Is there any chance you could run that strat I attached in a previous post and see if you can tell why? I have COBC = true on a 2 brick renko chart.

                          Comment


                            #14
                            MrTicks,

                            This will not work when using ATM strategies in NinjaScript:
                            Code:
                            Position.MarketPosition == MarketPosition.Flat
                            That is for NinjaScript positions and not ATM positions. To check ATM's position you need to use this instead: http://www.ninjatrader-support.com/H...yPosition.html
                            Josh P.NinjaTrader Customer Service

                            Comment


                              #15
                              Originally posted by NinjaTrader_Josh View Post
                              MrTicks,

                              This will not work when using ATM strategies in NinjaScript:
                              Code:
                              Position.MarketPosition == MarketPosition.Flat
                              That is for NinjaScript positions and not ATM positions. To check ATM's position you need to use this instead: http://www.ninjatrader-support.com/H...yPosition.html
                              Thanks for the prompt response Josh! Will edit that code.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                              0 responses
                              656 views
                              0 likes
                              Last Post Geovanny Suaza  
                              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                              0 responses
                              370 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by Mindset, 02-09-2026, 11:44 AM
                              0 responses
                              109 views
                              0 likes
                              Last Post Mindset
                              by Mindset
                               
                              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                              0 responses
                              574 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by RFrosty, 01-28-2026, 06:49 PM
                              0 responses
                              578 views
                              1 like
                              Last Post RFrosty
                              by RFrosty
                               
                              Working...
                              X