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

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 gm7_forum, Today, 05:51 PM
        0 responses
        1 view
        0 likes
        Last Post gm7_forum  
        Started by cre8able, Today, 03:20 PM
        1 response
        9 views
        0 likes
        Last Post cre8able  
        Started by fiddich, Today, 05:25 PM
        0 responses
        3 views
        0 likes
        Last Post fiddich
        by fiddich
         
        Started by gemify, 11-11-2022, 11:52 AM
        6 responses
        804 views
        2 likes
        Last Post ultls
        by ultls
         
        Started by ScottWalsh, Today, 04:52 PM
        0 responses
        4 views
        0 likes
        Last Post ScottWalsh  
        Working...
        X