Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Daily indicator recalculated for each intra day bar

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

    Daily indicator recalculated for each intra day bar

    I am trying to recalculte the value of a moving average in an intraday chart as if the last value known was the close.
    For some reason, it does not work

    Here is below the script :-)

    namespace NinjaTrader.NinjaScript.Indicators
    {
    public class intraDayMA : Indicator
    {
    private SMA sma;
    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = @"Enter the description for your new custom Indicator here.";
    Name = "intraDayMA";
    Calculate = Calculate.OnPriceChange;
    IsOverlay = true;
    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 = 3;

    AddPlot(new Stroke(Brushes.Green,0), PlotStyle.Line, "Daily MA");
    }
    else if (State == State.Configure)
    {
    AddDataSeries(Data.BarsPeriodType.Day, 1);
    }
    }


    protected override void OnBarUpdate()
    {
    if (CurrentBars[0] < 1 || CurrentBars[1] < Period) return;

    double myPlot = SMA(BarsArray[1], Period-1)[1];
    myPlotLine[0] = (myPlot * (Period - 1) + Close[0]) / Period;

    }

    #region Properties
    [NinjaScriptProperty]
    [Range(1, int.MaxValue)]
    [Display(Name="Period", Order=1, GroupName="Parameters")]
    public int Period
    { get; set; }


    [Browsable(false)]
    [XmlIgnore]
    public Series<double> myPlotLine
    {
    get { return Values[0]; }
    }

    #endregion

    }
    }

    If anyone can help me, I would be greatfull.

    Thanks

    #2
    Hello guilhem,

    Thanks for your post.

    You wrote, "For some reason, it does not work", Can you clarify what this means?

    Do you see a plot on the chart and it is not correct?

    Do you see any errors in the log tab of the control center that may help in understanding why it is not working?

    What is the time frame of the chart you are applying this to?

    How many days of data does the chart load?

    Comment


      #3
      It does give the right values for current candle but not for previous days.

      Comment


        #4
        Hello guilhem,

        Thanks for your reply.

        Historically the indicator will be using Calculate.OnBarClose even though you are using Calculate.OnPriceChange.

        You might try enabling tick replay for the data series.

        Comment


          #5
          got it ... need to use SMA(BarsArray[1], Period-1)[0] instead of SMA(BarsArray[1], Period-1)[1] ... AddDataSeries rules... :-)

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Geovanny Suaza, 02-11-2026, 06:32 PM
          0 responses
          596 views
          0 likes
          Last Post Geovanny Suaza  
          Started by Geovanny Suaza, 02-11-2026, 05:51 PM
          0 responses
          343 views
          1 like
          Last Post Geovanny Suaza  
          Started by Mindset, 02-09-2026, 11:44 AM
          0 responses
          103 views
          0 likes
          Last Post Mindset
          by Mindset
           
          Started by Geovanny Suaza, 02-02-2026, 12:30 PM
          0 responses
          556 views
          1 like
          Last Post Geovanny Suaza  
          Started by RFrosty, 01-28-2026, 06:49 PM
          0 responses
          554 views
          1 like
          Last Post RFrosty
          by RFrosty
           
          Working...
          X