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

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 carnitron, Today, 08:42 PM
      0 responses
      5 views
      0 likes
      Last Post carnitron  
      Started by strategist007, Today, 07:51 PM
      0 responses
      6 views
      0 likes
      Last Post strategist007  
      Started by StockTrader88, 03-06-2021, 08:58 AM
      44 responses
      3,970 views
      3 likes
      Last Post jhudas88  
      Started by rbeckmann05, Today, 06:48 PM
      0 responses
      8 views
      0 likes
      Last Post rbeckmann05  
      Started by rhyminkevin, Today, 04:58 PM
      4 responses
      58 views
      0 likes
      Last Post dp8282
      by dp8282
       
      Working...
      X