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

Draw rectangle syntax

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

    Draw rectangle syntax

    Greetings,
    I'm trying to figure out the syntax then draw a rectangle; for instance with a triple crossover that will cover the highs and lows of that entire crossover as in the screenshot.

    What would I need for the startBarsAgo, startY, endBarsAgo, endY parameters to accomplish this?

    Thanks for the assistance.

    #region Variables
    privatebool colorBars = true;
    #endregion

    protectedoverridevoid Initialize()
    {
    Overlay = false;
    CalculateOnBarClose = false;
    }
    protectedoverridevoid OnBarUpdate()
    {
    double MA5 = EMA(Close,5)[0];
    double MA10 = EMA(Close,10)[0];
    double MA20 = EMA(Close,20)[0];
    bool CrossUp = MA5 > MA10 && MA10 > MA20;
    bool CrossDown = MA5 < MA10 && MA10 < MA20;

    if ( CrossUp ) {
    if (colorBars ) {
    BarColor = Color.Cyan;
    }
    }
    else if (CrossDown ) {
    if (colorBars ) {
    BarColor = Color.Violet;
    }
    }
    else if (colorBars) {
    BarColor = Color.DimGray;
    }
    DrawRectangle("Area"+ CurrentBar.ToString(), false, startBarsAgo, startY, endBarsAgo, endY, Color.Empty, Color.Blue, 2);
    Attached Files

    #2
    Hello 2Look4Me,

    Thank you for your post.

    In your cross detections you would want to store the CurrentBar at the time to pass through the StartBarsAgo, and use CurrentBar - storedBar. This will be applied for each unique tag name for Rectangle.

    Your EndBarsAgo will remain 0 so that it stays active to the point where a new cross is detected.


    Let me know if this helps in your study
    Cal H.NinjaTrader Customer Service

    Comment


      #3
      Hi Cal,

      In trying to follow your suggestions, it doesn't seem like I got it right or even close to it!

      Thanks for your help.

      #region Variables
      privatedouble StoredBar;
      #endregion

      protectedoverridevoid OnBarUpdate()
      if ( CrossUp ) {
      StoredBar = CurrentBar;
      if (colorBars) {
      BarColor = Color.Cyan;
      }
      }

      else if (CrossDown ) {
      StoredBar1 = CurrentBar;
      if (colorBars ) {
      BarColor = Color.Violet;
      }
      }
      DrawRectangle("Area"+ CurrentBar.ToString(), 1, CurrentBar-StoredBar, 0, 0, Color.Blue);

      Comment


        #4
        Hello 2Look4me,

        Thank you for your response.

        I would change the conditions as well and work the DrawRectangle in as I have below:
        Code:
                #region Variables
                private int crossBar = 0;
                #endregion
        
                /// <summary>
                /// This method is used to configure the indicator and is called once before any bar data is loaded.
                /// </summary>
                protected override void Initialize()
                {
        			
                }
        
                /// <summary>
                /// Called on each bar update event (incoming tick)
                /// </summary>
                protected override void OnBarUpdate()
                {
        			if(EMA(5)[0] > EMA(10)[0] && EMA(10)[0] > EMA(20)[0]
        				&& (EMA(5)[1] <= EMA(10)[1] || EMA(10)[1] <= EMA(20)[1]))
        			{
        				crossBar = CurrentBar;
        			}
        			
        			if(EMA(5)[0] < EMA(10)[0] && EMA(10)[0] < EMA(20)[0]
        				&& (EMA(5)[1] >= EMA(10)[1] || EMA(10)[1] >= EMA(20)[1]))
        			{
        				crossBar = CurrentBar;
        			}
        			
        			if(crossBar != 0)
        			{
        				DrawRectangle("Area"+crossBar, false, CurrentBar - crossBar, MIN(Low, CurrentBar - crossBar)[0], 0, MAX(High, CurrentBar - crossBar)[0], Color.Empty, Color.Blue, 2);
        			}
        		}

        Comment


          #5
          Thanks again Patrick, that works.

          Comment


            #6
            Hi Patrick,

            Thanks again for the script. As I try to use the same logic to draw the rectangles, I come across problems when I the script with some other parameters. I know that my logic and syntax aren't there, but I'm stuck

            A) Start bullish rectangle when
            Close > EMA(200) &&
            Close > Yesterday's High &&
            EMA(10) > Ema(20) &&
            EMA(20)[0] > Ema(20)[1] &&
            ATR (6) <=2

            Complete bullish rectangle as EMA(20) turns down
            EMA(20)[0] < EMA(20)[1]

            B) Start bearish rectangle when
            Close < EMA(200) &&
            Close < Yesterday's Low &&
            EMA(10) < Ema(20) &&
            EMA(20)[0] < Ema(20)[1] &&
            ATR (6) <=2

            Complete bearish rectangle as EMA(20) turns up
            EMA(20)[0] > EMA(20)[1]


            #region Variables
            privateint crossBar = 0;
            #endregion

            protectedoverridevoid OnBarUpdate()
            {
            double HighY = PriorDayOHLC().PriorHigh[0];
            double LowY = PriorDayOHLC().PriorLow[0];
            bool avgRange = ATR(10)[0] <= 2;

            //Draw Bullish Rectangle
            if(Close[0] > EMA(200)[0] && Close[0] > HighY && avgRange && EMA(10)[0] > EMA(20)[0] && EMA(20)[0] > EMA(20)[1])
            {
            crossBar = CurrentBar;
            }
            if (EMA(20)[0] <= EMA(20)[1]) //Complete Rectangle as EMA(20) turns down
            {
            crossBar = CurrentBar;
            }
            if(crossBar != 0)
            {
            DrawRectangle("Area"+crossBar, false, CurrentBar - crossBar, MIN(Low, CurrentBar - crossBar)[0], 0, MAX(High, CurrentBar - crossBar)[0], Color.Empty, Color.Blue, 2);
            }

            //Draw Bearish Rectangle
            if(Close[0] < EMA(200)[0] && Close[0] < LowY && avgRange && EMA(10)[0] < EMA(20)[0] && EMA(20)[0] < EMA(20)[1])
            {
            crossBar = CurrentBar;
            }
            if (EMA(20)[0] > EMA(20)[1]) //Complete Rectangle as EMA(20) turns up
            {
            crossBar = CurrentBar;
            }
            if(crossBar != 0)
            {
            DrawRectangle("Area"+crossBar, false, CurrentBar - crossBar, MIN(Low, CurrentBar - crossBar)[0], 0, MAX(High, CurrentBar - crossBar)[0], Color.Empty, Color.Orange, 2);
            }
            Attached Files

            Comment


              #7
              2Look4me,

              I don't see anything wrong with the code or the screenshots. Could you please be a little more specific in what you are having trouble with?
              Cal H.NinjaTrader Customer Service

              Comment


                #8
                Cal,
                Thanks for taking the time to look thru the code and my apologies for not explaining myself clearly.

                The code compiles without errors, but the rectangles are not drawn out at all in the chart.

                As far as the screenshot goes, the rectangles were drawn manually to depict what I intended the code to do. Somehow the code must have an error in the logic.

                Comment


                  #9
                  2Look4Me,

                  I would expect that you are not hitting the conditions at all then in this case.

                  If you put Print() statements in the if statements do you see any prints being generated in the Output window?
                  Code:
                  if(Close[0] > EMA(200)[0] && Close[0] > HighY && avgRange && EMA(10)[0] > EMA(20)[0] && EMA(20)[0] > EMA(20)[1])
                  {
                  Print("Bull Bar Cross Up");
                  crossBar = CurrentBar;
                  }
                  Cal H.NinjaTrader Customer Service

                  Comment


                    #10
                    Cal,

                    It didn't draw the rectangles because I failed to check for enough bars, so I added:
                    if (CurrentBar < 1)
                    return;

                    Now, somehow the drawn rectangles are not meeting my conditions, i.e. when Close >/< HighY/LowY, avgRange, etc and the rectangles are drawn on each individual candle, instead of being drawn over the met conditions. I have included a screenshot.
                    Attached Files

                    Comment


                      #11
                      2Look4Me,

                      Can you please post your latest rendition of your code so I can review it on my end?
                      Cal H.NinjaTrader Customer Service

                      Comment


                        #12
                        Cal,

                        Thanks for the prompt response and for taking a close look at it.
                        Attached Files

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by The_Sec, Today, 02:29 PM
                        1 response
                        4 views
                        0 likes
                        Last Post NinjaTrader_Jesse  
                        Started by jeronymite, 04-12-2024, 04:26 PM
                        2 responses
                        30 views
                        0 likes
                        Last Post NinjaTrader_BrandonH  
                        Started by Mindset, 05-06-2023, 09:03 PM
                        10 responses
                        265 views
                        0 likes
                        Last Post NinjaTrader_BrandonH  
                        Started by michi08, 10-05-2018, 09:31 AM
                        5 responses
                        743 views
                        0 likes
                        Last Post NinjaTrader_ChelseaB  
                        Started by tsantospinto, 04-12-2024, 07:04 PM
                        4 responses
                        63 views
                        0 likes
                        Last Post aligator  
                        Working...
                        X