• 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

ninjascript explanation

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

    ninjascript explanation

    hi,

    I try to learn c# and so far i have read Liberty and Macdonald book and references on msdn.micosoft and i cant quite get the grip on how Ninjascript works yet. Ressourses in the support help guides arent very usefull.

    For exemple, in DATA BARS Close you have this exemple
    // OnBarUpdate method
    protected override void OnBarUpdate()
    {
    // Checks if the current close is greater than the prior bar close
    if (Close[0] > Close[1])
    Print("We had an up day");
    }
    If you plug this into the ninjascript it wont work. Its not clear what type of variable you must input to make it works?


    Is it
    Close[int barsAgo]?


    You should rewrite the whole thing because most course i took on c# over the internet are badly construct.



    Frank
    ty

    #2
    Hello Frank,

    If this is an indicator instead of a strategy, this may be an indexing error.

    If the this is run on bar 0, there is no bar 1 bar ago because its the first bar. So if you try and call 1 bar ago on the first bar, you get an error.

    Try:

    protected override void OnBarUpdate()
    {
    if (CurrentBar < 1)
    return;
    // Checks if the current close is greater than the prior bar close
    if (Close[0] > Close[1])
    Print("We had an up day");
    }

    Attached is this in a compiled script to demonstrate.

    Prints will go to the Output window so be sure to open the Output window before adding the indicator to a chart.

    Further, if you are wanting to find a green bar, compare the open of that bar with the close of the bar.
    if (Close[0] > Open[0])
    Print(string.Format("{0} | Up bar", Time[0]));
    Attached Files
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      the correction is right

      Thank you Chelsea,

      Your correction was right now it do works. It should be corrected in the support help guides.

      There is also another case in the help guide i cant make it work. I dont know if its me or an error of typo.

      In functions and methods explained we have this exemple:

      // Calculates the average range of the past three bars
      private double AverageRange()
      {
      return ((High[1] - Low[1]) + (High[2] - Low[2]) + (High[3] - Low[3])) / 3 ;
      }


      First of all i did input after protected override void OnBarUpdate() that part above. But it returns many error messages.



      I would like to try to Sum using the same assembly we say? Like




      {
      return ((Volume[1]) + (Volume[2]) + (Volume[3]));
      }


      Could it work?

      All those little test help me to understand how to program ninjascript.


      Frank

      Comment


        #4
        Originally posted by frankduc View Post
        Thank you Chelsea,

        Your correction was right now it do works. It should be corrected in the support help guides.

        There is also another case in the help guide i cant make it work. I dont know if its me or an error of typo.

        In functions and methods explained we have this exemple:

        // Calculates the average range of the past three bars
        private double AverageRange()
        {
        return ((High[1] - Low[1]) + (High[2] - Low[2]) + (High[3] - Low[3])) / 3 ;
        }


        First of all i did input after protected override void OnBarUpdate() that part above. But it returns many error messages.



        I would like to try to Sum using the same assembly we say? Like




        {
        return ((Volume[1]) + (Volume[2]) + (Volume[3]));
        }


        Could it work?

        All those little test help me to understand how to program ninjascript.


        Frank
        You must make sure that your CurrentBar check is such that it takes care of however many bars back you want to look.There is nothing sacrosanct about using "1".

        Comment


          #5
          Hello Frank,

          This information is in the help guide for CurrentBar.
          http://ninjatrader.com/support/helpG...currentbar.htm

          I will submit a request on your behalf to add this check to the all dataseries pages in the help guide.

          What is the error message that you are getting when calling the AverageRange() method?
          Are you checking there are at least 3 bars before calling this?
          Chelsea B.NinjaTrader Customer Service

          Comment


            #6
            A bunch!
            Its like i said an exemple in functions and methods from help guide version 7. I dont know if its me or an error in the help guide. One thing is sure i gave up for today. Thanks for the help.

            NinjaScript File Error Code Line Column Indicator\test1.cs Type or namespace definition, or end-of-file expected CS1022 - click for info 56 9 Indicator\test1.cs Type or namespace definition, or end-of-file expected CS1022 - click for info 66 5 Indicator\test1.cs Statement expected. 41 10 Indicator\test1.cs Invalid token 'return' in class, struct, or interface member declaration CS1519 - click for info 45 9 Indicator\test1.cs Invalid token ')' in class, struct, or interface member declaration CS1519 - click for info 45 34 Indicator\test1.cs Invalid token ')' in class, struct, or interface member declaration CS1519 - click for info 45 55 Indicator\test1.cs Invalid token ')' in class, struct, or interface member declaration CS1519 - click for info 45 76 Indicator\test1.cs Invalid token '-' in class, struct, or interface member declaration CS1519 - click for info 45 26 Indicator\test1.cs Invalid token '-' in class, struct, or interface member declaration CS1519 - click for info 45 47 Indicator\test1.cs Invalid token '-' in class, struct, or interface member declaration CS1519 - click for info 45 68 Indicator\test1.cs Identifier expected CS1001 - click for info 55 33 Indicator\test1.cs Expected class, delegate, enum, interface, or struct CS1518 - click for info 53 16 Indicator\test1.cs Expected class, delegate, enum, interface, or struct CS1518 - click for info 60 16 Indicator\test1.cs Array size cannot be specified in a variable declaration (try initializing with a 'new' expression) CS0270 - click for info 45 23 Indicator\test1.cs Array size cannot be specified in a variable declaration (try initializing with a 'new' expression) CS0270 - click for info 45 32 Indicator\test1.cs Array size cannot be specified in a variable declaration (try initializing with a 'new' expression) CS0270 - click for info 45 44 Indicator\test1.cs Array size cannot be specified in a variable declaration (try initializing with a 'new' expression) CS0270 - click for info 45 53 Indicator\test1.cs Array size cannot be specified in a variable declaration (try initializing with a 'new' expression) CS0270 - click for info 45 65 Indicator\test1.cs Array size cannot be specified in a variable declaration (try initializing with a 'new' expression) CS0270 - click for info 45 74 Indicator\test1.cs } expected CS1513 - click for info 41 10 Indicator\test1.cs ; expected. 43 36

            Comment


              #7
              Originally posted by frankduc View Post
              A bunch!
              Its like i said an exemple in functions and methods from help guide version 7. I dont know if its me or an error in the help guide. One thing is sure i gave up for today. Thanks for the help.

              NinjaScript File Error Code Line Column Indicator\test1.cs Type or namespace definition, or end-of-file expected CS1022 - click for info 56 9 Indicator\test1.cs Type or namespace definition, or end-of-file expected CS1022 - click for info 66 5 Indicator\test1.cs Statement expected. 41 10 Indicator\test1.cs Invalid token 'return' in class, struct, or interface member declaration CS1519 - click for info 45 9 Indicator\test1.cs Invalid token ')' in class, struct, or interface member declaration CS1519 - click for info 45 34 Indicator\test1.cs Invalid token ')' in class, struct, or interface member declaration CS1519 - click for info 45 55 Indicator\test1.cs Invalid token ')' in class, struct, or interface member declaration CS1519 - click for info 45 76 Indicator\test1.cs Invalid token '-' in class, struct, or interface member declaration CS1519 - click for info 45 26 Indicator\test1.cs Invalid token '-' in class, struct, or interface member declaration CS1519 - click for info 45 47 Indicator\test1.cs Invalid token '-' in class, struct, or interface member declaration CS1519 - click for info 45 68 Indicator\test1.cs Identifier expected CS1001 - click for info 55 33 Indicator\test1.cs Expected class, delegate, enum, interface, or struct CS1518 - click for info 53 16 Indicator\test1.cs Expected class, delegate, enum, interface, or struct CS1518 - click for info 60 16 Indicator\test1.cs Array size cannot be specified in a variable declaration (try initializing with a 'new' expression) CS0270 - click for info 45 23 Indicator\test1.cs Array size cannot be specified in a variable declaration (try initializing with a 'new' expression) CS0270 - click for info 45 32 Indicator\test1.cs Array size cannot be specified in a variable declaration (try initializing with a 'new' expression) CS0270 - click for info 45 44 Indicator\test1.cs Array size cannot be specified in a variable declaration (try initializing with a 'new' expression) CS0270 - click for info 45 53 Indicator\test1.cs Array size cannot be specified in a variable declaration (try initializing with a 'new' expression) CS0270 - click for info 45 65 Indicator\test1.cs Array size cannot be specified in a variable declaration (try initializing with a 'new' expression) CS0270 - click for info 45 74 Indicator\test1.cs } expected CS1513 - click for info 41 10 Indicator\test1.cs ; expected. 43 36
              Those are all distinct c# errors coming from c#. They even have the identifying numbers.

              Comment


                #8
                Hello Frank,

                These errors are compile errors and are not run-time errors.

                From the message, I think in the script there is syntax that is incorrect. I think you are missing a closing curly brace somewhere "}".

                "Type or namespace definition, or end-of-file expected CS1022"

                Can you attach the .cs file of your script to your next post?

                This will be located in Documents\NinjaTrader 7\bin\Custom\Indicator.
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  here's the file

                  The file Test is the exemple that i took from the help guide copy/paste.
                  Cant say what's not working.

                  The file Test1 is a time and date i try to set to an indicator. Got a bunch of errors, any idea what im doing wrong?

                  Test2 is another exemple i took in the help guide and return the same kind of errors than test and test1.

                  Frank
                  ty
                  Attached Files
                  Last edited by frankduc; 09-15-2016, 07:07 AM.

                  Comment


                    #10
                    Hello Frank,

                    In the Test.cs file you have not copied the code in to the correct locations and there is also code that could not have been copied from the help guide as there are comparisons between objects that are not comparable.

                    On line 54 there is an extra closing curly brace that is causing the public properties to be outside of the scope of the Test class.

                    The #endregion on line 88 should also be above the closing curly brace on line 87.

                    You are also trying to compare an integer with a DateTime object.
                    CurrentBar >= DateTime
                    This is not valid C#. Dates can only be compared with dates. Numbers can only be compared with numbers. Strings can only be compared with strings.
                    You cannot compare an integer like 5 to a DateTime like '12/1/99 12:05:12'.

                    Also, there is a Datetime that is used in the barsAgo index.
                    Volume[DateTime]
                    A DateTime value is not an acceptable value to use as a bars ago value.
                    The bars ago value can only be an integer. A number that represents the number of bars ago that you want a value for. This must be a whole number greater than 0.


                    In the test1.cs file, there is a method within the scope of another method.
                    This code is not valid C#
                    Code:
                    protected override void OnBarUpdate()
                    {
                    // Calculates the average range of the past three bars
                    private double AverageRange()
                    {
                    return ((High[1] - Low[1]) + (High[2] - Low[2]) + (High[3] - Low[3])) / 3 ;
                    }
                    }
                    This code is valid C#
                    Code:
                    protected override void OnBarUpdate()
                    {
                    // Calculates the average range of the past three bars
                    Print(AverageRange().ToString());
                    }
                    
                    private double AverageRange()
                    {
                    return ((High[1] - Low[1]) + (High[2] - Low[2]) + (High[3] - Low[3])) / 3 ;
                    }
                    Below is a link to a video that discusses scope and demonstrates where items can be copied and pasted.
                    https://www.youtube.com/watch?v=K8v_...utu.be&t=8m60s


                    You can also contact one of our professional NinjaScript Consultants who would be eager to create or modify this script at your request or assist you with your script. Please let me know if you would like our business development follow up with you with a list of professional NinjaScript Consultants who would be happy to create this script or any others at your request.
                    Last edited by NinjaTrader_ChelseaB; 09-15-2016, 09:21 AM.
                    Chelsea B.NinjaTrader Customer Service

                    Comment


                      #11
                      First of all, thank you for the reply and your patience.

                      Its more complicated than i was hoping it will be.
                      Still i wish the help guide was more explicit in its exemples.

                      In the list of professional scipt consultats there is none in Montreal and i have tough time trusting someone i cant communicate face to face with, not to mention it pose the issue of intellectual propriety.

                      Ill check on the youtube video.

                      Frank
                      ty again

                      Comment


                        #12
                        Hello Frank,

                        NinjaScript is written in C# and has a premade methods created to trigger your code during certain events such as when a bar closes, and to do actions such as submit orders.
                        The items that you are having difficulty with are not NinjaScript specific items. These are all general C# inquiries.
                        The help guide does not educate C# but instead provides a reference for usage of specfic NinjaScript objects and methods.
                        You may benefit from learning the syntax of how to use C# from third party education resources.

                        Below I am adding what resources are available from NinjaTrader.

                        A link to our Help Guide with tutorials can be found below:
                        http://ninjatrader.com/support/helpG.../tutorials.htm

                        The entire Alphabetical Reference can be found with the following link:
                        http://ninjatrader.com/support/helpG..._reference.htm

                        The best way to begin learning NinjaScript is to use the Strategy Wizard. With the Strategy Wizard you can setup conditions and variables and then see the generated code in the NinjaScript Editor by clicking the View Code button.

                        I'm also proving a link to a pre-recorded set of videos 'Strategy Wizard 301' and 'NinjaScript Editor 401' for you to view at your own convenience.
                        Strategy Wizard 301 - https://www.youtube.com/watch?v=FmBi...56536A44DD7105
                        NinjaScript Editor 401 - https://www.youtube.com/watch?v=K8v_...56536A44DD7105

                        To get a basic foundation for the concepts and syntax used in C#I would recommend this section of articles in our help guide first:
                        http://ninjatrader.com/support/helpG...g_concepts.htm

                        I am also linking you to the Educational Resources section of the Help Guide to help you get started specifically with NinjaScript: http://www.ninjatrader.com/support/h..._resources.htm

                        You will find Reference Samples online as well as some Tips and Tricks for both indicators and strategies:
                        Click here to see our NinjaScript Reference Samples: http://www.ninjatrader.com/support/f...splay.php?f=30
                        Click here to see our NinjaScript Tips: http://www.ninjatrader.com/support/f...ead.php?t=3229

                        These samples can be downloaded, installed and modified from NinjaTrader and hopefully serve as a good base for your custom works.

                        There is a also a growing library of user submitted custom indicators (100+) that can be downloaded from our support form. Please look in the NinjaScript File Sharing section of our support forum as you may find what you are looking for there: http://www.ninjatrader.com/support/f...splay.php?f=37
                        Chelsea B.NinjaTrader Customer Service

                        Comment


                          #13
                          Beleive it or not i have already red all of this.

                          Im still going to test and try. Maybe ill get the hang of it one of these days.

                          Thanks again

                          Comment


                            #14
                            It may help to see the big picture, which is: you are writing a plugin.

                            NT7 supports 2 types of plugins: Indicator and Strategy.

                            The NinjaTrader.exe make calls into your plugin via well known method names, which are,

                            Initialize
                            OnStartUp
                            OnBarUpdate
                            OnExecution
                            OnOrderUpdate
                            OnExecution
                            OnPositionUpdate
                            OnMarketData
                            OnConnectionStatus

                            [In NT8, these entry point names have been changed, because the plugin model was updated.]

                            You, or the Strategy Wizard, supplies the needed code (if any) for each method.

                            You identify your plugin to NinjaTrader via creating a new C# class, which must inherit from one (just one, not both) of the class names 'Indicator' or 'Strategy'.

                            The above entry points for your plugin then go inside the class body, for example,

                            public class FrankEMA : Indicator // <-- identifies FrankEMA as an 'Indicator' plugin
                            {
                            // entry points for FrankEMA plugin go here
                            }

                            Perhaps this big picture helps you see where your work fits into this organization?

                            After that, realize that writing a plugin is no small feat -- it absolutely requires some technical training. This education is known as computer programming, or as the new kids like to call it, software engineering.

                            The nice thing about computer programming is that many software products that support plugins try to present their plugin environment like an onion, meaning it has layers. The more you need to get done, the more layers you have to peel. In many cases, you can get by with a modest amount of programming knowledge, because only a few layers of simple programming techniques are necessary to write some basic code for a basic plugin.

                            Lastly, don't feel it's necessary that your programmer needs to be sitting across from you to understand what you want. Skype and email can solve all those problems in a heartbeat.

                            Comment


                              #15
                              Lastly, don't feel it's necessary that your programmer needs to be sitting across from you to understand what you want. Skype and email can solve all those problems in a heartbeat.
                              What about the customer who writes to you: "I am not educated enough to be able to explain the words that you do not understand, by stating how to physically do what I am asking!"

                              No kidding. That is a pretty clear paraphrase of what one customer said to me when I said he should describe physically what he does when he is doing a so-called "2-bar trail". Of course, I had an idea what he meant, but he had been so muddled in his thinking all along, that I wanted to be sure that he was describing what he meant, and that it matched what I thought that it meant.
                              Last edited by koganam; 09-16-2016, 02:50 PM.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by sun0781, Today, 05:02 PM
                              0 responses
                              6 views
                              0 likes
                              Last Post sun0781
                              by sun0781
                               
                              Started by ronhb107, 09-12-2019, 10:09 AM
                              4 responses
                              41 views
                              0 likes
                              Last Post Mr.Trader  
                              Started by rwilliam, Today, 03:55 PM
                              0 responses
                              3 views
                              0 likes
                              Last Post rwilliam  
                              Started by asmmbillah, Today, 03:42 PM
                              2 responses
                              7 views
                              0 likes
                              Last Post asmmbillah  
                              Started by NT-Roland, Today, 03:25 PM
                              0 responses
                              7 views
                              0 likes
                              Last Post NT-Roland  
                              Working...
                              X