Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Changing colors in area fill (Draw.Region) depending on slope of EMA

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

    Changing colors in area fill (Draw.Region) depending on slope of EMA

    Hi
    I need a little help on understanding why this does not work.
    I am trying to change the color of the fill between 2 EMAs depending on the slope of the EMAs. I have debugged and the different part of the statement are hit at the right times so it seems that the color value is somehow overwritten as the area fill only has one color.

    Code:
     protected override void OnBarUpdate()
    {
    if(CurrentBar < EmaHighLen)
    return;
    
    if(emaHigh[0]> emaHigh[1] && emaHigh[0]> emaHigh[2] && emaHigh[0]> emaHigh[3])
    Draw.Region(this, CurrentBar+"tag1", CurrentBar, 0, emaHigh, emaLow, null, Brushes.Green, 25);
    else if(emaHigh[0]< emaHigh[1] && emaHigh[0] < emaHigh[2] && emaHigh[0] < emaHigh[3])
    Draw.Region(this, CurrentBar+"tagDown", CurrentBar, 0, emaHigh, emaLow, null, Brushes.Red, 25);
    else
    Draw.Region(this, CurrentBar+"tagNeutral", CurrentBar, 0, emaHigh, emaLow, null, Brushes.Gray, 25);
    }
    Best Regards,
    Sune

    #2
    Hello Sune,
    I would expect it to draw 3 regions at the same time, so the results might not be as expected. In your if statements you're drawing the regions but not removing the older one to retain the latest one. Every region will have one static color which is definitely not as expected. You can goto drawing objects to find all regions drawn on chart.
    Workaround would be to identify the bar index where slope is changing & then draw a new region between these bar index.
    Hope it helps!
    Last edited by s.kinra; 09-12-2021, 06:56 AM. Reason: workaround modified

    Comment


      #3
      Hi
      Thanks for the input.
      I found the issue. The issue was the CurrentBar paramter i used in the
      Code:
      Draw.Region(this, CurrentBar+"tag1", CurrentBar, 0, emaHigh, emaLow, null, Brushes.Green, 25);
      As the value request in the method was
      Code:
      int startBarsAgo
      CurrentBar, because it is all bars until now, takes all bars until now and colors them so therefore the last color would overwrite all else.
      By changing the call to
      Code:
      Draw.Region(this, CurrentBar+"tag1", 1, 0, emaHigh, emaLow, null, Brushes.Green, 25);
      the code does what I want.

      Best Regards,
      Sune

      Comment


        #4
        Hello Sune,

        Below is a link to an example indicator you may find helpful for using Draw.Region between two plots.
        Rewrite of the MACrossBuilder 1) Various Moving averages: DEMA, EMA, LinReg, HMA, SMA, TEMA, TMA, VWMA, WMA, ZLEMA 2) Various indication options 3) Can output to alerts window 4) Can output to Market Analyzer (+1, 0, &#8208;1 = Cross up, No cross, cross down) 5) Colors region between Moving averages V1.02 Update 10&#8208;31&#8208;17, minor code [&#8230;]
        Chelsea B.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Geovanny Suaza, 02-11-2026, 06:32 PM
        0 responses
        566 views
        0 likes
        Last Post Geovanny Suaza  
        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
        0 responses
        330 views
        1 like
        Last Post Geovanny Suaza  
        Started by Mindset, 02-09-2026, 11:44 AM
        0 responses
        101 views
        0 likes
        Last Post Mindset
        by Mindset
         
        Started by Geovanny Suaza, 02-02-2026, 12:30 PM
        0 responses
        547 views
        1 like
        Last Post Geovanny Suaza  
        Started by RFrosty, 01-28-2026, 06:49 PM
        0 responses
        548 views
        1 like
        Last Post RFrosty
        by RFrosty
         
        Working...
        X