• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

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);
        			}
        		}
        Patrick H.NinjaTrader Customer Service

        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 n8dogg59, Today, 03:07 PM
                        0 responses
                        1 view
                        0 likes
                        Last Post n8dogg59  
                        Started by Newtrader101, Today, 10:31 AM
                        1 response
                        15 views
                        0 likes
                        Last Post koganam
                        by koganam
                         
                        Started by nepenthean, Today, 12:36 PM
                        1 response
                        6 views
                        0 likes
                        Last Post NinjaTrader_ChrisL  
                        Started by sun0781, Yesterday, 05:02 PM
                        1 response
                        23 views
                        0 likes
                        Last Post NinjaTrader_ChrisL  
                        Started by Emma1, Today, 12:14 PM
                        1 response
                        4 views
                        0 likes
                        Last Post NinjaTrader_ChrisL  
                        Working...
                        X