Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Horizontal Line Alert

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

    Horizontal Line Alert

    I need assistance adding an alert to a Draw.HorizontalLine If the condition below is met. (when 20ma Crosses over 50ma):
    if ((CrossAbove(EMA5, EMA3, 1))
    && (EMA5[1] <= EMA3[1])
    && (EMA5[2] < EMA3[2]))

    {
    If the above condition is met; I want to add a Horizontal Line at the price at which the crossover occurred (i.e. 20ma >= 50ma for bullish crossover or 20ma<= 50ma for bearish). I also want to be alerted whenever price pulls back and touches the horizontal line.

    Draw.HorizontalLine(....);

    }
    See attached image for more clarification.

    #2
    Hi cryfgg, thanks for posting. If the script is running OnBarClose, the condition will trigger on the first tick of the next bar, so you will need to draw the line using the Close[1] price to reference the price the cross happened at. Otherwise, you will need to run the script OnEachTick to get the exact price the cross happens on. To add an alert to the line, you can save the price where you draw the line and call the Alert() method when Close[0] == SavedPrice, or when Close[0] crosses above/below using the CrossAbove/CrossBelow methods. It can all be done using C# logic e.g.

    Code:
    private double savedPrice;
    private bool isLineDrawn = false;
    
    OnBarUpdate()
    {
        if(<condition>)
        {
            Draw.HorizontalLine(..., Close[1], ...);
            isLineDrawn = true;
            savedPrice = Close[1];
        }
    
        if(isLineDrawn && CrossBelow(Close, savedPrice))
        {
            Alert("myAlert", Priority.High, "Reached threshold", NinjaTrader.Core.Globals.InstallDir+@"\sounds\Alert4.wav", 10, Brushes.Black, Brushes.Yellow);
        }
    }

    Comment


      #3
      I made the changes based on your recommendation, but it doesn't work quite well because the alert also triggers when price did not crossover the <savedPricevariable>. Below is my code; plus I've added "// - comment syntax" to some variation of my code in an effort to fix this issue:

      HTML Code:
       if ((CrossAbove(EMA5, EMA3, 1)) 
      && (EMA5[1] <= EMA3[1])
      && (EMA5[2] < EMA3[2]))
      
      {
         Alert(...);
         Draw.HorizontalLine(this, @"Horizontal Line_1", EMA3[0], false,"hori");
         isLineDrawn = true;
         savedPrice = EMA3[0];
      }
      
      
      // if(isLineDrawn && CrossBelow(Close, savedPrice, 1)
      // && High[1] >= savedPrice
      if (Close[0] <= savedPrice
      && (Close[1] >= savedPrice))
      
      {
         Alert(....);
      }
      
      
      
      if ((CrossBelow(EMA5, EMA3, 1)) //(CrossBelow(MACD2.Default, MACD2.Avg, 4))
      && (EMA5[2] >= EMA3[2])
      && (EMA5[3] > EMA3[3]))
      
      {
         Alert(....);
         Draw.HorizontalLine(this, @"Horizontal Line_1", EMA3[0], false,"hori");
         isLineDrawn = true;
         savedPrice = EMA3[0];
      }

      Comment


        #4
        Hi cryfgg, thanks for the follow up.

        The best way to solve unexpected results is to use Print to see why a condition becomes true when it does. This can help to bridge the gap between expected results and actual results e.g.


        if (Close[0] <= savedPrice && (Close[1] >= savedPrice))
        {
        Print("Alert Triggered");
        Print("Close[0] " + Close[0]);
        Print("Close[1] " + Close[1]);
        Print("savedPrice" + savedPrice);
        Alert(....);
        }

        Another good tool to use is boolean variables that can act as switches that can be turned on and off as I showed with IsLinedrawn.

        Kind regards,
        -ChrisL

        Comment


          #5
          Thanks... i was able to figure out the issue.

          Thanks

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Geovanny Suaza, 02-11-2026, 06:32 PM
          0 responses
          572 views
          0 likes
          Last Post Geovanny Suaza  
          Started by Geovanny Suaza, 02-11-2026, 05:51 PM
          0 responses
          331 views
          1 like
          Last Post Geovanny Suaza  
          Started by Mindset, 02-09-2026, 11:44 AM
          0 responses
          101 views
          0 likes
          Last Post Mindset
          by Mindset
           
          Started by Geovanny Suaza, 02-02-2026, 12:30 PM
          0 responses
          549 views
          1 like
          Last Post Geovanny Suaza  
          Started by RFrosty, 01-28-2026, 06:49 PM
          0 responses
          550 views
          1 like
          Last Post RFrosty
          by RFrosty
           
          Working...
          X