Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

New functions (classes)? How to?

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

    New functions (classes)? How to?

    First: I am NinjaScript Newby and i didn't find something like that in the forum because i don't speak english very well....

    So sorry, if this is the 1001 th questtion to the same theme....

    what i want to do:
    i want to use for example an EMA like SMA:

    double variable = EMA(value, Period)[0];

    and not

    double variable = Input[0] * (2.0 / (1 + Period)) + (1 - (2.0 / (1 + Period))) * Value[1];

    #2
    xenayoo, welcome to the forums, just create a new indicator with your calculation - a method / function for quick access will be created for this then automatically, NinjaScript does not have separated indicator / function concepts like for example TradeStation with Easy Language.

    Here some good tutorials for a smooth start -

    Comment


      #3
      I know the diferenc between easy laguage and c#/Ninja script. And i know the tutorials. What do you mean with: '...a method / function for quick access will be created for this then automatically...'
      I want to create a method or a class with different methods for using this calculation in different indidicators without programmig the same calculation new for each indicator. It's not a question for one indicator.

      Thank you.

      Comment


        #4
        You could put those in the UserDefinedMethods file(s) -





        Comment


          #5
          Ok. Now the next Problem:
          Can anyone hlp me with that code:

          Code:
          [FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]
          public[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] DataSeries EMA(DataSeries Value, [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] Period)
          {
          [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2](CurrentBar == [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080]0[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2]) {ExMA = [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]new[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] DataSeries();}
          [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2](CurrentBar <= Period) {Return Value;}
          ExMA = ExMA[[/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080]1[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2]] + (([/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080]2[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2]/([/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080]1[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2]+Period)) * ( Value[[/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080]0[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2]] - ExMA[[/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080]1[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2]]));
          Return ExMA;
          }
          [/SIZE][/FONT][/SIZE][/FONT]
          Should be a User defined Metod for calculating an EMA.....

          Thanks a lot

          Comment


            #6
            xenayoo, you can use an EMA anywhere in NinjaScript by just using this:
            Code:
            // to get a 14 period EMA of the close prices
            double myEmaValue = EMA(Close, 14)[0];
            It isn't necessary to place every indicator into the User Defined Methods Class. In fact, I would actually recommend against re-creating the indicators in the User Defined methods because that could quickly create duplicate name issues.
            AustinNinjaTrader Customer Service

            Comment


              #7
              Not sure what your problem is but one issue I would like to comment:
              CurrentBar == 0 is not the best approach in case you use the indicator with "Calculate on bar close == false". I would add an additional test: FirstTickOfBar == true. Otherwise your dataseries could get instantiated quite a lot of times.

              Regards
              Ralph

              Comment


                #8
                Originally posted by NinjaTrader_Austin View Post
                xenayoo, you can use an EMA anywhere in NinjaScript by just using this:
                Code:
                // to get a 14 period EMA of the close prices
                double myEmaValue = EMA(Close, 14)[0];
                It isn't necessary to place every indicator into the User Defined Methods Class. In fact, I would actually recommend against re-creating the indicators in the User Defined methods because that could quickly create duplicate name issues.
                Hm, but why the build-in indicator like EMA and MACD doesn't use this build-in Method? This two indicators are the reason to create this method. Ok, it's not nessesairy to write this method for my own - but how does a working method for EMA look like? (just tu understand and learn more about Dataseries objects) My code doesn't work, because there are error-messages about a DataSeries object, which isn't defined (ExMA)...

                Thank You again.

                Comment


                  #9
                  Sorry, I do not follow you - Austin's sample provided you the last EMA double value for access.

                  If you want to work with the dataseries the EMA methods returns, just don't apply an index to it...

                  Comment


                    #10
                    Originally posted by NinjaTrader_Bertrand View Post
                    Sorry, I do not follow you - Austin's sample provided you the last EMA double value for access.
                    I didn't know, that there is allready a function 'EMA' build in. I only saw the indicators code for the indicator EMA and MACD. They both doesn't use this function - why? ;-)
                    I will use this buildin method.

                    Originally posted by NinjaTrader_Bertrand View Post
                    If you want to work with the dataseries the EMA methods returns, just don't apply an index to it...
                    My code should be a user-defined method for EMA. I now know, that this isn't nessesairy. I only want to get this method working just for learning. I saw the calculations in indicators 'EMA' and 'MACD'. In both cases the calculations are stored in a DataSeries-Object. So i supposed, that a user-defined method for EMA have to return a DataSeries too.
                    In my method i create a new DataSeries-object on currentbar == 0, but that won't work, because the error message informed, that there is no object with the specified name. This Object is used to refer the previous value of ema. So my question is: How should a working code for a userdefined-method look like. It's just for learning.

                    Thanks again.

                    Comment


                      #11
                      If you want to learn working with custom dataseries objects, please check this sample out - http://www.ninjatrader-support2.com/...ead.php?t=7299

                      In your sample indicator you most likely stumbled into this issue here, too - http://www.ninjatrader-support2.com/...ead.php?t=3170

                      You would not see a separate method / function for the EMA in NinjaScript, the indicator you looked into can be called as method from other scripts and strategies and supports passing in custom dataseries as input series.

                      Comment


                        #12
                        Originally posted by NinjaTrader_Bertrand View Post
                        If you want to learn working with custom dataseries objects, please check this sample out - http://www.ninjatrader-support2.com/...ead.php?t=7299
                        Thank you very much for this link.

                        Originally posted by NinjaTrader_Bertrand View Post
                        In your sample indicator you most likely stumbled into this issue here, too - http://www.ninjatrader-support2.com/...ead.php?t=3170
                        May be.

                        Originally posted by NinjaTrader_Bertrand View Post
                        You would not see a separate method / function for the EMA in NinjaScript, the indicator you looked into can be called as method from other scripts and strategies and supports passing in custom dataseries as input series.
                        Ah, this is it: in NinjaScript methods are created automaticaly while writing a new indicator. So, there are two other questions:
                        If i use something like an EMA, wich calculates ony one value (line), that's easy. But if i have a calculation licke MACD or BollingerBands, wich calculates more than one value, how does this works? Where do i get informations about parameters?
                        And: Is there a specific type to write while programming an indicator?

                        Thank you very much.

                        Comment


                          #13
                          You're welcome, if you define user defined inputs for your indicators those will later serve as parameters if you call as a method - http://www.ninjatrader-support2.com/...ead.php?t=5782

                          To get familiar with the indicators the tutorials would help getting a good foundation -



                          A good example for a multiplot indicator would be the DonchianChannel, all three plots are automatically exposed and thus you can access them from other indicators / strategies -

                          Code:
                           
                          myUpperValue = DonchianChannel(20).Upper[0];
                           
                          myLowerValue = DonchianChannel(20).Lower[0];

                          Comment


                            #14
                            Ok, there is much to learn...

                            Thank you very much for this examples. But now i have again a Prblem with EMA:

                            Nijatrader-Austin said:
                            xenayoo, you can use an EMA anywhere in NinjaScript by just using this:

                            Code:
                            // to get a 14 period EMA of the close pricesdouble myEmaValue = EMA(Close, 14)[0];
                            Problem: i only can set the Period to EMA:
                            Code:
                             
                            double myEma = EMA(14);
                            but i need a Function like that:
                            Code:
                             
                            int Period = 14;
                            MyEma = new DataSeries;
                            MyEma.Set(EMA(MyDataSeries, Period));
                            Help....

                            Edit: killing erors.... ;-)

                            Comment


                              #15
                              Ok, here is the answer:
                              EMA is an overloaded function and the example from intellisense only gives the first method....
                              My fault:
                              Code:
                              MyValue.Set(EMA(MyDataSeries, MyIntPeriod));
                              Right:
                              Code:
                              MyValue.Set(EMA(MyDataSeries, MyIntPeriod)[0]);

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                              0 responses
                              558 views
                              0 likes
                              Last Post Geovanny Suaza  
                              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                              0 responses
                              324 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by Mindset, 02-09-2026, 11:44 AM
                              0 responses
                              101 views
                              0 likes
                              Last Post Mindset
                              by Mindset
                               
                              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                              0 responses
                              545 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by RFrosty, 01-28-2026, 06:49 PM
                              0 responses
                              547 views
                              1 like
                              Last Post RFrosty
                              by RFrosty
                               
                              Working...
                              X