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

sessionBegin/End condition logic

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

    sessionBegin/End condition logic

    I'm trying to start an indicator if the current time is within the current session but having problems.

    I'm using:


    dateTag = DateTime.Now.Date.Month.ToString("00") + DateTime.Now.Date.Day.ToString("00");

    if(dateTag == sessionBegin.Month.ToString() + sessionBegin.Day.ToString())
    {
    runRealTime = true;
    Print("VolumeUpDownOne" + " runRealTime " + runRealTime );
    }

    but this logic never turns true. The code compiles, runs normally and produces no error in the Ninja Trader log.

    What am I doing wrong?

    regards,
    taddypole...

    #2
    Hello Taddypole,

    I'm not quite sure what you are trying to find out.

    Your script will only evaluate if it is receiving data. It will only receive data if the current time is during the session.

    If you change the session on the chart, this changes the time of Bars.Session.NextBeginTime.

    Are you instead using the Default 24/7 template and want to find the session times of the instrument in the Instrument Manager?
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Hi Chelsea,

      I'm using this logic in a volume indicator and don't want the previous day's volume to keep the scaling based on yesterday's volume. I want to start today's (this sessions) scaling fresh based on the current volume (usually low).

      So if I load the chart with a weeks worth of data, I don't want any historical volume data. Just start calculating volume at the beginning of the latest session.

      So the session settings are "use instrument settings".

      My thoughts are when a new session starts, I reload ninja script and use the code to determine when the new session started. If the new session is in progress, then calculate historical volume from the session begin time to Now and continue volume calculations as real time progresses.

      does this make sense?

      Comment


        #4
        Hi Taddypole,

        If you are accumulating data on each OnBarUpdate, you can reset your variable on the new session. This might be the easiest way.

        If (FirstBarOfSession)
        {
        myVariable = 0;
        }


        However, this does not quite answer the question.
        Are you using the Default 24/7 session template?
        Are you only checking the current time and not the time of the bar?

        I ask because you probably want something like:
        if (DateTime.Now > Instrument.MasterInstrument.Session.NextBeginTime. AddDays(-1) && Time[CurrentBar-Bars.GetBar(Instrument.MasterInstrument.Session.Ne xtBeginTime.AddDays(-1))+1].Hour == Instrument.MasterInstrument.Session.NextBeginTime. AddDays(-1).Hour)

        This checks that the current time is after the beginning of the session, and that there is a session today.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          I tried your suggestion which seems like it might work but I'm getting indexing errors as highlighted in the "Code" attachment.

          I increased the number of bars before starting calculations to 500 and still get the indexing error.

          What else might i need?

          thank you,
          taddypole...
          Attached Files

          Comment


            #6
            Hi Taddypole,

            That wasn't tested but was a general idea of what you would be looking for. Really the GetNextBeginTime returns a time without date which causes the GetBar to be incorrect.

            If you are using the DateTime.Now instead of the bar time, the following code is actually easier and I did test this to ensure it works.

            Code:
            private DateTime sessionBegin;
            private DateTime sessionEnd;
            
            protected override void OnBarUpdate()
            {
            Instrument.MasterInstrument.Session.GetNextBeginEnd(BarsArray[0], 0, out sessionBegin, out sessionEnd);
            if (DateTime.Now > sessionBegin && DateTime.Now < sessionEnd)
            {
            // execute code
            }
            Below is a link to the help guide on Bars.Session.GetNextBeginEnd().
            http://www.ninjatrader.com/support/h...xtbeginend.htm


            However, as I mentioned, the better way to go about this, is to reset a variable when there is a new session. Though I am just assuming that you are trying to reset an accumulation when there is a new session.
            Last edited by NinjaTrader_ChelseaB; 12-31-2014, 01:47 PM.
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Thank you Chelsea,

              that's exactly what I was looking for. As you can see in the attachment, it is working!

              regards,
              taddypole...
              Attached Files

              Comment


                #8
                I'm trying to modify the VolumeUpDown indicator by not plotting the volume bars for some period of time after sessionBegin.

                I'm using: sessionBegin.AddHour(1)

                But for some reason this is not working. It is still plotting the bars.

                Code and picture attached.

                Can someone tell me what I'm doing wrong?

                regards,

                taddypole...
                Attached Files
                Last edited by Taddypole; 01-15-2015, 01:13 PM.

                Comment


                  #9
                  TaddyPole,

                  You would want to use Time[0] instead of DateTime.Now as this would never be true.

                  Aditionally, you need to reset the CurrentSession bool otherwise once its set it will always be true.
                  Code:
                  CurrentSession = false;
                  if(Time[0] > sessionBegin.AddHours(1) && Time[0] < sessionEnd)
                       {     CurrentSession = true;     }
                  Cal H.NinjaTrader Customer Service

                  Comment


                    #10
                    Cal,

                    Thank you for the help.

                    Here is the solution: (Its a combination of both "if" statements)

                    if (DateTime.Now > sessionBegin && DateTime.Now < sessionEnd)
                    {
                    if(Time[0] > sessionBegin.AddHours(1) && Time[0] < sessionEnd)
                    {
                    CurrentSession = true;
                    }
                    }

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by algospoke, Yesterday, 06:40 PM
                    2 responses
                    19 views
                    0 likes
                    Last Post algospoke  
                    Started by ghoul, Today, 06:02 PM
                    3 responses
                    14 views
                    0 likes
                    Last Post NinjaTrader_Manfred  
                    Started by jeronymite, 04-12-2024, 04:26 PM
                    3 responses
                    45 views
                    0 likes
                    Last Post jeronymite  
                    Started by Barry Milan, Yesterday, 10:35 PM
                    7 responses
                    21 views
                    0 likes
                    Last Post NinjaTrader_Manfred  
                    Started by AttiM, 02-14-2024, 05:20 PM
                    10 responses
                    181 views
                    0 likes
                    Last Post jeronymite  
                    Working...
                    X