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

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.



    JesseNinjaTrader Customer Service

    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.

        JesseNinjaTrader Customer Service

        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 chbruno, 04-24-2024, 04:10 PM
          4 responses
          50 views
          0 likes
          Last Post chbruno
          by chbruno
           
          Started by TraderG23, 12-08-2023, 07:56 AM
          10 responses
          398 views
          1 like
          Last Post beobast
          by beobast
           
          Started by lorem, Yesterday, 09:18 AM
          5 responses
          20 views
          0 likes
          Last Post NinjaTrader_ChelseaB  
          Started by WHICKED, Today, 12:56 PM
          2 responses
          15 views
          0 likes
          Last Post WHICKED
          by WHICKED
           
          Started by Felix Reichert, Today, 02:12 PM
          0 responses
          4 views
          0 likes
          Last Post Felix Reichert  
          Working...
          X