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

NinjaScript Errors - Learning NinjaScript

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

    NinjaScript Errors - Learning NinjaScript

    Hello,

    I have been learning C# and I am now starting to learn NinjaScript, so that I can work with Strategies.

    I was told to start with NinjaScript indicators, to try to learn NinjaScript.

    I copied the code for the default RSI indicator, from within the NinjaScript Editor, and added code for an alert that I got from your online help files. I added this code just above the closing bracket of the following statement:

    protected override void OnBarUpdate()

    The code that I added was:

    protected override void OnBarUpdate()
    {
    // Generate an alert when the RSI value is greater or equal to 50
    if (RSI(14, 3)[0] >= 50)
    Alert("myAlert", Priority.High, "Potentially Bullish RSI", NinjaTrader.Core.Globals.InstallDir + @"\sounds\Alert1.wav", 10, Brushes.Black, Brushes.Green);
    }


    But after I added that code, I got 13 errors. Screenshot attached.

    How would I go about troubleshooting and fixing those errors? I don't know where to start.

    Thanks,
    Attached Files

    #2
    Issue Fixed, But Missing Parameters

    Hello,

    I discovered the problem, I think.

    I had two instances of the following code:

    protected override void OnBarUpdate()

    I was able to add the indicator to a chart, but I don't see any parameters for configuring the alert.

    Screenshot attached.

    Does it just automatically use what was in the code without the possibility of changing the inputs, the way it is now?

    If so, how can I allow the user (myself) to change the alert background color, font size, audio file, etc.?
    Attached Files

    Comment


      #3
      Okay, I figured out how to make the candlestick change color, based on the RSI, using the code below, but I don't remember how to make the parameters configurable.

      if (RSI(14, 3)[0] >= 50)
      BarBrush = Brushes.Blue;


      Any tips?

      Comment


        #4
        Hello i2w8am9ii2,

        Thanks for your posts.

        Correct, you can only have one OnBarUpdate() method, good job debugging!

        The way to create user adjustable settings is to create "public properties" that provide a means for the user interface to display the property and then take the user set input into the code. There are two suggestions for this:
        1) Take a look at other indicators that have properties and then look for the relevant code in the #region properties and then throughout the code to see how it ties together. Typically you would see the "default" value for the property set in State.Defaults.
        2) I recommend that you use the strategy builder to generate code examples of this which will make it very clear for you.

        Open the strategy builder and skip through until you get to the "Inputs and Variables" window, click 'add" for the "inputs" section, there are 5 input "types", create 1 of each type. Next click on "view code" button, look at the new variables that have default values assigned in state.defaults and open the "region properties" area to see how the properties are created. You can further test by adding the compiled strategy to a chart and see how the user interface works with what you have created. The strategy will not do anything, this is just to show you how to code the properties without the distraction of other code
        Paul H.NinjaTrader Customer Service

        Comment


          #5
          Hi Paul,

          Thanks for your reply, for your encouragement and for the info. I do appreciate it.

          I will give what you suggest a try. Sounds like a good way to go.

          In the meanwhile, yesterday I was able to add a few snippets of code instruction, but I want to know if I made it too repetitive and not in a way that will use resources in the best way or not.

          I used a lot of "if" statements in my code and no "else" or "then" statements. The screenshot will probably be easier to read than the code below.

          But either way, should I have added an "else" or "then" or other such code to my code here?

          {
          // Generate an alert when the RSI value is greater or equal to 50

          if (RSI(14, 3)[0] >= 50)
          Alert("myAlert", Priority.High, "Potentially Bullish RSI", NinjaTrader.Core.Globals.InstallDir + @"\sounds\Alert1.wav", 10, Brushes.Black, Brushes.Green);

          // Generate an alert when the RSI value is less than or equal to 45

          if (RSI(14, 3)[0] <= 45)
          Alert("myAlert", Priority.High, "Potentially Bearish RSI", NinjaTrader.Core.Globals.InstallDir + @"\sounds\Alert2.wav", 10, Brushes.Black, Brushes.DeepPink);

          // Change candlestick color to blue if RSI value is greater than or equal to 50

          if (RSI(14, 3)[0] >= 50)
          BarBrush = Brushes.Blue;

          // Change candlestick color to brown if RSI value is less than or equal to 45

          if (RSI(14, 3)[0] <= 45)
          BarBrush = Brushes.DeepPink;
          Attached Files

          Comment


            #6
            Hello i2w8am9ii2,

            Thanks for your reply.

            The code statements you are asking about are actually what I would recommend you use to begin with as these will make debugging code easier for you. The example you provided would not show any noticeable operational differences if written with if and else if structure.

            You may want to seek information concerning C# code design from other sources, here is a reference for the if else if question: http://www.tutorialsteacher.com/csharp/csharp-if-else
            Paul H.NinjaTrader Customer Service

            Comment


              #7
              Hi Paul,

              Thanks for the info and for the link. I will dig into C# some more.

              I appreciate the help.

              Take care,

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by GussJ, 03-04-2020, 03:11 PM
              16 responses
              3,281 views
              0 likes
              Last Post Leafcutter  
              Started by WHICKED, Today, 12:45 PM
              2 responses
              19 views
              0 likes
              Last Post WHICKED
              by WHICKED
               
              Started by Tim-c, Today, 02:10 PM
              1 response
              9 views
              0 likes
              Last Post NinjaTrader_ChelseaB  
              Started by Taddypole, Today, 02:47 PM
              0 responses
              5 views
              0 likes
              Last Post Taddypole  
              Started by chbruno, 04-24-2024, 04:10 PM
              4 responses
              53 views
              0 likes
              Last Post chbruno
              by chbruno
               
              Working...
              X