Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Plot a line once

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

    Plot a line once

    Hi,
    I am trying to plot a line once using the below codes:
    if (true)
    {
    int barIdx = CurrentBar;
    if (CurrentBar == barIdx)
    Draw.Line(this, "tag" /*+ CurrentBar*/, false, CurrentBar - barIdx, Low[barIdx] - (10 * TickSize), (CurrentBar - barIdx) + 15, Low[barIdx] - (10* TickSize), EntryQueLineColor, DashStyleHelper.Solid, 2);
    }
    but for some reason, it is only taking the low price of the bar first time in the past when the condition was true, after that it is not changing the plot line. Can you advise?

    #2
    Hello asmmbillah,

    Thank you for your post.

    Is there a reason why you're assigning CurrentBar to a barIdx variable, then checking if they equal each other and subtracting the two instead of just using 0 as the bars ago index?

    Using Low[barIdx] would get you the low of however many bars ago the chart was started, not the currently forming bar, so it's likely this would plot offscreen with the logic as you have it - is this intentional?

    Using the following plots the line for me and I see the line update as expected:

    protected override void OnBarUpdate()
    {
    if(CurrentBar < 15) return;


    Draw.Line(this, "tag" , false, 0, Low[0] - (10 * TickSize),15, Low[0] - (10* TickSize), EntryQueLineColor, DashStyleHelper.Solid, 2);
    }

    For example if the CurrentBar is 2658, your current logic would plot the line between the most recent bar and 15 bars ago, but at the low - 10 ticks of the bar 2658 bars ago.

    Draw.Line(this, "tag" /*+ CurrentBar*/, false, CurrentBar - barIdx, Low[barIdx] - (10 * TickSize), (CurrentBar - barIdx) + 15, Low[barIdx] - (10* TickSize), EntryQueLineColor, DashStyleHelper.Solid, 2);

    Is this the logic you're intending, or are you meaning to plot it at the low - 10 ticks of the current bar?

    Thanks in advance; I look forward to assisting you further.

    Comment


      #3
      Thanks for your reply. Probably you missed, I am checking a condition at the beginning and when it returns true, I want to plot this line once on the first bar, but its plotting for every bars whilst the condition is true. I hope that clarifies.

      Comment


        #4
        Hello asmmbillah,

        Thank you for your reply.

        If you only want the condition to plot once, on the first bar where the condition is true and stay there, but to be able to update the price at which it is drawn, you could use a bool to tell you when the line's been drawn, save the line to a variable, and then access the saved line to update it's values. Once the conditions to draw the line are no longer true, you'd reset the line drawn variable to false. Also, if you want to draw a new line once each time the conditions to draw are true, you'd want to make sure you use CurrentBar in the tag to make the tags unique to each line.

        // class level variables
        private bool LineNotDrawn == true;

        private DrawingTools.Line myLine;

        protected override void OnBarUpdate()
        {

        if(True)
        {

        if(LineNotDrawn)
        {
        myLine = Draw.Line(this, "tag"+ CurrentBar, false, CurrentBar - barIdx, Low[barIdx] - (10 * TickSize), (CurrentBar - barIdx) + 15, Low[barIdx] - (10* TickSize), EntryQueLineColor, DashStyleHelper.Solid, 2);
        LineNotDrawn = false;
        }
        else
        {
        // if we've drawn the line, update the price it's at on each new bar, but do not move the line
        myLine.StartAnchor.Price = Low[0] - (10 * TickSize);
        myLine.EndAnchor.Price = Low[0] - (10 * TickSize);
        }

        // reset the LineNotDrawn to true when your condition to draw becomes false

        }
        }

        If you're just wanting the line drawn when the condition is true and then to stay there and not be redrawn, simply use the first part of this setup:

        if(True)
        {

        if(LineNotDrawn)
        {
        myLine = Draw.Line(this, "tag"+ CurrentBar, false, CurrentBar - barIdx, Low[barIdx] - (10 * TickSize), (CurrentBar - barIdx) + 15, Low[barIdx] - (10* TickSize), EntryQueLineColor, DashStyleHelper.Solid, 2);
        LineNotDrawn = false;
        }
        }

        And then reset the LineNotDrawn bool when the conditions that triggered the drawing are false again.

        Please let us know if this does not resolve your inquiry.

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Geovanny Suaza, 02-11-2026, 06:32 PM
        0 responses
        581 views
        0 likes
        Last Post Geovanny Suaza  
        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
        0 responses
        338 views
        1 like
        Last Post Geovanny Suaza  
        Started by Mindset, 02-09-2026, 11:44 AM
        0 responses
        103 views
        0 likes
        Last Post Mindset
        by Mindset
         
        Started by Geovanny Suaza, 02-02-2026, 12:30 PM
        0 responses
        554 views
        1 like
        Last Post Geovanny Suaza  
        Started by RFrosty, 01-28-2026, 06:49 PM
        0 responses
        552 views
        1 like
        Last Post RFrosty
        by RFrosty
         
        Working...
        X