Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Track Where MACD Crossover Occured

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

    Track Where MACD Crossover Occured

    Per the attached image below. I want to be alerted whenever a MACD lines crossover occurs below the zero line as seen in the pic below (at point "B") and the last crossover occurred above the zero line (Point A). In other words, I want to be alerted based on the location of the last two MACD crossovers.
    Likewise, I also want to be alerted when the reverse occurs.

    #2
    Hi cryfgg, thanks for writing in. You can use C# boolean variables or counters to keep track of two crossovers e.g.

    Code:
    private bool flag1 = false;
    private bool flag1 = false;
    private int savedBar = -1;
    
    OnBarUpdate()
    {
        if(!flag1 && <MACD Cross>)
        {
            flag1 = true;
        }
    
        if(flag1 && !flag2 && <MACD Cross>)
        {
            flag2 = true;
            savedBar = CurrentBar;
            Print("Two MACD Signals");
        }
    
        if(CurrentBar == savedBar+1)
        {
            flag1 = false;
            flag2 = false;
            savedBar = -1;
        }
    }
    I omitted the CrossAbove/CrossBelow methods, the documentation for these methods is available in the help guide.

    Kind regards,
    -ChrisL

    Comment


      #3
      Originally posted by NinjaTrader_ChrisL View Post
      Hi cryfgg, thanks for writing in. You can use C# boolean variables or counters to keep track of two crossovers e.g.

      Code:
      private bool flag1 = false;
      private bool flag1 = false;
      private int savedBar = -1;
      
      OnBarUpdate()
      {
      if(!flag1 && <MACD Cross>)
      {
      flag1 = true;
      }
      
      if(flag1 && !flag2 && <MACD Cross>)
      {
      flag2 = true;
      savedBar = CurrentBar;
      Print("Two MACD Signals");
      }
      
      if(CurrentBar == savedBar+1)
      {
      flag1 = false;
      flag2 = false;
      savedBar = -1;
      }
      }
      I omitted the CrossAbove/CrossBelow methods, the documentation for these methods is available in the help guide.

      Kind regards,
      -ChrisL


      Wha'ts the function these codes:


      !flag1 // checks to see if flag1 is false?

      "savedBar = -1;" // ??

      if(CurrentBar == savedBar+1) // ????
      {
      flag1 = false; // To reset the boolean?
      flag2 = false; // To reset the boolean?
      savedBar = -1; // ????

      How do I use counter to track the crossovers?

      Lastly, is it possible to save/compare the time on the chart when both crossover occurred?
      Last edited by cryfgg; 04-20-2022, 12:26 PM.

      Comment


        #4
        Hi cryfgg, the savedbar and resetting the flags back to false is just to reset the entire setup once two MACD signals have been found. You can use a counter by setting up a private int value and incrementing it every time the MACD condition is triggered. To save the time when the MACD condition is reached, set up a DateTime variable e.g.

        private DateTime savedTime;

        OnBarUpdate()
        {

        if(<MACD Cross>)

        {
        savedTime = Time[0];
        }

        }

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by CarlTrading, 03-31-2026, 09:41 PM
        1 response
        81 views
        1 like
        Last Post NinjaTrader_ChelseaB  
        Started by CarlTrading, 04-01-2026, 02:41 AM
        0 responses
        42 views
        0 likes
        Last Post CarlTrading  
        Started by CaptainJack, 03-31-2026, 11:44 PM
        0 responses
        64 views
        2 likes
        Last Post CaptainJack  
        Started by CarlTrading, 03-30-2026, 11:51 AM
        0 responses
        68 views
        0 likes
        Last Post CarlTrading  
        Started by CarlTrading, 03-30-2026, 11:48 AM
        0 responses
        55 views
        0 likes
        Last Post CarlTrading  
        Working...
        X