Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Modifying DrawingText Objects dinamically

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

    Modifying DrawingText Objects dinamically

    I've written an indicator that plots some green text above the bars and red text below. When the current bar closes above a prior bar on the left with green text on its High or closes below the Low of a previous bar on the left with red text below its Low, I want to change either text color to gray.

    I'd start processing each bar from the latest backward, gleaning each bar's High and Low.
    Then, I would retrieve each text Y anchor data point to compare its value to the current bar, High and Low.
    To do that, I've come up with the following code:​

    Code:
    double HH, LL;
    
    protected override void OnRender(ChartControl chartControl, ChartScale chartScale)
    {
      if (ChartBars != null)
      {
        // reverse loop through all of the viewable range of the chart
        for (int barIndex = ChartBars.ToIndex; barIndex >= ChartBars.FromIndex; barIndex--)
        {
            // get the High and Low values for each index within the viewable range
            HH = High.GetValueAt(barIndex);
            LL  = Low.GetValueAt(barIndex);
            foreach (DrawingTool draw in DrawObjects.ToList())
            {
                 // Find text objects that are attached to the chart
                 if (draw is DrawingTools.Text)
                 {      
                      DrawingTools.Text msg = draw as DrawingTools.Text;
    
                      if (HH > msg.Y || LL  < msg.Y) msg.Brush = Brushes.Gray;
                 }
            }  ​
        }
      }
    }​


    Is there any hidden error in the above code?

    What about checking all the bars, not just those rendered on the ChartPanel?

    Or is there even a better way?
    Last edited by Gianpiero; 01-19-2023, 07:04 AM. Reason: Fixed typos

    #2
    Hi Gianpiero, thanks for writing in. I don't see anything wrong with this code just by looking at it. Unless there is a specific error coming from it there is nothing wrong as long as it works. If you want to run through all the bars on the chart during the historical data run, put your color changing code within OnBarUpdate so it can be performed on every bar.

    Comment


      #3
      Thanks for your kind and quick response, though I got confused about moving around this piece of code.

      Let's say that the text rectangles highlight prior swing highs and lows. Any bar closing beyond the swing's highs and lows should clear the text labels of the swings by changing their color.

      Currently, the code above sits in OnRender(), but you mentioned both "historical data run" and OnBarUpdate(). With "historical data run",​ do you mean moving the above code under the State.Historical condition of the OnStateChange() method?

      Could you please elaborate more?

      Comment


        #4
        Hello, thanks for the follow up. Each time you apply an indicator to a chart, it will go through every existing bar on the chart starting from the left most bar. This is what I call the "historical data run" You can separate logic ran in the historical state and real-time state with this code in OnBarUpdate:

        if(State == State.Historical)
        {
        //code here is running on historical bars
        }

        Or you can set some kind of flag in OnStateChanged as this will also be called once for the historical state, once for the transition state, and once for the realtime state. This is how one would check all of the bars for some condition in a way that will not hurt performance.

        Comment


          #5
          Hi Chris

          I'll have a shot at moving my code to State.Historical. Let me check if I got it right, though. OnBarUpdate() is called each time the price changes, meaning that only the last bar changes in real-time while all closed bars to the left of the live bar belong to the past. I don't want to confuse "belonging to the past" with State.Historical. I understand that code which runs in State.Historical applies to all bars-in-the-past and NOT on the live bar updating in real-time. Is that so, or else?

          Comment


            #6
            Hi, that is correct. all bars on the chart are historical except for the currently forming bar. You have a few options with how frequently OnBarUpdate is called, see the Calculate property:



            If you set Calculate to OnEachTick or OnPriceChange, you need to use Tick Replay to get this same effect in the historical run-through. See our guide on Tick Replay here:

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Geovanny Suaza, 02-11-2026, 06:32 PM
            0 responses
            633 views
            0 likes
            Last Post Geovanny Suaza  
            Started by Geovanny Suaza, 02-11-2026, 05:51 PM
            0 responses
            364 views
            1 like
            Last Post Geovanny Suaza  
            Started by Mindset, 02-09-2026, 11:44 AM
            0 responses
            105 views
            0 likes
            Last Post Mindset
            by Mindset
             
            Started by Geovanny Suaza, 02-02-2026, 12:30 PM
            0 responses
            567 views
            1 like
            Last Post Geovanny Suaza  
            Started by RFrosty, 01-28-2026, 06:49 PM
            0 responses
            568 views
            1 like
            Last Post RFrosty
            by RFrosty
             
            Working...
            X