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

Indicator compiles but nothing happens

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

    Indicator compiles but nothing happens

    Hi,

    I recently switched from NT7 to NT8 and I must say that the transition when it comes to NinjaScript is not easy.
    I successfully coded a 200+ lines indicator in NT7 but in NT8 even the most basic code (although it compiles OK) produces nothing.
    Could a kind soul please tell me what's wrong with the following snippet?

    Thanks so much.


    protected override void OnBarUpdate()
    {

    if (High[0] < High[1]);
    Draw.ArrowUp(this, "MyUpArrow" + CurrentBar, true, 0, Low[0] - 3 * TickSize, Brushes.Black);
    PlaySound(NinjaTrader.Core.Globals.InstallDir + @"\sounds\Alert1.wav");​

    #2
    Hello laocoon,

    Thank you for your post.

    I'm not seeing anything obviously wrong with the snippet you provided, other than making sure that if you want the Draw.ArrowUp and PlaySound() methods to happen if if (High[0] < High[1]) then these should be encapsulated in curly braces.


    Code:
    if (High[0] < High[1])
    {
    Draw.ArrowUp(this, "MyUpArrow" + CurrentBar, true, 0, Low[0] - 3 * TickSize, Brushes.Black);
    PlaySound(NinjaTrader.Core.Globals.InstallDir + @"\sounds\Alert1.wav");​
    }

    ​If the script is applied to a chart, is there data with new bars appearing on the chart the indicator is applied to?

    Importantly, are there errors appearing on the Log tab of the Control Center?

    If you test with a system indicator like the SMA, does this indicator produce results?

    In order to better understand how the code is working, it will be necessary to use Print to see how the conditions are evaluating.

    Below is a link to a forum post that demonstrates using prints to understand behavior.


    Print the time of the bar and all values used in the conditions. Include labels for all values and comparison operators.

    Let me know if you need any assistance creating a print.

    Save the output from the output window to a text file and provide this with your reply.

    I'll be happy to assist with analyzing the output.​
    Gaby V.NinjaTrader Customer Service

    Comment


      #3
      Thanks a lot for your reply Gaby. I didn't forget the curly braces, but there's indeed an error message in the log:
      Error on calling "OnBarUpdate" method on bar 0. You are accessing an index with a value that is invalid since it is out of range.

      Comment


        #4
        Hello laocoon,

        Thank you for your response.

        This message is indicating the specific index requested from a collection does not exist. Indexes must be a non-negative number and less than the size of the collection. The error may be indicating the index requested is larger than the number of elements in the collection.

        In the case of barsAgo values with Series, any barsAgo index must be less than CurrentBar (the total number of bars is the size of a Series collection).

        Code:
        // require BarsInProgress 0 to have 3 bars processed, require BarsInProgress 1 to have 4 bars processed
        if (CurrentBars[0] < 3 || CurrentBars[1] < 4)
        return;
        
        Print(Times[0][3]); // this does not cause an error as CurrentBars[0] is equal to or greater than 3
        
        Print(Times[1][4]); // this does not cause an error as CurrentBars[1] is equal to or greater than 4
        
        Print(Times[0][5]); // this line of code will cause an invalid index error. When CurrentBars[0] is 4 there is no bar 5 bars ago​
        ​​

        Further, Series are updated when OnBarUpdate() is updated. Attempting to use a Series or Series dependent object (such as Draw methods) from a non-data-driven method, including from: the OnRender() method, a button click event handler, or a timer elapsed method hander, will result in an indexing error as the Series indexes are not up-to-date.

        Call TriggerCustomEvent() from any non-data-driven method to update all Series before using the series in the coded logic.

        NinjaScript > Language Reference > Common > TriggerCustomEvent()

        This also applies to barsAgo values supplied to Drawing Objects.

        Code:
        if (CurrentBar < 1)
        return;
        
        Draw.Dot(this, “myDot”, 5, Brushes.Blue); // this line of code will cause an invalid index error as the start barsAgo parameter is 5 when the CurrentBar processed is bar 1


        Indexing errors can also occur when using an invalid index with arrays, lists, and other collections where the index used is equal to or larger than the .Count() or .Length of the collection, as well as with method calls that use indexes such as: <string>.Subtring() which uses a string position index, <string>.Format() which uses indexes for format items in the string (placeholders {0}), and Draw methods which use barsAgo indexes.

        Dotnetperls is a 3rd party educational site with an in-depth review of arrays.

        Create and loop over a string array. Access array Length and get elements at indexes.



        The help guide discusses ‘Make sure you have enough bars in the data series you are accessing’ .
        NinjaScript > Educational Resources > Tips > Make sure you have enough bars in the data series you are accessing

        This forum post also discusses this error:

        Hello, I want to create an indicator that show data in a chart but calculate in other charttime different to the time of the chart where is showed. For example:
        Gaby V.NinjaTrader Customer Service

        Comment


          #5
          Thanks for your detailed reply Gaby, got it.
          One last question: my code now works fine and the arrows are drawn correctly, but the alert is triggered on every bar.
          Any idea on why that's the case?

          Thanks so much

          Comment


            #6
            It would be helpful to print out the values from the condition that triggers the alert so we can see why this is evaluating as true.

            Print the time of the bar and all values used in the conditions. Include labels for all values and comparison operators.

            Let me know if you need any assistance creating a print.

            Save the output from the output window to a text file and provide this with your reply.

            I'll be happy to assist with analyzing the output.​​
            Gaby V.NinjaTrader Customer Service

            Comment


              #7
              Originally posted by NinjaTrader_Gaby View Post
              It would be helpful to print out the values from the condition that triggers the alert so we can see why this is evaluating as true.

              Print the time of the bar and all values used in the conditions. Include labels for all values and comparison operators.

              Let me know if you need any assistance creating a print.

              Save the output from the output window to a text file and provide this with your reply.

              I'll be happy to assist with analyzing the output.​​
              Dear Gaby, Ill gladly accept your offer to assist with creating a print!

              Comment


                #8
                Hello laocoon,

                What is the condition that triggers the alert? If you would please share this and I can provide a sample print based on that.

                I look forward to assisting.
                Gaby V.NinjaTrader Customer Service

                Comment


                  #9
                  Originally posted by NinjaTrader_Gaby View Post
                  Hello laocoon,

                  What is the condition that triggers the alert? If you would please share this and I can provide a sample print based on that.

                  I look forward to assisting.
                  Of course Gaby, here you go:

                  if (High[0] < High[1])
                  Draw.ArrowUp(this, "MyUpArrow" + CurrentBar, true, 0, Low[0] - 3 * TickSize, Brushes.Black);​

                  Thanks so much

                  Comment


                    #10
                    To follow up on my previous post, the arrows are drawn correctly but the alert is triggered on every bar.
                    The way I see it, the Draw function takes the prior "if" condition into account, but the PlaySound function does not.
                    Is there a way to add the PlaySound function to the Draw function, so that both are only triggered if the prior condition is met?
                    I tried Draw.Arrow....... && PlaySound.... but it doesn't compile.

                    if (High[0] < High[1] )
                    Draw.ArrowUp(this, "MyUpArrow" + CurrentBar, true, 0, Low[0] - 3 * TickSize, Brushes.Black);
                    PlaySound(NinjaTrader.Core.Globals.InstallDir + @"\sounds\Alert1.wav");​
                    Last edited by laocoon; 02-29-2024, 05:42 AM.

                    Comment


                      #11
                      Hello laocoon,

                      Thank you for your post.

                      Both the draw function and the PlaySound() should only happen if the condition is true. Can you send a reduced script that reproduces the issue so I can test on my end?
                      Gaby V.NinjaTrader Customer Service

                      Comment


                        #12
                        Originally posted by NinjaTrader_Gaby View Post
                        Hello laocoon,

                        Thank you for your post.

                        Both the draw function and the PlaySound() should only happen if the condition is true. Can you send a reduced script that reproduces the issue so I can test on my end?
                        Sure, I'll do that, thanks Gaby.
                        In what format should I send it? Should I post it here on the forum or send it directly to you?

                        Thanks

                        Comment


                          #13
                          Hello laocoon,

                          To export a NinjaTrader 8 NinjaScript so this can be shared and imported by the recipient do the following:
                          1. Click Tools -> Export -> NinjaScript Add-on...
                          2. Click the 'add' link -> check the box(es) for the script(s) and reference(s) you want to include
                          3. Click the 'Export' button
                          4. Enter a unique name for the file in the value for 'File name:'
                          5. Choose a save location -> click Save
                          6. Click OK to clear the export location message
                          By default your exported file will be in the following location:
                          • (My) Documents/NinjaTrader 8/bin/Custom/ExportNinjaScript/<export_file_name.zip>
                          Below is a link to the help guide on Exporting NinjaScripts.


                          Once exported, please attach the file as an attachment to your reply.

                          You can post it here or if you would like to continue privately over email, send us an email at scriptingsupport[AT]ninjatrader[DOT]com, in your email include the message "04260712 Attn Gaby".​
                          Gaby V.NinjaTrader Customer Service

                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by fx.practic, 10-15-2013, 12:53 AM
                          5 responses
                          5,406 views
                          0 likes
                          Last Post Bidder
                          by Bidder
                           
                          Started by Shai Samuel, 07-02-2022, 02:46 PM
                          4 responses
                          98 views
                          0 likes
                          Last Post Bidder
                          by Bidder
                           
                          Started by DJ888, Yesterday, 10:57 PM
                          0 responses
                          8 views
                          0 likes
                          Last Post DJ888
                          by DJ888
                           
                          Started by MacDad, 02-25-2024, 11:48 PM
                          7 responses
                          160 views
                          0 likes
                          Last Post loganjarosz123  
                          Started by Belfortbucks, Yesterday, 09:29 PM
                          0 responses
                          9 views
                          0 likes
                          Last Post Belfortbucks  
                          Working...
                          X