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

max value

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

    max value

    how i can get maximum value of three different SMAs for different periods.

    #2
    Hello shaista parveen,

    Welcome to the NinjaTrader support forum.

    You could use the MAX indicator for this: https://ninjatrader.com/support/help...aximum_max.htm

    Code:
    double smaMax = MAX(SMA(12), 30)[0];
    Print(smaMax);
    You could repeat making a variable for each SMA you wanted to calculate, for example making another line below this one with smaMax2 and another below that with smaMax3. You would change the (12) to whatever other periods you wanted to test for the next two lines.

    Code:
    double smaMax = MAX(SMA(12), 30)[0];
    double smaMax2 = MAX(SMA(15), 30)[0];

    I look forward to being of further assistance.
    JesseNinjaTrader Customer Service

    Comment


      #3
      Originally posted by shaista parveen View Post
      how i can get maximum value of three different SMAs for different periods.
      Code:
      double maxValue = Math.Max(SMA(Period1), Math.Max(SMA(Period2), SMA(Period3)));

      Comment


        #4
        i have create three different SMAs like i want finding maximum & minimum through Math function
        namespace NinjaTrader.NinjaScript.Strategies
        {
        public class SMA1to2 : Strategy
        {
        private SMA SMA1;
        private SMA SMA2;
        private SMA SMA3;
        private double Maximum;
        private double Minimum;


        protected override void OnStateChange()
        {
        if (State == State.SetDefaults)
        {
        Description = @"Enter the description for your new custom Strategy here.";
        Name = "SMA1to2";
        Calculate = Calculate.OnBarClose;
        EntriesPerDirection = 1;
        EntryHandling = EntryHandling.AllEntries;
        IsExitOnSessionCloseStrategy = true;
        ExitOnSessionCloseSeconds = 30;
        IsFillLimitOnTouch = false;
        MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
        OrderFillResolution = OrderFillResolution.Standard;
        Slippage = 0;
        StartBehavior = StartBehavior.WaitUntilFlat;
        TimeInForce = TimeInForce.Gtc;
        TraceOrders = false;
        RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
        StopTargetHandling = StopTargetHandling.PerEntryExecution;
        BarsRequiredToTrade = 20;
        // Disable this property for performance gains in Strategy Analyzer optimizations
        // See the Help Guide for additional information
        IsInstantiatedOnEachOptimizationIteration = true;
        Period1 = 1;
        Period2 = 1;
        Period3 = 1;
        else if (State == State.DataLoaded)
        {
        SMA1 =SMA(Typical, Convert.ToInt32(Period1));
        SMA2 =SMA(Typical, Convert.ToInt32(Period2));
        SMA3 =SMA(Typical, Convert.ToInt32(Period3));

        }
        }

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

        if (CurrentBars[0] < 1)
        return;


        for (int barIndex = ChartBars.FromIndex; barIndex <= CharBars.ToIndex; barIndex++)

        {
        double val1 = SMA1[barIndex];
        double val2 = SMA2[barIndex];
        double val3 = SMA3[barIndex];

        double maximum = Math.Max(val1, Math.Max(val2,val3));
        double manimum = Math.Min(val1, Math.Min(val2,val3));

        }

        EnterLong(Convert.ToInt32(DefaultQuantity), "");
        }

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

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

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

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

        [NinjaScriptProperty]
        [Range(1, double.MaxValue)]
        [Display(Name="Low_multi", Order=4, GroupName="Parameters")]
        public double Low_multi
        { get; set; }

        [NinjaScriptProperty]
        [Range(1, double.MaxValue)]
        [Display(Name="Vol_multi", Order=5, GroupName="Parameters")]
        public double Vol_multi
        { get; set; }

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

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

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

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

        [NinjaScriptProperty]
        [Range(1, double.MaxValue)]
        [Display(Name="Profit_Target", Order=10, GroupName="Parameters")]
        public double Profit_Target
        { get; set; }

        [NinjaScriptProperty]
        [Range(1, double.MaxValue)]
        [Display(Name="Stop_Loss", Order=11, GroupName="Parameters")]
        public double Stop_Loss
        { get; set; }

        [NinjaScriptProperty]
        [Range(1, double.MaxValue)]
        [Display(Name="TstopLoss", Order=12, GroupName="Parameters")]
        public double TstopLoss
        { get; set; }
        #endregion

        }
        }

        Comment


          #5
          Hello shaista parveen,

          If you wanted to gather the max of all 3 values it would be like koganam had shown in post #3, this is the same concept I am describing in post #2 it is just combining the max's to find the max of all values used.

          Please let me now if I may be of additional assistance.
          JesseNinjaTrader Customer Service

          Comment


            #6
            hello KOGANAM Sir
            but how i can print this and write on which method
            double maxValue = Math.Max(SMA(Period1), Math.Max(SMA(Period2), SMA(Period3)));

            Comment


              #7
              and Math.Max is apllicable on SMA because Math.Max takes double value as in arguments

              Comment


                #8
                Hello shaista parveen,
                but how i can print this and write on which method
                There is a sample of using a Print in post #2, you can find more information about using Prints in NinjaScript here:

                https://ninjatrader.com/support/help...lightsub=print
                https://ninjatrader.com/support/help...nt8/output.htm

                To print this value would look like Print(maxValue);

                and Math.Max is applicable on SMA because Math.Max takes double value as in arguments
                Math.Max is a C# function, you can read about this here:

                https://docs.microsoft.com/en-us/dot...tframework-4.5

                NinjaScript is C# programming language so you can use any existing methods or namespaces within the standard C# libraries.

                Math.Max takes a double so any double value in NinjaScript could be used with this method.

                In the prior examples the SMA is used but it appears there was an error, you need a BarsAgo to retrieve a value. It should look like this:
                Code:
                  double maxValue = Math.Max(SMA(12)[0], Math.Max(SMA(14)[0], SMA(16)[0]));

                I look forward to being of further assistance.
                Last edited by NinjaTrader_Jesse; 12-30-2019, 11:14 AM.
                JesseNinjaTrader Customer Service

                Comment


                  #9
                  i doesn't find to max value SMA(period1).
                  i just find maximum value of between three SMAs which have different periods.

                  Comment


                    #10
                    Originally posted by shaista parveen View Post
                    hello KOGANAM Sir
                    but how i can print this and write on which method
                    double maxValue = Math.Max(SMA(Period1), Math.Max(SMA(Period2), SMA(Period3)));
                    My mistake. You need to get the index for the values.
                    Code:
                    double maxValue = Math.Max(SMA(Period1)[0], Math.Max(SMA(Period2)[0], SMA(Period3)[0]));
                    Apologies.

                    Comment


                      #11
                      this code is written in this section
                      else if (State == State.DataLoaded)

                      Comment


                        #12
                        its not work
                        else if (State == State.DataLoaded)
                        {
                        //double maxSma = Math.Max(SMA(Period1)[0], Math.Max(SMA(Period2)[0], SMA(Period3)[0]));
                        //Print(maxSma);

                        SMA1 =SMA(Typical, Convert.ToInt32(Period1));
                        SMA2 =SMA(Typical, Convert.ToInt32(Period2));
                        SMA3 =SMA(Typical, Convert.ToInt32(Period3));
                        // adx =ADX()

                        }
                        }

                        protected override void OnBarUpdate()
                        {
                        if (CurrentBar < 1)
                        return;
                        double maxSma = Math.Max(SMA1(Period1)[0], Math.Max(SMA(Period2)[0], SMA(Period3)[0]));
                        Print(maxSma);

                        Comment


                          #13
                          No one helps me

                          Comment


                            #14
                            Hello shaista parveen,

                            this code is written in this section
                            else if (State == State.DataLoaded)
                            This is not an area where you can execute bar related code, this just signals that data has been loaded and is called before bars begin to process. You can read about the state system in the following link: https://ninjatrader.com/support/help...=onstatechange

                            OnBarUpdate is used for processing bar data and would be where you can calculate values. It looks like in post #12 you have done that.

                            its not work
                            I can't really tell from this excerpt what the problem is, are you seeing an error or was nothing printed?
                            Do you have the NinjaScript output window open?


                            To use the code you have shown you would need to run it and then observe the output window, if you want to attach the whole file so I can run it or see the error that you are that would help. The file is located in the folder: Documents\NinjaTrader 8\bin\Custom\Indicators

                            No one helps me
                            It looks like you posted after our support hours so our support would not be able to comment until now. Other members can reply if they want or not so in some situations you may need to wait for a reply if you are using the forum.

                            I would suggest using the learning resources that are available if you are awaiting replies. I have provided some links to those resource below, if you have follow up questions please feel free to ask.






                            I look forward to being of further assistance.
                            JesseNinjaTrader Customer Service

                            Comment


                              #15
                              Originally posted by shaista parveen View Post
                              its not work
                              else if (State == State.DataLoaded)
                              {
                              //double maxSma = Math.Max(SMA(Period1)[0], Math.Max(SMA(Period2)[0], SMA(Period3)[0]));
                              //Print(maxSma);

                              SMA1 =SMA(Typical, Convert.ToInt32(Period1));
                              SMA2 =SMA(Typical, Convert.ToInt32(Period2));
                              SMA3 =SMA(Typical, Convert.ToInt32(Period3));
                              // adx =ADX()

                              }
                              }

                              protected override void OnBarUpdate()
                              {
                              if (CurrentBar < 1)
                              return;
                              double maxSma = Math.Max(SMA1(Period1)[0], Math.Max(SMA(Period2)[0], SMA(Period3)[0]));
                              Print(maxSma);
                              • Have you defined the Periodn? Silly question, I know, but you would be surprised how many times I have done it myself.
                              • You have tried to directly pass a parameter to an object. In effect, you have tried to use a variable as if it were a method. (see the "1" I have emphasized in bold red?)
                              • "Its not work" is not a problem report, even if true. At the very least, tell us what errors you are getting. As it is, I am not even sure if you have declared your SMAn variables.
                              If you have declared Period1, Period2, and Period3 correctly,
                              Code:
                              double maxSma = Math.Max(SMA(Period1)[0], Math.Max(SMA(Period2)[0], SMA(Period3)[0]));
                              will always give you the max value, without your having to explicitly declare objects to hold your SMAs. You are mixing your paradigms. If you want to declare object holders, then compare the values from the holders, rather than going back to make comparisons of the base indexed values. Granted, that line will process the SMA of the Close, the default. If you want the SMA of the Typical, then you can pass that into the SMA directly, thus:
                              Code:
                              double maxSma = Math.Max(SMA(Typical, Period1)[0], Math.Max(SMA(Typical, Period2)[0], SMA(Typical, Period3)[0]));
                              You have to process bar data in OnBarUpdate(), not in State.DataLoaded.

                              What are the errors reported, either in a compilation, or in the log?

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by rocketman7, Today, 09:41 AM
                              4 responses
                              15 views
                              0 likes
                              Last Post rocketman7  
                              Started by selu72, Today, 02:01 PM
                              1 response
                              9 views
                              0 likes
                              Last Post NinjaTrader_Zachary  
                              Started by WHICKED, Today, 02:02 PM
                              2 responses
                              12 views
                              0 likes
                              Last Post WHICKED
                              by WHICKED
                               
                              Started by f.saeidi, Today, 12:14 PM
                              8 responses
                              21 views
                              0 likes
                              Last Post f.saeidi  
                              Started by Mikey_, 03-23-2024, 05:59 PM
                              3 responses
                              56 views
                              0 likes
                              Last Post Sam2515
                              by Sam2515
                               
                              Working...
                              X