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

Drawing Rectangles. Not sure what I'm doing wrong

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

    #16
    Hello Kay,

    Adding CurrentBar to the tag makes drawing objects unique to the bar. But this means you can only have 1 drawing object with that tag name on that bar.

    If you want 2 drawing objects dawn on that bar, they need different names.

    Increment means to increase. I'm saying add 1 to the value.


    You should increment by the number of objects you want to draw. If you want to start drawing a new object while updating a previous object you don't want to stop drawing yet, they need different names.

    GapUP 1 555
    GapUp 2 555

    By having these different, the first object won't stop drawing when you update it.

    Re-use a tag name to update an existing object.
    Make a new unique tag name to draw a new object.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #17
      Hi Chelsea,

      I think this is where I'm stuck:-

      if (/* new object condition*/)
      {
      objectIncrement++;
      }​

      How do I create such a condition when there could be multiple more tags for similar objects?

      I think I created a unique tag according to your suggestion. I've attached them all below.

      What am I missing cause at the moment, it doesn't look like anything has changed other than the tags. And I'm at a loss how I can use the above.

      Regards
      Kay Wai

      Code:
      if (CurrentBar < (2)) return;
      
      
                  if( Low[0] > High [1])
                  {
                      GapUp = true;
                      Gaps++;
                      if( GapUp == true)
                      {
                          drawTag = "GapUP" + Gaps + "-" +CurrentBar;
                          GapHigh = High[1];
                          GapLow = Low[0];
                          GapLength = CurrentBar;
      
                          Print( Time[0].ToString());
                          Print (drawTag.ToString());
                      }
                  }
      
                  if ((GapUp == true) )
                  {
                     if( Low[0] > GapHigh)
                      {
                          Draw.Rectangle(this, drawTag, false, -1, GapLow, CurrentBar-GapLength+1, GapHigh, Brushes.Green, Brushes.Green,10);
      
                      }
                      else if( Low[0] <= GapHigh )
                      {
                          GapUp = false;    
                      }
                  }
              }​
      Attached Files

      Comment


        #18
        Hello Kay Wai,

        Thanks for your note.

        A Draw object will use the Tag name that you give the object. In the Prints you shared, we can see that a Draw object has a tag name of say 'GapUP14-220'.

        To modify this specific drawing object, you would call your Draw method again and use the exact same tag name of "GapUP14-220" when calling the Draw method if you want to modify/update that specific drawing object.

        As Chelsea stated:
        Re-use a tag name to update an existing object.
        Make a new unique tag name to draw a new object.
        https://ninjatrader.com/support/foru...630#post838630

        Please review the forum thread linked in my colleague Chelsea's post and linked above.​
        Brandon H.NinjaTrader Customer Service

        Comment


          #19
          kaywai
          You may want to look at Gemify's Inner Circle Trader (ICT) Fair Value Gap (FVG) Indicator as it draws rectangles then closes them after they are filled. It draws the candle out into the future using -100000 as the endBarsAgo value. Something like this:


          string tag = "GAPUP" + Instrument.Id.ToString() + CurrentBar;
          Draw.Rectangle(this, tag, false, 2, Low[0], -100000, High[2], UpBrush, UpAreaBrush, ActiveAreaOpacity, true);​


          It also stores tags into a list to close the candle when gap fills. It should be extremely simple to modify the code to get daily gaps instead of FVG's.
          Simple implementation of The Inner Circle Trader’s (ICT – http://theinnercircletrader.com) Fair Value Gap (FVG). I couldn’t find an NT8 implementation and so figured I’d share. The indicator has the following parameters: Max Lookback Bars This is the maximum number of prior bars to use for FVG detection. Defaults to 500. ATR Period The indicator uses […]

          Comment


            #20
            Hi BrandonH,

            I thought I made the tag unique. I used int Gaps rather than object increment. I also watched the video and post ChelseaB shared but I couldn't follow it. Perhaps I could redo ChelseaB's video on the Strategy Builder and see how the code is written. The difficulty i have is with Modifying the draw object when when tag has been replaced by a new drawobject. I can't figure out how to keep the old tag and keep it going till the condition to stop it is fulfilled while at the same time start drawing the new draw object.

            Hi ticket2themoon,

            Thanks for sharing. I actually found the NT8 version for Gemify's ICTFVG and have actually looked at it and I could probably use it as a final attempt but the extension into the "indefinite" future is not what I really want because it would clutter the chart unnecessarily and I might miss something. Additonally, I found the code too complicated for me to understand. I may need a couple of weeks to slowly figure out what and how it works and how to adapt it to my code. If you need the ICTFVG code, let me know. I will share it with you.

            Regards
            Kay Wai

            Comment


              #21
              Hello Kay Wai,

              Thanks for your note.

              To modify an existing Draw object, you would need to call the Draw method again and use the same Tag name as the drawing object you want to modify.

              For example, say you want to modify a Draw object that has the tag name "GapUP14-220", then you would call your Draw method and pass in "GapUP14-220" for the Tag name of the Draw object.

              The code below would modify a draw object on a chart that has the tag name "GapUP14-220".

              Draw.Rectangle(this, "GapUP14-220", false, -1, GapLow, CurrentBar-GapLength+1, GapHigh, Brushes.Green, Brushes.Green,10);

              To draw a new object, you would use a unique tag name. If you are drawing two objects on the same bar, this is why you would need to use objectIncrement that Chelsea had mentioned, or Gaps in the code you shared.

              From Chelsea's post:
              "You should increment by the number of objects you want to draw. If you want to start drawing a new object while updating a previous object you don't want to stop drawing yet, they need different names.

              GapUP 1 555
              GapUP 2 555"

              "Perhaps I could redo ChelseaB's video on the Strategy Builder and see how the code is written. "


              This would be a good idea to gain a full understanding of exactly how the code is created and functions. ​

              Let me know if I may assist further.
              Brandon H.NinjaTrader Customer Service

              Comment


                #22
                Kay Wai,

                The issue you have is you need the ability to store multiple tags for the draw objects (rectangles in this case). The reason the 11/10/2022 rectangle stops at 12/13/2022 is because drawTag string changes when the 12/13/2022 gap/rectangle is created. At that point you no longer have a reference to the 11/10/2022 tag.

                Couple of options: You can look through the list of all drawobjects and find the tags that begin with GapUP and then redraw those tags. Issue will be the filled gaps need to be ignored. In this case you would need to capture the gap information, delete the rectangle, then redraw it with a different tag prefix (i.e. GapClosed).

                Or you can create a list and store tags into the list then iterate through the list updating the rectangles until the gap is filled. Once filled, you would remove the tag from the list so you no longer update that rectangle.

                I implemented this method below and included both gaps up and down.

                Code:
                namespace NinjaTrader.NinjaScript.Indicators
                {
                    public class TDGap : Indicator
                    {
                        #region Variables
                
                        enum GapType
                        {
                            Up, Down
                        }
                        class GAPS
                        {
                            public double upperPrice;
                            public double lowerPrice;
                            public string tag;
                            public GapType type;
                
                            public GAPS(string tag, GapType type, double lowerPrice, double upperPrice)
                            {
                                this.tag = tag;
                                this.type = type;
                                this.lowerPrice = lowerPrice;
                                this.upperPrice = upperPrice;
                            }
                        }
                
                        private List<GAPS> gapList = new List<GAPS>();
                
                        #endregion
                
                        protected override void OnStateChange()
                        {
                            if (State == State.SetDefaults)
                            {
                                Description                                    = @"Draw Rectangles for Gap Up/Down Candles until gap is filled";
                                Name                                        = "TDGap";
                                Calculate                                    = Calculate.OnBarClose;
                                IsOverlay                                    = true;
                                DisplayInDataBox                            = true;
                                DrawOnPricePanel                            = true;
                                DrawHorizontalGridLines                        = true;
                                DrawVerticalGridLines                        = true;
                                PaintPriceMarkers                            = true;
                                ScaleJustification                            = NinjaTrader.Gui.Chart.ScaleJustification.Right;
                                //Disable this property if your indicator requires custom values that cumulate with each new market data event.
                                //See Help Guide for additional information.
                                IsSuspendedWhileInactive                    = true;
                
                
                            }
                            else if (State == State.Configure)
                            {
                            }
                        }
                
                        protected override void OnBarUpdate()
                        {
                
                            if (CurrentBar < (2)) return;
                
                            string drawTag = "";
                
                            double GapLow;
                            double GapHigh;
                
                            if( Low[0] > High [1]) //Gap Up
                            {
                                drawTag = "GapUP" + Instrument.Id.ToString() + CurrentBar;
                                GapHigh = Low[0];
                                GapLow = High[1];
                
                                //Print(drawTag.ToString() + " Time: " + Time[1].ToString());
                
                                Draw.Rectangle(this, drawTag, false, 1, GapLow, -1, GapHigh, Brushes.Green, Brushes.Green, 10);
                                gapList.Add(new GAPS(drawTag, GapType.Up, High[1], Low[0]));
                            }
                
                
                            if (High[0] < Low[1]) //Gap Down
                            {
                                drawTag = "GapDown" + Instrument.Id.ToString() + CurrentBar;
                                GapHigh = Low[1];
                                GapLow = High[0];
                
                                //Print(drawTag.ToString() + " Time: " + Time[1].ToString());
                
                                Draw.Rectangle(this, drawTag, false, 1, GapLow, -1, GapHigh, Brushes.Red, Brushes.Red, 10);
                                gapList.Add(new GAPS(drawTag, GapType.Down, GapLow, GapHigh));
                            }
                
                            CheckExistingGaps(CurrentBar);
                
                
                        }
                
                        private void CheckExistingGaps(int iCurrentBar)
                        {
                            List<GAPS> closedGaps = new List<GAPS>();
                
                
                            foreach (GAPS gap in gapList)
                            {
                
                                if (DrawObjects[gap.tag] != null && DrawObjects[gap.tag] is DrawingTools.Rectangle)
                                {
                                    //Update EndAnchor of Gap to Expand to currentbar.
                                    Rectangle gapRect = (Rectangle)DrawObjects[gap.tag];
                                    gapRect.EndAnchor.SlotIndex = CurrentBar;
                                    gapRect.EndAnchor.Time = Time[0];
                                }
                
                                if (gap.type == GapType.Up)
                                {
                                    if (Low[0] < gap.upperPrice) //gap entered
                                    {
                                        closedGaps.Add(gap);
                                        //Print(gap.tag.ToString() + " Closed Time: " + Time[0].ToString());
                                    }
                                }
                                else if (gap.type == GapType.Down)
                                {
                                    if (High[0] > gap.lowerPrice) //gap entered
                                    {
                                        closedGaps.Add(gap);
                                        //Print(gap.tag.ToString() + " Closed Time: " + Time[0].ToString());
                                    }
                                }
                
                            }
                
                            foreach (GAPS gap in closedGaps)
                            {
                                gapList.Remove(gap);
                            }
                        }
                    }
                }​​
                Attached Files
                Last edited by tickets2themoon; 02-27-2023, 09:24 PM.

                Comment


                  #23
                  Hi ticket2themoon,

                  Thank you so much for your help! It is exactly what I was hoping to achieve!

                  Thanks again.

                  Kay Wai

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by rhyminkevin, Today, 04:58 PM
                  3 responses
                  48 views
                  0 likes
                  Last Post Anfedport  
                  Started by iceman2018, Today, 05:07 PM
                  0 responses
                  5 views
                  0 likes
                  Last Post iceman2018  
                  Started by lightsun47, Today, 03:51 PM
                  0 responses
                  7 views
                  0 likes
                  Last Post lightsun47  
                  Started by 00nevest, Today, 02:27 PM
                  1 response
                  14 views
                  0 likes
                  Last Post 00nevest  
                  Started by futtrader, 04-21-2024, 01:50 AM
                  4 responses
                  50 views
                  0 likes
                  Last Post futtrader  
                  Working...
                  X