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

Can't solve this error

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

    Can't solve this error

    I am trying to print the highest ATR value over a certain period. But I get this Operator '*' cannot be applied to operands of type 'double' and 'NinjaTrader.NinjaScript.Indicators.ATR' error which I can't solve.


    I add the following code to the indicator code generated by the wizard:

    private double TS;

    ATR_Period = 5;
    HHV_Period = 10;
    Multiplier = 2.5;

    TS = MAX(High - Multiplier * ATR(ATR_Period),HHV_Period);



    #2
    The reason is that ATR is returned as an array of atr values of the previous bars.. SO you need to do something like
    ATR(ATR_Period)[0] to get the value of the current bar. Try:

    TS = MAX(High - Multiplier * ATR(ATR_Period)[0],HHV_Period);
    Last edited by iq200; 06-02-2020, 12:13 PM.

    Comment


      #3
      Thanks iq200, but I still get Operator '-' cannot be applied to operands of type 'NinjaTrader.NinjaScript.ISeries<double>' and 'double' error.

      I also tried TS = MAX(High[0] - Multiplier * ATR(ATR_Period)[0], HHV_Period); and TS = MAX(High[0] - Multiplier * ATR(ATR_Period)[0],HHV_Period)[0]; Both also doesn't work. I will get The best overloaded method match for 'NinjaTrader.NinjaScript.Indicators.Indicator.MAX( NinjaTrader.NinjaScript.ISeries<double>, int)' has some invalid arguments and Argument 1: cannot convert from 'double' to 'NinjaTrader.NinjaScript.ISeries<double>' errors.

      Comment


        #4
        Sorry my mistake. so MAX is an indicator/fn that takes in an array of values. I just realized MAX does a lookback.
        So in your first instance you were trying to multiply a vector (which is what you need) by a scalar value. That was the reason for the error.

        So what you will need to do is to create a Series array of High - Multiplier * ATR(ATR_Period) and pass this into MAX.
        Last edited by iq200; 06-02-2020, 12:39 PM.

        Comment


          #5
          You will need to create an array such as:
          private Series<double> myScaledATR;

          Once you have instantiated the array using new, in your update fn, you need to do something like:

          myScaledATR[0] = High[0] - Multiplier * ATR(ATR_Period)[0];

          TS = MAX(myScaledATR, HHV_Period);

          Comment


            #6
            Thanks iq200. I need to write TS = MAX(myScaledATR, HHV_Period)[0]; to compile correctly. But somehow the indicator doesn't load. I got Indicator 'ATRTrend': Error on calling 'OnBarUpdate' method on bar 30: Object reference not set to an instance of an object error. I tried putting if(CurrentBar<30) return; code in but it still didn't work. Attached the full code below.
            Attached Files

            Comment


              #7
              Like I said, you need to instatiate your Series array:

              protected override void OnStateChange()
              {
              if (State == State.SetDefaults)
              {
              Description = @"ATR Trend";
              Name = "ATRTrend";
              Calculate = Calculate.OnBarClose;
              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;
              ATR_Period = 5;
              HHV_Period = 10;
              Multiplier = 2.5;
              AddPlot(new Stroke(Brushes.SpringGreen, 2), PlotStyle.Dot, "Lower");

              }
              else if (State == State.Configure)
              {
              }
              else if (State == State.DataLoaded)
              {
              Plotset = new Series<double>(this);
              }
              }

              Comment


                #8
                Hello tjendra,

                Thank you for your post.

                I apologize for my delayed reply as I got stuck on a call, but iq200 is doing a great job here. Do you still get an error after instantiating your plot as seen above?

                Thanks in advance, I look forward to assisting you further.
                Kate W.NinjaTrader Customer Service

                Comment


                  #9
                  Hi Kate, everything is working now. Thanks iq200 for your help!

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by algospoke, Yesterday, 06:40 PM
                  2 responses
                  19 views
                  0 likes
                  Last Post algospoke  
                  Started by ghoul, Today, 06:02 PM
                  3 responses
                  14 views
                  0 likes
                  Last Post NinjaTrader_Manfred  
                  Started by jeronymite, 04-12-2024, 04:26 PM
                  3 responses
                  45 views
                  0 likes
                  Last Post jeronymite  
                  Started by Barry Milan, Yesterday, 10:35 PM
                  7 responses
                  21 views
                  0 likes
                  Last Post NinjaTrader_Manfred  
                  Started by AttiM, 02-14-2024, 05:20 PM
                  10 responses
                  181 views
                  0 likes
                  Last Post jeronymite  
                  Working...
                  X