Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Indicator for background color and mid band color

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

    Indicator for background color and mid band color

    Hi, I have been trying for a while to write a script that changes the background color to green if the bar closes above the open price and vice versa. The problem is that the background color and the mid band color are not synchronizing at the same bar i.e. the mid band color change exactly at the same bar where the price chage happend but the background color changes on the next bar. If I change the Close[0] to Close[1] then it just reverses i.e. background changes first and then the mid band. Could someone please help me what could be the issue? I have attached an image for better understanding.

    Here is the code:

    midBand = SMA(Close, Period)[0];
    double offset = OffsetMultiplier * ATR(Period)[0];
    upperBand = midBand + offset;
    lowerBand = midBand - offset;

    double openPrice = Open[0];
    double closePrice = Close[0];

    if(closePrice > openPrice) {
    trendDirection = 1;
    } else if(closePrice < openPrice) {
    trendDirection = -1;
    } else {
    trendDirection = 0;
    }

    // Update the plots for the bands
    Values[0][0] = upperBand;
    Values[1][0] = lowerBand;

    Plots[0].Width = 3;
    Plots[1].Width = 3;
    Plots[2].Width = 6;

    // Plot the mid band on the chart
    if (trendDirection == 1)
    {
    PlotBrushes[2][0] = Brushes.Green; // MidlineUp
    upBackgroundColor = new SolidColorBrush(Color.FromArgb(50, 0, 255, 0));
    upBackgroundColor.Freeze();
    BackBrush = upBackgroundColor;
    Draw.TextFixed(this, "TrendInfo",
    trendDirection == 1 ? "Bullish" : "Bearish",
    TextPos,
    Brushes.White,
    new Gui.Tools.SimpleFont("Arial", 12) { Bold = true },
    Brushes.Transparent,
    Brushes.Transparent, 0);​
    }
    else if (trendDirection == -1)
    {
    PlotBrushes[2][0] = Brushes.Magenta; // MidlineDown
    downBackgroundColor = new SolidColorBrush(Color.FromArgb(50, 255, 0, 0));
    downBackgroundColor.Freeze();
    BackBrush = downBackgroundColor;
    Draw.TextFixed(this, "TrendInfo",
    trendDirection == 1 ? "Bullish" : "Bearish",
    TextPos,
    Brushes.White,
    new Gui.Tools.SimpleFont("Arial", 12) { Bold = true },
    Brushes.Transparent,
    Brushes.Transparent, 0);​
    }
    else
    {
    //BackBrush = Brushes.Transparent;
    PlotBrushes[2][0] = Brushes.Transparent;
    }
    Values[2][0] = midBand;​​

    Click image for larger version

Name:	MNQ 12-242.png
Views:	175
Size:	94.4 KB
ID:	1325569
    Last edited by tradingAnts; 11-25-2024, 06:54 AM.

    #2
    Hello tradingAnts,

    Plots extend to the previous bar which is why you would see the plot coloring start first, to have similar background coloring you would have to paint the previous bar. You can do that using BackBrushes[1] = downBackgroundColor;

    Comment


      #3
      Hi Jesse, Thanks for your reply and solution. Unfortunately, they both still stay unsynchronized. I tried ChatGPT but couldn't get any effective results. I think the problem is with the Plot midband because the background is changing the color at the perfect spot but the midband color stays at half of the bar width. Do you see any issue with the Band Plots?

      Comment


        #4
        Hello tradingAnts,

        You would have to continue to explore the code you made in that case and make sure you are setting the plot and bar color at the same time. Plots go from bar to bar so they will have some extra spacing compared to the back color which only exists directly behind the bar in the bars slot.

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Geovanny Suaza, 02-11-2026, 06:32 PM
        0 responses
        599 views
        0 likes
        Last Post Geovanny Suaza  
        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
        0 responses
        344 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
        558 views
        1 like
        Last Post Geovanny Suaza  
        Started by RFrosty, 01-28-2026, 06:49 PM
        0 responses
        557 views
        1 like
        Last Post RFrosty
        by RFrosty
         
        Working...
        X