Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Code problems

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

    Code problems

    Greetings,

    I'm new to NinjaTrader. I'm trying to code my first indicator. Can somebody please point out what the errors are with the following code I wrote, it appears I have a problem with definitions:

    double StK = 0; double Pr = 0;
    MAX Diff1 = MAX(StK,Pr); MIN Diff2 = MIN(StK,Pr);

    The error codes given after the compile:

    "The best overloaded method match for Ninjatrader.Indicator.Indicator.MAX(NinjaTrader,Da ta,IDataSeries,int) has some valid arguements"
    "Cannot convert from double to Ninjatrader.Data.IDataSeries"
    "Cannot convert from double to int"

    #2
    Hi Burga1,

    MAX and MIN are methods not object types. They are used for determining max and mins on DataSeries objects though instead of just comparing two doubles. Instead you want to use Math.Max and Math.Min for what you are trying to do.

    http://www.ninjatrader-support.com/H...aximumMAX.html
    http://www.ninjatrader-support.com/H...inimumMIN.html

    Code:
    double StK = 0;
    double Pr = 0; 
    double Diff1 = Math.Max(StK, Pr);
    double Diff2 = Math.Min(StK, Pr);
    Josh P.NinjaTrader Customer Service

    Comment


      #3
      Hi,

      Thanks for the response, that seems to take OK so what is the following with the following, it is also a min/max definition...


      double
      MIN1 = Math.Min(Low,K); double MAX1 = Math.Max(High,K);

      On a related note, is the following code acceptable?

      StK = ((Close - MIN(Low,K)) / (MAX(High,K) - MIN(Low,K))) *
      100;

      Thanks in advance.

      Comment


        #4
        Low and High are series objects. You will need to tell it exactly which Low and which High you want to compare. For example, if you want the current bar's Low you would do Low[0]. The previous bar's low can be referenced by Low[1].

        Code:
        double MIN1 = Math.Min(Low,K); double MAX1 = Math.Max(High,K);
        should probably be
        Code:
        double MIN1 = Math.Min(Low[0], K); double MAX1 = Math.Max(High[0], K);
        and
        Code:
        StK = ((Close - MIN(Low,K)) / (MAX(High,K) - MIN(Low,K))) * 100

        should be something like
        Code:
        StK = ((Close[0] - MIN(Low, K)[0]) / (MAX(High, K)[0] - MIN(Low,K)[0])) * 100;
        Also make sure K is an integer.

        Josh P.NinjaTrader Customer Service

        Comment


          #5
          It looks like you're trying to code something to do with a Stochastics indicator.

          If so, you might want to have a look at the source code for the Stochastics indicator that is stardardly supplied with NinjaTrader. Click on Tools/EditNinjaScript/Indicator and select Stochastics.

          If you do this, you will notice code that looks very similar to what Josh just posted for you.

          Comment


            #6
            Thanks again for the help. Where are my variable definitions supposed to be? Should they be listed in the "protectedoverridevoid Initialize()" function?

            thanks again.

            Comment


              #7
              The variable definitions would either be defined just before the "protected override void initialize()" method or just before they're used in OnBarUpdate() if it's a temporary variable (i.e., not used outside OnBarUpdate.)

              Comment


                #8
                Thanks again. The definitions are not what I'm used to, a bit more confusing...

                I'm trying to convert this line into something recognized by Ninja:


                StVarK = SMA((((VarSto - MIN(VarSto, K)) / (MAX(VarSto, K) - MIN(VarSto, K))) * 100), Sl);

                I've tried:

                double VarSto1 = Math.Min(VarSto, K); double VarSto2 = Math.Max(VarSto, K);
                SMA StVarK = new SMA((((VarSto - VarSto1) / (VarSto2 - VarSto1)) * 100), Sl);

                I seem to get an error on the definition--however I took it straight from the online Help...

                Thanks KBJ I'll take a look...

                Regards

                Comment


                  #9
                  SMA isn't an object type. What you want to try and do is create a DataSeries object that contains your mathematics. Then you want to run the SMA on that DataSeries.

                  http://www.ninjatrader-support.com/H...iesObject.html

                  Code:
                  #region Variables
                      private double VarSto = 0;
                      private double VarSto1 = 0;
                      private double VarSto2 = 0;
                      private double StVarK = 0;
                      private int S1 = 20; // Period variable for the SMA
                      private DataSeries myDataSeries;
                  #endregion
                   
                  protected override void Initialize() 
                  { 
                      myDataSeries = new DataSeries(this);
                  }
                  
                  protected override void OnBarUpdate()
                  {
                      VarSto1 = Math.Min(VarSto, K);
                      VarSto2 = Math.Max(VarSto, K);
                      myDataSeries.Set(((VarSto - VarSto1) / (VarSto2 - VarSto1)) * 100);
                      StVarK = SMA(myDataSeries, S1)[0];
                  }
                  Josh P.NinjaTrader Customer Service

                  Comment


                    #10
                    Thanks, is this the correct approach to code every SMA or EMA whenever they are needed?

                    Regards

                    Comment


                      #11
                      This approach can be used to get the SMA, EMA, etc. values of whatever calculation you are doing. I'm sure there are other acceptable ways to do it, but this is the way I most commonly employ.
                      Josh P.NinjaTrader Customer Service

                      Comment


                        #12
                        Greetings,

                        Why does the compiler give me errors with this code?

                        #region Variables
                        private DataSeries myDataSeries2;
                        #endregion

                        protectedoverridevoid Initialize()
                        {
                        ...
                        myDataSeries2 = new DataSeries(this);
                        ...
                        }

                        protectedoverridevoid OnBarUpdate()
                        {
                        ...
                        myDataSeries2.Set(((1 - MyConst) * (VarSto[1])) + (MyConst * Close)); //ERROR ON THIS LINE...
                        if(CurrentBar < (Pr + (Pr * 1.5))){VarSto = StK;}
                        else{VarSto = SMA(myDataSeries2, 2)[0];}
                        ...
                        }

                        Comment


                          #13
                          It's hard to tell with just part of the code and variable definitions.

                          But, assuming that this is part of the code from Josh's post (#9, below), then the problem is that VarSto is a scalar double, but you're referencing it as an array element or a dataseries element.

                          So, change the "VarSto[1]" to just "VarSto" and the compile error will go away.

                          I don't know if the code will work, but this particular error will be gone.

                          Comment


                            #14
                            Thanks. Here's the code I'd like to convert (from another app.):

                            VarSto:= If(Barnum<Pr+(Pr*1.5),StK,MOV((((1-MyConst)*Ref(VarSto,-1))+(MyConst*Close)),2,s));

                            NOTE: Read as "IF(statement, then do this, else do this);"...

                            The problem seems to be with the way NT requires moving averages to be coded, I'm not sure how to perform the "else" part of the previous statement correctly...

                            if(CurrentBar < (Pr + (Pr * 1.5))){VarSto = StK;} //this is OK

                            For the "else" statement, I need to define the "myDataSeries2.set" BEFORE I assign the VarSto variable a value...but if the "else" statement is called the VarSto has no value...I think that's the problem???

                            else
                            {
                            myDataSeries2.Set(((1 - MyConst) * (VarSto[1])) + (MyConst * Close)); //Error on this line, because VarSto has no value??
                            VarSto = SMA(myDataSeries2, 2)[0];
                            }

                            Comment


                              #15
                              "Close" is not correct, likely you want Close[0].
                              RayNinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by ayrton74, Today, 08:40 AM
                              0 responses
                              1 view
                              0 likes
                              Last Post ayrton74  
                              Started by rafaelabe1977, Today, 08:34 AM
                              0 responses
                              2 views
                              0 likes
                              Last Post rafaelabe1977  
                              Started by qm trader, Yesterday, 05:51 PM
                              2 responses
                              22 views
                              0 likes
                              Last Post NinjaTrader_ChrisL  
                              Started by pdennis, Today, 07:43 AM
                              1 response
                              6 views
                              0 likes
                              Last Post NinjaTrader_Jesse  
                              Started by NinjaTrader_Brandon, Today, 07:49 AM
                              0 responses
                              11 views
                              0 likes
                              Last Post NinjaTrader_Brandon  
                              Working...
                              X