Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Candle Body Size indicator help needed

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

    Candle Body Size indicator help needed

    Hello,

    I am trying to create Candle body size indicator, but I am getting errors. Since I am new with indicator coding, I need help with code revision

    I am pasting my code here from OnBarUpdate:

    Code:
     protected override void OnBarUpdate()
    {
    double body = Math.Abs(Close[0] - Open[0]);
    double sma = SMA(body, Period);
    Value[0] = ((body[0])/(sma[0] + 0.0001)) * 100;
    }
    Idea is to 1) calculate body range, 2) calculate body range SMA of 30 bars 3) Value[0] returns body compared to SMA turned to percentage.

    #2
    Hello UltraNIX,

    Thanks for your post.

    It looks like you are trying to get the simple moving average of the body over some period. The issue here is that the body is a single (double type) value, there is no sequence or array/data series of values for the SMA to work with.

    From the help guide you can see that the input to the SMA must be a data series: SMA(ISeries<double> input, int period)
    Reference: https://ninjatrader.com/support/help...simple_sma.htm

    To accomplish your goal, you would need to create a custom data series and populate the series with the results of Math.Abs(Close[0] - Open[0]);

    Please see the help guide page here: https://ninjatrader.com/support/help...8/?seriest.htm The first example shows how to create a private data series and initialize that series. The second example show populating that series in OnBarUpdate().


    Comment


      #3
      Thanks, I solved it like this:

      Code:
       public class CBS : Indicator
      {
      private Series<double> body;
      private SMA sma;
      protected override void OnStateChange()
      {
      if (State == State.SetDefaults)
      {
      Description = @"Calculates the value of body relative to X bar range x 10.";
      Name = "CBS";
      Calculate = Calculate.OnBarClose;
      IsOverlay = false;
      DisplayInDataBox = true;
      DrawOnPricePanel = true;
      DrawHorizontalGridLines = true;
      DrawVerticalGridLines = true;
      PaintPriceMarkers = true;
      ScaleJustification = NinjaTrader.Gui.Chart.ScaleJustification.Right;
      //Disable this property if your indicator requires custom values that cumulate with each new market data event.
      //See Help Guide for additional information.
      IsSuspendedWhileInactive = true;
      Period = 30;
      AddPlot(new Stroke(Brushes.MediumSpringGreen, 2), PlotStyle.Bar, "CandleBody");
      }
      else if (State == State.Configure)
      {
      }
      else if (State == State.DataLoaded)
      {
      body = new Series<double>(this);
      sma = SMA(body, Period);
      }
      }
      
      protected override void OnBarUpdate()
      {
      double c0 = Close[0];
      double o0 = Open[0];
      body[0] = Math.Abs(c0 - o0);
      if (CurrentBar == 0)
      {
      Value[0] = 10;
      }
      if (CurrentBar > 0)
      {
      Value[0] = ((body[0])/(sma[0] + 0.0001)) * 10;
      }
      
      }
      However, I want to add another condition, like If (CurrentBar < Period, then use CurrentBar number instead of Period in SMA calculation.

      I tried to do a calculation like this: sma = SMA(body, CurrentBar < Period ? CurrentBar : Period)

      But it crashes indicator. Any suggestions?

      Comment


        #4
        Hello UltraNIX,

        Thanks for your reply.

        You could not access CurrentBar in State.DataLoaded, only in OnBarUpdate().

        The SMA indicator already has that built in to its code: Value[0] = sum / (CurrentBar < Period ? CurrentBar + 1 : Period); so you should be fine just using your period.

        Comment


          #5
          Thanks, PaulH!

          Comment


            #6
            Hello UltraNIX,
            Are you happy with the Candle body indicator you built? Did you build it to scalp large spike candles? How's that strategy working for you?
            Thx,

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Geovanny Suaza, 02-11-2026, 06:32 PM
            0 responses
            571 views
            0 likes
            Last Post Geovanny Suaza  
            Started by Geovanny Suaza, 02-11-2026, 05:51 PM
            0 responses
            331 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
            549 views
            1 like
            Last Post Geovanny Suaza  
            Started by RFrosty, 01-28-2026, 06:49 PM
            0 responses
            550 views
            1 like
            Last Post RFrosty
            by RFrosty
             
            Working...
            X