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 modify DrawObject by by their TAG as RemoveDrawObject?

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

    How modify DrawObject by by their TAG as RemoveDrawObject?

    the purpose of the strategy is to
    1. draw an object at each new bar
    2. then delete these objects except the last 5
    3. Modify the opacity and color of the 4 objects
    we do nothing for the draw object on CurrentBar


    in this link https://ninjatrader.com/fr/support/h...rawobjects.htm
    there is this line which allows to modify the color of the object in black
    globalLine.Stroke.Brush = Brushes.Black;

    my question
    is there a solution that allows to modify the opavity, color, width... of the object by its TAG like RemoveDrawObject("tag_" + (CurrentBar-5));

    Thanks in advance

    Code:
    protected override void OnBarUpdate()
    {
    if (BarsInProgress != 0)
    return;
    
    if (CurrentBars[0] < 1)
    return;
    
    Draw.VerticalLine(this, "tag_" + Convert.ToString(CurrentBars[0]), 0, Brushes.Lime, DashStyleHelper.Solid, 2);
    
    RemoveDrawObject("tag_" + (CurrentBar-5));
    
    foreach (DrawingTool draw in DrawObjects.ToList())
    {
    if (draw is DrawingTools.Line)
    {
    DrawingTools.Line globalLine = draw as DrawingTools.Line;
    
    globalLine.Stroke.Brush = Brushes.Black;
    
    }
    
    }
    }
    Last edited by aekzof; 06-17-2022, 10:09 AM.

    #2
    Hello aekzof,

    Thank you for your reply.

    If I'm understanding correctly, you always want vertical lines on the last 5 bars. It would be simpler to just redraw the same line, and for the lines you wish to be a different color with a different opacity, a custom brush needs to be constructed to control Opacity (alpha value). Instead of using one of the pre-built brushes (Brushes.Red, Brushes.Blue, etc), construct your own SolidColorBrush employing the Color.FromArgb() method, the first value is the "alpha":

    https://ninjatrader.com/support/help...gcustombrushes

    myBrush = new SolidColorBrush(Color.FromArgb(123, 0, 0, 0)); // create a 50% opaque black brush
    myBrush.Freeze();

    I'm attaching a very simple example script that demonstrates.

    Please let us know if we may be of further assistance to you.
    Attached Files
    Kate W.NinjaTrader Customer Service

    Comment


      #3
      Thank you for your reply .
      Unfortunately that's not what I want.
      to fully understand the question I am attaching a script .




      in this stratefy a vertical line will be drawn each bar.

      what i want is to
      1. delete all lines except the last 5 lines
      2. change the color and opacity of the 4 lines from the left
      so we don't take line[0] draw on currentbar

      thanks again

      Comment


        #4
        RemovePreviousLines_NT8.zip

        Comment


          #5
          Hello aekzof,

          Thank you for your reply.

          You wouldn't be able to change color and opacity of prior lines using lines drawn in the Strategy Builder as opacity would need to be applied using the manually coded approach seen in my prior reply.

          The example you have supplied is unclear - can you clarify how what the desired behavior you're describing is different from the example I supplied when the indicator is applied to a chart?

          Thanks in advance; I look forward to assisting you further.
          Kate W.NinjaTrader Customer Service

          Comment


            #6
            Hello
            Sorry for my late response
            I found a solution I don't know if it's good or not and if there is a more effective solution.

            my solution is to draw the same line twice on each new bar with a different color and tag
            then for the lines with 1st color we delete all the lines except Line0
            for lines with 2nd color we delete everything except the last 4 lines
            what we have left. 2 lines on bar[0] with the 1st color dominating
            and 3 lines on bar[1],bar[2],bar[3] with the 2nd color
            after we change the opacity of these 3 lines



            you can try my script it works very well and that's what I want.

            Code:
            protected override void OnBarUpdate()
            {
            if (BarsInProgress != 0)
            return;
            
            if (CurrentBars[0] < 5)
            return;
            
            if (IsFirstTickOfBar)
            {
            
            var someTag = "sometag_" + Convert.ToString(CurrentBars[0]);
            var oldTag = "oldtag_" + Convert.ToString(CurrentBars[0]);
            
            
            //draw two lines on each new bar (the difference just the color and the name for later use)
            
            Draw.VerticalLine(this, someTag, 0, Brushes.Blue, DashStyleHelper.Solid, 3);
            NinjaTrader.NinjaScript.DrawingTools.Line myOldLine = Draw.VerticalLine(this, oldTag, 0, Brushes.Yellow, DashStyleHelper.Dot, 3);
            
            RemoveDrawObject("sometag_" + (CurrentBar - 1)); //delete all Blue lines except the last on currentbar
            RemoveDrawObject("oldtag_" + (CurrentBar - 4)); //delete all Yellow lines except the last 4
            
            myOldLine.Stroke.Opacity = 25;
            
            }
            }

            indeed what I need is like a trendline
            with each new trendline
            the old trendline takes another color with less opacity.
            and the same for a broken resistor
            it takes another color and we reduce the opacity
            then we control the number of old trendlines or broken resistances to display
            Thanks

            Comment


              #7
              Hello aekzof,

              Thank you for your reply.

              Drawing multiple lines on every bar only to immediately remove them when you only want lines drawn on the most recent 5 bars would impact performance considerably more than simply drawing 5 lines that move to new bars as they form. In fact, the only difference I can see between my example above and your code would be my example has one more line and different colors. You would get the same results with considerably less performance impact going the route illustrated above.

              Please let us know if we may be of further assistance to you.
              Kate W.NinjaTrader Customer Service

              Comment


                #8
                Hello
                Your answer is based on a question that I worded incorrectly.
                because I simplified my question by this example to arrive at a solution. it is not necessarily that the lines are drawn for each new bar.
                let's take an example trendline included with ninjatrader there is always the history of trendlines that take place but with different opacity and color from the current trendline


                so it's the same principle
                it's not a professional script but it does the trick.Click image for larger version

Name:	Capture.PNG
Views:	194
Size:	125.0 KB
ID:	1206971

                thank you for your help and see you in my next questions

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by alifarahani, Today, 09:40 AM
                6 responses
                39 views
                0 likes
                Last Post alifarahani  
                Started by Waxavi, Today, 02:10 AM
                1 response
                18 views
                0 likes
                Last Post NinjaTrader_LuisH  
                Started by Kaledus, Today, 01:29 PM
                5 responses
                15 views
                0 likes
                Last Post NinjaTrader_Jesse  
                Started by Waxavi, Today, 02:00 AM
                1 response
                12 views
                0 likes
                Last Post NinjaTrader_LuisH  
                Started by gentlebenthebear, Today, 01:30 AM
                3 responses
                17 views
                0 likes
                Last Post NinjaTrader_Jesse  
                Working...
                X