Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Custom Indicator

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

    Custom Indicator

    Hello NT,

    I want to create a custom indicator that does the following:

    if (Close[0] > Close[5] && Close[0] < Close[15])
    Color the bars blue

    if (Close[0] < Close[5] && Close[0] > Close[15])
    Color the bars orange

    I was looking for something like the Strategy Builder to set up all the methods and properties but did not see one. Where is a good starting point for a simple indicator like this one and how would I code this expression?

    Thanks
    Ben

    #2
    Hi Ben, thanks for posting.

    The strategy builder does not have access to the BarBrush property. The condition you have listed can be made using the builder. We have an example here about making price data comparisons.

    In an indicator, this could be made like this:

    Code:
    OnBarUpdate:
    if(CurrentBar < 15) return;
    if (Close[0] > Close[5] && Close[0] < Close[15])
        BarBrush = Brushes.Blue;
    if (Close[0] < Close[5] && Close[0] > Close[15])
        BarBrush = Brushes.Orange
    Kind regards,
    -ChrisL

    Comment


      #3
      Thank you, Chris,

      I'm looking to edit an existing Ninjasript indicator and save it as a new one to see if I can get this code to work. Do you know of any that has similar declarations, objects and methods? DEMA looks pretty simple. Can you tell me what part of the following code I would need to adjust to get the BarBrush method to work?


      public class DEMA Change to new name : Indicator
      {
      private EMA ema; not sure I need to declare this
      private EMA emaEma;

      protected override void OnStateChange()
      {
      if (State == State.SetDefaults)
      {
      Description = NinjaTrader.Custom.Resource.NinjaScriptIndicatorDe scriptionDEMAnewname;
      Name = NinjaTrader.Custom.Resource.NinjaScriptIndicatorNa meDEMAnewname;
      IsSuspendedWhileInactive = true;
      IsOverlay = true;
      Period = 14; may not be applicable

      AddPlot(Brushes.Goldenrod, NinjaTrader.Custom.Resource.NinjaScriptIndicatorNa meDEMAnew name);


      OnBarUpdate:
      if(CurrentBar < 15) return;
      if (Close[0] > Close[5] && Close[0] < Close[15]) BarBrush = Brushes.Blue;
      if (Close[0] < Close[5] && Close[0] > Close[15]) BarBrush = Brushes.Orange

      Regards,
      Ben

      Comment


        #4
        Hi Ben, thanks for your reply.

        You can make a copy of any script by right-clicking the original code>Save As>enter a new name, and this will automatically change the class names and Name property. We reccomend those modifying or making their own NinjaScript files be at least intermediate to advanced level in C# programming. C# object oriented programming is a pre-requisite as one needs to knowhow classes, variables, and class methods function. One notable indicator that colors bars based on the indicator is here:

        https://ninjatraderecosystem.com/use...rpaintbar-nt8/ (publicly available link)

        Kind regards,
        -ChrisL

        The NinjaTrader Ecosystem website is for educational and informational purposes only and should not be considered a solicitation to buy or sell a futures contract or make any other type of investment decision. The add-ons listed on this website are not to be considered a recommendation and it is the reader's responsibility to evaluate any product, service, or company. NinjaTrader Ecosystem LLC is not responsible for the accuracy or content of any product, service or company linked to on this website.

        Comment


          #5
          Thanks for the SMAOverUnderPaintBar indicator, Chris. Very helpful. I was able to substitute my code into and get it working. Now I am trying to introduce indicator inputs so I don't have to recode it every time I want to change the parameters. I almost got it working but it seems there is a step I am missing in the declaration..

          else if (State == State.Configure)
          {

          }
          }
          [NinjaScriptProperty]
          [Description("entry1_sl")]
          [Category("Parameters")]
          public int entry1_sl
          { get; set; }

          [NinjaScriptProperty]
          [Description("entry1_slx")]
          [Category("Parameters")]
          public int entry1_slx
          { get; set; }

          protected override void OnBarUpdate()
          {

          int entry1_sl =10, entry1_slx = 30;
          //entry1_sl = short term trend length
          //entry1_slx = longer term trend length

          if (BarsInProgress != 0)
          return;

          if (CurrentBars[0] < 60)
          return;

          if (Close[0] > Close[entry1_sl] && Close[0] < Close[entry1_slx])
          {if (showpaintbars)
          BarBrushes[0] = Brushes.Blue;

          }
          else if (Close[0] < Close[entry1_sl] && Close[0] > Close[entry1_slx])
          {
          if (showpaintbars)
          BarBrushes[0] = Brushes.Orange;
          }

          Thanks,
          Ben

          Comment


            #6
            Hi Ben,

            The line if (CurrentBars[0] < 60) must also contain the maximum value for lookback. e.g.

            if (CurrentBars[0] < Math.Max(entry1_slx, entry1_sl))
            return;

            You can check if this was causing an indexing error in the Log tab of the Control Center.

            Kind regards,
            -ChrisL

            Comment


              #7
              Hi Chris, that looks like a good addition to avoid having to change the max number of bars. It's still not changing the inputs from the indicator menu from the chart. It responds only when I change them in the ninjascript manually. No errors were coming up in the log.

              Comment


                #8
                Hi Ben,

                It looks like you are defining another set of the same variables here:
                int entry1_sl =10, entry1_slx = 30;

                Remove that and the script will use the public properties.

                Kind regards,
                -ChrisL

                Comment


                  #9
                  That did the trick. Thanks!

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                  0 responses
                  578 views
                  0 likes
                  Last Post Geovanny Suaza  
                  Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                  0 responses
                  334 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
                  554 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