Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Indicator Help

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

    Indicator Help

    New here. I am trying to create an indicator that plots the % change in the NQ - % change in the YM. I am not getting any plots. Not sure where I am going wrong. Any help would be appreciated. Thanks!


    namespace NinjaTrader.NinjaScript.Indicators
    {
    public class ATest : Indicator
    {
    private double YM;
    private double NQ;
    private double YMDay;
    private double NQDay;

    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = @"Enter the description for your new custom Indicator here.";
    Name = "ATest";
    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;
    AddPlot(Brushes.Orange, "Plot");
    YM = 1;
    NQ = 1;
    YMDay = 1;
    NQDay = 1;
    }
    else if (State == State.Configure)
    {
    AddDataSeries("YM 09-22", Data.BarsPeriodType.Minute, 1, Data.MarketDataType.Last);
    AddDataSeries("NQ 09-22", Data.BarsPeriodType.Minute, 1, Data.MarketDataType.Last);
    AddDataSeries("YM 09-22", Data.BarsPeriodType.Day, 1, Data.MarketDataType.Last);
    AddDataSeries("NQ 09-22", Data.BarsPeriodType.Day, 1, Data.MarketDataType.Last);
    }
    }

    protected override void OnBarUpdate()
    {
    if (BarsInProgress != 0)
    return;

    if (CurrentBars[1] < 1
    || CurrentBars[2] < 1)
    return;

    // Set 1
    if (Closes[1][0] > 0)
    {
    YM = Closes[1][0];
    }

    // Set 2
    if (Closes[2][0] > 0)
    {
    NQ = Closes[2][0];
    }

    // Set 3
    if (Closes[3][0] > 0)
    {
    YMDay = Closes[3][1];
    }

    // Set 4
    if (Closes[4][0] > 0)
    {
    NQDay = Closes[4][1];
    }

    double average = (((NQ - NQDay)/NQDay)*100) - (((YM - YMDay)/YMDay) *100);
    Plot[0] = average;
    }
    Last edited by HarvOS; 06-23-2022, 10:58 AM.

    #2
    Hi HarvOS, welcome to the forums and thanks for posting.

    If you ever run an indicator that compiles but doesn't act as expected when launched check the Log tab of the Control Center for run time errors.
    The script needs a CurrentBar check for each series you added here, it's likely having an indexing error when there are no bars on the chart and the indicator runs.

    Code:
    if(CurrentBars[0] < 1 || CurrentBars[1] < 1 || CurrentBars[2] < 1 || CurrentBars[3] < 1 || CurrentBars[4] < 1)
        return;
    Kind regards,
    -ChrisL
    Chris L.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_ChrisL View Post
      Hi HarvOS, welcome to the forums and thanks for posting.

      If you ever run an indicator that compiles but doesn't act as expected when launched check the Log tab of the Control Center for run time errors.
      The script needs a CurrentBar check for each series you added here, it's likely having an indexing error when there are no bars on the chart and the indicator runs.

      Code:
      if(CurrentBars[0] < 1 || CurrentBars[1] < 1 || CurrentBars[2] < 1 || CurrentBars[3] < 1 || CurrentBars[4] < 1)
      return;
      Kind regards,
      -ChrisL
      That's exactly it. Thank you!

      Comment


        #4
        Originally posted by NinjaTrader_ChrisL View Post
        Hi HarvOS, welcome to the forums and thanks for posting.

        If you ever run an indicator that compiles but doesn't act as expected when launched check the Log tab of the Control Center for run time errors.
        The script needs a CurrentBar check for each series you added here, it's likely having an indexing error when there are no bars on the chart and the indicator runs.

        Code:
        if(CurrentBars[0] < 1 || CurrentBars[1] < 1 || CurrentBars[2] < 1 || CurrentBars[3] < 1 || CurrentBars[4] < 1)
        return;
        Kind regards,
        -ChrisL
        New problem. The indicator works great, but only for the current day. All days prior to the current session plots a 0 line. How do I get the indicator to work for prior days? Reset variables on the first bar of session? Thanks.

        Comment


          #5
          Hi HarvOS, I loaded the code into a test file and it works with my modifications. Please see attached.

          Kind regards,
          -ChrisL
          Attached Files
          Chris L.NinjaTrader Customer Service

          Comment


            #6
            Originally posted by NinjaTrader_ChrisL View Post
            Hi HarvOS, I loaded the code into a test file and it works with my modifications. Please see attached.

            Kind regards,
            -ChrisL
            Thank you for your time/help. I reviewed/copied your file and I can get it to work fine for today's session, but still nothing for yesterday or prior. Here is what I have. What am I still missing? Thanks.


            namespace NinjaTrader.NinjaScript.Indicators
            {
            public class ATest : Indicator
            {
            private double YM;
            private double NQ;
            private double YMDay;
            private double NQDay;

            protected override void OnStateChange()
            {
            if (State == State.SetDefaults)
            {
            Description = @"Enter the description for your new custom Indicator here.";
            Name = "ATest";
            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;
            AddPlot(Brushes.Orange, "Plot");

            }
            else if (State == State.Configure)
            {
            AddDataSeries("YM 09-22", Data.BarsPeriodType.Minute, 1, Data.MarketDataType.Last);
            AddDataSeries("NQ 09-22", Data.BarsPeriodType.Minute, 1, Data.MarketDataType.Last);
            AddDataSeries("YM 09-22", Data.BarsPeriodType.Day, 1, Data.MarketDataType.Last);
            AddDataSeries("NQ 09-22", Data.BarsPeriodType.Day, 1, Data.MarketDataType.Last);
            }
            else if (State == State.DataLoaded)
            {

            }
            }

            protected override void OnBarUpdate()
            {
            if (BarsInProgress != 0)
            return;

            if(CurrentBars[3] < 1 || CurrentBars[4] < 1) return;

            // Set 1
            if (Closes[1][0] > 0)
            {
            YM = Closes[1][0];
            }

            // Set 2
            if (Closes[2][0] > 0)
            {
            NQ = Closes[2][0];
            }

            // Set 3
            if (Closes[3][0] > 0)
            {
            YMDay = Closes[3][1];
            }

            // Set 4
            if (Closes[4][0] > 0)
            {
            NQDay = Closes[4][1];
            }

            double average = (((NQ - NQDay)/NQDay)*100) - (((YM - YMDay)/YMDay) *100);
            Value[0] = average;
            }

            #region Properties

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

            }
            }

            Comment


              #7
              Hi HarvOS, This is my output for a Daily NQ chart:


              Make sure you are connected to a data source. If you are using a tick based chart for the primary series, its possible you get much more tick data than daily and minute data, so that could also be the cause. Try out a Daily chart like this one.

              Kind regards,
              -ChrisL
              Chris L.NinjaTrader Customer Service

              Comment


                #8
                Originally posted by NinjaTrader_ChrisL View Post
                Hi HarvOS, This is my output for a Daily NQ chart:


                Make sure you are connected to a data source. If you are using a tick based chart for the primary series, its possible you get much more tick data than daily and minute data, so that could also be the cause. Try out a Daily chart like this one.

                Kind regards,
                -ChrisL
                I got it to work by starting with a daily chart and then backing it down to the 1 min. Works great now. Thank you very much for your help!

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by Stanfillirenfro, Today, 07:23 AM
                1 response
                4 views
                0 likes
                Last Post NinjaTrader_Gaby  
                Started by cmtjoancolmenero, Yesterday, 03:58 PM
                2 responses
                22 views
                0 likes
                Last Post cmtjoancolmenero  
                Started by olisav57, Yesterday, 07:39 PM
                1 response
                9 views
                0 likes
                Last Post NinjaTrader_ChelseaB  
                Started by cocoescala, 10-12-2018, 11:02 PM
                7 responses
                944 views
                0 likes
                Last Post Jquiroz1975  
                Started by oviejo, Today, 12:28 AM
                1 response
                12 views
                0 likes
                Last Post NinjaTrader_Gaby  
                Working...
                X