Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

NinjaScript Strategy Code for Indicator

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

    NinjaScript Strategy Code for Indicator

    Hello,

    I created a NinjaScript Strategy to attempt to get some of the code for an indicator I'm wanting to put together.

    I am still learning NinjaScript and C#.

    The Strategy code screenshot that is attached should show what I'm trying to do, in regards to the indicator I want to create.

    I am assuming that I will need to use the Strategy 'if Statements' code in the OnBarUpdate() section for my indicator. Is that correct?

    And to add the indicators to be analyzed to my new indicator, I am assuming that I would need to add the State == State.State.DataLoaded 'else if' code on my Strategy, to my indicator, is that correct?

    Would I also need to add the 'private' code in the 'public class' that is shown in my Strategy screenshot, to my indicator?

    Anything else I would need to add to my new indicator to get it to work?

    Thanks,
    Attached Files

    #2
    Hello i2w8am9ii2,

    Thanks for your post.

    Yes, you can use all of the code in the OnBarUpdate() in the new indicator as none of the code would be strategy specific (such as EnterLong(), etc). Technically, you would not need if (BarsInProgress != 0) return; unless you have added another data series. The Strategy Builder just adds that automatically.

    Yes, you would need the private use of the various indicators called. Technically you could call the indicator(s) directly in OnBarUpdate() and do not need to declare private instances and then initialize them in State.DataLoaded , however this is the preferred practice (as the strategy builder shows) as it prevents creating multiple copies of the indicator thus improving performance. This would be a good chance for you to review a related section of the help guide, please see "Performance practices" here: https://ninjatrader.com/support/help..._practices.htm to reinforce what you are doing.

    Yes, you would need to add the else if (State == State.DataLoaded) and the definitions for the private versions of the indicators used.

    That should be all you need. Just be careful to make sure that for each opening "{" that you have a closing "}", this is the one error that generates many error messages and happens a lot when copy and pasting code sections.

    Comment


      #3
      Hi Paul,

      Thanks so much for your help.

      I scanned through the Performance Practices, and much of it is still over my head at this time, but I'm continuing to learn C#. I will start learning OOP, C# Generics and more soon, then hopefully such content will be something I can comprehend.

      I started building my new indicator, based on your info, and I have just one error at this time. And I think it is an easy one to fix. It is Code CS0102 and I think the problem is that I have private EMA EMA; twice, one on row 48 and one on row 49.

      Should I delete one of them?

      Attached are screenshots showing the code that I added. If you find something I forgot or that I should change or add, please let me know.

      Thanks again Paul. Your help is very much appreciated.
      Attached Files

      Comment


        #4
        Hello i2w8am9ii2,

        Thanks for your reply.

        Please review your original Strategy builder code, there are 2 different EMAs used and they need to be named differently, I would recommend removing what you have Copy/pasted and recopy-paste the original code.

        Reference: You should not use private EMA EMA; Instead, use as the strategy builder script shows, private EMA EMA1; You always want to call the local instance of the indicator something else( to avoid confusion). So, the name of EMA1 needs to match what you have when you declare it, and in state.DataLoaded and in the OnBarUpdate. (Same for the other indicator local instances as well).

        Background: Using private EMA EMA1 is creating your own instance of the EMA indicator and in the state.DataLoaded you initializing EMA1 to have the EMA parameters of EMA(Close, 5);

        Comment


          #5
          Hi Paul,

          I now have four errors based on line 86, 87, 88 and 89 (CS0118 Error). Screenshots attached.

          I added the private EMA EMA1; as shown in the screenshot attached. That did not create new errors.

          What do you suggest I do at this point?
          Attached Files
          Last edited by i2w8am9ii2; 09-04-2018, 09:26 AM.

          Comment


            #6
            Hello i2w8am9ii2,

            Thanks for your reply.

            I'm sorry, I was not clear, I was using EMA1 as an educational example of what needs to be done, not as the only change to make. You were looking for a specific direction and I was trying to help you understand what was going on.

            Without going through each bit, I refer back to my original recommendation:"...I would recommend removing what you have Copy/pasted and recopy-paste the original code." Doing this will keep all of the strategy builder created code connected and should get you to easy street.

            Comment


              #7
              Hi Paul,

              Thanks for the info. Yes, I do like easy street much better than struggle street.

              I copied and pasted the original code from the Strategy and now I just have one error, it is a Statement expected error.

              Screenshot attached.

              Please let me know specifically how to fix that error. I'm still new to this.

              Thanks Paul,
              Attached Files

              Comment


                #8
                Hi Paul,

                Okay, I saw duplicate code and commented out one of the statements and now I do not get any compile errors.

                Screenshot attached.
                Attached Files

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                0 responses
                561 views
                0 likes
                Last Post Geovanny Suaza  
                Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                0 responses
                325 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
                547 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