• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

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, 06: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]);
    http://www.ninjatrader.com/support/h...simple_sma.htm

    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.
            http://www.ninjatrader.com/support/h...ed_methods.htm

            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, 07: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, 09:01 AM.

                Comment


                  #9
                  Thanks Koganan for your help, I appreciate it

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by PeterGolz, 01-17-2020, 10:42 AM
                  11 responses
                  71 views
                  0 likes
                  Last Post reubenlasky  
                  Started by utilizator, Today, 11:36 AM
                  0 responses
                  4 views
                  0 likes
                  Last Post utilizator  
                  Started by EnveousColnago, Today, 07:24 AM
                  1 response
                  9 views
                  0 likes
                  Last Post NinjaTrader_ShawnB  
                  Started by tornadoatc, Today, 10:31 AM
                  0 responses
                  3 views
                  0 likes
                  Last Post tornadoatc  
                  Started by EnveousColnago, Yesterday, 05:43 PM
                  5 responses
                  30 views
                  0 likes
                  Last Post EnveousColnago  
                  Working...
                  X