Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Alert on first instance of multiple indicators meeting criteria

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

    Alert on first instance of multiple indicators meeting criteria

    I am trying to build an alert for when multiple indicators meet a criteria for the first time. I would like the indicator to alert on the first instance that all of the Momentum indicators are above 0. Then if any of the indicators go below 0 and then go back above zero I'd like it to alert again. Same with if they all go below zero.

    I have played around with some true/false statements and although I've had luck with them in the past can't get them to work this time around. What am I missing?

    I also have an array that I've written out long form. 1.) This isn't working anyway and 2.)I know there has to be an easier way of doing this.

    Thanks in advance!

    Code:
    public class Toggle : Indicator
    {
    
    
    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    
    Description = @"Alert when action takes place.";
    Name = "Toggle";
    
    xRisingLast = false;
    xFallingLast = false;
    
    Calculate = Calculate.OnBarClose;
    IsOverlay = true;
    IsSuspendedWhileInactive = false;
    
    }
    
    }
    
    protected override void OnBarUpdate()
    {
    if(CurrentBars[0] < 100)
    return;
    
    
    if (
    Momentum(100)[0] > 0
    &&Momentum(116)[0] > 0
    &&Momentum(133)[0] > 0
    &&Momentum(150)[0] > 0
    )
    {
    
    // Draw.TriangleUp(this,"Up1"+CurrentBar,true,0,Close[0]-60,Brushes.MediumSpringGreen);
    // PlaySound (@"C:\Program Files (x86)\NinjaTrader 8\sounds\Alert2.wav");
    xRisingLast=true;
    xFallingLast=false;
    BarBrushes[0] = Brushes.DeepSkyBlue;
    }
    
    
    
    
    if (
    Momentum(100)[0] < 0
    &&Momentum(116)[0] < 0
    &&Momentum(133)[0] < 0
    &&Momentum(150)[0] < 0
    )
    {
    
    // Draw.TriangleDown(this,"Down1"+CurrentBar,true,0,C lose[0]+60,Brushes.OrangeRed);
    // PlaySound (@"C:\Program Files (x86)\NinjaTrader 8\sounds\Alert2.wav");
    xRisingLast=false;
    xFallingLast=true;
    BarBrushes[0] = Brushes.Magenta;
    }
    
    
    
    if (
    Momentum(100)[0] > 0
    &&Momentum(116)[0] < 0
    &&Momentum(133)[0] < 0
    &&Momentum(150)[0] < 0
    ||
    Momentum(100)[0] > 0
    &&Momentum(116)[0] > 0
    &&Momentum(133)[0] < 0
    &&Momentum(150)[0] < 0
    ||
    Momentum(100)[0] > 0
    &&Momentum(116)[0] > 0
    &&Momentum(133)[0] > 0
    &&Momentum(150)[0] < 0
    ||
    Momentum(100)[0] > 0
    &&Momentum(116)[0] < 0
    &&Momentum(133)[0] > 0
    &&Momentum(150)[0] < 0
    ||
    Momentum(100)[0] > 0
    &&Momentum(116)[0] < 0
    &&Momentum(133)[0] < 0
    &&Momentum(150)[0] > 0
    ||
    Momentum(100)[0] > 0
    &&Momentum(116)[0] > 0
    &&Momentum(133)[0] > 0
    &&Momentum(150)[0] < 0
    ||
    Momentum(100)[0] < 0
    &&Momentum(116)[0] < 0
    &&Momentum(133)[0] < 0
    &&Momentum(150)[0] > 0
    ||
    Momentum(100)[0] < 0
    &&Momentum(116)[0] < 0
    &&Momentum(133)[0] > 0
    &&Momentum(150)[0] > 0
    ||
    Momentum(100)[0] < 0
    &&Momentum(116)[0] > 0
    &&Momentum(133)[0] > 0
    &&Momentum(150)[0] > 0
    ||
    Momentum(100)[0] < 0
    &&Momentum(116)[0] > 0
    &&Momentum(133)[0] < 0
    &&Momentum(150)[0] < 0
    ||
    Momentum(100)[0] < 0
    &&Momentum(116)[0] > 0
    &&Momentum(133)[0] < 0
    &&Momentum(150)[0] > 0
    ||
    Momentum(100)[0] < 0
    &&Momentum(116)[0] > 0
    &&Momentum(133)[0] > 0
    &&Momentum(150)[0] < 0
    
    )
    {
    xRisingLast=false;
    xFallingLast=false;
    }

    #2
    Hello BReal,

    The code you have shown would be what would be suggested for this type of use case. You would need your initial condition which sets a bool variable to true based on the conditions you mentioned. A second condition would be needed to reset it to false when the indicators cross back below.

    I would suggest to simplify the logic you made to only handle 1 situation so you can better understand what is happening and what the problem may be. For example remove the logic for falling for now and just work on the rising conditions.

    Your first condition seems correct, you require all indicators to go above 0 before setting the variable to true by using AND or &&.

    Your second condition for the reset of rising is not correct for what you described, if ANY of the indicators below 0 should reset it your condition should look like this:


    Code:
    if ( Momentum(100)[0] > 0 && Momentum(116)[0] > 0 && Momentum(133)[0] > 0 && Momentum(150)[0] > 0 )
    {
        xRisingLast=true;
     } 
    else if (Momentum(100)[0] < 0 || Momentum(116)[0] < 0 || Momentum(133)[0] < 0 || Momentum(150)[0] < 0)
    {
        xRisingLast=false;
    }
    This says if all the indicators are above 0 set it to true, if any of the indicators are less than 0 set it to false.


    To test this type of condition Prints would be much easier than alerts or drawing so you can output the values for every bar. For example:

    Code:
    Print(Time[0] + " " + Momentum(100)[0] + " > 0 " + Momentum(116)[0] + " > 0 " + Momentum(133)[0] + " > 0 " + Momentum(150)[0] + " > 0" );
    if ( Momentum(100)[0] > 0 && Momentum(116)[0] > 0 && Momentum(133)[0] > 0 && Momentum(150)[0] > 0 )
    {..........
    You could see what each indicator value was in addition to the Time to correlate that data with bars on the chart.




    Comment


      #3
      I think I figured out what I was looking for! Thank you very much for your help. It got me over the hump. I had to go back to some old code of mine to find the "xRisingLast != true;" logic and it all clicked again. But your "else if" section really helped me out.

      Thanks again!!!!

      Code:
      protected override void OnBarUpdate()
      {
      if(CurrentBars[0] < 100)
      return;
      
      
      if ( Momentum(150)[0] > 0
      && Momentum(166)[0] > 0
      && Momentum(183)[0] > 0
      && Momentum(200)[0] > 0
      && xRisingLast != true
      )
      {
      xRisingLast=true;
      BarBrushes[0] = Brushes.DeepSkyBlue;
      }
      
      else if (Momentum(150)[0] < 0
      || Momentum(166)[0] < 0
      || Momentum(183)[0] < 0
      || Momentum(200)[0] < 0
      )
      
      {
      xRisingLast=false;
      }
      
      if ( Momentum(150)[0] < 0
      && Momentum(166)[0] < 0
      && Momentum(183)[0] < 0
      && Momentum(200)[0] < 0
      && xFallingLast != true
      )
      {
      xFallingLast=true;
      BarBrushes[0] = Brushes.Magenta;
      }
      
      else if (Momentum(150)[0] > 0
      || Momentum(166)[0] > 0
      || Momentum(183)[0] > 0
      || Momentum(200)[0] > 0
      )
      
      {
      xFallingLast=false;
      }
      
      
      }

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by Geovanny Suaza, 02-11-2026, 06:32 PM
      0 responses
      580 views
      0 likes
      Last Post Geovanny Suaza  
      Started by Geovanny Suaza, 02-11-2026, 05:51 PM
      0 responses
      335 views
      1 like
      Last Post Geovanny Suaza  
      Started by Mindset, 02-09-2026, 11:44 AM
      0 responses
      102 views
      0 likes
      Last Post Mindset
      by Mindset
       
      Started by Geovanny Suaza, 02-02-2026, 12:30 PM
      0 responses
      554 views
      1 like
      Last Post Geovanny Suaza  
      Started by RFrosty, 01-28-2026, 06:49 PM
      0 responses
      552 views
      1 like
      Last Post RFrosty
      by RFrosty
       
      Working...
      X