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

Should one always define an indicator before using it in a strategy?

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

    Should one always define an indicator before using it in a strategy?

    Hi NinjaTrader support,

    This question refers to this Strategy Builder Screen's trading logic:



    When using the Strategy Builder, I noticed that when an indicator time series, like SMA(Close, 14), or an item of an indicator time series, like EMA(10)[5], is selected as part of the condition logic or is used to set a variable as shown in the example above, that the code output always automatically defines an indicator variable like "private SMA SMA1;" and sets the indicator variable in the "else if (State == State.DataLoaded)" branch of the "OnStateChange()" method with code like "SMA1 = SMA(Close, 14);", and then uses that variable's automatically created variable name at the location in the code where the "SMA(Close, 14)" or "EMA(10)[5]" was selected in the Strategy Builder.

    Here is the relevant code output in the OnBarUpdate section for the strategy whose Strategy Builder screen is shown above:

    Code:
    if ((RSI1.Avg[0] > 80)
    && (CrossAbove(EMA1, SMA1, 1)))
    {
    TestDoubleVar = (EMA2[5] + (SMA2[7])) ;
    }
    QUESTION #1: If the Strategy Builder didn't define variables for these indicators, and instead just inserted "SMA(Close, 14)" or "EMA(10)[5]" at the place in the code where they are used, would this cause logic errors in the code?

    QUESTION #2: For those users who want to program their own NinjaScript strategies without using the Strategy Builder, is it important to always define indicator variables before using them, or is it safe to write something like this in OnBarUpdate?

    Code:
    if ((RSI(Close, 14, 3).Avg[0] > 80)
    && (CrossAbove(EMA(Close, 14), SMA(Close, 14), 1)))
    {
    TestDoubleVar = (EMA(Close, 10)[5] + (SMA(Close, 10)[7])) ;
    }
    One concern I have about the code written directly above is that only the RSI condition is guaranteed to execute on every bar, whereas the EMA and the SMA in the "CrossAbove(EMA(Close, 14), SMA(Close, 14), 1))" condition may not be because this code is written after the short-circuiting C# && boolean operator, and the two indicators that set the variable inside of the "if" block of course will only be executed on the bars where the condition set is true.

    QUESTION #3: If indicators should always be defined before using them instead of using them directly, then how can a NinjaScript programmer safely call an SMA or EMA in a loop, where the "Period" input parameter of the EMA or SMA isn't a literal numeric value but is a variable that changes on each iteration of the loop, like this?

    (Its the same code as above, but I added a loop and I use indicators inside of the loop that are not pre-defined).

    Code:
    if ((RSI1.Avg[0] > 80)
    && (CrossAbove(EMA1, SMA1, 1)))
    {
    TestDoubleVar = (EMA2[5] + (SMA2[7])) ;
    
    double threshold;
    double tempThreshold = 0;
    int countOfValuesForAverage = 0;
    for (int i = 10; i < 50; i+=2)
    {
    tempThreshold += (EMA(Close, i)[5] + SMA(Close, i)[7]) / 2.0;
    countOfValuesForAverage++;
    }
    threshold = tempThreshold / countOfValuesForAverage;
    }


    Thanks in advance!

    EquityTrader

    #2
    Hello EquityTrader,

    Thanks for your post and questions.

    QUESTION #1: If the Strategy Builder didn't define variables for these indicators, and instead just inserted "SMA(Close, 14)" or "EMA(10)[5]" at the place in the code where they are used, would this cause logic errors in the code? No, there would be no errors.

    QUESTION #2: For those users who want to program their own NinjaScript strategies without using the Strategy Builder, is it important to always define indicator variables before using them, or is it safe to write something like this in OnBarUpdate? The way the strategy builder shows would be the preferred means to do so because it is more resource-efficient, but is not a requirement. This is discussed in the Ninjascript best practices document in the section "referencing indicator methods" https://ninjatrader.com/support/help...tm#Performance

    QUESTION #3: If indicators should always be defined before using them instead of using them directly, then how can a NinjaScript programmer safely call an SMA or EMA in a loop, where the "Period" input parameter of the EMA or SMA isn't a literal numeric value but is a variable that changes on each iteration of the loop, like this? In that situation, you likely would have use inline (direct) code as you have shown.
    Paul H.NinjaTrader Customer Service

    Comment


      #3
      Hi NinjaTrader_PaulH,

      Thank you for the excellent answers.

      I'm pleasantly surprised and impressed to hear that:
      1. NinjaScript has the ability to handle dynamic initializations of cached indicators in a loop where the "Period" input parameter changes on each loop
      2. NinjaScript has the ability to handle the proper caching of indicators that are in places in the code that are not called on every bar update.
      3. NinjaScript supports using indicators without first defining them without any caveats regarding scenarios where they won't work right.
      If any of my above statements are false or have caveats or warnings attached (other than the performance issues of not first defining the indicators), could you please correct me?

      QUESTION #4: Performance issues aside, are there literally no known ways that a NinjaScript coder could accidentally create a logic error by using indicators in ways that are valid C# and look correct to a competent programmer but that a highly experienced NinjaScript programmer would "just know" doesn't work due to a quirk in the handling of indicators by the NinjaScript engine, perhaps as a consequence of indicator caching, or maybe due to a problem NinjaScript may have with nesting undefined indicators like "SMA(EMA(SMA(EMA(Close, 8), 10), 12), 14)"?


      Thanks again!

      EquityTrader


      Comment


        #4
        Hello EquityTrader,

        Thanks for your reply.

        Regarding your question 4, that is unanswerable.

        Ninjascript is not bulletproof if that is what you are looking for.

        The best advice I can provide is if your code is not working as expected to plan to spend time debugging, that is part of the programming process, imo.


        Paul H.NinjaTrader Customer Service

        Comment


          #5
          Hi NinjaTrader_PaulH,

          Thanks for your help today.

          Have a great weekend.

          EquityTrader

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by r68cervera, Today, 05:29 AM
          0 responses
          3 views
          0 likes
          Last Post r68cervera  
          Started by geddyisodin, Today, 05:20 AM
          0 responses
          6 views
          0 likes
          Last Post geddyisodin  
          Started by JonesJoker, 04-22-2024, 12:23 PM
          6 responses
          35 views
          0 likes
          Last Post JonesJoker  
          Started by GussJ, 03-04-2020, 03:11 PM
          12 responses
          3,241 views
          0 likes
          Last Post Leafcutter  
          Started by AveryFlynn, Today, 04:57 AM
          0 responses
          7 views
          0 likes
          Last Post AveryFlynn  
          Working...
          X