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 charlesugo_1, 05-26-2026, 05:03 PM
          0 responses
          73 views
          0 likes
          Last Post charlesugo_1  
          Started by DannyP96, 05-18-2026, 02:38 PM
          1 response
          152 views
          0 likes
          Last Post NinjaTrader_ChelseaB  
          Started by CarlTrading, 05-11-2026, 05:56 AM
          0 responses
          162 views
          0 likes
          Last Post CarlTrading  
          Started by CarlTrading, 05-10-2026, 08:12 PM
          0 responses
          100 views
          0 likes
          Last Post CarlTrading  
          Started by Hwop38, 05-04-2026, 07:02 PM
          0 responses
          288 views
          0 likes
          Last Post Hwop38
          by Hwop38
           
          Working...
          X