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 pvincent, 06-23-2022, 12:53 PM
      14 responses
      238 views
      0 likes
      Last Post Nyman
      by Nyman
       
      Started by TraderG23, 12-08-2023, 07:56 AM
      9 responses
      383 views
      1 like
      Last Post Gavini
      by Gavini
       
      Started by oviejo, Today, 12:28 AM
      0 responses
      1 view
      0 likes
      Last Post oviejo
      by oviejo
       
      Started by pechtri, 06-22-2023, 02:31 AM
      10 responses
      125 views
      0 likes
      Last Post Leeroy_Jenkins  
      Started by judysamnt7, 03-13-2023, 09:11 AM
      4 responses
      59 views
      0 likes
      Last Post DynamicTest  
      Working...
      X