Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Problem with Draw.Region

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

    Problem with Draw.Region

    Hi

    First of all I'm not a coder, though lately I've been playing with ChatGPT with some major successes. Now I'm trying to to fill with a color an area close to the EMA. Those area is determined with ATR. This time I can not handle this one error that keeps popping which says: "Argument 6: cannot convert from 'NinjaTrader.NinjaScript.Series<double> to 'double'.

    Code:
    using NinjaTrader.Cbi;
    using NinjaTrader.NinjaScript;
    using NinjaTrader.Data;
    using NinjaTrader.Gui.Tools;
    using NinjaTrader.NinjaScript.DrawingTools;
    using System;
    using System.Windows.Media;
    
    namespace NinjaTrader.NinjaScript.Indicators
    {
        public class EMAWithATR : Indicator
        {
            private Series<double> upperBandSeries;
            private Series<double> lowerBandSeries;
    
            [NinjaScriptProperty]
            public int EMAPeriod { get; set; } = 14;
    
            [NinjaScriptProperty]
            public int ATRLength { get; set; } = 14;
    
            [NinjaScriptProperty]
            public double ATRMultiplier { get; set; } = 2.5;
    
            [NinjaScriptProperty]
            public Color RegionFillColor { get; set; } = Colors.LightGray;
    
            protected override void OnStateChange()
            {
                if (State == State.SetDefaults)
                {
                    Description = "EMA with ATR Bands and Region Fill";
                    Name = "EMAWithATR";
    
                    IsOverlay = true;
    
                    AddPlot(new SolidColorBrush(Colors.Blue), "EMA");
                }
                else if (State == State.DataLoaded)
                {
                    upperBandSeries = new Series<double>(this);
                    lowerBandSeries = new Series<double>(this);
                }
            }
    
            protected override void OnBarUpdate()
            {
                if (CurrentBar < Math.Max(EMAPeriod, ATRLength))
                    return;
    
                double emaValue = EMA(EMAPeriod)[0];
                double atrValue = ATR(ATRLength)[0];
    
                double upperBand = emaValue + atrValue * ATRMultiplier;
                double lowerBand = emaValue - atrValue * ATRMultiplier;
    
                upperBandSeries[0] = upperBand;
                lowerBandSeries[0] = lowerBand;
    
                Values[0][0] = emaValue;
    
                if (CurrentBar > 0)
                {
                    Draw.Region(this, "ATRRegion" + CurrentBar, CurrentBar - 1, CurrentBar,
                        lowerBandSeries, upperBandSeries,
                        Brushes.Transparent, 50);
                }
            }
        }
    }​
    Please help me with this issue.
    Is there possibility to set a gradient on this filled area? So that it would start with 100% opacity at the EMA and fade out moving away from it?
    Last edited by Pabulon; 11-18-2024, 03:46 AM.

    #2
    Hello Pabulon,

    You need to use the correct overload which includes two brushes, the area and outline brushes. I am also not certain if your Bars ago are correct, are you trying to color the whole series meaning from the first bar to last bar on the chart? If so you would need to use CurrentBar as the bars ago to start and 0 as the end bars ago. That would look like the following with the added brush parameter.

    Draw.Region(this, "ATRRegion" + CurrentBar, CurrentBar, 0, lowerBandSeries, upperBandSeries, Brushes.Transparent, Brushes.Red, 50);

    A gradient wouldn't be possible with the built in region tool, that would be possible using custom rendering.

    Comment


      #3
      Odlly this change made ATR bands disapear (no color aswell). No compilation errors though.

      Comment


        #4
        Hello Pabulon,

        That sounds like potentially a runtime error, check the control center log tab after applying the indicator, is there an error?

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by argusthome, 03-08-2026, 10:06 AM
        0 responses
        88 views
        0 likes
        Last Post argusthome  
        Started by NabilKhattabi, 03-06-2026, 11:18 AM
        0 responses
        48 views
        0 likes
        Last Post NabilKhattabi  
        Started by Deep42, 03-06-2026, 12:28 AM
        0 responses
        31 views
        0 likes
        Last Post Deep42
        by Deep42
         
        Started by TheRealMorford, 03-05-2026, 06:15 PM
        0 responses
        34 views
        0 likes
        Last Post TheRealMorford  
        Started by Mindset, 02-28-2026, 06:16 AM
        0 responses
        69 views
        0 likes
        Last Post Mindset
        by Mindset
         
        Working...
        X