Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

NEED URGENT HELP Regarding Custom Bar Types

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

    NEED URGENT HELP Regarding Custom Bar Types

    Hello there
    I just started learning about ninjascript 8 and gone through few online tutorials and the documentation part but can't able to get a good command over creating custom bar types.
    I was able to create custom indicator easily but facing difficulty in understand where to start and how to start building custom bar types.

    Issues that I'm facing --
    • Not able even understand the pre-build Minute bartype code in ninjascript -- means what all the functions does and what that code inside those functions do and how to modify that to one's needs
    • not enough resources, I checked the documentation but there's only few functions description and not the whole code explanation

    Would be great if anybody could help me get over this.

    THANKS

    #2
    Hello UniPiece-daivil,

    Welcome to the NinjaTrader forums!

    The methods are documented in the help guide linked below, with the exception of the ChartLabel() method (which formats the time information).



    Is there a specific method you are finding is not documented you would like more information about?

    You may find the custom bar types on the UserAppShare helpful.
    (Update March 11th, 2020 – Fix for TickReplay, calculates values if bar object is mid-session and have not been calculated as least once) This is a conversion of the UniRenko ‐ Universal Renko BarType developed and originally coded for the NinjaTrader 7 platform by monpere. Please contact the original author for any questions or comments. […]




    In general the main method is the OnDataPoint() method. This updates when a tick a received.
    The
    By calling AddBar() this will add a new bar with the open, high, low, and close values provided.
    By calling UpdateBar() this will update the existing bar with the high, low, and close values provided.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Thanks for this but It would be better if you could explain this code and what each function does in this ninjaScript's inbuilt Minute Bar type so that i can get a fair idea of what changes i need to do where

      Comment


        #4
        namespace NinjaTrader.NinjaScript.BarsTypes
        {
        public class MinuteBarsType : BarsType
        {
        public override void ApplyDefaultBasePeriodValue(BarsPeriod period) {}

        public override void ApplyDefaultValue(BarsPeriod period)
        {
        period.Value = 1;
        }

        public override string ChartLabel(DateTime time) { return time.ToString("HH:mm"); }

        public override int GetInitialLookBackDays(BarsPeriod barsPeriod, TradingHours tradingHours, int barsBack)
        {
        int minutesPerWeek = 0;
        lock (tradingHours.Sessions)
        {
        foreach (Session session in tradingHours.Sessions)
        {
        int beginDay = (int) session.BeginDay;
        int endDay = (int) session.EndDay;
        if (beginDay > endDay)
        endDay += 7;

        minutesPerWeek += (endDay - beginDay) * 1440 + ((session.EndTime / 100) * 60 + (session.EndTime % 100)) - ((session.BeginTime / 100) * 60 + (session.BeginTime % 100));
        }
        }

        return (int) Math.Max(1, Math.Ceiling(barsBack / Math.Max(1, minutesPerWeek / 7.0 / barsPeriod.Value) * 1.05));
        }

        public override double GetPercentComplete(Bars bars, DateTime now)
        {
        return now <= bars.LastBarTime ? 1.0 - (bars.LastBarTime.Subtract(now).TotalMinutes / bars.BarsPeriod.Value) : 1;
        }

        protected override void OnDataPoint(Bars bars, double open, double high, double low, double close, DateTime time, long volume, bool isBar, double bid, double ask)
        {
        if (SessionIterator == null)
        SessionIterator = new SessionIterator(bars);

        if (bars.Count == 0)
        AddBar(bars, open, high, low, close, TimeToBarTime(bars, time, isBar), volume);
        else if (!isBar && time < bars.LastBarTime)
        UpdateBar(bars, high, low, close, bars.LastBarTime, volume);
        else if (isBar && time <= bars.LastBarTime)
        UpdateBar(bars, high, low, close, bars.LastBarTime, volume);
        else
        {
        time = TimeToBarTime(bars, time, isBar);
        AddBar(bars, open, high, low, close, time, volume);
        }
        }

        protected override void OnStateChange()
        {
        if (State == State.SetDefaults)
        {
        Name = Custom.Resource.NinjaScriptBarsTypeMinute;
        BarsPeriod = new BarsPeriod { BarsPeriodType = BarsPeriodType.Minute, Value = 1 };
        BuiltFrom = BarsPeriodType.Minute;
        DaysToLoad = 5;
        IsIntraday = true;
        IsTimeBased = true;
        }
        else if (State == State.Configure)
        {
        Name = string.Format(Core.Globals.GeneralOptions.CurrentC ulture, Custom.Resource.DataBarsTypeMinute, BarsPeriod.Value, (BarsPeriod.MarketDataType != MarketDataType.Last ? string.Format(" - {0}", Core.Globals.ToLocalizedObject(BarsPeriod.MarketDa taType, Core.Globals.GeneralOptions.CurrentUICulture)) : string.Empty));

        Properties.Remove(Properties.Find("BaseBarsPeriodT ype", true));
        Properties.Remove(Properties.Find("BaseBarsPeriodV alue", true));
        Properties.Remove(Properties.Find("PointAndFigureP riceType", true));
        Properties.Remove(Properties.Find("ReversalType", true));
        Properties.Remove(Properties.Find("Value2", true));
        }
        }

        private DateTime TimeToBarTime(Bars bars, DateTime time, bool isBar)
        {
        if (SessionIterator.IsNewSession(time, isBar))
        SessionIterator.GetNextSession(time, isBar);

        if (bars.IsResetOnNewTradingDay || (!bars.IsResetOnNewTradingDay && bars.Count == 0))
        {
        DateTime barTimeStamp = isBar
        ? SessionIterator.ActualSessionBegin.AddMinutes(Math .Ceiling(Math.Ceiling(Math.Max(0, time.Subtract(SessionIterator.ActualSessionBegin). TotalMinutes)) / bars.BarsPeriod.Value) * bars.BarsPeriod.Value)
        : SessionIterator.ActualSessionBegin.AddMinutes(bars .BarsPeriod.Value + Math.Floor(Math.Floor(Math.Max(0, time.Subtract(SessionIterator.ActualSessionBegin). TotalMinutes)) / bars.BarsPeriod.Value) * bars.BarsPeriod.Value);
        if (bars.TradingHours.Sessions.Count > 0 && barTimeStamp > SessionIterator.ActualSessionEnd) // Cut last bar in session down to session end on odd session end time
        barTimeStamp = SessionIterator.ActualSessionEnd;
        return barTimeStamp;
        }
        else
        {
        DateTime lastBarTime = bars.GetTime(bars.Count - 1);
        DateTime barTimeStamp = isBar
        ? lastBarTime.AddMinutes(Math.Ceiling(Math.Ceiling(M ath.Max(0, time.Subtract(lastBarTime).TotalMinutes)) / bars.BarsPeriod.Value) * bars.BarsPeriod.Value)
        : lastBarTime.AddMinutes(bars.BarsPeriod.Value + Math.Floor(Math.Floor(Math.Max(0, time.Subtract(lastBarTime).TotalMinutes)) / bars.BarsPeriod.Value) * bars.BarsPeriod.Value);
        if (bars.TradingHours.Sessions.Count > 0 && barTimeStamp > SessionIterator.ActualSessionEnd)
        {
        DateTime saveActualSessionEnd = SessionIterator.ActualSessionEnd;
        SessionIterator.GetNextSession(SessionIterator.Act ualSessionEnd.AddSeconds(1), isBar);
        barTimeStamp = SessionIterator.ActualSessionBegin.AddMinutes((int ) barTimeStamp.Subtract(saveActualSessionEnd).TotalM inutes);
        }
        return barTimeStamp;
        }
        }
        }

        Comment


          #5
          Originally posted by UniPiece-daivil View Post
          namespace NinjaTrader.NinjaScript.BarsTypes
          {
          public class MinuteBarsType : BarsType
          {
          public override void ApplyDefaultBasePeriodValue(BarsPeriod period) {}

          public override void ApplyDefaultValue(BarsPeriod period)
          {
          period.Value = 1;
          }

          public override string ChartLabel(DateTime time) { return time.ToString("HH:mm"); }

          public override int GetInitialLookBackDays(BarsPeriod barsPeriod, TradingHours tradingHours, int barsBack)
          {
          int minutesPerWeek = 0;
          lock (tradingHours.Sessions)
          {
          foreach (Session session in tradingHours.Sessions)
          {
          int beginDay = (int) session.BeginDay;
          int endDay = (int) session.EndDay;
          if (beginDay > endDay)
          endDay += 7;

          minutesPerWeek += (endDay - beginDay) * 1440 + ((session.EndTime / 100) * 60 + (session.EndTime % 100)) - ((session.BeginTime / 100) * 60 + (session.BeginTime % 100));
          }
          }

          return (int) Math.Max(1, Math.Ceiling(barsBack / Math.Max(1, minutesPerWeek / 7.0 / barsPeriod.Value) * 1.05));
          }

          public override double GetPercentComplete(Bars bars, DateTime now)
          {
          return now <= bars.LastBarTime ? 1.0 - (bars.LastBarTime.Subtract(now).TotalMinutes / bars.BarsPeriod.Value) : 1;
          }

          protected override void OnDataPoint(Bars bars, double open, double high, double low, double close, DateTime time, long volume, bool isBar, double bid, double ask)
          {
          if (SessionIterator == null)
          SessionIterator = new SessionIterator(bars);

          if (bars.Count == 0)
          AddBar(bars, open, high, low, close, TimeToBarTime(bars, time, isBar), volume);
          else if (!isBar && time < bars.LastBarTime)
          UpdateBar(bars, high, low, close, bars.LastBarTime, volume);
          else if (isBar && time <= bars.LastBarTime)
          UpdateBar(bars, high, low, close, bars.LastBarTime, volume);
          else
          {
          time = TimeToBarTime(bars, time, isBar);
          AddBar(bars, open, high, low, close, time, volume);
          }
          }

          protected override void OnStateChange()
          {
          if (State == State.SetDefaults)
          {
          Name = Custom.Resource.NinjaScriptBarsTypeMinute;
          BarsPeriod = new BarsPeriod { BarsPeriodType = BarsPeriodType.Minute, Value = 1 };
          BuiltFrom = BarsPeriodType.Minute;
          DaysToLoad = 5;
          IsIntraday = true;
          IsTimeBased = true;
          }
          else if (State == State.Configure)
          {
          Name = string.Format(Core.Globals.GeneralOptions.CurrentC ulture, Custom.Resource.DataBarsTypeMinute, BarsPeriod.Value, (BarsPeriod.MarketDataType != MarketDataType.Last ? string.Format(" - {0}", Core.Globals.ToLocalizedObject(BarsPeriod.MarketDa taType, Core.Globals.GeneralOptions.CurrentUICulture)) : string.Empty));

          Properties.Remove(Properties.Find("BaseBarsPeriodT ype", true));
          Properties.Remove(Properties.Find("BaseBarsPeriodV alue", true));
          Properties.Remove(Properties.Find("PointAndFigureP riceType", true));
          Properties.Remove(Properties.Find("ReversalType", true));
          Properties.Remove(Properties.Find("Value2", true));
          }
          }

          private DateTime TimeToBarTime(Bars bars, DateTime time, bool isBar)
          {
          if (SessionIterator.IsNewSession(time, isBar))
          SessionIterator.GetNextSession(time, isBar);

          if (bars.IsResetOnNewTradingDay || (!bars.IsResetOnNewTradingDay && bars.Count == 0))
          {
          DateTime barTimeStamp = isBar
          ? SessionIterator.ActualSessionBegin.AddMinutes(Math .Ceiling(Math.Ceiling(Math.Max(0, time.Subtract(SessionIterator.ActualSessionBegin). TotalMinutes)) / bars.BarsPeriod.Value) * bars.BarsPeriod.Value)
          : SessionIterator.ActualSessionBegin.AddMinutes(bars .BarsPeriod.Value + Math.Floor(Math.Floor(Math.Max(0, time.Subtract(SessionIterator.ActualSessionBegin). TotalMinutes)) / bars.BarsPeriod.Value) * bars.BarsPeriod.Value);
          if (bars.TradingHours.Sessions.Count > 0 && barTimeStamp > SessionIterator.ActualSessionEnd) // Cut last bar in session down to session end on odd session end time
          barTimeStamp = SessionIterator.ActualSessionEnd;
          return barTimeStamp;
          }
          else
          {
          DateTime lastBarTime = bars.GetTime(bars.Count - 1);
          DateTime barTimeStamp = isBar
          ? lastBarTime.AddMinutes(Math.Ceiling(Math.Ceiling(M ath.Max(0, time.Subtract(lastBarTime).TotalMinutes)) / bars.BarsPeriod.Value) * bars.BarsPeriod.Value)
          : lastBarTime.AddMinutes(bars.BarsPeriod.Value + Math.Floor(Math.Floor(Math.Max(0, time.Subtract(lastBarTime).TotalMinutes)) / bars.BarsPeriod.Value) * bars.BarsPeriod.Value);
          if (bars.TradingHours.Sessions.Count > 0 && barTimeStamp > SessionIterator.ActualSessionEnd)
          {
          DateTime saveActualSessionEnd = SessionIterator.ActualSessionEnd;
          SessionIterator.GetNextSession(SessionIterator.Act ualSessionEnd.AddSeconds(1), isBar);
          barTimeStamp = SessionIterator.ActualSessionBegin.AddMinutes((int ) barTimeStamp.Subtract(saveActualSessionEnd).TotalM inutes);
          }
          return barTimeStamp;
          }
          }
          }
          This code I have took from NinjaScript's InBuild MinuteBarType

          Comment


            #6
            Hello UniPiece-daivil,

            AddBar() adds a new bar.

            ApplyDefaultBasePeriodValue() sets the interval for an underlying bar period if the bar type uses an underlying bar type.

            ApplyDefaultValue() sets the default interval.

            GetInitialLookBackDays() sets the default number of days to load.

            GetPercentComplete() calculates the percent before a bar will close, for bar types where this can be calculated.

            OnDataPoint updates when there is data to process.

            OnStateChange() updates with the State changes throughout the life-cycle of the script. (SetDefaults, Configure, Active)

            UpdateBar() updates the building bar with new HLC values.


            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Thanks for the help, but could you please explain the code in each function aswell ??
              I'm not able to understand that

              Comment


                #8
                Hello UniPiece-daivil,

                This would become C# education (and general programming education) which is outside of the realm of our support.
                Our support team is here to answer questions about documented methods and properties, and to assist with issues / bug reporting, however assisting with logic is beyond what our team can provide. We do put out examples from time to time for our users to learn from by studying, but

                Unfortunately, in the support department at NinjaTrader it is against our policy to create, debug, or modify, code or logic for our clients. Further, we do not provide C# programming education services or one on one educational support in our NinjaScript Support department. This is so that we can maintain a high level of service for all of our clients as well as our associates.

                You can also contact a professional NinjaScript Consultant who would be eager to provide educational services as well as custom script development. The NinjaTrader Ecosystem has affiliate contacts who provide educational as well as consulting services. Please let me know if you would like a list of affiliate consultants who would be happy to provide C# education.​
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  okay but i think the function working for standard builtin bartype comes under your ninjasupport system
                  kindly explain this GetPercentComplete function, cuz there's ninjascript's internal functions are used

                  THanks

                  Comment


                    #10
                    Hello UniPiece-daivil,

                    GetPercentComplete() is used for scripts like the TickCounter indicator and VolumeCounter indicators which use the Bars.PercentComplete property to count down to the bar closing.

                    If the bar type has a predictable close, such as based on time, number of ticks, number of volume, etc. This method allows you to calculate and provide the Bars.PercentComplete to any indicators running on the bar type.

                    For the Minute bar type, this is checking the percent of the time of the current update is the amount of minutes greater than the previous bar close.
                    Chelsea B.NinjaTrader Customer Service

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                    0 responses
                    574 views
                    0 likes
                    Last Post Geovanny Suaza  
                    Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                    0 responses
                    333 views
                    1 like
                    Last Post Geovanny Suaza  
                    Started by Mindset, 02-09-2026, 11:44 AM
                    0 responses
                    101 views
                    0 likes
                    Last Post Mindset
                    by Mindset
                     
                    Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                    0 responses
                    553 views
                    1 like
                    Last Post Geovanny Suaza  
                    Started by RFrosty, 01-28-2026, 06:49 PM
                    0 responses
                    551 views
                    1 like
                    Last Post RFrosty
                    by RFrosty
                     
                    Working...
                    X