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

Indicator DrawRay function

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

    Indicator DrawRay function

    Within a custom indicator I make a call to DrawRay(). The Y coordinate of a drawn ray may be the same as a previously drawn ray.

    How do I insure that the most recent ray is drawn On Top of any previously drawn rays. (Each ray tag string includes the bar number)

    Thanks,

    #2
    Hello phineas,

    Thanks for your post.

    There is not a built in way to change Drawing Object ZOrder in NinjaTrader 7, but you could devise a work around to detect that there are drawing objects on the same point, and then to remove and redraw the draw objects in the order you desire to get the behavior you are looking for.

    For example:
    Code:
    private IRay ray1 = null, ray2 = null, ray3 = null;
    protected override void OnBarUpdate()
    {
        if(CurrentBar > 2 || CurrentBar < 1)
    		return;
    	
    	if(CurrentBar % 2 == 1)
    		ray1 = DrawRay("tag"+CurrentBar, CurrentBar, Close[CurrentBar], CurrentBar-1, Close[CurrentBar-1], Color.Red);
    	if(CurrentBar % 2 == 0)
    		ray2 = DrawRay("tag"+CurrentBar, CurrentBar, Close[CurrentBar], CurrentBar-1, Close[CurrentBar-1], Color.Blue);
    				
    	if(ray1 != null && ray2 != null)
    	{
    		//return; // Comment me to switch order.
    		if(ray1.Anchor1Y == ray2.Anchor1Y)
    		{
    			RemoveDrawObject(ray1.Tag);
    			ray1 = DrawRay("tag"+CurrentBar, CurrentBar, Close[CurrentBar], CurrentBar-1, Close[CurrentBar-1], Color.Red); 
    			ray2 = DrawRay("tag"+CurrentBar, CurrentBar, Close[CurrentBar], CurrentBar-1, Close[CurrentBar-1], Color.Blue);
    		}
    	}
    }
    Please let us know if we can be of further assistance.
    JimNinjaTrader Customer Service

    Comment


      #3
      thanks for the alternative approach. I do want to keep the older rays on the chart, that is why i'm using ray's not Horizontal lines.

      Could you help me better understand Drawing Object ZOrder in NinjaTrader 7:

      1: When creating a ray from within an indicator, does each ray created have the same Drawing Object Zorder. (same line of code but different tag: associated with bar num)

      2: If not, how is the Zorder assigned. Is it random or based on a the time created, or something else?

      Thanks

      Comment


        #4
        Hello phineas,

        Drawing objects will inherit the ZOrder from the NinjaScript that draws them. Draw Objects could have their SeparateZOrder property set so the user could change the objects ZOrder independently using Shift + Mouse Wheel, but this would not change the ZOrder for the object or the order in which it is displayed programmatically.

        IDrawingObject (SeparateZOrder) - https://ninjatrader.com/support/help...drawobject.htm

        Items that are drawn on the same bar will be presented with the last drawn object on top, while objects that are drawn on separate bars will have the first drawn object on top.

        If you have any additional questions, please let us know.
        JimNinjaTrader Customer Service

        Comment


          #5
          This comment directly applies to my issue:

          "objects that are drawn on separate bars will have the first drawn object on top."

          Is there anything that can be done programmatically to reverse this. i.e. Last drawn object would be on top?

          Thanks,

          Comment


            #6
            Hello phineas,

            This is the observed behavior and I am not aware of any other ways to work around this programmatically. NinjaTrader 7 does not offer as much flexibility for setting unique ZOrders. This is improved in NinjaTrader 8, however.
            JimNinjaTrader Customer Service

            Comment


              #7
              I will try the suggested alternative. Could you please provide a code example to search thru potentially hundreds of previous horizontal rays, to check if they have the same Y coordinate, as the current ray?

              Comment


                #8
                Hello phineas,

                I do not have a code sample for this specific task, but it could be accomplished by looping through the drawing objects collection, checking if the objects you are looping through are IRays, checking for equality, and then modifying your drawing objects accordingly.

                You may use the following as a rough example to provide further direction:
                Code:
                foreach (IDrawObject draw1 in DrawObjects)
                {
                	if (draw1 is IRay)
                	{
                		foreach (IDrawObject draw2 in DrawObjects)
                		{
                			if (draw2 is IRay)
                			{
                				IRay myRay1 = draw1 as IRay;
                				IRay myRay2 = draw2 as IRay;
                				
                				if (myRay1.Anchor1Y == myRay2.Anchor1Y)
                				{
                					RemoveDrawObject(ray1.Tag);
                					ray1 = DrawRay("tag"+CurrentBar, CurrentBar, Close[CurrentBar], CurrentBar-1, Close[CurrentBar-1], Color.Red); 
                					ray2 = DrawRay("tag"+CurrentBar, CurrentBar, Close[CurrentBar], CurrentBar-1, Close[CurrentBar-1], Color.Blue);
                				}
                			}
                		}
                	}	
                }
                DrawObjects collection - https://ninjatrader.com/support/help...rawobjects.htm

                Please let us know if you have any additional questions.
                JimNinjaTrader Customer Service

                Comment


                  #9
                  Thanks for the help, I will use that as my starting point.

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by CortexZenUSA, Today, 12:53 AM
                  0 responses
                  1 view
                  0 likes
                  Last Post CortexZenUSA  
                  Started by CortexZenUSA, Today, 12:46 AM
                  0 responses
                  0 views
                  0 likes
                  Last Post CortexZenUSA  
                  Started by usazencortex, Today, 12:43 AM
                  0 responses
                  2 views
                  0 likes
                  Last Post usazencortex  
                  Started by sidlercom80, 10-28-2023, 08:49 AM
                  168 responses
                  2,262 views
                  0 likes
                  Last Post sidlercom80  
                  Started by Barry Milan, Yesterday, 10:35 PM
                  3 responses
                  10 views
                  0 likes
                  Last Post NinjaTrader_Manfred  
                  Working...
                  X