Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

same bar entries--Calculate on bar close=False

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

    same bar entries--Calculate on bar close=False

    Hi there:

    I have Calculate on bar close False on a strategy (with a type of moving average as a base signal). I would like to limit the amount of entries it attempts to make on a individual bar (as the False...sometimes...will cause the moving averages to go back and forth and to where it gets stopped out and then when it comes back it will enter again on the same bar etc...does not happen a lot but when it does it needs to be limited.)

    What functions/options could I use to add to the code to tell it:

    On the current bar only attempt a entry "x" times. Then, I could put it 1,2,3, etc for the amount of attempts I want it to try.

    Thanks...

    Any other ideas would be great too...



    Greg

    #2
    Hello,

    Can you use FirstTickOfBar and this will limit the condition from only being evaluated once during that bar:

    Separating logic to either calculate once on bar close or on every tick

    Otherwise, if you'd like more attempts during that bar, you'd have to create a custom counter and loop through this to re-try the submission again until a certain threshold has been reached

    Code:
    				int myTradeCounter = 4;
    
    				for (int x = 0; x < myTradeCounter; x++)
    				{
    					if(x < myTradeCounter)
    					{
    					//try order up to 4 times
    					}
    				}
    MatthewNinjaTrader Product Management

    Comment


      #3
      Matt:

      This sounds good...yea, I would want it to execute anything during the bar, but just want to limit the amount of attempts...couple things:

      1. To change the amount of tries, all I have to do is change the "4" in this line right "int myTradeCounter = 4;" to something like this if I wanted only 2 times "int myTradeCounter = 2;" (and of course notate this //try order up to 2 times)? I don't have to put a number where you have "x" right or anything else?

      2. Where exactly do I put the code (here)?

      ---Code---

      protected override void OnBarUpdate()
      {
      int myTradeCounter = 4;

      for (int x = 0; x < myTradeCounter; x++)
      {
      if(x < myTradeCounter)
      {
      //try order up to 4 times
      }
      }
      if (Historical) return;
      if (Position.MarketPosition == MarketPosition.Flat)

      ---Code---

      Thanks so much!!!

      This will really help!!!



      Greg
      Last edited by birdog; 01-15-2013, 02:54 PM.

      Comment


        #4
        1) That's correct, you can use whatever value you want. If it's set to 2, it'll only try up to 2 times. Once trade counter gets to 3, if(x < myTradeCounter) will not be evlauted as true and will not process the logic containted in that block

        2) You would put that code under the condition you're trading in. I guess I don't have enough context to tell you exactly where, but instead of "EnterLong" you'd use this logic, and then put "EnterLong()" in side the for loop.

        Hope that helps.
        MatthewNinjaTrader Product Management

        Comment


          #5
          Great...ok...here is some more context...does this look right?

          if (CrossAbove(SMA(Fast), SMA(Slow), 1
          {
          int myTradeCounter = 4;
          for (int x = 0; x < myTradeCounter; x++)
          {
          if(x < myTradeCounter)
          {
          //try order up to 4 times
          }
          EnterLong(DefaultQuantity, "BuyMkt");
          }
          Last edited by birdog; 01-15-2013, 03:18 PM.

          Comment


            #6
            Matt: This is what I came up with...is it right?

            Code:
            {
                                int myTradeCounter = 1;
            
                                for (int x = 0; x < myTradeCounter; x++)
                                {
                                    if(x < myTradeCounter)
                                    {
                                    //try order up to 1 times
                                    }
                                }
                                EnterLong(DefaultQuantity, "BuyMkt");  
            }

            Comment


              #7
              I have put this in the code and using replay to confirm it works...not able to get it to work...can you suggest anything?

              Comment


                #8
                Anyone else is open to mention any ideas as well...thanks folks...Greg

                Comment


                  #9
                  Lets try another approach.

                  In the regigion variables, declare these two variables to keep track of your order attempts

                  Code:
                       #region Variables
                     		private int x = 0;
                  		private int myTradeCounter = 4;
                          #endregion
                  Then in OnBarUpdate, you can add the following. We will want to ensure we're resetting the x value using FirstTickOfBar to ensure your strategy continues to take trades

                  Code:
                  				//resets value every new bars
                  				if(FirstTickOfBar)
                  				{
                  					x = 0;
                  				}
                  
                  				// checks for your entry condition and that the value is less than your trade count
                                                  if(CrossAbove(SMA(Fast), SMA(Slow), 1) && x <= myTradeCounter)
                  				{
                  				  //attempts to enter long			
                                                     EnterLong(DefaultQuantity, "BuyMkt");
                  
                  				  //increments the trade count by 1
                  				  x++;
                                          	
                  				}
                  MatthewNinjaTrader Product Management

                  Comment


                    #10
                    ---By the way...I am testing it to restrict to taking only 1 trade per bar so I set the int to only 1 not 4 for this example just so you know...smile...G---

                    Hey Matthew:

                    I have included a screen shot of a small sample of data from yesterday on NQ. That top bar (as an example) enters multiple times. I have included the following code per your help, but it is still entering multiple times (like the screen shot here) on Market Replay. Could you please look at the code I have entered and let me know if it all looks good and is in the proper placement etc. There is still something not working and I have compiled it fine and disabled the strategy and then re-enabled after compiling...thanks Matthew...

                    still can't get it to work...

                    Here is what I have...in the #region Variables:

                    Code:
                    /// </summary>
                        [Description("XXX")]
                        public class Gver5_IntraPP_AvdSB : Strategy
                        {
                            #region Variables
                            private int        fast    = 10;
                            private int        slow    = 25;
                            
                            private double shortTarget = 20; // Default setting for ShortTarget
                            private double shortStop = 10; // Default setting for ShortStop
                            private double longTarget = 20; // Default setting for LongTarget
                            private double longStop = 10; // Default setting for LongStop
                            
                            // Greg> Trade Counter
                            private int x = 0;
                            private int myTradeCounter = 1;
                            
                            #endregion
                                    
                            /// <summary>
                    Here is the code I have for OnBarUpdate()...

                    Code:
                    /// </summary>
                            protected override void OnBarUpdate()
                            {                        
                                if (Historical) return;
                                if (Position.MarketPosition == MarketPosition.Flat)             
                                {
                                    //Greg> Trade Counter resets value every new bars
                                    if(FirstTickOfBar)
                                    {
                                        x = 0;
                                    }
                                    //Greg> Trade Counter checks for your entry condition and that the value is less than your trade count
                                    if (CrossAbove(SMA(Fast), SMA(Slow), 1) && x <= myTradeCounter 
                                        && Close[0] >= SessionPivots(true, PivotRange.Daily, PivotType.All, HLCCalculationMode.CalcFromIntradayData, ProfileType.TPO, 0, 0, 0).L3[0])
                                    {
                                        //Greg> Trade Counter attempts to enter long    
                                        EnterLong(DefaultQuantity, "BuyMkt"); 
                    
                                        //Greg> Trade Counter increments the trade count by 1
                                        x++;
                                    }
                                    
                                    //Greg> Trade Counter resets value every new bars
                                    if(FirstTickOfBar)
                                    {
                                        x = 0;
                                    }
                                    //Greg> Trade Counter checks for your entry condition and that the value is less than your trade count
                                    else if (CrossBelow(SMA(Fast), SMA(Slow), 1) && x <= myTradeCounter //Bearish trade conditions; replace with your own
                                        && Close[0] <= SessionPivots(true, PivotRange.Daily, PivotType.All, HLCCalculationMode.CalcFromIntradayData, ProfileType.TPO, 0, 0, 0).H3[0])
                                    {
                                        //Greg> Trade Counter attempts to enter long    
                                        EnterShort(DefaultQuantity, "SellMkt"); 
                    
                                        //Greg> Trade Counter increments the trade count by 1
                                        x++;
                                    }
                                }
                    Attached Files
                    Last edited by birdog; 01-16-2013, 05:47 PM.

                    Comment


                      #11
                      Originally posted by birdog View Post
                      Matt: This is what I came up with...is it right?

                      Code:
                       
                      {
                                          int myTradeCounter = 1;
                       
                                          for (int x = 0; x < myTradeCounter; x++)
                                          {
                                              if(x < myTradeCounter)
                                              {
                                              //try order up to 1 times
                                              }
                                          }
                                          EnterLong(DefaultQuantity, "BuyMkt");  
                      }
                      I do not think that a for loop works in the context. The loop will all run on one tick it seems to me.

                      More likely would be:

                      Code:
                       
                      RequiredCount = 4; //or whatever
                       
                      if (FirstTickOfBar)
                      {
                      loopCounter = 0;
                      //any other necessary code
                      }
                       
                      if (ConditionMet && loopCounter < RequiredCount)
                      {
                      //Entry orders go here
                      loopCounter++;
                      }
                      All the variables would have to be class variables, as they must persist from tick to tick.
                      Last edited by koganam; 01-16-2013, 10:49 PM.

                      Comment


                        #12
                        Hey there koganam:

                        Could you please look at post #10 in this thread. It is the latest code I have at the moment with variables etc, but I still can not get it to work...ideas? There is a attachment .jpg too on post #10.



                        Greg

                        Comment


                          #13
                          Originally posted by birdog View Post
                          Hey there koganam:

                          Could you please look at post #10 in this thread. It is the latest code I have at the moment with variables etc, but I still can not get it to work...ideas? There is a attachment .jpg too on post #10.



                          Greg
                          What are you expecting to see, and how is your output different from what you expect ?

                          Comment


                            #14
                            Hi Koganam:

                            I run the strategy with Calculate on bar close=False so signals are immediate when conditions are meet.

                            1. I am expecting to allow only 1 entry attempt per bar. If it is stopped out, then it should not re-enter on the same bar again and again. When the next bar happens then it should then begin evaluating for another attempt etc.

                            2. The current output is that if the cross-over of my SMAs happen (and other conditions are met), then it will enter the market but if it gets stopped out it will continually re-enter the market on the same bar as the SMAs cross back and forth (and other conditions are met). So, it could attempt entries on the same bar continuously. I want to be able to limit how many entry attempts on the same bar it is allowed to take.

                            Note: If it is currently in a position one way (for example Long) and a reversal happens, then I still want to allow it to Sell to Cover and then Sell to Enter a new position as well (and vice versa)...just to be sure of that...

                            Thanks Koganam...Greg
                            Last edited by birdog; 01-16-2013, 11:54 PM. Reason: Added a Note on Reversals

                            Comment


                              #15
                              Hey there Matthew...can you take a look at post #10 when you get a chance? Also, had some developing communication with Koganam last night if you can look at that too.

                              I think we are close but just not quite there yet...smile.

                              Thanks Matthew!!!



                              Greg

                              Comment

                              Latest Posts

                              Collapse

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