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

help me understand Plot0.Set() better

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

    help me understand Plot0.Set() better

    Question 1:
    This code works great:
    protected override void OnBarUpdate()
    {
    Plot0.Set(EMA(21)[0]);
    }
    Now all I do is change the "[0]" to "[1]".
    This code doesn't plot anything... just blank... not even a zero-line:
    protected override void OnBarUpdate()
    {
    Plot0.Set(EMA(21)[1]);
    }
    why doesn't it plot anything?
    ------------------------------------------------------------------------------------------------------------
    Question 2:
    This code works great on "/ES 09-16" between 6AM and 2PM pacific time 8/19/16:
    protected override void OnBarUpdate()
    {
    double v1;
    if (EMA(21)[0] > 2178.5) {v1 = 1.0;} else {v1 = 0.0;}
    Plot0.Set(v1);
    }

    Again, just change the "[0]" to "[1]" and, as expected...
    This code doesn't plot anything... just blank... not even a zero-line:
    protected override void OnBarUpdate()
    {
    double v1;
    if (EMA(21)[1] > 2178.5) {v1 = 1.0;} else {v1 = 0.0;}
    Plot0.Set(v1);
    }

    Now add in the ADX(14)[0] < 50 qualification to the if statement, and, voila...
    This code works great:
    protected override void OnBarUpdate()
    {
    double v1;
    if (ADX(14)[0] < 50 && EMA(21)[1] > 2178.5) {v1 = 1.0;} else {v1 = 0.0;}
    Plot0.Set(v1);
    }
    why does adding the "ADX(14)[0] < 50" clause suddenly perk it up again?

    #2
    Hello jalexan1,

    Thank you for writing in.

    Question 1

    Before attempting to access EMA(21)[1], you'll need to ensure that you have at least two bars on your chart. EMA(21)[1] would be grabbing the EMA value from the previous bar. When your script attempts to access a previous bar's value when evaluating on the first bar, that value does not exist since there isn't a bar previous to the first bar on the chart.

    You will want to use this code to prevent your script from evaluating EMA(21)[1] before two bars have elapsed:

    Code:
    if (CurrentBar < 1)
         return;
    More information about this concept can be found here: http://ninjatrader.com/support/forum...ead.php?t=3170

    Question 2

    When using && in your if statement, both conditions specified need to be true.

    The reason why your indicator does not run into any error is because, at the time it is checking for ADX(14)[0] < 50, it is not true. And because it is not true, there is no need to check if EMA(21)[0] > 2178.5 is true, therefore, preventing any errors from occurring because that second condition is never evaluated in the first place.

    The next time this condition is checked again, at least two bars have elapsed and therefore EMA(21)[1] will not produce any error.
    Zachary G.NinjaTrader Customer Service

    Comment


      #3
      thanks

      very subtle, very tricky, very elegant, and VERY WELL EXPLAINED!!! thanks so much, Zachary. : )

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by cls71, Today, 04:45 AM
      0 responses
      1 view
      0 likes
      Last Post cls71
      by cls71
       
      Started by mjairg, 07-20-2023, 11:57 PM
      3 responses
      213 views
      1 like
      Last Post PaulMohn  
      Started by TheWhiteDragon, 01-21-2019, 12:44 PM
      4 responses
      544 views
      0 likes
      Last Post PaulMohn  
      Started by GLFX005, Today, 03:23 AM
      0 responses
      3 views
      0 likes
      Last Post GLFX005
      by GLFX005
       
      Started by XXtrader, Yesterday, 11:30 PM
      2 responses
      12 views
      0 likes
      Last Post XXtrader  
      Working...
      X