Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Trend Based Chart Background

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

    Trend Based Chart Background

    Hi, I've developed an indicator which changes the charts background to red or green depending on whether the previous bars open and close was below or above its 14 period ema.

    The issue I'm having is when the previous bars open and close are on opposite sides of the ema. Right now under those conditions after the second bar which opens and closes on opposite sides of the ema, the chart background colour reverts to white.

    I'd like to have the indicator perform such that if the open and close are on opposite sides of the ema the chart backgrounds colour remains the same as the most recent bar which opened and closed on the same side of the ema.

    I tried creating and calling a setColour() and getColour() function to solve this issue, but was not able to code it properly.

    Any help is greatly appreciated. Here is the code I've written:

    protected override void OnBarUpdate()
    {
    if (BarsInProgress != 0)
    return;

    if (CurrentBars[0] < 0)
    return;

    // bullish trend
    if ((Open[0] > EMA1[0])
    && (Close[0] > EMA1[0]))
    {
    BackBrush = Brushes.Honeydew;
    }

    // bearish trend
    if ((Open[0] < EMA1[0])
    && (Close[0] < EMA1[0]))
    {
    BackBrush = Brushes.MistyRose;
    }

    //bar that opens and closes on opposite side of ema
    if ((Open[0] > EMA1[0] && Close[0] < EMA1[0])||(Open[0] < EMA1[0] && Close[0] > EMA1[0]))
    {
    if ((Open[1] > EMA1[1]) && (Close[1] > EMA1[1]))
    {
    BackBrush = Brushes.Honeydew;
    }
    if ((Open[1] < EMA1[1]) && (Close[1] < EMA1[1]))
    {
    BackBrush = Brushes.MistyRose;
    }
    }
    }
    Last edited by OctaveE; 03-20-2021, 08:43 AM.

    #2
    Try to use closing price only and see if that helps. Thats the most basic calculation

    Comment


      #3
      Hello OctaveE,
      Your code so far is fine, the issue you're facing is a condition which you haven't coded. You've defined trend colors based on current & previous bars so if there are 2 consecutive bars with ema crossing the body code is fine but when you get more than 2 such bars your code reverts to undefined colors which is the original as per chart template.
      Let me simplify your code, use below code:
      Code:
      protected override void OnBarUpdate()
      {
      if (Open[0] > ema[0] && Close[0] > ema[0])[INDENT]BackBrushes[0] = Brushes.Honeydew;[/INDENT]
       else if (Open[0] < ema[0] && Close[0] < ema[0])[INDENT]BackBrushes[0] = Brushes.MistyRose;[/INDENT]
       else
      {[INDENT]BackBrushes[0] = BackBrushes[1];[/INDENT]
       }
      }
      Hope it helps!

      Comment


        #4
        Originally posted by Teebone21 View Post
        Try to use closing price only and see if that helps. Thats the most basic calculation
        Thanks for the reply, Teebone. I have this indicator on 6 charts of different timeframes and I've found that having an open and close on the same side of its ema provides a cleaner picture of the overall trend. Cheers!

        Comment


          #5
          Originally posted by s.kinra View Post
          Hello OctaveE,
          Your code so far is fine, the issue you're facing is a condition which you haven't coded. You've defined trend colors based on current & previous bars so if there are 2 consecutive bars with ema crossing the body code is fine but when you get more than 2 such bars your code reverts to undefined colors which is the original as per chart template.
          Let me simplify your code, use below code:
          Code:
          protected override void OnBarUpdate()
          {
          if (Open[0] > ema[0] && Close[0] > ema[0])[INDENT]BackBrushes[0] = Brushes.Honeydew;[/INDENT]
          else if (Open[0] < ema[0] && Close[0] < ema[0])[INDENT]BackBrushes[0] = Brushes.MistyRose;[/INDENT]
          else
          {[INDENT]BackBrushes[0] = BackBrushes[1];[/INDENT]
          }
          }
          Hope it helps!
          Nice! Thanks a ton! I had no idea you could call backbrushes as an array like that. I knew my code for the opposite trend bars was clunky and if I wanted to fix it, it would increase by a power of 2 for each consecutive opposite trend bar.
          This is exactly what I was looking for. Thanks again!

          Comment


            #6
            s.kinra, after changing the code to how you suggested, the background for any chart is entirely mistyrose. not sure what's causing that

            edit: found out the issue. I hadn't changed the downtrend condition from if to else if. works perfectly now.
            Last edited by OctaveE; 03-21-2021, 08:10 AM.

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Geovanny Suaza, 02-11-2026, 06:32 PM
            0 responses
            663 views
            0 likes
            Last Post Geovanny Suaza  
            Started by Geovanny Suaza, 02-11-2026, 05:51 PM
            0 responses
            376 views
            1 like
            Last Post Geovanny Suaza  
            Started by Mindset, 02-09-2026, 11:44 AM
            0 responses
            110 views
            0 likes
            Last Post Mindset
            by Mindset
             
            Started by Geovanny Suaza, 02-02-2026, 12:30 PM
            0 responses
            575 views
            1 like
            Last Post Geovanny Suaza  
            Started by RFrosty, 01-28-2026, 06:49 PM
            0 responses
            580 views
            1 like
            Last Post RFrosty
            by RFrosty
             
            Working...
            X