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

How to convert the SMA indicator into a Method/Function ?

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

    How to convert the SMA indicator into a Method/Function ?

    Hi ninjas,

    I'd like to add the SMA indicator as a Method or Function within a main Script, because several reasons. As you must imagine that's not so easy as Cut & Paste, cause its variables ,out of Indicator environment, give errors. My goal is calling this method within my script a whenever you call the external SMA indicator

    So would you please help me indicating me what must I change in the SMA script in order to put it as a Method/Function within my main Script ?

    Thanks
    Last edited by pstrusi; 11-22-2013, 07:29 AM.

    #2
    Hello pstrusi,

    I am not sure I fully understand what you are trying to accomplish here. You can call the SMA indicator inside of any NinjaScript file like a method to be able to get its values so you do not have to do the calculations manually.

    For example:
    Code:
    // Sets "value" to the current price of a 20 period SMA and also Prints it in the output window.
    double value = SMA(20)[0];
    Print("The current SMA value is " + SMA(20)[0]);


    Is that how you are wanting to use the SMA or could you give me an example of what you would like to do so that I may try to assist you further?
    JCNinjaTrader Customer Service

    Comment


      #3
      Hi JC thanks for your response.

      Yes, I know that I can call it as a method or function in any script; but for compilation and velocity purpose I've been wondering how to code the very same SMA into the main Script, so I don't have to add external scripts to my own. I imagine that this can be done. So, what do you think about it?

      Comment


        #4
        Hello pstrusi,

        Just to clarify, your main script that you are referring to is outside of NinjaTrader meaning that it is not a NinjaScript file that you are opening by going to Tools -> Edit NinjaScript?
        JCNinjaTrader Customer Service

        Comment


          #5
          It is a Ninja script file, created by me

          Comment


            #6
            Hello pstrusi,

            If it is inside of NinjaTrader I still am not sure I understand what you are trying to do. The only thing that comes to mind is a the "User Defined Methods" which can be setup to create a method externally from your main script but still be able to be called in any of your indicator/strategy to return a value like the SMA.

            Here is a few samples on what a "User Defined Method" can do that you may view to see if this is what you are looking for.


            Let me know if this is what you are looking for.
            JCNinjaTrader Customer Service

            Comment


              #7
              It's not even an issue, so no worries, but let put it this way

              Whenever you're gonna compile a NT script, if you use an indicator out of the main script, the system will let u know and asking you if you want to add it to the export file, so what I'd like to know is:

              How can add the SMA indicator as a method within the main script,so when it must be compiled, you won't need to add any external file?
              Last edited by pstrusi; 11-22-2013, 08:51 AM.

              Comment


                #8
                Originally posted by pstrusi View Post
                Hi JC thanks for your response.

                Yes, I know that I can call it as a method or function in any script; but for compilation and velocity purpose I've been wondering how to code the very same SMA into the main Script, so I don't have to add external scripts to my own. I imagine that this can be done. So, what do you think about it?
                The quick and dirty way to create a varying cumulative function is to use a loop construct. There are more efficient ways. However, depending on how often the function will be called, that inefficiency may be a non-issue.

                Here is an example of such:
                Code:
                        protected override void OnBarUpdate()
                        {
                Print(null); //Are they really equivalent? Examine the Output Window
                Print("Current Bar: " + CurrentBar);
                            Print("SMAClass value: " + SMA(Close, 10)[0]);
                            Print("InternalSMA value: " + InternalSMA(Close, 10));
                        }
                        
                        private double InternalSMA(IDataSeries Price, int Period)
                        {
                            if (CurrentBar < Period) return 0;
                            double SumPrice = 0;
                            for (int index = 0; index < Period; index++) 
                            {
                                SumPrice += Price[index];
                            }
                            return (SumPrice/Period);
                        }
                If you want a more efficient method, unfortunately, it would not be free.
                Last edited by koganam; 11-22-2013, 10:01 AM.

                Comment


                  #9
                  Thanks Koganan for your help, I appreciate it

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by DJ888, 04-16-2024, 06:09 PM
                  6 responses
                  18 views
                  0 likes
                  Last Post DJ888
                  by DJ888
                   
                  Started by Jon17, Today, 04:33 PM
                  0 responses
                  1 view
                  0 likes
                  Last Post Jon17
                  by Jon17
                   
                  Started by Javierw.ok, Today, 04:12 PM
                  0 responses
                  6 views
                  0 likes
                  Last Post Javierw.ok  
                  Started by timmbbo, Today, 08:59 AM
                  2 responses
                  10 views
                  0 likes
                  Last Post bltdavid  
                  Started by alifarahani, Today, 09:40 AM
                  6 responses
                  41 views
                  0 likes
                  Last Post alifarahani  
                  Working...
                  X