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

Bars.Session.GetNextBeginEnd Problem

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

    Bars.Session.GetNextBeginEnd Problem

    I am trying to get the beginning and end times of yesterdays session but im not getting the results I expect. When I run the below script on a chart FGBL 03-13 from 6/12/12 to 6/3/13 the first printout in the output windows is:-

    Session Start: 01/01/0001 00:00:00 Session End: 01/01/0001 00:00:00143.513333333333

    and then the output starts at 10/1/2013 not 6/12/12 (when the chart starts),

    Session Start: 10/01/2013 08:00:00 Session End: 10/01/2013 22:00:00143.513333333333

    Here is the code,

    #region Using declarations
    using System;
    using System.ComponentModel;
    using System.Diagnostics;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Xml.Serialization;
    using NinjaTrader.Cbi;
    using NinjaTrader.Data;
    using NinjaTrader.Indicator;
    using NinjaTrader.Gui.Chart;
    using NinjaTrader.Strategy;
    #endregion

    // This namespace holds all strategies and is required. Do not change it.
    namespace NinjaTrader.Strategy
    {
    /// <summary>
    /// Enter the description of your strategy here
    /// </summary>
    [Description("Enter the description of your strategy here")]
    public class PivotTrader : Strategy
    {
    #region Variables

    private double r2;
    private double r1;
    private double pp;
    private double s1;
    private double s2;

    private DateTime sessionBegin;
    private DateTime sessionEnd;

    #endregion

    /// <summary>
    /// This method is used to configure the strategy and is called once before any strategy method is called.
    /// </summary>
    protected override void Initialize()
    {

    Add(PeriodType.Day, 1);



    CalculateOnBarClose = true;

    }

    /// <summary>
    /// Called on each bar update event (incoming tick)
    /// </summary>
    protected override void OnBarUpdate()
    {
    if(CurrentBars[1] <= 1)
    return;
    if(CurrentBars[1] == null)
    return;

    if (Bars.FirstBarOfSession)
    Bars.Session.GetNextBeginEnd(BarsArray[1], 0, out sessionBegin, out sessionEnd);

    // Calculate Pivot Points

    pp = (Highs[1][1] + Lows[1][1] + Closes[1][1])/3;

    //DrawLine("pp" + CurrentBars[1], false, sessionBegin, pp, sessionEnd, pp, Color.Red, DashStyle.Solid,1);

    Print("Session Start: " + sessionBegin + " Session End: " + sessionEnd + pp);


    }



    }


    }

    #2
    GKonheiser, that call would not be useful on the daily series, since the daily data would not be build according to the session template hours but would be what your data provider natively records the data with. The session template hours would only come in play with the intraday charts in NT.
    BertrandNinjaTrader Customer Service

    Comment


      #3
      OK thanks, then what is an effective way to define start and end times of a session for using in a DrawLine,

      ie DrawLine("pp" + CurrentBars[1], false, sessionBegin, pp, sessionEnd, pp, Color.Red, DashStyle.Solid,1);

      Comment


        #4
        You could for example look into this approach - it will draw and update a draw line per session. Currently just use the open value of the new session as example:

        if (Bars.SessionBreak)
        {
        sessionCount++;
        currOpen = Open[0];
        }

        DrawLine(sessionCount.ToString(), Bars.BarsSinceSession, currOpen, 0, currOpen, Color.Blue);
        BertrandNinjaTrader Customer Service

        Comment


          #5
          its starting to get there now but for some reason the lines are appering only from 10th Jan and the chart starts on 6thDec, also the Pivots are been draw over 2 days not just the one day.

          here is what I have so far,

          #region Using declarations
          using System;
          using System.ComponentModel;
          using System.Diagnostics;
          using System.Drawing;
          using System.Drawing.Drawing2D;
          using System.Xml.Serialization;
          using NinjaTrader.Cbi;
          using NinjaTrader.Data;
          using NinjaTrader.Indicator;
          using NinjaTrader.Gui.Chart;
          using NinjaTrader.Strategy;
          #endregion

          // This namespace holds all strategies and is required. Do not change it.
          namespace NinjaTrader.Strategy
          {
          /// <summary>
          /// Enter the description of your strategy here
          /// </summary>
          [Description("Enter the description of your strategy here")]
          public class PivotTrader : Strategy
          {
          #region Variables

          private double r2;
          private double r1;
          private double pp;
          private double s1;
          private double s2;

          private int sessionCount;
          private double currOpen;


          #endregion

          /// <summary>
          /// This method is used to configure the strategy and is called once before any strategy method is called.
          /// </summary>
          protected override void Initialize()
          {

          Add(PeriodType.Day, 1);



          CalculateOnBarClose = true;

          }

          /// <summary>
          /// Called on each bar update event (incoming tick)
          /// </summary>
          protected override void OnBarUpdate()
          {
          if(CurrentBars[1] <= 1)
          return;
          if(CurrentBars[1] == null)
          return;





          // Calculate Pivot Points and draw Pivot

          pp = (Highs[1][1] + Lows[1][1] + Closes[1][1])/3;

          DrawLine("pp" + CurrentBar, Bars.BarsSinceSession, pp, 0, pp , Color.Blue);




          }



          }


          }

          Comment


            #6
            Because you have the daily bars added, your BarsRequired would also apply to those, so with a default of 20 > you would not see OnBarUpdate calls then until 20 days have passed.

            For the duplicate, you do not filter for any BarsInProgress calls in your script and also would suggest to tag the drawing object by session simply -

            Code:
            if(CurrentBars[1] <= 1)
             	return;
             			
            if(CurrentBars[1] == null)
             	return;
            			
            if (BarsInProgress != 0) return;
            			
            if (Bars.SessionBreak)
            	sessionCount++;
            				
            // Calculate Pivot Points and draw Pivot
            
             	pp = (Highs[1][1] + Lows[1][1] + Closes[1][1])/3;
            
             	DrawLine("PP" + sessionCount, Bars.BarsSinceSession, pp, 0, pp , Color.Blue);
            BertrandNinjaTrader Customer Service

            Comment


              #7
              Just want to point to a pivots indicator that uses sessions and allows for calculating floor pivots / Globex pivots

              -> for the full session
              -> for the regular session (bond market session)
              -> by using the settlement price, the regular or full session close

              The indicator can also be used to display JacksonZones, wide floor pivots (different formula for levels S3, S4 and S5), Camarilla pivots or Fibonacci pivots There is an option to display the pivot range (mirroring of central pivot around floor pivot) and midpivots.

              Attached are two charts showing regular session and full session pivots for FGBL 12-13. If you wish to modify the indicators, do not hesitate to come back. I am happy to share the details. The indicator is available for free download



              For showing the regular session pivots, a specific session template is required that singles out the regular session. For the full session pivots, the default template for FGBL should be fine.

              The indicator comes open source and uses both Bars.Session.GetNextBeginEnd(Bars, 0, out sessionBegin, out sessionEnd) and Bars.GetTradingDayFromLocal(time). I have not used the Add() method, because that method does not insert the daily data correctly, if a template with several sessions per day is used. I have therefore reused the old asynchronous method, which is also used for the NinjaTrader default pivots.
              Attached Files
              Last edited by Harry; 10-22-2013, 01:55 PM.

              Comment


                #8
                Thanks very much Harry. I will take a look. I want to program my own pivots as I am new to coding and need as much practice as possible. It will be a useful reference.

                Comment


                  #9
                  Thanks Bertrand,

                  Is there a resource of all the definitions of all the overload methods such as Bars.SessionBreak, I had a look in the help guild but could find any reference it that?

                  Comment


                    #10
                    Sure, you can check into our language reference from the helpguide, for example the Bars object would documented here - http://www.ninjatrader.com/support/h...s/nt7/bars.htm
                    BertrandNinjaTrader Customer Service

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by Radano, 06-10-2021, 01:40 AM
                    19 responses
                    606 views
                    0 likes
                    Last Post Radano
                    by Radano
                     
                    Started by KenneGaray, Today, 03:48 AM
                    0 responses
                    4 views
                    0 likes
                    Last Post KenneGaray  
                    Started by thanajo, 05-04-2021, 02:11 AM
                    4 responses
                    470 views
                    0 likes
                    Last Post tradingnasdaqprueba  
                    Started by aa731, Today, 02:54 AM
                    0 responses
                    5 views
                    0 likes
                    Last Post aa731
                    by aa731
                     
                    Started by Christopher_R, Today, 12:29 AM
                    0 responses
                    11 views
                    0 likes
                    Last Post Christopher_R  
                    Working...
                    X