Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Help to explain sample code

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

    Help to explain sample code

    Hi, Could somebody explain to me briefly how this snippet works?
    It is from sample code from NT "Strategy: Monitoring for and trading a breakout "

    I especially wonder if how "FirstBarOfSession" and "BarsSinceSession " work here.

    Thanks


    protectedoverridevoid OnBarUpdate()
    {
    // Resets the highest high at the start of every new session
    if (Bars.FirstBarOfSession)
    highestHigh = High[
    0];

    // Stores the highest high from the first 30 bars
    if (Bars.BarsSinceSession < 30 && High[0] > highestHigh)
    highestHigh = High[
    0];

    // Entry Condition: Submit a buy stop order one tick above the first 30 bar high. Cancel if not filled within 10 bars.
    if (Bars.BarsSinceSession > 30 && Bars.BarsSinceSession < 40)
    {
    // EnterLongStop() can be used to generate buy stop orders.
    EnterLongStop(highestHigh + TickSize);
    }

    // Exit Condition: Close positions after 10 bars have passed
    if (BarsSinceEntry() > 10)
    ExitLong();
    }

    #2
    Hi ssg10, Bars.FirstBarOfSession returns true when a new session begins, this is then used in the code reset the high breakout variable.

    Bars.BarsSinceSession counts the bars since Bars.FirstBarOfSession was true...this is then used to track the highestHigh of the first 30 bars in the new session.

    Comment


      #3
      How do you define "session" ?

      Comment


        #4
        The session is defined when running a backtest, these are the start time and end time parameters.
        RayNinjaTrader Customer Service

        Comment


          #5
          So, does this strategy only for backtesting?

          From the code, this buy or sell only gets executed once, right? since the condition below might be true only once (<30)

          if (Bars.BarsSinceSession < 30 && High[0] > highestHigh)
          highestHigh = High[
          0];

          Comment


            #6
            ssg10,

            Your strategy can run on real-time too. In real-time, the session is defined by the chart you throw your strategy onto. If you run it from the Strategies tab you will have similar options to select it as you did when backtesting.

            The condition has 30 opportunities to be true. Every bar after session start before bar 30 is available.
            Josh P.NinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Geovanny Suaza, 02-11-2026, 06:32 PM
            0 responses
            650 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
            577 views
            1 like
            Last Post RFrosty
            by RFrosty
             
            Working...
            X