Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

MACD crossover above 0 (zeroline)

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

    MACD crossover above 0 (zeroline)

    Hi,

    I've been trying to create buy order only when MACD crosses above 0 (zeroline). I'm seeing order is taken when MACD crosses below zeroline also.

    In the attachment, red line trade crossed below 0 so it shouldn't have taken. Green line trade is correct as it crosses above 0 line.

    Can anyone suggest me to fix this?

    Code:
    if (CrossAbove(MACD(12, 26, 9).Diff, 0, 1))
    {
        EnterLong(1, "Buy");
    }
    
    if ((CrossBelow(MACD(Close, 12, 26, 9).Diff, 0, 1))
    && (Position.MarketPosition == MarketPosition.Long))
    {
        ExitLong(1);
    }
    Last edited by visvabalaji; 04-13-2022, 01:45 AM.

    #2
    Hello visvabalaji,

    Are you running the script OnEachTick or OnPriceChange? From the image it is possible that the condition was true and then became false if you were using OnEachTick/PriceChange. With OnBarClose that should be false.

    Another factor could be if you manually applied the indicator, you need to make sure the strategy is adding the indicator in order to confirm visually against what the strategy is actually using.



    Comment


      #3
      Thanks for your reply. MACD is added through strategy and still getting the same issue. Attached in the latest screenshot from today's emini chart.

      Red marked region shouldn't be taken as MACD crosses below 0 line but the trade was taken.
      Green marked region is correct as MACD crosses above 0 line.

      I think strategy need to find out if the very next bar, after MACD crossover lies under 0, I shouldn't be taken the trade.

      Appreciate your help on this.

      Code:
      protected override void OnStateChange()
      {
        if (State == State.SetDefaults)
          {
            TimeInForce = TimeInForce.Gtc;
            IsExitOnSessionCloseStrategy = true;
            Calculate = Calculate.OnBarClose;
            ExitOnSessionCloseSeconds = 30;
            OrderFillResolution = OrderFillResolution.Standard;
            RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
      
            // This strategy has been designed to take advantage of performance gains in Strategy Analyzer optimizations
            // See the Help Guide for additional information
            IsInstantiatedOnEachOptimizationIteration = false;
          }
      else if (State == State.DataLoaded)
         {
           MACD1 = MACD(12, 26, 9);
           AddChartIndicator(MACD1);
         }
      }
      
      protected override void OnBarUpdate()
      {
        if (CrossAbove(MACD(12, 26, 9).Diff, 0, 1))
          {
              EnterLong(1, "Buy");
          }
      
        if ((CrossBelow(MACD(Close, 12, 26, 9).Diff, 0, 1))
          && (Position.MarketPosition == MarketPosition.Long))
          {
              ExitLong(1);
          }
      }

      Comment


        #4
        Hello visvabalaji,

        It looks like this post involves a script which you have another post open for currently. I would suggest to consolidate the questions for this script into the other post. I provided some debug instructions on how you can use print to observe what values the strategy is seeing. That would be the next step to identify why its conditions are happening at a specific time.

        Comment


          #5
          How to I use the GUI Strategy Builder to create a strategy where one of the conditions is where a line crosses over the zero line?

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Geovanny Suaza, 02-11-2026, 06:32 PM
          0 responses
          571 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
          549 views
          1 like
          Last Post RFrosty
          by RFrosty
           
          Working...
          X