Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How Can I Learn to Use Methods

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

    How Can I Learn to Use Methods

    Support Forum,

    I'm working on my first strategy and am coming up with a lot of CS1525 error codes.

    if (Close[0] > Open[0] && (Open[0] - Low[0]) >= (Close[0] - Open[0]) * wickMultiplier && (High[0] - Close[0])<= (High[0] - Low[0])* bodyPercentage)
    && (High[
    0] >= pp - near * TickSize && High[0] <= pp + near * TickSize )
    EnterLong();

    Where can I to learn how to break this up into method statements? I tried following a couple of the NinjaScript manual examples but they don't seem to work and I'm not sure if I am even putting them in the right section.

    Thanks,
    Gennaker

    #2
    Hello Gennaker,
    Thanks for writing in and I am happy to assist you.

    Please add a round bracket before and at the end of the if statement

    Code:
    if [B][COLOR="Blue"]([/COLOR][/B](Close[0] > Open[0] && (Open[0] - Low[0]) >= (Close[0] - Open[0]) * wickMultiplier && (High[0] - Close[0])<= (High[0] - Low[0])* bodyPercentage) && (High[0] >= pp - near * TickSize && High[0] <= pp + near * TickSize )[B][COLOR="blue"])[/COLOR][/B]
    				EnterLong();
    Please let me know if I can assist you any further.
    JoydeepNinjaTrader Customer Service

    Comment


      #3
      Thanks Joydeep,

      That took care of the error messages I was receiving. Now that it is working, I would like to know how I can split this code into two methods earlier on in the strategy and be able to call them in the OnBarUpate() section.

      I would like to call the first method (I hope I am using the correct terminology) "WickReversal1" and would consist of

      (Close[0] > Open[0] && (Open[0] - Low[0]) >= (Close[0] - Open[0]) * wickMultiplier && (High[0] - Close[0])<= (High[0] - Low[0])* bodyPercentage)

      I would like to call the second method "NearPivot" and consists of

      (High[0] >= pp - near * TickSize && High[0] <= pp + near * TickSize )

      Then I would like to be able to just say within the OnBarUpdate section:

      if WickReversal1 && NearPivot
      EnterLong();

      Can you show me how to do this?

      Thanks,
      Gennaker

      Comment


        #4
        Hello Gennaker,
        The code I shown before essentially does what you want to do
        Code:
        if ((Close[0] > Open[0] && (Open[0] - Low[0]) >= (Close[0] - Open[0]) * wickMultiplier && (High[0] - Close[0])<= (High[0] - Low[0])* bodyPercentage) && (High[0] >= pp - near * TickSize && High[0] <= pp + near * TickSize ))
        				EnterLong();
        Alternatively you can also write it as
        Code:
        if (Close[0] > Open[0] && (Open[0] - Low[0]) >= (Close[0] - Open[0]) * wickMultiplier && (High[0] - Close[0])<= (High[0] - Low[0])* bodyPercentage)
        
        {
           if (High[0] >= pp - near * TickSize && High[0] <= pp + near * TickSize )
           {
        	EnterLong();
           }
        }
        Both the code check for the first condition (WickReversal1) and if its true then it checks for the second condition (NearPivot). If both the conditions are true then a long market order is placed.

        Please refer to our help files in the below link which explains some basic programming concepts http://www.ninjatrader.com/support/h...g_concepts.htm

        Please let me know if I can assist you any further.
        JoydeepNinjaTrader Customer Service

        Comment


          #5
          Joydeep,

          In the link you provided there is a section called "Functions and Methods Explained". In that section, there is an example of what I am trying to do. It declares a method called "AverageRange ()" and uses it later on in the script.

          That is what I am trying to learn how to do. I want to declare WickReversal1 as a method and NearPivot as a method earlier in the script. I then want to reference them both later to execute a EnterLong () command.

          Thanks,
          Gennaker

          Comment


            #6
            Hello Gennaker,
            Thanks for the clarification.

            This is more of a C# concept and beyond what we could support. However a basic code will look like

            Code:
            private bool WickReversal1()
            {
                //logic for the wick reversal
            }
            
            private bool NearPivot()
            {
                //logic for the near suppot
            }
            
            //call the functions in OnBarUpdate as
            public override void OnBarUpdate()
            {
                if (WickReversal() && NearPivot())
                {
                       EnterLong();
                 }
            }
            To learn more on functions please refer here http://csharp.net-tutorials.com/basics/functions/

            Please let me know if I can assist you any further.
            JoydeepNinjaTrader Customer Service

            Comment


              #7
              Joydeep,

              That helps. I was using Private Void and not Private Bool.

              Should these methods be declared in the Initialize () section or does it matter where?

              Thanks,
              Gennaker

              Comment


                #8
                Hello Gennaker,
                Glad to know you are able to get the concepts.

                These are functions and you basically call it. You dont have to "declare" it.

                For example a basic indicator class will look like

                Code:
                public MyIndicator : Indicator
                {
                
                   public override void Initialize()
                   {
                       //codes
                    }
                
                   public override void OnBarUpdate()
                   {
                          //you can call the private fuctions in OnBarUpdate
                          if (WickReReversal1() && NearPivot())
                           {
                               EnterLong();
                           }
                    }
                
                   private bool WickReversal1()
                   {
                           //codes
                   }
                
                   private bool NearPivot()
                   {
                        //codes
                   }
                }
                Please let me know if I can assist you any further.
                JoydeepNinjaTrader Customer Service

                Comment


                  #9
                  Joydeep,

                  I tried calling the private functions as you suggested, but got a couple of errors when I did the following:

                  protectedoverridevoid OnBarUpdate()
                  {

                  privatebool WickReversal1()
                  {
                  (Close[
                  0] > Open[0] && (Open[0] - Low[0]) >= (Close[0] - Open[0]) * wickMultiplier && (High[0] - Close[0])<= (High[0] - Low[0])* bodyPercentage);
                  }
                  //other codes that didn't produce errors before adding this snippet
                  }




                  The first error was a "statement expected" on the same line as the first curly brace (}) below the OnBarUpdate () line.

                  The second error was a " ; expected" at the end of the WickReversal1 () line.

                  Would appreciate your advice.

                  Thanks,
                  Gennaker
                  Last edited by Gennaker; 04-25-2012, 03:52 AM.

                  Comment


                    #10
                    Hello Gennaker,
                    You are defining the function inside the OnBarUpdate method.

                    You can only call the function in OnBarUpdate method. You have to define it outside OnBarUpdate.

                    Please refer here to get more ideas on Functions and Methods


                    Please let me know if I can assist you any further.
                    JoydeepNinjaTrader Customer Service

                    Comment


                      #11
                      Originally posted by Gennaker View Post
                      Joydeep,

                      I tried calling the private functions as you suggested, but got a couple of errors when I did the following:

                      protectedoverridevoid OnBarUpdate()
                      {

                      privatebool WickReversal1()
                      {
                      (Close[
                      0] > Open[0] && (Open[0] - Low[0]) >= (Close[0] - Open[0]) * wickMultiplier && (High[0] - Close[0])<= (High[0] - Low[0])* bodyPercentage);
                      }
                      //other codes that didn't produce errors before adding this snippet
                      }




                      The first error was a "statement expected" on the same line as the first curly brace (}) below the OnBarUpdate () line.

                      The second error was a " ; expected" at the end of the WickReversal1 () line.

                      Would appreciate your advice.

                      Thanks,
                      Gennaker
                      In addition to what NinjaTrader_Joydeep says below, your method is defective anyway. You specify that the method should return a bool value, but you are not returning anything in the body of the function. Try this:

                      Code:
                       
                      private bool WickReversal1()
                      {
                      bool testValue = false;
                      testValue = ((Close[0] > Open[0] && (Open[0] - Low[0]) >= (Close[0] - Open[0]) * wickMultiplier && (High[0] - Close[0])<= (High[0] - Low[0])* bodyPercentage));
                      return testValue;
                      }

                      Comment


                        #12
                        Thanks, Koganam. That was helpful. Looking forward to trying it next week after my holiday.

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                        0 responses
                        656 views
                        0 likes
                        Last Post Geovanny Suaza  
                        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                        0 responses
                        371 views
                        1 like
                        Last Post Geovanny Suaza  
                        Started by Mindset, 02-09-2026, 11:44 AM
                        0 responses
                        109 views
                        0 likes
                        Last Post Mindset
                        by Mindset
                         
                        Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                        0 responses
                        574 views
                        1 like
                        Last Post Geovanny Suaza  
                        Started by RFrosty, 01-28-2026, 06:49 PM
                        0 responses
                        579 views
                        1 like
                        Last Post RFrosty
                        by RFrosty
                         
                        Working...
                        X