Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Background Coloring Indicator Not Working

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

    Background Coloring Indicator Not Working

    Hi would anyone know why this indicator works on some charts but fails to load on others?
    Thanks

    Code:
    protected override void OnBarUpdate()
    {
    BackBrush = Brushes.PaleGreen;
    BackBrush = null;
    
    if (EMA(30)[0] >= Close[0] && EMA(30)[0] >= Close[1] && EMA(30)[0] >= Close[2])
    {
    BackBrush = Brushes.LightPink;
    }
    else if (EMA(30)[0] <= Close[0] && EMA(30)[0] <= Close[1] && EMA(30)[0] <= Close[2])
    {
    BackBrush = Brushes.DarkSeaGreen;
    }
    else
    {
    BackBrush = Brushes.PapayaWhip;
    }
    }

    #2
    Hello redart1021,

    Thanks for your post and welcome to the NinjaTrader forums!

    When an indicator is not working as expected, please check the "log" tab of the NinjaTrader control center.

    Based on the code you have shared I suspect that you would see an error messages about bar indexing.

    What happens when you load a script is that it will start processing the very first bar in the data series that is applied to the chart. If on the first bar of data processed the script tries to access a prior bar (I see [1] and [2] in your code) then it will fail because those bars are not available at that time.

    The reason it is inconsistent ( works on some charts but fails to load on others) is related to the conditions you have. If part of the condition is not true the rest is not evaluated so in that case you can get lucky and see the indicator. I would expect that the indicator could fail on any given chart on any given day when it is loaded.

    What you need to do is to prevent your code from processing until, from what I see, at least 2 bars have been loaded. In OnBarUpdate() before your code accesses any previous bars, add this line of code:

    if (CurrentBar < 2) return; // do not process below this line until at least 2 bars

    You could also use 20 bars as a number because no indicator will print before 20 bars have been loaded.

    Reference: https://ninjatrader.com/support/help...currentbar.htm
    Paul H.NinjaTrader Customer Service

    Comment


      #3
      Thanks Paul. This worked great

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by tonynt, Today, 09:39 AM
      0 responses
      5 views
      0 likes
      Last Post tonynt
      by tonynt
       
      Started by ervin, Today, 09:23 AM
      0 responses
      5 views
      0 likes
      Last Post ervin
      by ervin
       
      Started by HansLenders, Today, 09:15 AM
      1 response
      4 views
      0 likes
      Last Post NinjaTrader_PatrickG  
      Started by sdauteuil, Today, 08:23 AM
      1 response
      10 views
      0 likes
      Last Post NinjaTrader_PaulH  
      Started by tonynt, Today, 08:27 AM
      3 responses
      23 views
      0 likes
      Last Post NinjaTrader_Heath  
      Working...
      X