Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

FirstBarOfSession problem

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

    FirstBarOfSession problem

    I have a strategy that runs on 5 minute bars. The buy signal occurs when the close of the current bar occurs above the high of the first bar of the session. The problem is after one minute the enterlong command fires at a significantly higher price than the current price. here is the enterlong code
    Code:
     
     
    /// Rule 1; enter long if close above first bar high...;
    if (Bars.FirstBarOfSession)
    { 
    return;
    }
    else
    { barOne = Bars.BarsSinceSession;
    myStop = ExitLongStop(Low[barOne],"initial","Entry");
    if (Close[0] > High[barOne] && Position.Quantity == 0)
    { 
    firstTime = false;
    Print(CurrentBar);
     
    if (!firstTime)
    {
    firstTime = true;
    EnterLong("Entry");
    barLong = 0;
    size = Position.Quantity;
    mxi = 0;
     
    }
    }
    }

    Question 1; Why is this not waiting on the bar to complete?
    question 2; Why is the slippage so high?
    Last edited by Ragdoll; 10-03-2013, 02:06 PM.

    #2
    Is calculate on bar close set to true or false? Is this a real time trade or historical trade?

    The logic here is a little hard for me to read, would you mind posting the .cs file? Located in (MY)Documents\NinjaTrader 7\bin\Custom\Strategy

    Ultimately the script will need to be debugged and tips for this can be found here: http://www.ninjatrader.com/support/f...ead.php?t=3418

    Also instead of checking the position you can use this to check for flat or not: http://www.ninjatrader.com/support/h...etposition.htm

    Let me know how I can further assist.
    LanceNinjaTrader Customer Service

    Comment


      #3
      Here is the file.
      To answer your questions this occurred in both real time and I was able to recreate the problem in market replay. Calculate on bar close is true. The Print(CurrentBar) returned a value of 307. I'm assuming that is probably random.

      While I appreciate the troubleshooting tips, I have been coding and troubleshooting code for over 30 years. That being said please understand that this is a work in progress and as such contains a lot of print statements and extraneous comments.
      Attached Files

      Comment


        #4
        CurrentBar will print out the current bar as it corresponds to total number of bars on the chart.


        As for why you received a higher than expected fill, what instrument and what time/timezone was this trade at?

        Additionally you'll want to ensure you're in a position before placing an exit long
        myStop = ExitLongStop(Low[barOne],"initial","Entry");

        As this order would be ignored.

        Here is a good script for learning more about IOrders and advanced handling: http://www.ninjatrader.com/support/f...ead.php?t=7499

        Let me know how I can further assist.
        LanceNinjaTrader Customer Service

        Comment


          #5
          The instruments were aapl and rgr. They are both stocks AAPL is on the Nasdaq and RGR is on Nyse. Timezone is eastern. They both fired before the first bar finished. Session is US Equities RTH


          I am counting on the ExitLongStop being ignored until a position is established. That part works fine. The primary problem is the first bar of session call.
          Last edited by Ragdoll; 10-03-2013, 03:39 PM.

          Comment


            #6
            After testing this script further I've found that it's entering off of the condition from the bar at the end of the prior session. Once the first tick of the new session comes in it's triggering an entry.

            You will need to implement logic to prevent trading off of the final candle of the prior session.
            Let me know if I can further assist.
            LanceNinjaTrader Customer Service

            Comment


              #7
              I HAVE! It's called FirstBarOfSession!

              Comment


                #8
                Yes that will identify the first bar of the session, but your trade is coming from the prior bar.

                On the last bar this is triggering

                Code:
                { 
                				barOne = Bars.BarsSinceSession;
                			  	myStop = ExitLongStop(Low[barOne],"initial","Entry");
                				if (Close[0] > High[barOne] && Position.Quantity == 0)
                					{	
                						firstTime = false;
                						Print(CurrentBar);
                						
                						if (!firstTime)
                						{
                							firstTime = true;
                							[B]EnterLong("Entry");[/B]
                							barLong = 0;
                				 			size = Position.Quantity;
                							mxi = 0;
                							
                						}
                					}
                			}
                Let me know if I can further assist
                LanceNinjaTrader Customer Service

                Comment


                  #9
                  I realize the EnterLong is being triggered but this code should keep it from ever getting there!

                  Code:
                   
                  /// Rule 1; enter long if close above first bar high...;
                  i[B]f (Bars.FirstBarOfSession)[/B]
                  [B]{ [/B]
                  [B]return;[/B]
                  [B]}[/B]
                  else
                  { barOne = Bars.BarsSinceSession;
                  myStop = ExitLongStop(Low[barOne],"initial","Entry");
                  if (Close[0] > High[barOne] && Position.Quantity == 0)
                  { 
                  firstTime = false;
                  Print(CurrentBar);
                   
                  if (!firstTime)
                  {
                  firstTime = true;
                  EnterLong("Entry");
                  barLong = 0;
                  size = Position.Quantity;
                  mxi = 0;
                   
                  }
                  }
                  }

                  Comment


                    #10
                    Hello,

                    This line of code prevents the first bar from getting past this point.

                    However the first bar is not the bar triggering the entry.

                    It is the final bar of the last session. When the final bar closes, this is where your entry is taking place.

                    A bar will not register as closed until the next tick of the following bar comes in: http://www.ninjatrader.com/support/h..._are_built.htm

                    Let me know if I can further assist.
                    LanceNinjaTrader Customer Service

                    Comment


                      #11
                      So if I am reading this right this code should work.
                      Code:
                       
                       
                      /// Rule 1; enter long if close above first bar high...;
                      if (Bars.FirstBarOfSession)
                      { 
                      return;
                      }
                      else
                      { barOne = Bars.BarsSinceSession;
                      myStop = ExitLongStop(Low[barOne],"initial","Entry");
                      if (Close[0] > High[barOne] && Position.Quantity == 0)
                      { 
                      firstTime = false;
                      Print(CurrentBar);
                       
                      [B]if (!firstTime && !FirstTickOfBar)[/B]
                      {
                      firstTime = true;
                      EnterLong("Entry");
                      barLong = 0;
                      size = Position.Quantity;
                      mxi = 0;
                       
                      }
                      }
                      }

                      Comment


                        #12
                        You will likely have the same issue with this as well.

                        FirstTickOfBar is only a factor when CalculateOnBarClose = false

                        In your case you would need to add some logic to prevent trading after the last bar.

                        If you don't need historical trades you could do this
                        if(Historical)
                        return;

                        Otherwise you could add some logic to your if statement to not enter long if its greater than a specified time: http://www.ninjatrader.com/support/h...tml?totime.htm

                        There are other ways you could do this as well but those would likely be the simplest.

                        Let me know if I can further assist.
                        LanceNinjaTrader Customer Service

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                        0 responses
                        637 views
                        0 likes
                        Last Post Geovanny Suaza  
                        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                        0 responses
                        366 views
                        1 like
                        Last Post Geovanny Suaza  
                        Started by Mindset, 02-09-2026, 11:44 AM
                        0 responses
                        107 views
                        0 likes
                        Last Post Mindset
                        by Mindset
                         
                        Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                        0 responses
                        569 views
                        1 like
                        Last Post Geovanny Suaza  
                        Started by RFrosty, 01-28-2026, 06:49 PM
                        0 responses
                        571 views
                        1 like
                        Last Post RFrosty
                        by RFrosty
                         
                        Working...
                        X