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 Brevo, Today, 01:45 AM
      0 responses
      3 views
      0 likes
      Last Post Brevo
      by Brevo
       
      Started by aussugardefender, Today, 01:07 AM
      0 responses
      3 views
      0 likes
      Last Post aussugardefender  
      Started by pvincent, 06-23-2022, 12:53 PM
      14 responses
      239 views
      0 likes
      Last Post Nyman
      by Nyman
       
      Started by TraderG23, 12-08-2023, 07:56 AM
      9 responses
      384 views
      1 like
      Last Post Gavini
      by Gavini
       
      Started by oviejo, Today, 12:28 AM
      0 responses
      6 views
      0 likes
      Last Post oviejo
      by oviejo
       
      Working...
      X