Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Strategy entering and exiting trades at the same bar

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

    Strategy entering and exiting trades at the same bar

    I am in the process of writing a strategy, I am currently doing optimization. I have noticed a lot of break-even trades in the trade log. I have checked the chart, and all the breakeven trades were only one bar trades and the strategy entered and exited the trades at the same bar. And all of these trades have a gap up or gap down.
    I have tried to handle this situation by entering a limit order at the gap fill.
    Code:
    if(Open[0] != Close[1]) // Handling gap areas
    {
    	EnterLongLimit(Close[1],"limit order gap _ long");
    }
    But its still doing similar trades. Any insight into the situation will be helpful. Thanks.

    #2
    sherazwaqar,

    There are discrepencies between live trading and back-testing.



    This issue is probably related to granularity (i.e. tick data isn't being run through the strategy, higher time frame data is). You can get intra-bar granularity using the following reference sample.

    You can submit orders to different Bars objects. This allows you the flexibility of submitting orders to different timeframes. Like in live trading, taking entry conditions from a 5min chart means executing your order as soon as possible instead of waiting until the next 5min bar starts building. You can achieve this by
    Adam P.NinjaTrader Customer Service

    Comment


      #3
      I have wrapped by strategy code in OnBarUpdate() method

      if(BarsInProgress == 0)
      {
      Strategy code
      }
      else
      {
      return;
      }

      Also added the following to Initialize() method
      Add(PeriodType.Minute,1);

      I am running the strategy on 3 min chart ...

      I have backtested and it is not executing any trades. Is there anything else I need to do.
      Thanks

      Comment


        #4
        sherezwaqar,

        Before you mentioned it had a lot of break-even trades. Is it now not entering trades at all?

        From the small piece of code it is difficult to determine. Could you perhaps post more?
        Adam P.NinjaTrader Customer Service

        Comment


          #5
          Code:
          if(Buy code)
          {
          	if(Open[0] == Close[1])
          	{
          		EnterLong();
          	}
          	if(Open[0] != Close[1]) // Handling gap areas
          	{
          		EnterLongLimit(Close[1],"limit order gap _ long");
          	}
          }
          This is the code handling buying part ...

          Comment


            #6
            sherazwaqar,

            So far I see nothing immediately wrong with it, but you may want to use some EntriesPerDirection handling here or some logic ensuring you aren't entering a long limit order when you are already in a long. Its possible you could be in a long position, then the other condition becomes true.

            What is the If(Buy Code) portion?

            Are you attaching this to a chart or backtesting it? Is it not generating any trades whatsoever?

            Here is a forum on EntriesPerDirection : http://www.ninjatrader.com/support/f...ad.php?t=45117

            Here is a help guide entry on MarketPosition : http://www.ninjatrader.com/support/h...etposition.htm
            Adam P.NinjaTrader Customer Service

            Comment


              #7
              Buy code are the conditions that I am using to identify long opportunities. It make sure that I am already flat before entering a long trade.

              Code:
              if(Position.MarketPosition == MarketPosition.Flat && Close[0] > fractals().lastUpFractal(1)
              				&& Math.Abs(fibMC1[0])>TickSize*threshold && CountIf(delegate { return Math.Abs(fibMC1[1]) <= TickSize*threshold;},congestion)>=congestion && Close[0]>EMA(period3)[0])
              {
              	if(Open[0] == Close[1])
              	{
              		EnterLong();
              	}
              	if(Open[0] != Close[1]) // Handling gap areas
              	{
              		EnterLongLimit(Close[1],"limit order gap _ long");
              	}
              }
              here is the complete code

              Comment


                #8
                sheraxwaqar,

                If you just do this :
                Code:
                f(Position.MarketPosition == MarketPosition.Flat && Close[0] > fractals().lastUpFractal(1)
                				&& Math.Abs(fibMC1[0])>TickSize*threshold && CountIf(delegate { return Math.Abs(fibMC1[1]) <= TickSize*threshold;},congestion)>=congestion && Close[0]>EMA(period3)[0])
                {
                	//if(Open[0] == Close[1])
                	//{
                		EnterLong();
                	//}
                	//if(Open[0] != Close[1]) // Handling gap areas
                	//{
                	//	EnterLongLimit(Close[1],"limit order gap _ long");
                	//}
                }
                Does it enter trades?
                Adam P.NinjaTrader Customer Service

                Comment


                  #9
                  No it does not enter trades ... after commenting out the portions you mentioned...

                  Comment


                    #10
                    sherazwaqar,

                    if(Position.MarketPosition == MarketPosition.Flat && Close[0] > fractals().lastUpFractal(1)
                    && Math.Abs(fibMC1[0])>TickSize*threshold && CountIf(delegate { return Math.Abs(fibMC1[1]) <= TickSize*threshold;},congestion)>=congestion && Close[0]>EMA(period3)[0])

                    Is probably causing the issue somewhere. It may be an unsatisfiable condition, or one of the indicators being used is giving incorrect results.

                    We essentially isolated the issue to there.
                    Adam P.NinjaTrader Customer Service

                    Comment


                      #11
                      I have checked the output window and it say that the CountIf cannot be run on multi-series strategies.
                      Any workarounds of using countif.
                      Thanks

                      Comment


                        #12
                        sherazwaqar,

                        I would recommend creating a for loop to look back over the set number of bars, then have it increment a counter each time your statement is true. You can then use them for your comparison.

                        Here is some code to demonstrate the idea :

                        Code:
                        int counter = 0;
                        for( int i = 0 ; i<=8 ; i++)
                        {
                           if( Condition is true for Array[i] )
                           {
                               counter++;
                           }
                        }
                        
                        if ( other_conditions && counter>=8 )
                        {
                        
                            // do something
                        }
                        Adam P.NinjaTrader Customer Service

                        Comment


                          #13
                          Thanks .. Adam for your generous help.

                          Comment


                            #14
                            sherazwaqar,

                            No problem.

                            Please don't hesitate to post in our forums or contact us if you require assistance.
                            Adam P.NinjaTrader Customer Service

                            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