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

How to use the "tag" of a line in a condition

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

    How to use the "tag" of a line in a condition

    Hello Support,
    Can someone tell me the syntax to use in the following example.
    if (CrossAbove( Close, "tag", 1))
    {
    Do something;
    }
    where "tag" is the tag in Horizontal Line Properties.
    Thank you

    #2
    Hello oldhiker,
    Thanks for writing in and I am happy to assist you.

    CrossAbove do not accept any string parameter. Please refer here to know more about the CrossAbove method http://www.ninjatrader.com/support/h...crossabove.htm

    You can use the Y value of the horizontal line instead, in your code.

    Code:
    IHorizontalLine hLine = DrawHorizontalLine("tag", Close[0] + 10 * TickSize, Color.Blue);  //horizontal line
    if (CrossAbove(Close, hLine.Y, 1))
    {  // do something }
    Please let me know if I can assist you any further.
    JoydeepNinjaTrader Customer Service

    Comment


      #3
      Hello Joydeep,
      Thank you for your quick reply.
      I knew that a string would not work, but I used it to place a marker in that location of the code.
      Your answer worked as expected. I then tried to expand the idea to more than one line. After several attempts, I finally found that the prefix to Line was the unique indentifier for each Line.
      However, what is the use of the unique tag that is applied by NT when a line is drawn manually.
      Thank you for your ready assistance.

      Comment


        #4
        Hello oldhiker,
        If there are 2 or more lines with the same parameters (x axis, y axis, color etc are same) then NinjaTrader cannot distinguish between the 2 lines unless there is an additional parameter which separates them. Thus the tag works as an unique identifier for each draw object to separate it with each other whether manually drawn or drawn via NinjaScript.

        Please let me know if I can assist you any further.
        JoydeepNinjaTrader Customer Service

        Comment


          #5
          Hello Joydeep,
          Thank you for your reply. This confirms what I was thinking.

          Comment


            #6
            Hello oldhiker,
            Thanks for your note,
            Please let me know if I can assist you any further.
            JoydeepNinjaTrader Customer Service

            Comment


              #7
              Checking each and every IHorizontalLine to see if price has crossed it on each Bar Up

              Hello Support,
              I can code the requirement for testing when the price crosses above/below an horizontal line, and I have found the clue to plotting multiple horizontal lines. However, I would now like to know if there is an elegant snippet of code to keep track of when the price crosses above/below more than 10 horizontal lines, ie, on each Bar Update, to test each and every horizontal lines to see if the price has crosses one or more of the lines. OR,
              Is it necessary to write the code for each and every one of the horizontal lines?
              I have studied the "foreach" menthod in the NT manual, but I cannot translate the examples shown, to the code for rotating throuhg each of the many horizantal lines.
              Thank you from Oldhiker.

              Comment


                #8
                Hello oldhiker,
                A sample code to loop through all the horizontal lines in OnBarUpdate will be:
                Code:
                foreach(IDrawObject o in DrawObjects)
                {
                	
                	if (o.DrawType == DrawType.HorizontalLine)
                	{
                		IHorizontalLine hLine = (IHorizontalLine)o;
                		if (CrossAbove(Close, hLine.Y, 1)
                		{
                			//do something
                		}
                	}
                }



                Please let me know if I can assist you any further.
                JoydeepNinjaTrader Customer Service

                Comment


                  #9
                  Hello Joydeep,
                  You are on the job early this evening, and thank you for our speedy reply.

                  I have studied both of your references. I can now write the code to draw the 10+ horizontal lines, and I have tagged them with the letters 'a' through 'm' as the prefix to 'aLine'. I cannot understand the meaning 'o' in the line of code
                  o.DrawType?
                  How much of the code do I have to repeat for each of the horizontal lines with tags of;- 'aLine', bLine', 'cLine' etc down to 'mLine'
                  Thank you

                  Comment


                    #10
                    Hello oldhiker,
                    o is the IDrawObject http://www.ninjatrader.com/support/h...drawobject.htm

                    There can be various draw object like Line, Ray, Dot etc etc and when we loop through the code
                    Code:
                    foreach(IDrawObject o in DrawObjects)
                    {}
                    returns all the draw object. Since we are concerned with Horizontal lines only we further filter it with the draw type so it may return the horizontal lines only.
                    Code:
                    if (o.DrawType == DrawType.HorizontalLine)
                    {}

                    If you want to further filter it with the tag name then a sample code will look like:
                    Code:
                    foreach(IDrawObject o in DrawObjects)
                    {	
                    	if (o.DrawType == DrawType.HorizontalLine)
                    	{
                    		IHorizontalLine hLine = (IHorizontalLine)o;
                    		if (CrossAbove(Close, hLine.Y, 1))
                    		{
                    			if (hLine.Tag == "aLine")
                    			{
                    				//do something
                    			}
                    			else if (hLine.Tag == "bLine")
                    			{
                    				//do something
                    			}
                                            //more tag check codes
                    		}
                    	}
                    
                     }
                    Please let me know if I can assist you any further.
                    JoydeepNinjaTrader Customer Service

                    Comment


                      #11
                      Hello Joydeep,
                      Thank you for the information provided. I have got it to work in a sort of fashion, but I have noticed two pecularilities.
                      This is the code that I have used repeatedly to generate all of my Horizontal lines, and it works OK (I have shown only the 'h' line and the 'i' line

                      IHorizontalLine haLine = DrawHorizontalLine("240Mh", 1.04308, Color.Black);
                      haLine.Pen.Width = 2;
                      haLine.Pen.DashStyle = DashStyle.Dash;
                      haLine.Locked = true;

                      IHorizontalLine iLine = DrawHorizontalLine("240Mi", 1.04105, Color.Black);
                      iLine.Pen.Width = 2;
                      iLine.Pen.DashStyle = DashStyle.Dash;
                      iLine.Locked = true;

                      I had to change the prefix of the 'h' line to 'haLine', because of using your code as below;

                      foreach(IDrawObject o in DrawObjects)
                      {
                      if (o.DrawType == DrawType.HorizontalLine)
                      {
                      IHorizontalLine hLine = (IHorizontalLine)o;
                      if (CrossAbove(Close, hLine.Y, 1) ||
                      CrossBelow(Close, hLine.Y, 1))
                      {
                      BackColor = Color.SkyBlue;
                      DrawDiamond(CurrentBar.ToString(), false,0,High[0] + 100 * TickSize ,Color.Black);
                      }
                      }
                      }

                      I had to use the prefix 'haLine' to avoid an error in using the 'hLine' immediately above, even though I have used different 'tags'.
                      This is not a problem as I have overcome it with the 'haLine'.

                      However, I cannot overcome the second problem; ie,
                      when using the 'foreach' block of code, the BackColor is printed correctly, when the 'DrawDiamond .....' part is '//' commented out,
                      but when I enable the 'DrawDiamond .....' part, as well as the BackColor, all the colours disappear and only the candles and a uniform default panel color is presented.
                      No matter what I try, I cannot get the diamonds to be printed where the BackColor had been printed. This has never happened before when I have used both BackColor and some Draw method on either the main chart, or some indicator panel.

                      Is this my coding problem, or some kinky behaviour of NT.
                      I would appreciate if you could point me in the correct direction.
                      Thank you from Oldhiker.

                      Comment


                        #12
                        Hello oldhiker,
                        Unfortunately you cannot modify an collection while in a foreach loop of that collection.

                        Thus you cannot add any DrawObject to the IDrawObjects collection while in the loop.

                        Please let me know if I can assist you any further.
                        JoydeepNinjaTrader Customer Service

                        Comment


                          #13
                          Hello Joydeep,
                          Thanks once again for your comments, but, I think that I may have confused my problem statement by putting two problems on the one sheet. When I use the following code I have problems as stated below.
                          -----------------------
                          foreach(IDrawObject o in DrawObjects)
                          {
                          if (o.DrawType == DrawType.HorizontalLine)
                          {
                          IHorizontalLine hLine = (IHorizontalLine)o;
                          if (CrossAbove(Close, hLine.Y, 1) ||
                          CrossBelow(Close, hLine.Y, 1))
                          {
                          BackColor = Color.SkyBlue;
                          DrawDiamond(CurrentBar.ToString(), false,0,High[0] + 100 * TickSize ,Color.Black);
                          }
                          }
                          }
                          -------------------------------------
                          When I comment out the 'DrawDiamond.....' line, the BackColor is printed correctly, but when I enable the 'DrawDiamond .....' part, as well as the BackColor, all the BackColors disappear and only the candles and a uniform default panel color is presented, and no diamonds, and the same occurs when I comment out the BackColor and leave the Diamonds only to be printed.
                          No matter what I try, I cannot get the Diamonds to be printed where the BackColor had been printed. This has never happened before when I have used both BackColor and some Draw method on either the main chart, or some indicator panel.
                          I have not tried to modify anything while in the 'foreach' loop.
                          I hope that this clarifies my problem.
                          Thank you for the old Oldhiker

                          Comment


                            #14
                            Hello oldhiker,
                            The code
                            Code:
                            DrawDiamond(CurrentBar.ToString(), false,0,High[0] + 100 * TickSize ,Color.Black);
                            added an item in the DrawObjects collections which is not allowed. This is a normal C# rule.

                            A work around will be like this.

                            Code:
                            bool drawDiamond = false;
                            
                            foreach(IDrawObject o in DrawObjects)
                            { 
                            	if (o.DrawType == DrawType.HorizontalLine)
                            	{
                            		IHorizontalLine hLine = (IHorizontalLine)o;
                            		if (CrossAbove(Close, hLine.Y, 1) ||
                            			CrossBelow(Close, hLine.Y, 1))
                            		{
                            			BackColor = Color.SkyBlue;
                            			drawDiamond = true;
                            		}
                            	}
                            }
                            
                            if (drawDiamond)
                            {
                            	DrawDiamond(CurrentBar.ToString(), false,0,High[0] + 100 * TickSize ,Color.Black);	
                            }
                            Please let me know if I can assist you any further.
                            JoydeepNinjaTrader Customer Service

                            Comment


                              #15
                              Hello Joydeep,
                              Thank you once again for coming to my rescue. Not being formally trained in computer programming, the intricies of NT and C# never cease to amaze me, and even more so is how someone like you can find the way through the maze.

                              Enough to say that your 'work around' was successful.

                              Having now got the process working OK, I can now proceed to refine the conditions to more accurately highlight what I am seeking. This success has lifted my confidence!
                              Thank you * 10
                              PS. I cannot find where to say if this reply was useful- 5/5 usefulness

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Waxavi, Today, 02:10 AM
                              0 responses
                              4 views
                              0 likes
                              Last Post Waxavi
                              by Waxavi
                               
                              Started by TradeForge, Today, 02:09 AM
                              0 responses
                              10 views
                              0 likes
                              Last Post TradeForge  
                              Started by Waxavi, Today, 02:00 AM
                              0 responses
                              2 views
                              0 likes
                              Last Post Waxavi
                              by Waxavi
                               
                              Started by elirion, Today, 01:36 AM
                              0 responses
                              4 views
                              0 likes
                              Last Post elirion
                              by elirion
                               
                              Started by gentlebenthebear, Today, 01:30 AM
                              0 responses
                              4 views
                              0 likes
                              Last Post gentlebenthebear  
                              Working...
                              X