Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

DrawDiamond won't show with a second condition

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

    DrawDiamond won't show with a second condition

    I modify the sample code to add a condition of crossing above or below SMA(20) one bar ago before drawing a black diamond. No diamonds are drawn. Any suggestions would be appreciated:

    protected override void OnBarUpdate()
    {
    double prevSma = SMA(20)[1];
    // When the close of the bar crosses above the SMA(50) & above the SMA(20) one bar ago, draw a black diamond
    if (CrossAbove(Close, SMA(50), 1) && Close[1] > prevSma)
    {
    /* Adding the 'CurrentBar' to the string creates unique draw objects because they will all have unique IDs
    Having unique ID strings may cause performance issues if many objects are drawn */
    DrawDiamond("Up Diamond" + CurrentBar, false, 0, SMA(50)[0], Color.Black);
    }

    // But when the close crosses below the SMA(50) and below the SMA(20) one bar ago, draw a black diamond
    else if (CrossBelow(Close, SMA(50), 1) && Close[1] < prevSma)
    {
    /* Adding the 'CurrentBar' to the string creates unique draw objects because they will all have unique IDs
    Having unique ID strings may cause performance issues if many objects are drawn */
    DrawDiamond("Down Diamond" + CurrentBar, false, 0, SMA(50)[0], Color.Black);
    }

    #2
    Hi Richard168,

    The draw statement looks OK. Have you tried any other confirmation that your condition is ever true? You could do this with a simple print statement Print("Condition true"); and then view by clicking Tools menu > Output window.

    Also, check log tab of control center for error messages. If it's an indicator and you index [1], then you will likely need to add this:
    if (CurrentBar < 1) return;
    Ryan M.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_RyanM View Post
      Hi Richard168,

      The draw statement looks OK. Have you tried any other confirmation that your condition is ever true? You could do this with a simple print statement Print("Condition true"); and then view by clicking Tools menu > Output window.

      Also, check log tab of control center for error messages. If it's an indicator and you index [1], then you will likely need to add this:
      if (CurrentBar < 1) return;
      Hi Ryan,

      Thanks for your reply.

      After adding if (CurrentBar < 1) return; the indicator works but not for all the bars. For the more recent bars, the DrawDiamond does not work.There is no error in the error log. Apparently, the condition is not true but the chart says otherwise.

      Any suggestions.

      The following is my new code:

      protected override void Initialize()
      {
      CalculateOnBarClose = true;
      Overlay = true;
      }

      /// <summary>
      /// Called on each bar update event (incoming tick)
      /// </summary>
      protected override void OnBarUpdate()
      {
      if (CurrentBar < 1) return;

      double prevSma = SMA(18)[1];
      double thisSma = SMA(18)[0];

      if (CrossAbove(Close, SMA(50), 1) && Close[1] > prevSma && Close[0] > thisSma)
      {
      /* Adding the 'CurrentBar' to the string creates unique draw objects because they will all have unique IDs
      Having unique ID strings may cause performance issues if many objects are drawn */
      Print("Condition True");
      DrawDiamond("Up Diamond" + CurrentBar, false, 0, SMA(50)[0], Color.Black);
      }

      else if (CrossBelow(Close, SMA(50), 1) && Close[1] < prevSma && Close[0] < thisSma)
      {
      /* Adding the 'CurrentBar' to the string creates unique draw objects because they will all have unique IDs
      Having unique ID strings may cause performance issues if many objects are drawn */
      Print("Condition True");
      DrawDiamond("Down Diamond" + CurrentBar, false, 0, SMA(50)[0], Color.Black);
      }






      }

      Comment


        #4
        Next is to debug more fully. Identify a bar where you feel an arrow should be drawn, and print all values related to the condition that should draw it to confirm they're what you expect. This page can help additionally using print statements to debug your code.

        Ryan M.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Geovanny Suaza, 02-11-2026, 06:32 PM
        0 responses
        574 views
        0 likes
        Last Post Geovanny Suaza  
        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
        0 responses
        333 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
        553 views
        1 like
        Last Post Geovanny Suaza  
        Started by RFrosty, 01-28-2026, 06:49 PM
        0 responses
        551 views
        1 like
        Last Post RFrosty
        by RFrosty
         
        Working...
        X