Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

First bar of session

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

    First bar of session

    hi, i want to know the first bar of this session (in this day)

    Example:

    bool Inizio=false;

    if it's first bar of session daily ===> inizio=true;

    how can do it ? thanks
    Last edited by esignal; 01-20-2017, 09:00 AM.

    #2
    Hello esignal,

    Thank you for your note.

    You would use the following method to identify the first bar of session, which will return true if so.

    if (Bars.IsFirstBarOfSession)

    See isfirstbarofsession section of our helpguide.


    The following would draw an arrow on each bar that was the first bar of session and if the day was the current day.

    Code:
    if (Bars.IsFirstBarOfSession && Time[0].Date == nowDate.Date)
    {
        Draw.ArrowDown(this, @"MyCustomStrategy14" + CurrentBar, false, 0, High[0], Brushes.Red);
    }
    Note, most futures contracts the First Bar of Session is in the previous day so the above condition would be best applied to equities.

    Please let us know if you need further assistance.
    Alan P.NinjaTrader Customer Service

    Comment


      #3
      Excuse me...nowDate what is? ....i can't find it

      Another question :

      is correct to use :

      CurrentDayOHL().High[0]


      to have High of this session day? thanks
      Last edited by esignal; 01-20-2017, 09:24 AM.

      Comment


        #4
        Hello esignal,

        I apologize I left that line of code out in my previous message. nowDate is a DateTime variable set to today.

        The full code,

        Code:
        DateTime nowDate =DateTime.Now;
        
        if (Bars.IsFirstBarOfSession && Time[0].Date == nowDate.Date)
        {
        Draw.ArrowDown(this, @"MyCustomStrategy14" + CurrentBar, false, 0, High[0], Brushes.Red);
        }
        Link to DateTime.Now,
        Get the current time with the DateTime.Now property. The current day is part of Now.


        To get the current sessions high you would use CurrentDayOHL().CurrentHigh[0] rather than CurrentDayOHL().High[0], which will give you the current bars high.

        Please let us know if you need further assistance.
        Alan P.NinjaTrader Customer Service

        Comment


          #5
          Time[0].Date == nowDate.Date

          hi..there is another problem

          if i do backtest with my strategy don't run correctly because nowDate.Date is time of execution and Time[0].Date is time of candle in chart

          Comment


            #6
            Hello Esignal,

            If you’d like your strategy to execute or calculate off the first bar of the session, you should just use,

            if (Bars.IsFirstBarOfSession)

            Please let us know if you need further assistance.
            Alan P.NinjaTrader Customer Service

            Comment


              #7
              ok..

              but if i use it in indicator

              example :

              if (Bars.IsFirstBarOfSession && Time[0].Date == nowDate.Date)
              {
              Draw.ArrowDown(this, @"MyCustomStrategy14" + CurrentBar, false, 0, High[0], Brushes.Red);
              }


              i don't see ArrowDown in historical chart !..

              i see only indicator in realtime correctly represented...but not in historical
              Last edited by esignal; 01-21-2017, 09:43 AM.

              Comment


                #8
                Hello Esignal,

                The reason why the statement below will only be true in real time, is that nowDate.Date will only equal today, even historically.

                If you’d like this to work historically you should remove the Time[0].Date == nowDate.Date), so that you are just looking for the first bar of session.

                Use,

                if (Bars.IsFirstBarOfSession)

                Not,

                if (Bars.IsFirstBarOfSession && Time[0].Date == nowDate.Date)

                I would suggest adding print statements to check whether your conditions are becoming true/what variables equal. I’ve provided a link to a youtube video which covers an example of using prints to understand behavior:

                Dive into manipulating C# code from within an unlocked NinjaScript strategy using the NinjaScript Editor.NinjaTrader 7 is an award winning end to end online ...


                I’ve provided a link covering debugging which you may find helpful.
                Debugging: http://ninjatrader.com/support/forum...ead.php?t=3418

                Please let us know if you need further assistance.
                Alan P.NinjaTrader Customer Service

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by The_Sec, Yesterday, 03:37 PM
                1 response
                11 views
                0 likes
                Last Post NinjaTrader_Gaby  
                Started by vecnopus, Today, 06:15 AM
                0 responses
                1 view
                0 likes
                Last Post vecnopus  
                Started by Aviram Y, Today, 05:29 AM
                0 responses
                5 views
                0 likes
                Last Post Aviram Y  
                Started by quantismo, 04-17-2024, 05:13 PM
                3 responses
                27 views
                0 likes
                Last Post NinjaTrader_Gaby  
                Started by ScottWalsh, 04-16-2024, 04:29 PM
                7 responses
                36 views
                0 likes
                Last Post NinjaTrader_Gaby  
                Working...
                X